]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ffs/ffs_alloc.c
f4aa9630efdb89d43fc8eb5955ff21724d3a17b0
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1982, 1986, 1989, 1993
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)ffs_alloc.c 8.18 (Berkeley) 5/26/95
65 #include <rev_endian_fs.h>
66 #include <vm/vm_pager.h>
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/buf_internal.h>
72 #include <sys/kauth.h>
73 #include <sys/vnode_internal.h>
74 #include <sys/mount_internal.h>
75 #include <sys/kernel.h>
76 #include <sys/syslog.h>
77 #include <sys/quota.h>
81 #include <ufs/ufs/quota.h>
82 #include <ufs/ufs/inode.h>
84 #include <ufs/ffs/fs.h>
85 #include <ufs/ffs/ffs_extern.h>
88 #include <ufs/ufs/ufs_byte_order.h>
89 #include <libkern/OSByteOrder.h>
90 #endif /* REV_ENDIAN_FS */
92 extern u_long nextgennumber
;
94 static ufs_daddr_t
ffs_alloccg(struct inode
*, int, ufs_daddr_t
, int);
95 static ufs_daddr_t
ffs_alloccgblk(struct fs
*, struct cg
*, ufs_daddr_t
);
96 static ufs_daddr_t
ffs_clusteralloc(struct inode
*, int, ufs_daddr_t
, int);
97 static ino_t
ffs_dirpref(struct inode
*);
98 static ufs_daddr_t
ffs_fragextend(struct inode
*, int, long, int, int);
99 static void ffs_fserr(struct fs
*, u_int
, char *);
100 static u_long ffs_hashalloc
101 (struct inode
*, int, long, int, u_int32_t (*)());
102 static ino_t
ffs_nodealloccg(struct inode
*, int, ufs_daddr_t
, int);
103 static ufs_daddr_t
ffs_mapsearch(struct fs
*, struct cg
*, ufs_daddr_t
, int);
104 static void ffs_clusteracct
105 (struct fs
*fs
, struct cg
*cgp
, ufs_daddr_t blkno
, int cnt
);
108 * Allocate a block in the file system.
110 * The size of the requested block is given, which must be some
111 * multiple of fs_fsize and <= fs_bsize.
112 * A preference may be optionally specified. If a preference is given
113 * the following hierarchy is used to allocate a block:
114 * 1) allocate the requested block.
115 * 2) allocate a rotationally optimal block in the same cylinder.
116 * 3) allocate a block in the same cylinder group.
117 * 4) quadradically rehash into other cylinder groups, until an
118 * available block is located.
119 * If no block preference is given the following heirarchy is used
120 * to allocate a block:
121 * 1) allocate a block in the cylinder group that contains the
122 * inode for the file.
123 * 2) quadradically rehash into other cylinder groups, until an
124 * available block is located.
126 ffs_alloc(ip
, lbn
, bpref
, size
, cred
, bnp
)
127 register struct inode
*ip
;
128 ufs_daddr_t lbn
, bpref
;
133 register struct fs
*fs
;
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");
146 panic("ffs_alloc: missing credential\n");
147 #endif /* DIAGNOSTIC */
148 if (size
== fs
->fs_bsize
&& fs
->fs_cstotal
.cs_nbfree
== 0)
150 if (suser(cred
, NULL
) && freespace(fs
, fs
->fs_minfree
) <= 0)
152 devBlockSize
= vfs_devblocksize(vnode_mount(ITOV(ip
)));
154 if (error
= chkdq(ip
, (int64_t)size
, cred
, 0))
157 if (bpref
>= fs
->fs_size
)
160 cg
= ino_to_cg(fs
, ip
->i_number
);
162 cg
= dtog(fs
, bpref
);
163 bno
= (ufs_daddr_t
)ffs_hashalloc(ip
, cg
, (long)bpref
, size
,
164 (u_int32_t (*)())ffs_alloccg
);
166 ip
->i_blocks
+= btodb(size
, devBlockSize
);
167 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
173 * Restore user's disk quota because allocation failed.
175 (void) chkdq(ip
, (int64_t)-size
, cred
, FORCE
);
178 ffs_fserr(fs
, kauth_cred_getuid(cred
), "file system full");
179 uprintf("\n%s: write failed, file system is full\n", fs
->fs_fsmnt
);
184 * Reallocate a fragment to a bigger size
186 * The number and size of the old block is given, and a preference
187 * and new size is also specified. The allocator attempts to extend
188 * the original block. Failing that, the regular block allocator is
189 * invoked to get an appropriate block.
191 ffs_realloccg(ip
, lbprev
, bpref
, osize
, nsize
, cred
, bpp
)
192 register struct inode
*ip
;
199 register struct fs
*fs
;
201 int cg
, request
, error
;
202 ufs_daddr_t bprev
, bno
;
208 if ((u_int
)osize
> fs
->fs_bsize
|| fragoff(fs
, osize
) != 0 ||
209 (u_int
)nsize
> fs
->fs_bsize
|| fragoff(fs
, nsize
) != 0) {
211 "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
212 ip
->i_dev
, fs
->fs_bsize
, osize
, nsize
, fs
->fs_fsmnt
);
213 panic("ffs_realloccg: bad size");
216 panic("ffs_realloccg: missing credential\n");
217 #endif /* DIAGNOSTIC */
218 if (suser(cred
, NULL
) != 0 && freespace(fs
, fs
->fs_minfree
) <= 0)
220 if ((bprev
= ip
->i_db
[lbprev
]) == 0) {
221 printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
222 ip
->i_dev
, fs
->fs_bsize
, bprev
, fs
->fs_fsmnt
);
223 panic("ffs_realloccg: bad bprev");
226 * Allocate the extra space in the buffer.
228 if (error
= (int)buf_bread(ITOV(ip
), (daddr64_t
)((unsigned)lbprev
), osize
, NOCRED
, &bp
)) {
232 devBlockSize
= vfs_devblocksize(vnode_mount(ITOV(ip
)));
235 if (error
= chkdq(ip
, (int64_t)(nsize
- osize
), cred
, 0))
242 * Check for extension in the existing location.
244 cg
= dtog(fs
, bprev
);
245 if (bno
= ffs_fragextend(ip
, cg
, (long)bprev
, osize
, nsize
)) {
246 if ((ufs_daddr_t
)buf_blkno(bp
) != fsbtodb(fs
, bno
))
247 panic("bad blockno");
248 ip
->i_blocks
+= btodb(nsize
- osize
, devBlockSize
);
249 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
251 buf_setflags(bp
, B_DONE
);
252 bzero((char *)buf_dataptr(bp
) + osize
, (u_int
)buf_size(bp
) - osize
);
257 * Allocate a new disk location.
259 if (bpref
>= fs
->fs_size
)
261 switch ((int)fs
->fs_optim
) {
264 * Allocate an exact sized fragment. Although this makes
265 * best use of space, we will waste time relocating it if
266 * the file continues to grow. If the fragmentation is
267 * less than half of the minimum free reserve, we choose
268 * to begin optimizing for time.
271 if (fs
->fs_minfree
< 5 ||
272 fs
->fs_cstotal
.cs_nffree
>
273 fs
->fs_dsize
* fs
->fs_minfree
/ (2 * 100))
275 log(LOG_NOTICE
, "%s: optimization changed from SPACE to TIME\n",
277 fs
->fs_optim
= FS_OPTTIME
;
281 * At this point we have discovered a file that is trying to
282 * grow a small fragment to a larger fragment. To save time,
283 * we allocate a full sized block, then free the unused portion.
284 * If the file continues to grow, the `ffs_fragextend' call
285 * above will be able to grow it in place without further
286 * copying. If aberrant programs cause disk fragmentation to
287 * grow within 2% of the free reserve, we choose to begin
288 * optimizing for space.
290 request
= fs
->fs_bsize
;
291 if (fs
->fs_cstotal
.cs_nffree
<
292 fs
->fs_dsize
* (fs
->fs_minfree
- 2) / 100)
294 log(LOG_NOTICE
, "%s: optimization changed from TIME to SPACE\n",
296 fs
->fs_optim
= FS_OPTSPACE
;
299 printf("dev = 0x%x, optim = %d, fs = %s\n",
300 ip
->i_dev
, fs
->fs_optim
, fs
->fs_fsmnt
);
301 panic("ffs_realloccg: bad optim");
304 bno
= (ufs_daddr_t
)ffs_hashalloc(ip
, cg
, (long)bpref
, request
,
305 (u_int32_t (*)())ffs_alloccg
);
307 buf_setblkno(bp
, (daddr64_t
)((unsigned)fsbtodb(fs
, bno
)));
308 ffs_blkfree(ip
, bprev
, (long)osize
);
310 ffs_blkfree(ip
, bno
+ numfrags(fs
, nsize
),
311 (long)(request
- nsize
));
312 ip
->i_blocks
+= btodb(nsize
- osize
, devBlockSize
);
313 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
315 buf_setflags(bp
, B_DONE
);
316 bzero((char *)buf_dataptr(bp
) + osize
, (u_int
)buf_size(bp
) - osize
);
322 * Restore user's disk quota because allocation failed.
324 (void) chkdq(ip
, (int64_t)-(nsize
- osize
), cred
, FORCE
);
331 ffs_fserr(fs
, kauth_cred_getuid(cred
), "file system full");
332 uprintf("\n%s: write failed, file system is full\n", fs
->fs_fsmnt
);
337 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
339 * The vnode and an array of buffer pointers for a range of sequential
340 * logical blocks to be made contiguous is given. The allocator attempts
341 * to find a range of sequential blocks starting as close as possible to
342 * an fs_rotdelay offset from the end of the allocation for the logical
343 * block immediately preceeding the current range. If successful, the
344 * physical block numbers in the buffer pointers and in the inode are
345 * changed to reflect the new allocation. If unsuccessful, the allocation
346 * is left unchanged. The success in doing the reallocation is returned.
347 * Note that the error return is not reflected back to the user. Rather
348 * the previous block allocation will be used.
350 /* Note: This routine is unused in UBC cluster I/O */
353 int doreallocblks
= 1;
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.
379 register struct inode
*pip
;
380 register struct fs
*fs
;
381 register struct inode
*ip
;
389 if (fs
->fs_cstotal
.cs_nifree
== 0)
392 if ((mode
& IFMT
) == IFDIR
)
393 ipref
= ffs_dirpref(pip
);
395 ipref
= pip
->i_number
;
396 if (ipref
>= fs
->fs_ncg
* fs
->fs_ipg
)
398 cg
= ino_to_cg(fs
, ipref
);
400 * Track the number of dirs created one after another
401 * in a cg without intervening files.
403 if ((mode
& IFMT
) == IFDIR
) {
404 if (fs
->fs_contigdirs
[cg
] < 255)
405 fs
->fs_contigdirs
[cg
]++;
407 if (fs
->fs_contigdirs
[cg
] > 0)
408 fs
->fs_contigdirs
[cg
]--;
410 ino
= (ino_t
)ffs_hashalloc(pip
, cg
, (long)ipref
, mode
, ffs_nodealloccg
);
414 error
= ffs_vget_internal(pvp
->v_mount
, ino
, vpp
, NULL
, NULL
, mode
, 0);
416 ffs_vfree(pvp
, ino
, mode
);
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.
436 if (++nextgennumber
< (u_long
)tv
.tv_sec
)
437 nextgennumber
= tv
.tv_sec
;
438 ip
->i_gen
= nextgennumber
;
441 ffs_fserr(fs
, kauth_cred_getuid(cred
), "out of inodes");
442 uprintf("\n%s: create/symlink failed, no inodes free\n", fs
->fs_fsmnt
);
447 * Find a cylinder group to place a directory.
449 * The policy implemented by this algorithm is to allocate a
450 * directory inode in the same cylinder group as its parent
451 * directory, but also to reserve space for its files inodes
452 * and data. Restrict the number of directories which may be
453 * allocated one after another in the same cylinder group
454 * without intervening allocation of files.
460 register struct fs
*fs
;
461 int cg
, prefcg
, dirsize
, cgsize
;
462 int avgifree
, avgbfree
, avgndir
, curdirsize
;
463 int minifree
, minbfree
, maxndir
;
468 avgifree
= fs
->fs_cstotal
.cs_nifree
/ fs
->fs_ncg
;
469 avgbfree
= fs
->fs_cstotal
.cs_nbfree
/ fs
->fs_ncg
;
470 avgndir
= fs
->fs_cstotal
.cs_ndir
/ fs
->fs_ncg
;
473 * Force allocation in another cg if creating a first level dir.
475 if (ITOV(pip
)->v_flag
& VROOT
) {
477 prefcg
= random() % fs
->fs_ncg
;
479 prefcg
= arc4random() % fs
->fs_ncg
;
482 minndir
= fs
->fs_ipg
;
483 for (cg
= prefcg
; cg
< fs
->fs_ncg
; cg
++)
484 if (fs
->fs_cs(fs
, cg
).cs_ndir
< minndir
&&
485 fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
&&
486 fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
488 minndir
= fs
->fs_cs(fs
, cg
).cs_ndir
;
490 for (cg
= 0; cg
< prefcg
; cg
++)
491 if (fs
->fs_cs(fs
, cg
).cs_ndir
< minndir
&&
492 fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
&&
493 fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
495 minndir
= fs
->fs_cs(fs
, cg
).cs_ndir
;
497 return ((ino_t
)(fs
->fs_ipg
* mincg
));
501 * Count various limits which used for
502 * optimal allocation of a directory inode.
504 maxndir
= min(avgndir
+ fs
->fs_ipg
/ 16, fs
->fs_ipg
);
505 minifree
= avgifree
- fs
->fs_ipg
/ 4;
508 minbfree
= avgbfree
- fs
->fs_fpg
/ fs
->fs_frag
/ 4;
511 cgsize
= fs
->fs_fsize
* fs
->fs_fpg
;
512 dirsize
= fs
->fs_avgfilesize
* fs
->fs_avgfpdir
;
513 curdirsize
= avgndir
? (cgsize
- avgbfree
* fs
->fs_bsize
) / avgndir
: 0;
514 if (dirsize
< curdirsize
)
515 dirsize
= curdirsize
;
516 maxcontigdirs
= min(cgsize
/ dirsize
, 255);
517 if (fs
->fs_avgfpdir
> 0)
518 maxcontigdirs
= min(maxcontigdirs
,
519 fs
->fs_ipg
/ fs
->fs_avgfpdir
);
520 if (maxcontigdirs
== 0)
524 * Limit number of dirs in one cg and reserve space for
525 * regular files, but only if we have no deficit in
528 prefcg
= ino_to_cg(fs
, pip
->i_number
);
529 for (cg
= prefcg
; cg
< fs
->fs_ncg
; cg
++)
530 if (fs
->fs_cs(fs
, cg
).cs_ndir
< maxndir
&&
531 fs
->fs_cs(fs
, cg
).cs_nifree
>= minifree
&&
532 fs
->fs_cs(fs
, cg
).cs_nbfree
>= minbfree
) {
533 if (fs
->fs_contigdirs
[cg
] < maxcontigdirs
)
534 return ((ino_t
)(fs
->fs_ipg
* cg
));
536 for (cg
= 0; cg
< prefcg
; cg
++)
537 if (fs
->fs_cs(fs
, cg
).cs_ndir
< maxndir
&&
538 fs
->fs_cs(fs
, cg
).cs_nifree
>= minifree
&&
539 fs
->fs_cs(fs
, cg
).cs_nbfree
>= minbfree
) {
540 if (fs
->fs_contigdirs
[cg
] < maxcontigdirs
)
541 return ((ino_t
)(fs
->fs_ipg
* cg
));
544 * This is a backstop when we have deficit in space.
546 for (cg
= prefcg
; cg
< fs
->fs_ncg
; cg
++)
547 if (fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
)
548 return ((ino_t
)(fs
->fs_ipg
* cg
));
549 for (cg
= 0; cg
< prefcg
; cg
++)
550 if (fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
)
552 return ((ino_t
)(fs
->fs_ipg
* cg
));
556 * Select the desired position for the next block in a file. The file is
557 * logically divided into sections. The first section is composed of the
558 * direct blocks. Each additional section contains fs_maxbpg blocks.
560 * If no blocks have been allocated in the first section, the policy is to
561 * request a block in the same cylinder group as the inode that describes
562 * the file. If no blocks have been allocated in any other section, the
563 * policy is to place the section in a cylinder group with a greater than
564 * average number of free blocks. An appropriate cylinder group is found
565 * by using a rotor that sweeps the cylinder groups. When a new group of
566 * blocks is needed, the sweep begins in the cylinder group following the
567 * cylinder group from which the previous allocation was made. The sweep
568 * continues until a cylinder group with greater than the average number
569 * of free blocks is found. If the allocation is for the first block in an
570 * indirect block, the information on the previous allocation is unavailable;
571 * here a best guess is made based upon the logical block number being
574 * If a section is already partially allocated, the policy is to
575 * contiguously allocate fs_maxcontig blocks. The end of one of these
576 * contiguous blocks and the beginning of the next is physically separated
577 * so that the disk head will be in transit between them for at least
578 * fs_rotdelay milliseconds. This is to allow time for the processor to
579 * schedule another I/O transfer.
582 ffs_blkpref(ip
, lbn
, indx
, bap
)
588 register struct fs
*fs
;
590 int avgbfree
, startcg
;
594 struct vnode
*vp
=ITOV(ip
);
595 struct mount
*mp
=vp
->v_mount
;
596 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
597 #endif /* REV_ENDIAN_FS */
603 if (bap
!= &ip
->i_db
[0])
604 prev
= OSSwapInt32(bap
[indx
- 1]);
606 prev
= bap
[indx
- 1];
607 } else prev
= bap
[indx
- 1];
609 if (indx
% fs
->fs_maxbpg
== 0 || prev
== 0)
610 #else /* REV_ENDIAN_FS */
611 if (indx
% fs
->fs_maxbpg
== 0 || bap
[indx
- 1] == 0)
612 #endif /* REV_ENDIAN_FS */
615 cg
= ino_to_cg(fs
, ip
->i_number
);
616 return (fs
->fs_fpg
* cg
+ fs
->fs_frag
);
619 * Find a cylinder with greater than average number of
620 * unused data blocks.
623 if (indx
== 0 || prev
== 0)
624 #else /* REV_ENDIAN_FS */
625 if (indx
== 0 || bap
[indx
- 1] == 0)
626 #endif /* REV_ENDIAN_FS */
628 ino_to_cg(fs
, ip
->i_number
) + lbn
/ fs
->fs_maxbpg
;
631 startcg
= dtog(fs
, prev
) + 1;
632 #else /* REV_ENDIAN_FS */
633 startcg
= dtog(fs
, bap
[indx
- 1]) + 1;
634 #endif /* REV_ENDIAN_FS */
635 startcg
%= fs
->fs_ncg
;
636 avgbfree
= fs
->fs_cstotal
.cs_nbfree
/ fs
->fs_ncg
;
637 for (cg
= startcg
; cg
< fs
->fs_ncg
; cg
++)
638 if (fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
640 return (fs
->fs_fpg
* cg
+ fs
->fs_frag
);
642 for (cg
= 0; cg
<= startcg
; cg
++)
643 if (fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
645 return (fs
->fs_fpg
* cg
+ fs
->fs_frag
);
650 * One or more previous blocks have been laid out. If less
651 * than fs_maxcontig previous blocks are contiguous, the
652 * next block is requested contiguously, otherwise it is
653 * requested rotationally delayed by fs_rotdelay milliseconds.
657 nextblk
= prev
+ fs
->fs_frag
;
658 if (indx
< fs
->fs_maxcontig
) {
661 if (bap
!= &ip
->i_db
[0])
662 prev
= OSSwapInt32(bap
[indx
- fs
->fs_maxcontig
]);
664 prev
= bap
[indx
- fs
->fs_maxcontig
];
665 if (prev
+ blkstofrags(fs
, fs
->fs_maxcontig
) != nextblk
)
668 #endif /* REV_ENDIAN_FS */
669 nextblk
= bap
[indx
- 1] + fs
->fs_frag
;
670 if (indx
< fs
->fs_maxcontig
|| bap
[indx
- fs
->fs_maxcontig
] +
671 blkstofrags(fs
, fs
->fs_maxcontig
) != nextblk
)
675 #endif /* REV_ENDIAN_FS */
676 if (fs
->fs_rotdelay
!= 0)
678 * Here we convert ms of delay to frags as:
679 * (frags) = (ms) * (rev/sec) * (sect/rev) /
680 * ((sect/frag) * (ms/sec))
681 * then round up to the next block.
683 nextblk
+= roundup(fs
->fs_rotdelay
* fs
->fs_rps
* fs
->fs_nsect
/
684 (NSPF(fs
) * 1000), fs
->fs_frag
);
689 * Implement the cylinder overflow algorithm.
691 * The policy implemented by this algorithm is:
692 * 1) allocate the block in its requested cylinder group.
693 * 2) quadradically rehash on the cylinder group number.
694 * 3) brute force search for a free block.
698 ffs_hashalloc(ip
, cg
, pref
, size
, allocator
)
702 int size
; /* size for data blocks, mode for inodes */
703 u_int32_t (*allocator
)();
705 register struct fs
*fs
;
711 * 1: preferred cylinder group
713 result
= (*allocator
)(ip
, cg
, pref
, size
);
717 * 2: quadratic rehash
719 for (i
= 1; i
< fs
->fs_ncg
; i
*= 2) {
721 if (cg
>= fs
->fs_ncg
)
723 result
= (*allocator
)(ip
, cg
, 0, size
);
728 * 3: brute force search
729 * Note that we start at i == 2, since 0 was checked initially,
730 * and 1 is always checked in the quadratic rehash.
732 cg
= (icg
+ 2) % fs
->fs_ncg
;
733 for (i
= 2; i
< fs
->fs_ncg
; i
++) {
734 result
= (*allocator
)(ip
, cg
, 0, size
);
738 if (cg
== fs
->fs_ncg
)
745 * Determine whether a fragment can be extended.
747 * Check to see if the necessary fragments are available, and
748 * if they are, allocate them.
751 ffs_fragextend(ip
, cg
, bprev
, osize
, nsize
)
757 register struct fs
*fs
;
758 register struct cg
*cgp
;
765 struct vnode
*vp
=ITOV(ip
);
766 struct mount
*mp
=vp
->v_mount
;
767 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
768 #endif /* REV_ENDIAN_FS */
771 if (fs
->fs_cs(fs
, cg
).cs_nffree
< numfrags(fs
, nsize
- osize
))
773 frags
= numfrags(fs
, nsize
); /* number of fragments needed */
774 bbase
= fragnum(fs
, bprev
); /* offset in a frag (it is mod fragsize */
775 if (bbase
> fragnum(fs
, (bprev
+ frags
- 1))) {
776 /* cannot extend across a block boundary */
779 /* read corresponding cylinder group info */
780 error
= (int)buf_bread(ip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, cg
))),
781 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
786 cgp
= (struct cg
*)buf_dataptr(bp
);
789 byte_swap_cgin(cgp
, fs
);
791 #endif /* REV_ENDIAN_FS */
793 if (!cg_chkmagic(cgp
)) {
796 byte_swap_cgout(cgp
,fs
);
797 #endif /* REV_ENDIAN_FS */
802 cgp
->cg_time
= tv
.tv_sec
;
803 bno
= dtogd(fs
, bprev
);
804 for (i
= numfrags(fs
, osize
); i
< frags
; i
++)
805 if (isclr(cg_blksfree(cgp
), bno
+ i
)) {
808 byte_swap_cgout(cgp
,fs
);
809 #endif /* REV_ENDIAN_FS */
814 * the current fragment can be extended
815 * deduct the count on fragment being extended into
816 * increase the count on the remaining fragment (if any)
817 * allocate the extended piece
819 for (i
= frags
; i
< fs
->fs_frag
- bbase
; i
++)
820 if (isclr(cg_blksfree(cgp
), bno
+ i
))
822 cgp
->cg_frsum
[i
- numfrags(fs
, osize
)]--;
824 cgp
->cg_frsum
[i
- frags
]++;
825 for (i
= numfrags(fs
, osize
); i
< frags
; i
++) {
826 clrbit(cg_blksfree(cgp
), bno
+ i
);
827 cgp
->cg_cs
.cs_nffree
--;
828 fs
->fs_cstotal
.cs_nffree
--;
829 fs
->fs_cs(fs
, cg
).cs_nffree
--;
834 byte_swap_cgout(cgp
,fs
);
835 #endif /* REV_ENDIAN_FS */
841 * Determine whether a block can be allocated.
843 * Check to see if a block of the appropriate size is available,
844 * and if it is, allocate it.
847 ffs_alloccg(ip
, cg
, bpref
, size
)
853 register struct fs
*fs
;
854 register struct cg
*cgp
;
858 int error
, bno
, frags
, allocsiz
;
860 struct vnode
*vp
=ITOV(ip
);
861 struct mount
*mp
=vp
->v_mount
;
862 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
863 #endif /* REV_ENDIAN_FS */
866 if (fs
->fs_cs(fs
, cg
).cs_nbfree
== 0 && size
== fs
->fs_bsize
)
868 error
= (int)buf_bread(ip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, cg
))),
869 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
874 cgp
= (struct cg
*)buf_dataptr(bp
);
877 byte_swap_cgin(cgp
,fs
);
878 #endif /* REV_ENDIAN_FS */
879 if (!cg_chkmagic(cgp
) ||
880 (cgp
->cg_cs
.cs_nbfree
== 0 && size
== fs
->fs_bsize
)) {
883 byte_swap_cgout(cgp
,fs
);
884 #endif /* REV_ENDIAN_FS */
889 cgp
->cg_time
= tv
.tv_sec
;
890 if (size
== fs
->fs_bsize
) {
891 bno
= ffs_alloccgblk(fs
, cgp
, bpref
);
894 byte_swap_cgout(cgp
,fs
);
895 #endif /* REV_ENDIAN_FS */
900 * check to see if any fragments are already available
901 * allocsiz is the size which will be allocated, hacking
902 * it down to a smaller size if necessary
904 frags
= numfrags(fs
, size
);
905 for (allocsiz
= frags
; allocsiz
< fs
->fs_frag
; allocsiz
++)
906 if (cgp
->cg_frsum
[allocsiz
] != 0)
908 if (allocsiz
== fs
->fs_frag
) {
910 * no fragments were available, so a block will be
911 * allocated, and hacked up
913 if (cgp
->cg_cs
.cs_nbfree
== 0) {
916 byte_swap_cgout(cgp
,fs
);
917 #endif /* REV_ENDIAN_FS */
921 bno
= ffs_alloccgblk(fs
, cgp
, bpref
);
922 bpref
= dtogd(fs
, bno
);
923 for (i
= frags
; i
< fs
->fs_frag
; i
++)
924 setbit(cg_blksfree(cgp
), bpref
+ i
);
925 i
= fs
->fs_frag
- frags
;
926 cgp
->cg_cs
.cs_nffree
+= i
;
927 fs
->fs_cstotal
.cs_nffree
+= i
;
928 fs
->fs_cs(fs
, cg
).cs_nffree
+= i
;
933 byte_swap_cgout(cgp
,fs
);
934 #endif /* REV_ENDIAN_FS */
938 bno
= ffs_mapsearch(fs
, cgp
, bpref
, allocsiz
);
942 byte_swap_cgout(cgp
,fs
);
943 #endif /* REV_ENDIAN_FS */
947 for (i
= 0; i
< frags
; i
++)
948 clrbit(cg_blksfree(cgp
), bno
+ i
);
949 cgp
->cg_cs
.cs_nffree
-= frags
;
950 fs
->fs_cstotal
.cs_nffree
-= frags
;
951 fs
->fs_cs(fs
, cg
).cs_nffree
-= frags
;
953 cgp
->cg_frsum
[allocsiz
]--;
954 if (frags
!= allocsiz
)
955 cgp
->cg_frsum
[allocsiz
- frags
]++;
958 byte_swap_cgout(cgp
,fs
);
959 #endif /* REV_ENDIAN_FS */
961 return (cg
* fs
->fs_fpg
+ bno
);
965 * Allocate a block in a cylinder group.
967 * This algorithm implements the following policy:
968 * 1) allocate the requested block.
969 * 2) allocate a rotationally optimal block in the same cylinder.
970 * 3) allocate the next available block on the block rotor for the
971 * specified cylinder group.
972 * Note that this routine only allocates fs_bsize blocks; these
973 * blocks may be fragmented by the routine that allocates them.
976 ffs_alloccgblk(fs
, cgp
, bpref
)
977 register struct fs
*fs
;
978 register struct cg
*cgp
;
981 ufs_daddr_t bno
, blkno
;
982 int cylno
, pos
, delta
;
986 if (bpref
== 0 || dtog(fs
, bpref
) != cgp
->cg_cgx
) {
987 bpref
= cgp
->cg_rotor
;
990 bpref
= blknum(fs
, bpref
);
991 bpref
= dtogd(fs
, bpref
);
993 * if the requested block is available, use it
995 if (ffs_isblock(fs
, cg_blksfree(cgp
), fragstoblks(fs
, bpref
))) {
999 if (fs
->fs_nrpos
<= 1 || fs
->fs_cpc
== 0) {
1001 * Block layout information is not available.
1002 * Leaving bpref unchanged means we take the
1003 * next available free block following the one
1004 * we just allocated. Hopefully this will at
1005 * least hit a track cache on drives of unknown
1006 * geometry (e.g. SCSI).
1011 * check for a block available on the same cylinder
1013 cylno
= cbtocylno(fs
, bpref
);
1014 if (cg_blktot(cgp
)[cylno
] == 0)
1017 * check the summary information to see if a block is
1018 * available in the requested cylinder starting at the
1019 * requested rotational position and proceeding around.
1021 cylbp
= cg_blks(fs
, cgp
, cylno
);
1022 pos
= cbtorpos(fs
, bpref
);
1023 for (i
= pos
; i
< fs
->fs_nrpos
; i
++)
1026 if (i
== fs
->fs_nrpos
)
1027 for (i
= 0; i
< pos
; i
++)
1032 * found a rotational position, now find the actual
1033 * block. A panic if none is actually there.
1035 pos
= cylno
% fs
->fs_cpc
;
1036 bno
= (cylno
- pos
) * fs
->fs_spc
/ NSPB(fs
);
1037 if (fs_postbl(fs
, pos
)[i
] == -1) {
1038 printf("pos = %d, i = %d, fs = %s\n",
1039 pos
, i
, fs
->fs_fsmnt
);
1040 panic("ffs_alloccgblk: cyl groups corrupted");
1042 for (i
= fs_postbl(fs
, pos
)[i
];; ) {
1043 if (ffs_isblock(fs
, cg_blksfree(cgp
), bno
+ i
)) {
1044 bno
= blkstofrags(fs
, (bno
+ i
));
1047 delta
= fs_rotbl(fs
)[i
];
1049 delta
+ i
> fragstoblks(fs
, fs
->fs_fpg
))
1053 printf("pos = %d, i = %d, fs = %s\n", pos
, i
, fs
->fs_fsmnt
);
1054 panic("ffs_alloccgblk: can't find blk in cyl");
1058 * no blocks in the requested cylinder, so take next
1059 * available one in this cylinder group.
1061 bno
= ffs_mapsearch(fs
, cgp
, bpref
, (int)fs
->fs_frag
);
1064 cgp
->cg_rotor
= bno
;
1066 blkno
= fragstoblks(fs
, bno
);
1067 ffs_clrblock(fs
, cg_blksfree(cgp
), (long)blkno
);
1068 ffs_clusteracct(fs
, cgp
, blkno
, -1);
1069 cgp
->cg_cs
.cs_nbfree
--;
1070 fs
->fs_cstotal
.cs_nbfree
--;
1071 fs
->fs_cs(fs
, cgp
->cg_cgx
).cs_nbfree
--;
1072 cylno
= cbtocylno(fs
, bno
);
1073 cg_blks(fs
, cgp
, cylno
)[cbtorpos(fs
, bno
)]--;
1074 cg_blktot(cgp
)[cylno
]--;
1076 return (cgp
->cg_cgx
* fs
->fs_fpg
+ bno
);
1080 * Determine whether a cluster can be allocated.
1082 * We do not currently check for optimal rotational layout if there
1083 * are multiple choices in the same cylinder group. Instead we just
1084 * take the first one that we find following bpref.
1087 ffs_clusteralloc(ip
, cg
, bpref
, len
)
1093 register struct fs
*fs
;
1094 register struct cg
*cgp
;
1096 int i
, got
, run
, bno
, bit
, map
;
1100 struct vnode
*vp
=ITOV(ip
);
1101 struct mount
*mp
=vp
->v_mount
;
1102 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1103 #endif /* REV_ENDIAN_FS */
1106 if (fs
->fs_maxcluster
[cg
] < len
)
1108 if (buf_bread(ip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, cg
))), (int)fs
->fs_cgsize
,
1111 cgp
= (struct cg
*)buf_dataptr(bp
);
1114 byte_swap_cgin(cgp
,fs
);
1115 #endif /* REV_ENDIAN_FS */
1116 if (!cg_chkmagic(cgp
)) {
1119 byte_swap_cgout(cgp
,fs
);
1120 #endif /* REV_ENDIAN_FS */
1124 * Check to see if a cluster of the needed size (or bigger) is
1125 * available in this cylinder group.
1127 lp
= &cg_clustersum(cgp
)[len
];
1128 for (i
= len
; i
<= fs
->fs_contigsumsize
; i
++)
1131 if (i
> fs
->fs_contigsumsize
) {
1133 * This is the first time looking for a cluster in this
1134 * cylinder group. Update the cluster summary information
1135 * to reflect the true maximum sized cluster so that
1136 * future cluster allocation requests can avoid reading
1137 * the cylinder group map only to find no clusters.
1139 lp
= &cg_clustersum(cgp
)[len
- 1];
1140 for (i
= len
- 1; i
> 0; i
--)
1143 fs
->fs_maxcluster
[cg
] = i
;
1146 byte_swap_cgout(cgp
,fs
);
1147 #endif /* REV_ENDIAN_FS */
1151 * Search the cluster map to find a big enough cluster.
1152 * We take the first one that we find, even if it is larger
1153 * than we need as we prefer to get one close to the previous
1154 * block allocation. We do not search before the current
1155 * preference point as we do not want to allocate a block
1156 * that is allocated before the previous one (as we will
1157 * then have to wait for another pass of the elevator
1158 * algorithm before it will be read). We prefer to fail and
1159 * be recalled to try an allocation in the next cylinder group.
1161 if (dtog(fs
, bpref
) != cg
)
1164 bpref
= fragstoblks(fs
, dtogd(fs
, blknum(fs
, bpref
)));
1165 mapp
= &cg_clustersfree(cgp
)[bpref
/ NBBY
];
1167 bit
= 1 << (bpref
% NBBY
);
1168 for (run
= 0, got
= bpref
; got
< cgp
->cg_nclusterblks
; got
++) {
1169 if ((map
& bit
) == 0) {
1176 if ((got
& (NBBY
- 1)) != (NBBY
- 1)) {
1183 if (got
== cgp
->cg_nclusterblks
) {
1186 byte_swap_cgout(cgp
,fs
);
1187 #endif /* REV_ENDIAN_FS */
1191 * Allocate the cluster that we have found.
1193 for (i
= 1; i
<= len
; i
++)
1194 if (!ffs_isblock(fs
, cg_blksfree(cgp
), got
- run
+ i
))
1195 panic("ffs_clusteralloc: map mismatch");
1196 bno
= cg
* fs
->fs_fpg
+ blkstofrags(fs
, got
- run
+ 1);
1197 if (dtog(fs
, bno
) != cg
)
1198 panic("ffs_clusteralloc: allocated out of group");
1199 len
= blkstofrags(fs
, len
);
1200 for (i
= 0; i
< len
; i
+= fs
->fs_frag
)
1201 if ((got
= ffs_alloccgblk(fs
, cgp
, bno
+ i
)) != bno
+ i
)
1202 panic("ffs_clusteralloc: lost block");
1205 byte_swap_cgout(cgp
,fs
);
1206 #endif /* REV_ENDIAN_FS */
1216 * Determine whether an inode can be allocated.
1218 * Check to see if an inode is available, and if it is,
1219 * allocate it using the following policy:
1220 * 1) allocate the requested inode.
1221 * 2) allocate the next available inode after the requested
1222 * inode in the specified cylinder group.
1225 ffs_nodealloccg(ip
, cg
, ipref
, mode
)
1231 register struct fs
*fs
;
1232 register struct cg
*cgp
;
1235 int error
, start
, len
, loc
, map
, i
;
1237 struct vnode
*vp
=ITOV(ip
);
1238 struct mount
*mp
=vp
->v_mount
;
1239 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1240 #endif /* REV_ENDIAN_FS */
1243 if (fs
->fs_cs(fs
, cg
).cs_nifree
== 0)
1245 error
= (int)buf_bread(ip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, cg
))),
1246 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
1251 cgp
= (struct cg
*)buf_dataptr(bp
);
1254 byte_swap_cgin(cgp
,fs
);
1255 #endif /* REV_ENDIAN_FS */
1256 if (!cg_chkmagic(cgp
) || cgp
->cg_cs
.cs_nifree
== 0) {
1259 byte_swap_cgout(cgp
,fs
);
1260 #endif /* REV_ENDIAN_FS */
1266 cgp
->cg_time
= tv
.tv_sec
;
1268 ipref
%= fs
->fs_ipg
;
1269 if (isclr(cg_inosused(cgp
), ipref
))
1272 start
= cgp
->cg_irotor
/ NBBY
;
1273 len
= howmany(fs
->fs_ipg
- cgp
->cg_irotor
, NBBY
);
1274 loc
= skpc(0xff, len
, &cg_inosused(cgp
)[start
]);
1278 loc
= skpc(0xff, len
, &cg_inosused(cgp
)[0]);
1280 printf("cg = %d, irotor = %d, fs = %s\n",
1281 cg
, cgp
->cg_irotor
, fs
->fs_fsmnt
);
1282 panic("ffs_nodealloccg: map corrupted");
1286 i
= start
+ len
- loc
;
1287 map
= cg_inosused(cgp
)[i
];
1289 for (i
= 1; i
< (1 << NBBY
); i
<<= 1, ipref
++) {
1290 if ((map
& i
) == 0) {
1291 cgp
->cg_irotor
= ipref
;
1295 printf("fs = %s\n", fs
->fs_fsmnt
);
1296 panic("ffs_nodealloccg: block not in map");
1299 setbit(cg_inosused(cgp
), ipref
);
1300 cgp
->cg_cs
.cs_nifree
--;
1301 fs
->fs_cstotal
.cs_nifree
--;
1302 fs
->fs_cs(fs
, cg
).cs_nifree
--;
1304 if ((mode
& IFMT
) == IFDIR
) {
1305 cgp
->cg_cs
.cs_ndir
++;
1306 fs
->fs_cstotal
.cs_ndir
++;
1307 fs
->fs_cs(fs
, cg
).cs_ndir
++;
1311 byte_swap_cgout(cgp
,fs
);
1312 #endif /* REV_ENDIAN_FS */
1314 return (cg
* fs
->fs_ipg
+ ipref
);
1318 * Free a block or fragment.
1320 * The specified block or fragment is placed back in the
1321 * free map. If a fragment is deallocated, a possible
1322 * block reassembly is checked.
1325 ffs_blkfree(ip
, bno
, size
)
1326 register struct inode
*ip
;
1330 register struct fs
*fs
;
1331 register struct cg
*cgp
;
1335 int i
, error
, cg
, blk
, frags
, bbase
;
1337 struct vnode
*vp
=ITOV(ip
);
1338 struct mount
*mp
=vp
->v_mount
;
1339 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1340 #endif /* REV_ENDIAN_FS */
1343 if ((u_int
)size
> fs
->fs_bsize
|| fragoff(fs
, size
) != 0) {
1344 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
1345 ip
->i_dev
, fs
->fs_bsize
, size
, fs
->fs_fsmnt
);
1346 panic("blkfree: bad size");
1349 if ((u_int
)bno
>= fs
->fs_size
) {
1350 printf("bad block %d, ino %d\n", bno
, ip
->i_number
);
1351 ffs_fserr(fs
, ip
->i_uid
, "bad block");
1354 error
= (int)buf_bread(ip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, cg
))),
1355 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
1360 cgp
= (struct cg
*)buf_dataptr(bp
);
1363 byte_swap_cgin(cgp
,fs
);
1364 #endif /* REV_ENDIAN_FS */
1365 if (!cg_chkmagic(cgp
)) {
1368 byte_swap_cgout(cgp
,fs
);
1369 #endif /* REV_ENDIAN_FS */
1374 cgp
->cg_time
= tv
.tv_sec
;
1375 bno
= dtogd(fs
, bno
);
1376 if (size
== fs
->fs_bsize
) {
1377 blkno
= fragstoblks(fs
, bno
);
1378 if (ffs_isblock(fs
, cg_blksfree(cgp
), blkno
)) {
1379 printf("dev = 0x%x, block = %d, fs = %s\n",
1380 ip
->i_dev
, bno
, fs
->fs_fsmnt
);
1381 panic("blkfree: freeing free block");
1383 ffs_setblock(fs
, cg_blksfree(cgp
), blkno
);
1384 ffs_clusteracct(fs
, cgp
, blkno
, 1);
1385 cgp
->cg_cs
.cs_nbfree
++;
1386 fs
->fs_cstotal
.cs_nbfree
++;
1387 fs
->fs_cs(fs
, cg
).cs_nbfree
++;
1388 i
= cbtocylno(fs
, bno
);
1389 cg_blks(fs
, cgp
, i
)[cbtorpos(fs
, bno
)]++;
1390 cg_blktot(cgp
)[i
]++;
1392 bbase
= bno
- fragnum(fs
, bno
);
1394 * decrement the counts associated with the old frags
1396 blk
= blkmap(fs
, cg_blksfree(cgp
), bbase
);
1397 ffs_fragacct(fs
, blk
, cgp
->cg_frsum
, -1);
1399 * deallocate the fragment
1401 frags
= numfrags(fs
, size
);
1402 for (i
= 0; i
< frags
; i
++) {
1403 if (isset(cg_blksfree(cgp
), bno
+ i
)) {
1404 printf("dev = 0x%x, block = %d, fs = %s\n",
1405 ip
->i_dev
, bno
+ i
, fs
->fs_fsmnt
);
1406 panic("blkfree: freeing free frag");
1408 setbit(cg_blksfree(cgp
), bno
+ i
);
1410 cgp
->cg_cs
.cs_nffree
+= i
;
1411 fs
->fs_cstotal
.cs_nffree
+= i
;
1412 fs
->fs_cs(fs
, cg
).cs_nffree
+= i
;
1414 * add back in counts associated with the new frags
1416 blk
= blkmap(fs
, cg_blksfree(cgp
), bbase
);
1417 ffs_fragacct(fs
, blk
, cgp
->cg_frsum
, 1);
1419 * if a complete block has been reassembled, account for it
1421 blkno
= fragstoblks(fs
, bbase
);
1422 if (ffs_isblock(fs
, cg_blksfree(cgp
), blkno
)) {
1423 cgp
->cg_cs
.cs_nffree
-= fs
->fs_frag
;
1424 fs
->fs_cstotal
.cs_nffree
-= fs
->fs_frag
;
1425 fs
->fs_cs(fs
, cg
).cs_nffree
-= fs
->fs_frag
;
1426 ffs_clusteracct(fs
, cgp
, blkno
, 1);
1427 cgp
->cg_cs
.cs_nbfree
++;
1428 fs
->fs_cstotal
.cs_nbfree
++;
1429 fs
->fs_cs(fs
, cg
).cs_nbfree
++;
1430 i
= cbtocylno(fs
, bbase
);
1431 cg_blks(fs
, cgp
, i
)[cbtorpos(fs
, bbase
)]++;
1432 cg_blktot(cgp
)[i
]++;
1438 byte_swap_cgout(cgp
,fs
);
1439 #endif /* REV_ENDIAN_FS */
1445 * Verify allocation of a block or fragment. Returns true if block or
1446 * fragment is allocated, false if it is free.
1448 ffs_checkblk(ip
, bno
, size
)
1456 int i
, error
, frags
, free
;
1458 struct vnode
*vp
=ITOV(ip
);
1459 struct mount
*mp
=vp
->v_mount
;
1460 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1461 #endif /* REV_ENDIAN_FS */
1464 if ((u_int
)size
> fs
->fs_bsize
|| fragoff(fs
, size
) != 0) {
1465 printf("bsize = %d, size = %d, fs = %s\n",
1466 fs
->fs_bsize
, size
, fs
->fs_fsmnt
);
1467 panic("checkblk: bad size");
1469 if ((u_int
)bno
>= fs
->fs_size
)
1470 panic("checkblk: bad block %d", bno
);
1471 error
= (int)buf_bread(ip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, dtog(fs
, bno
)))),
1472 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
1477 cgp
= (struct cg
*)buf_dataptr(bp
);
1480 byte_swap_cgin(cgp
,fs
);
1481 #endif /* REV_ENDIAN_FS */
1482 if (!cg_chkmagic(cgp
)) {
1485 byte_swap_cgout(cgp
,fs
);
1486 #endif /* REV_ENDIAN_FS */
1490 bno
= dtogd(fs
, bno
);
1491 if (size
== fs
->fs_bsize
) {
1492 free
= ffs_isblock(fs
, cg_blksfree(cgp
), fragstoblks(fs
, bno
));
1494 frags
= numfrags(fs
, size
);
1495 for (free
= 0, i
= 0; i
< frags
; i
++)
1496 if (isset(cg_blksfree(cgp
), bno
+ i
))
1498 if (free
!= 0 && free
!= frags
)
1499 panic("checkblk: partially free fragment");
1503 byte_swap_cgout(cgp
,fs
);
1504 #endif /* REV_ENDIAN_FS */
1508 #endif /* DIAGNOSTIC */
1513 * The specified inode is placed back in the free map.
1516 ffs_vfree(struct vnode
*vp
, ino_t ino
, int mode
)
1518 register struct fs
*fs
;
1519 register struct cg
*cgp
;
1520 register struct inode
*pip
;
1525 struct mount
*mp
=vp
->v_mount
;
1526 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1527 #endif /* REV_ENDIAN_FS */
1531 if ((u_int
)ino
>= fs
->fs_ipg
* fs
->fs_ncg
)
1532 panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n",
1533 pip
->i_dev
, ino
, fs
->fs_fsmnt
);
1534 cg
= ino_to_cg(fs
, ino
);
1535 error
= (int)buf_bread(pip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, cg
))),
1536 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
1541 cgp
= (struct cg
*)buf_dataptr(bp
);
1544 byte_swap_cgin(cgp
,fs
);
1545 #endif /* REV_ENDIAN_FS */
1546 if (!cg_chkmagic(cgp
)) {
1549 byte_swap_cgout(cgp
,fs
);
1550 #endif /* REV_ENDIAN_FS */
1555 cgp
->cg_time
= tv
.tv_sec
;
1557 if (isclr(cg_inosused(cgp
), ino
)) {
1558 printf("dev = 0x%x, ino = %d, fs = %s\n",
1559 pip
->i_dev
, ino
, fs
->fs_fsmnt
);
1560 if (fs
->fs_ronly
== 0)
1561 panic("ifree: freeing free inode");
1563 clrbit(cg_inosused(cgp
), ino
);
1564 if (ino
< cgp
->cg_irotor
)
1565 cgp
->cg_irotor
= ino
;
1566 cgp
->cg_cs
.cs_nifree
++;
1567 fs
->fs_cstotal
.cs_nifree
++;
1568 fs
->fs_cs(fs
, cg
).cs_nifree
++;
1569 if ((mode
& IFMT
) == IFDIR
) {
1570 cgp
->cg_cs
.cs_ndir
--;
1571 fs
->fs_cstotal
.cs_ndir
--;
1572 fs
->fs_cs(fs
, cg
).cs_ndir
--;
1577 byte_swap_cgout(cgp
,fs
);
1578 #endif /* REV_ENDIAN_FS */
1584 * Find a block of the specified size in the specified cylinder group.
1586 * It is a panic if a request is made to find a block if none are
1590 ffs_mapsearch(fs
, cgp
, bpref
, allocsiz
)
1591 register struct fs
*fs
;
1592 register struct cg
*cgp
;
1597 int start
, len
, loc
, i
;
1598 int blk
, field
, subfield
, pos
;
1601 * find the fragment by searching through the free block
1602 * map for an appropriate bit pattern
1605 start
= dtogd(fs
, bpref
) / NBBY
;
1607 start
= cgp
->cg_frotor
/ NBBY
;
1608 len
= howmany(fs
->fs_fpg
, NBBY
) - start
;
1609 loc
= scanc((u_int
)len
, (u_char
*)&cg_blksfree(cgp
)[start
],
1610 (u_char
*)fragtbl
[fs
->fs_frag
],
1611 (u_char
)(1 << (allocsiz
- 1 + (fs
->fs_frag
% NBBY
))));
1615 loc
= scanc((u_int
)len
, (u_char
*)&cg_blksfree(cgp
)[0],
1616 (u_char
*)fragtbl
[fs
->fs_frag
],
1617 (u_char
)(1 << (allocsiz
- 1 + (fs
->fs_frag
% NBBY
))));
1619 printf("start = %d, len = %d, fs = %s\n",
1620 start
, len
, fs
->fs_fsmnt
);
1621 panic("ffs_alloccg: map corrupted");
1625 bno
= (start
+ len
- loc
) * NBBY
;
1626 cgp
->cg_frotor
= bno
;
1628 * found the byte in the map
1629 * sift through the bits to find the selected frag
1631 for (i
= bno
+ NBBY
; bno
< i
; bno
+= fs
->fs_frag
) {
1632 blk
= blkmap(fs
, cg_blksfree(cgp
), bno
);
1634 field
= around
[allocsiz
];
1635 subfield
= inside
[allocsiz
];
1636 for (pos
= 0; pos
<= fs
->fs_frag
- allocsiz
; pos
++) {
1637 if ((blk
& field
) == subfield
)
1643 printf("bno = %d, fs = %s\n", bno
, fs
->fs_fsmnt
);
1644 panic("ffs_alloccg: block not in map");
1649 * Update the cluster map because of an allocation or free.
1651 * Cnt == 1 means free; cnt == -1 means allocating.
1654 ffs_clusteracct(struct fs
*fs
, struct cg
*cgp
, ufs_daddr_t blkno
, int cnt
)
1658 u_char
*freemapp
, *mapp
;
1659 int i
, start
, end
, forw
, back
, map
, bit
;
1661 if (fs
->fs_contigsumsize
<= 0)
1663 freemapp
= cg_clustersfree(cgp
);
1664 sump
= cg_clustersum(cgp
);
1666 * Allocate or clear the actual block.
1669 setbit(freemapp
, blkno
);
1671 clrbit(freemapp
, blkno
);
1673 * Find the size of the cluster going forward.
1676 end
= start
+ fs
->fs_contigsumsize
;
1677 if (end
>= cgp
->cg_nclusterblks
)
1678 end
= cgp
->cg_nclusterblks
;
1679 mapp
= &freemapp
[start
/ NBBY
];
1681 bit
= 1 << (start
% NBBY
);
1682 for (i
= start
; i
< end
; i
++) {
1683 if ((map
& bit
) == 0)
1685 if ((i
& (NBBY
- 1)) != (NBBY
- 1)) {
1694 * Find the size of the cluster going backward.
1697 end
= start
- fs
->fs_contigsumsize
;
1700 mapp
= &freemapp
[start
/ NBBY
];
1702 bit
= 1 << (start
% NBBY
);
1703 for (i
= start
; i
> end
; i
--) {
1704 if ((map
& bit
) == 0)
1706 if ((i
& (NBBY
- 1)) != 0) {
1710 bit
= 1 << (NBBY
- 1);
1715 * Account for old cluster and the possibly new forward and
1718 i
= back
+ forw
+ 1;
1719 if (i
> fs
->fs_contigsumsize
)
1720 i
= fs
->fs_contigsumsize
;
1727 * Update cluster summary information.
1729 lp
= &sump
[fs
->fs_contigsumsize
];
1730 for (i
= fs
->fs_contigsumsize
; i
> 0; i
--)
1733 fs
->fs_maxcluster
[cgp
->cg_cgx
] = i
;
1737 * Fserr prints the name of a file system with an error diagnostic.
1739 * The form of the error message is:
1743 ffs_fserr(fs
, uid
, cp
)
1749 log(LOG_ERR
, "uid %d on %s: %s\n", uid
, fs
->fs_fsmnt
, cp
);