]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ffs/ffs_alloc.c
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1982, 1986, 1989, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * @(#)ffs_alloc.c 8.18 (Berkeley) 5/26/95
58 #include <rev_endian_fs.h>
59 #include <vm/vm_pager.h>
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/buf_internal.h>
65 #include <sys/kauth.h>
66 #include <sys/vnode_internal.h>
67 #include <sys/mount_internal.h>
68 #include <sys/kernel.h>
69 #include <sys/syslog.h>
70 #include <sys/quota.h>
74 #include <ufs/ufs/quota.h>
75 #include <ufs/ufs/inode.h>
77 #include <ufs/ffs/fs.h>
78 #include <ufs/ffs/ffs_extern.h>
81 #include <ufs/ufs/ufs_byte_order.h>
82 #include <architecture/byte_order.h>
83 #endif /* REV_ENDIAN_FS */
85 extern u_long nextgennumber
;
87 static ufs_daddr_t
ffs_alloccg(struct inode
*, int, ufs_daddr_t
, int);
88 static ufs_daddr_t
ffs_alloccgblk(struct fs
*, struct cg
*, ufs_daddr_t
);
89 static ufs_daddr_t
ffs_clusteralloc(struct inode
*, int, ufs_daddr_t
, int);
90 static ino_t
ffs_dirpref(struct inode
*);
91 static ufs_daddr_t
ffs_fragextend(struct inode
*, int, long, int, int);
92 static void ffs_fserr(struct fs
*, u_int
, char *);
93 static u_long ffs_hashalloc
94 (struct inode
*, int, long, int, u_int32_t (*)());
95 static ino_t
ffs_nodealloccg(struct inode
*, int, ufs_daddr_t
, int);
96 static ufs_daddr_t
ffs_mapsearch(struct fs
*, struct cg
*, ufs_daddr_t
, int);
97 static void ffs_clusteracct
98 (struct fs
*fs
, struct cg
*cgp
, ufs_daddr_t blkno
, int cnt
);
101 * Allocate a block in the file system.
103 * The size of the requested block is given, which must be some
104 * multiple of fs_fsize and <= fs_bsize.
105 * A preference may be optionally specified. If a preference is given
106 * the following hierarchy is used to allocate a block:
107 * 1) allocate the requested block.
108 * 2) allocate a rotationally optimal block in the same cylinder.
109 * 3) allocate a block in the same cylinder group.
110 * 4) quadradically rehash into other cylinder groups, until an
111 * available block is located.
112 * If no block preference is given the following heirarchy is used
113 * to allocate a block:
114 * 1) allocate a block in the cylinder group that contains the
115 * inode for the file.
116 * 2) quadradically rehash into other cylinder groups, until an
117 * available block is located.
119 ffs_alloc(ip
, lbn
, bpref
, size
, cred
, bnp
)
120 register struct inode
*ip
;
121 ufs_daddr_t lbn
, bpref
;
126 register struct fs
*fs
;
133 if ((u_int
)size
> fs
->fs_bsize
|| fragoff(fs
, size
) != 0) {
134 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
135 ip
->i_dev
, fs
->fs_bsize
, size
, fs
->fs_fsmnt
);
136 panic("ffs_alloc: bad size");
139 panic("ffs_alloc: missing credential\n");
140 #endif /* DIAGNOSTIC */
141 if (size
== fs
->fs_bsize
&& fs
->fs_cstotal
.cs_nbfree
== 0)
143 if (suser(cred
, NULL
) && freespace(fs
, fs
->fs_minfree
) <= 0)
145 devBlockSize
= vfs_devblocksize(vnode_mount(ITOV(ip
)));
147 if (error
= chkdq(ip
, (int64_t)size
, cred
, 0))
150 if (bpref
>= fs
->fs_size
)
153 cg
= ino_to_cg(fs
, ip
->i_number
);
155 cg
= dtog(fs
, bpref
);
156 bno
= (ufs_daddr_t
)ffs_hashalloc(ip
, cg
, (long)bpref
, size
,
157 (u_int32_t (*)())ffs_alloccg
);
159 ip
->i_blocks
+= btodb(size
, devBlockSize
);
160 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
166 * Restore user's disk quota because allocation failed.
168 (void) chkdq(ip
, (int64_t)-size
, cred
, FORCE
);
171 ffs_fserr(fs
, kauth_cred_getuid(cred
), "file system full");
172 uprintf("\n%s: write failed, file system is full\n", fs
->fs_fsmnt
);
177 * Reallocate a fragment to a bigger size
179 * The number and size of the old block is given, and a preference
180 * and new size is also specified. The allocator attempts to extend
181 * the original block. Failing that, the regular block allocator is
182 * invoked to get an appropriate block.
184 ffs_realloccg(ip
, lbprev
, bpref
, osize
, nsize
, cred
, bpp
)
185 register struct inode
*ip
;
192 register struct fs
*fs
;
194 int cg
, request
, error
;
195 ufs_daddr_t bprev
, bno
;
201 if ((u_int
)osize
> fs
->fs_bsize
|| fragoff(fs
, osize
) != 0 ||
202 (u_int
)nsize
> fs
->fs_bsize
|| fragoff(fs
, nsize
) != 0) {
204 "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
205 ip
->i_dev
, fs
->fs_bsize
, osize
, nsize
, fs
->fs_fsmnt
);
206 panic("ffs_realloccg: bad size");
209 panic("ffs_realloccg: missing credential\n");
210 #endif /* DIAGNOSTIC */
211 if (suser(cred
, NULL
) != 0 && freespace(fs
, fs
->fs_minfree
) <= 0)
213 if ((bprev
= ip
->i_db
[lbprev
]) == 0) {
214 printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
215 ip
->i_dev
, fs
->fs_bsize
, bprev
, fs
->fs_fsmnt
);
216 panic("ffs_realloccg: bad bprev");
219 * Allocate the extra space in the buffer.
221 if (error
= (int)buf_bread(ITOV(ip
), (daddr64_t
)((unsigned)lbprev
), osize
, NOCRED
, &bp
)) {
225 devBlockSize
= vfs_devblocksize(vnode_mount(ITOV(ip
)));
228 if (error
= chkdq(ip
, (int64_t)(nsize
- osize
), cred
, 0))
235 * Check for extension in the existing location.
237 cg
= dtog(fs
, bprev
);
238 if (bno
= ffs_fragextend(ip
, cg
, (long)bprev
, osize
, nsize
)) {
239 if ((ufs_daddr_t
)buf_blkno(bp
) != fsbtodb(fs
, bno
))
240 panic("bad blockno");
241 ip
->i_blocks
+= btodb(nsize
- osize
, devBlockSize
);
242 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
244 buf_setflags(bp
, B_DONE
);
245 bzero((char *)buf_dataptr(bp
) + osize
, (u_int
)buf_size(bp
) - osize
);
250 * Allocate a new disk location.
252 if (bpref
>= fs
->fs_size
)
254 switch ((int)fs
->fs_optim
) {
257 * Allocate an exact sized fragment. Although this makes
258 * best use of space, we will waste time relocating it if
259 * the file continues to grow. If the fragmentation is
260 * less than half of the minimum free reserve, we choose
261 * to begin optimizing for time.
264 if (fs
->fs_minfree
< 5 ||
265 fs
->fs_cstotal
.cs_nffree
>
266 fs
->fs_dsize
* fs
->fs_minfree
/ (2 * 100))
268 log(LOG_NOTICE
, "%s: optimization changed from SPACE to TIME\n",
270 fs
->fs_optim
= FS_OPTTIME
;
274 * At this point we have discovered a file that is trying to
275 * grow a small fragment to a larger fragment. To save time,
276 * we allocate a full sized block, then free the unused portion.
277 * If the file continues to grow, the `ffs_fragextend' call
278 * above will be able to grow it in place without further
279 * copying. If aberrant programs cause disk fragmentation to
280 * grow within 2% of the free reserve, we choose to begin
281 * optimizing for space.
283 request
= fs
->fs_bsize
;
284 if (fs
->fs_cstotal
.cs_nffree
<
285 fs
->fs_dsize
* (fs
->fs_minfree
- 2) / 100)
287 log(LOG_NOTICE
, "%s: optimization changed from TIME to SPACE\n",
289 fs
->fs_optim
= FS_OPTSPACE
;
292 printf("dev = 0x%x, optim = %d, fs = %s\n",
293 ip
->i_dev
, fs
->fs_optim
, fs
->fs_fsmnt
);
294 panic("ffs_realloccg: bad optim");
297 bno
= (ufs_daddr_t
)ffs_hashalloc(ip
, cg
, (long)bpref
, request
,
298 (u_int32_t (*)())ffs_alloccg
);
300 buf_setblkno(bp
, (daddr64_t
)((unsigned)fsbtodb(fs
, bno
)));
301 ffs_blkfree(ip
, bprev
, (long)osize
);
303 ffs_blkfree(ip
, bno
+ numfrags(fs
, nsize
),
304 (long)(request
- nsize
));
305 ip
->i_blocks
+= btodb(nsize
- osize
, devBlockSize
);
306 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
308 buf_setflags(bp
, B_DONE
);
309 bzero((char *)buf_dataptr(bp
) + osize
, (u_int
)buf_size(bp
) - osize
);
315 * Restore user's disk quota because allocation failed.
317 (void) chkdq(ip
, (int64_t)-(nsize
- osize
), cred
, FORCE
);
324 ffs_fserr(fs
, kauth_cred_getuid(cred
), "file system full");
325 uprintf("\n%s: write failed, file system is full\n", fs
->fs_fsmnt
);
330 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
332 * The vnode and an array of buffer pointers for a range of sequential
333 * logical blocks to be made contiguous is given. The allocator attempts
334 * to find a range of sequential blocks starting as close as possible to
335 * an fs_rotdelay offset from the end of the allocation for the logical
336 * block immediately preceeding the current range. If successful, the
337 * physical block numbers in the buffer pointers and in the inode are
338 * changed to reflect the new allocation. If unsuccessful, the allocation
339 * is left unchanged. The success in doing the reallocation is returned.
340 * Note that the error return is not reflected back to the user. Rather
341 * the previous block allocation will be used.
343 /* Note: This routine is unused in UBC cluster I/O */
346 int doreallocblks
= 1;
350 * Allocate an inode in the file system.
352 * If allocating a directory, use ffs_dirpref to select the inode.
353 * If allocating in a directory, the following hierarchy is followed:
354 * 1) allocate the preferred inode.
355 * 2) allocate an inode in the same cylinder group.
356 * 3) quadradically rehash into other cylinder groups, until an
357 * available inode is located.
358 * If no inode preference is given the following heirarchy is used
359 * to allocate an inode:
360 * 1) allocate an inode in cylinder group 0.
361 * 2) quadradically rehash into other cylinder groups, until an
362 * available inode is located.
372 register struct inode
*pip
;
373 register struct fs
*fs
;
374 register struct inode
*ip
;
382 if (fs
->fs_cstotal
.cs_nifree
== 0)
385 if ((mode
& IFMT
) == IFDIR
)
386 ipref
= ffs_dirpref(pip
);
388 ipref
= pip
->i_number
;
389 if (ipref
>= fs
->fs_ncg
* fs
->fs_ipg
)
391 cg
= ino_to_cg(fs
, ipref
);
393 * Track the number of dirs created one after another
394 * in a cg without intervening files.
396 if ((mode
& IFMT
) == IFDIR
) {
397 if (fs
->fs_contigdirs
[cg
] < 255)
398 fs
->fs_contigdirs
[cg
]++;
400 if (fs
->fs_contigdirs
[cg
] > 0)
401 fs
->fs_contigdirs
[cg
]--;
403 ino
= (ino_t
)ffs_hashalloc(pip
, cg
, (long)ipref
, mode
, ffs_nodealloccg
);
407 error
= ffs_vget_internal(pvp
->v_mount
, ino
, vpp
, NULL
, NULL
, mode
, 0);
409 ffs_vfree(pvp
, ino
, mode
);
415 printf("mode = 0%o, inum = %d, fs = %s\n",
416 ip
->i_mode
, ip
->i_number
, fs
->fs_fsmnt
);
417 panic("ffs_valloc: dup alloc");
419 if (ip
->i_blocks
) { /* XXX */
420 printf("free inode %s/%d had %d blocks\n",
421 fs
->fs_fsmnt
, ino
, ip
->i_blocks
);
426 * Set up a new generation number for this inode.
429 if (++nextgennumber
< (u_long
)tv
.tv_sec
)
430 nextgennumber
= tv
.tv_sec
;
431 ip
->i_gen
= nextgennumber
;
434 ffs_fserr(fs
, kauth_cred_getuid(cred
), "out of inodes");
435 uprintf("\n%s: create/symlink failed, no inodes free\n", fs
->fs_fsmnt
);
440 * Find a cylinder group to place a directory.
442 * The policy implemented by this algorithm is to allocate a
443 * directory inode in the same cylinder group as its parent
444 * directory, but also to reserve space for its files inodes
445 * and data. Restrict the number of directories which may be
446 * allocated one after another in the same cylinder group
447 * without intervening allocation of files.
453 register struct fs
*fs
;
454 int cg
, prefcg
, dirsize
, cgsize
;
455 int avgifree
, avgbfree
, avgndir
, curdirsize
;
456 int minifree
, minbfree
, maxndir
;
461 avgifree
= fs
->fs_cstotal
.cs_nifree
/ fs
->fs_ncg
;
462 avgbfree
= fs
->fs_cstotal
.cs_nbfree
/ fs
->fs_ncg
;
463 avgndir
= fs
->fs_cstotal
.cs_ndir
/ fs
->fs_ncg
;
466 * Force allocation in another cg if creating a first level dir.
468 if (ITOV(pip
)->v_flag
& VROOT
) {
470 prefcg
= random() % fs
->fs_ncg
;
472 prefcg
= arc4random() % fs
->fs_ncg
;
475 minndir
= fs
->fs_ipg
;
476 for (cg
= prefcg
; cg
< fs
->fs_ncg
; cg
++)
477 if (fs
->fs_cs(fs
, cg
).cs_ndir
< minndir
&&
478 fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
&&
479 fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
481 minndir
= fs
->fs_cs(fs
, cg
).cs_ndir
;
483 for (cg
= 0; cg
< prefcg
; 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 return ((ino_t
)(fs
->fs_ipg
* mincg
));
494 * Count various limits which used for
495 * optimal allocation of a directory inode.
497 maxndir
= min(avgndir
+ fs
->fs_ipg
/ 16, fs
->fs_ipg
);
498 minifree
= avgifree
- fs
->fs_ipg
/ 4;
501 minbfree
= avgbfree
- fs
->fs_fpg
/ fs
->fs_frag
/ 4;
504 cgsize
= fs
->fs_fsize
* fs
->fs_fpg
;
505 dirsize
= fs
->fs_avgfilesize
* fs
->fs_avgfpdir
;
506 curdirsize
= avgndir
? (cgsize
- avgbfree
* fs
->fs_bsize
) / avgndir
: 0;
507 if (dirsize
< curdirsize
)
508 dirsize
= curdirsize
;
509 maxcontigdirs
= min(cgsize
/ dirsize
, 255);
510 if (fs
->fs_avgfpdir
> 0)
511 maxcontigdirs
= min(maxcontigdirs
,
512 fs
->fs_ipg
/ fs
->fs_avgfpdir
);
513 if (maxcontigdirs
== 0)
517 * Limit number of dirs in one cg and reserve space for
518 * regular files, but only if we have no deficit in
521 prefcg
= ino_to_cg(fs
, pip
->i_number
);
522 for (cg
= prefcg
; cg
< fs
->fs_ncg
; cg
++)
523 if (fs
->fs_cs(fs
, cg
).cs_ndir
< maxndir
&&
524 fs
->fs_cs(fs
, cg
).cs_nifree
>= minifree
&&
525 fs
->fs_cs(fs
, cg
).cs_nbfree
>= minbfree
) {
526 if (fs
->fs_contigdirs
[cg
] < maxcontigdirs
)
527 return ((ino_t
)(fs
->fs_ipg
* cg
));
529 for (cg
= 0; cg
< prefcg
; 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
));
537 * This is a backstop when we have deficit in space.
539 for (cg
= prefcg
; cg
< fs
->fs_ncg
; cg
++)
540 if (fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
)
541 return ((ino_t
)(fs
->fs_ipg
* cg
));
542 for (cg
= 0; cg
< prefcg
; cg
++)
543 if (fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
)
545 return ((ino_t
)(fs
->fs_ipg
* cg
));
549 * Select the desired position for the next block in a file. The file is
550 * logically divided into sections. The first section is composed of the
551 * direct blocks. Each additional section contains fs_maxbpg blocks.
553 * If no blocks have been allocated in the first section, the policy is to
554 * request a block in the same cylinder group as the inode that describes
555 * the file. If no blocks have been allocated in any other section, the
556 * policy is to place the section in a cylinder group with a greater than
557 * average number of free blocks. An appropriate cylinder group is found
558 * by using a rotor that sweeps the cylinder groups. When a new group of
559 * blocks is needed, the sweep begins in the cylinder group following the
560 * cylinder group from which the previous allocation was made. The sweep
561 * continues until a cylinder group with greater than the average number
562 * of free blocks is found. If the allocation is for the first block in an
563 * indirect block, the information on the previous allocation is unavailable;
564 * here a best guess is made based upon the logical block number being
567 * If a section is already partially allocated, the policy is to
568 * contiguously allocate fs_maxcontig blocks. The end of one of these
569 * contiguous blocks and the beginning of the next is physically separated
570 * so that the disk head will be in transit between them for at least
571 * fs_rotdelay milliseconds. This is to allow time for the processor to
572 * schedule another I/O transfer.
575 ffs_blkpref(ip
, lbn
, indx
, bap
)
581 register struct fs
*fs
;
583 int avgbfree
, startcg
;
587 struct vnode
*vp
=ITOV(ip
);
588 struct mount
*mp
=vp
->v_mount
;
589 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
590 #endif /* REV_ENDIAN_FS */
596 if (bap
!= &ip
->i_db
[0])
597 prev
= NXSwapLong(bap
[indx
- 1]);
599 prev
= bap
[indx
- 1];
600 } else prev
= bap
[indx
- 1];
602 if (indx
% fs
->fs_maxbpg
== 0 || prev
== 0)
603 #else /* REV_ENDIAN_FS */
604 if (indx
% fs
->fs_maxbpg
== 0 || bap
[indx
- 1] == 0)
605 #endif /* REV_ENDIAN_FS */
608 cg
= ino_to_cg(fs
, ip
->i_number
);
609 return (fs
->fs_fpg
* cg
+ fs
->fs_frag
);
612 * Find a cylinder with greater than average number of
613 * unused data blocks.
616 if (indx
== 0 || prev
== 0)
617 #else /* REV_ENDIAN_FS */
618 if (indx
== 0 || bap
[indx
- 1] == 0)
619 #endif /* REV_ENDIAN_FS */
621 ino_to_cg(fs
, ip
->i_number
) + lbn
/ fs
->fs_maxbpg
;
624 startcg
= dtog(fs
, prev
) + 1;
625 #else /* REV_ENDIAN_FS */
626 startcg
= dtog(fs
, bap
[indx
- 1]) + 1;
627 #endif /* REV_ENDIAN_FS */
628 startcg
%= fs
->fs_ncg
;
629 avgbfree
= fs
->fs_cstotal
.cs_nbfree
/ fs
->fs_ncg
;
630 for (cg
= startcg
; cg
< fs
->fs_ncg
; cg
++)
631 if (fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
633 return (fs
->fs_fpg
* cg
+ fs
->fs_frag
);
635 for (cg
= 0; cg
<= startcg
; cg
++)
636 if (fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
638 return (fs
->fs_fpg
* cg
+ fs
->fs_frag
);
643 * One or more previous blocks have been laid out. If less
644 * than fs_maxcontig previous blocks are contiguous, the
645 * next block is requested contiguously, otherwise it is
646 * requested rotationally delayed by fs_rotdelay milliseconds.
650 nextblk
= prev
+ fs
->fs_frag
;
651 if (indx
< fs
->fs_maxcontig
) {
654 if (bap
!= &ip
->i_db
[0])
655 prev
= NXSwapLong(bap
[indx
- fs
->fs_maxcontig
]);
657 prev
= bap
[indx
- fs
->fs_maxcontig
];
658 if (prev
+ blkstofrags(fs
, fs
->fs_maxcontig
) != nextblk
)
661 #endif /* REV_ENDIAN_FS */
662 nextblk
= bap
[indx
- 1] + fs
->fs_frag
;
663 if (indx
< fs
->fs_maxcontig
|| bap
[indx
- fs
->fs_maxcontig
] +
664 blkstofrags(fs
, fs
->fs_maxcontig
) != nextblk
)
668 #endif /* REV_ENDIAN_FS */
669 if (fs
->fs_rotdelay
!= 0)
671 * Here we convert ms of delay to frags as:
672 * (frags) = (ms) * (rev/sec) * (sect/rev) /
673 * ((sect/frag) * (ms/sec))
674 * then round up to the next block.
676 nextblk
+= roundup(fs
->fs_rotdelay
* fs
->fs_rps
* fs
->fs_nsect
/
677 (NSPF(fs
) * 1000), fs
->fs_frag
);
682 * Implement the cylinder overflow algorithm.
684 * The policy implemented by this algorithm is:
685 * 1) allocate the block in its requested cylinder group.
686 * 2) quadradically rehash on the cylinder group number.
687 * 3) brute force search for a free block.
691 ffs_hashalloc(ip
, cg
, pref
, size
, allocator
)
695 int size
; /* size for data blocks, mode for inodes */
696 u_int32_t (*allocator
)();
698 register struct fs
*fs
;
704 * 1: preferred cylinder group
706 result
= (*allocator
)(ip
, cg
, pref
, size
);
710 * 2: quadratic rehash
712 for (i
= 1; i
< fs
->fs_ncg
; i
*= 2) {
714 if (cg
>= fs
->fs_ncg
)
716 result
= (*allocator
)(ip
, cg
, 0, size
);
721 * 3: brute force search
722 * Note that we start at i == 2, since 0 was checked initially,
723 * and 1 is always checked in the quadratic rehash.
725 cg
= (icg
+ 2) % fs
->fs_ncg
;
726 for (i
= 2; i
< fs
->fs_ncg
; i
++) {
727 result
= (*allocator
)(ip
, cg
, 0, size
);
731 if (cg
== fs
->fs_ncg
)
738 * Determine whether a fragment can be extended.
740 * Check to see if the necessary fragments are available, and
741 * if they are, allocate them.
744 ffs_fragextend(ip
, cg
, bprev
, osize
, nsize
)
750 register struct fs
*fs
;
751 register struct cg
*cgp
;
758 struct vnode
*vp
=ITOV(ip
);
759 struct mount
*mp
=vp
->v_mount
;
760 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
761 #endif /* REV_ENDIAN_FS */
764 if (fs
->fs_cs(fs
, cg
).cs_nffree
< numfrags(fs
, nsize
- osize
))
766 frags
= numfrags(fs
, nsize
); /* number of fragments needed */
767 bbase
= fragnum(fs
, bprev
); /* offset in a frag (it is mod fragsize */
768 if (bbase
> fragnum(fs
, (bprev
+ frags
- 1))) {
769 /* cannot extend across a block boundary */
772 /* read corresponding cylinder group info */
773 error
= (int)buf_bread(ip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, cg
))),
774 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
779 cgp
= (struct cg
*)buf_dataptr(bp
);
782 byte_swap_cgin(cgp
, fs
);
784 #endif /* REV_ENDIAN_FS */
786 if (!cg_chkmagic(cgp
)) {
789 byte_swap_cgout(cgp
,fs
);
790 #endif /* REV_ENDIAN_FS */
795 cgp
->cg_time
= tv
.tv_sec
;
796 bno
= dtogd(fs
, bprev
);
797 for (i
= numfrags(fs
, osize
); i
< frags
; i
++)
798 if (isclr(cg_blksfree(cgp
), bno
+ i
)) {
801 byte_swap_cgout(cgp
,fs
);
802 #endif /* REV_ENDIAN_FS */
807 * the current fragment can be extended
808 * deduct the count on fragment being extended into
809 * increase the count on the remaining fragment (if any)
810 * allocate the extended piece
812 for (i
= frags
; i
< fs
->fs_frag
- bbase
; i
++)
813 if (isclr(cg_blksfree(cgp
), bno
+ i
))
815 cgp
->cg_frsum
[i
- numfrags(fs
, osize
)]--;
817 cgp
->cg_frsum
[i
- frags
]++;
818 for (i
= numfrags(fs
, osize
); i
< frags
; i
++) {
819 clrbit(cg_blksfree(cgp
), bno
+ i
);
820 cgp
->cg_cs
.cs_nffree
--;
821 fs
->fs_cstotal
.cs_nffree
--;
822 fs
->fs_cs(fs
, cg
).cs_nffree
--;
827 byte_swap_cgout(cgp
,fs
);
828 #endif /* REV_ENDIAN_FS */
834 * Determine whether a block can be allocated.
836 * Check to see if a block of the appropriate size is available,
837 * and if it is, allocate it.
840 ffs_alloccg(ip
, cg
, bpref
, size
)
846 register struct fs
*fs
;
847 register struct cg
*cgp
;
851 int error
, bno
, frags
, allocsiz
;
853 struct vnode
*vp
=ITOV(ip
);
854 struct mount
*mp
=vp
->v_mount
;
855 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
856 #endif /* REV_ENDIAN_FS */
859 if (fs
->fs_cs(fs
, cg
).cs_nbfree
== 0 && size
== fs
->fs_bsize
)
861 error
= (int)buf_bread(ip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, cg
))),
862 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
867 cgp
= (struct cg
*)buf_dataptr(bp
);
870 byte_swap_cgin(cgp
,fs
);
871 #endif /* REV_ENDIAN_FS */
872 if (!cg_chkmagic(cgp
) ||
873 (cgp
->cg_cs
.cs_nbfree
== 0 && size
== fs
->fs_bsize
)) {
876 byte_swap_cgout(cgp
,fs
);
877 #endif /* REV_ENDIAN_FS */
882 cgp
->cg_time
= tv
.tv_sec
;
883 if (size
== fs
->fs_bsize
) {
884 bno
= ffs_alloccgblk(fs
, cgp
, bpref
);
887 byte_swap_cgout(cgp
,fs
);
888 #endif /* REV_ENDIAN_FS */
893 * check to see if any fragments are already available
894 * allocsiz is the size which will be allocated, hacking
895 * it down to a smaller size if necessary
897 frags
= numfrags(fs
, size
);
898 for (allocsiz
= frags
; allocsiz
< fs
->fs_frag
; allocsiz
++)
899 if (cgp
->cg_frsum
[allocsiz
] != 0)
901 if (allocsiz
== fs
->fs_frag
) {
903 * no fragments were available, so a block will be
904 * allocated, and hacked up
906 if (cgp
->cg_cs
.cs_nbfree
== 0) {
909 byte_swap_cgout(cgp
,fs
);
910 #endif /* REV_ENDIAN_FS */
914 bno
= ffs_alloccgblk(fs
, cgp
, bpref
);
915 bpref
= dtogd(fs
, bno
);
916 for (i
= frags
; i
< fs
->fs_frag
; i
++)
917 setbit(cg_blksfree(cgp
), bpref
+ i
);
918 i
= fs
->fs_frag
- frags
;
919 cgp
->cg_cs
.cs_nffree
+= i
;
920 fs
->fs_cstotal
.cs_nffree
+= i
;
921 fs
->fs_cs(fs
, cg
).cs_nffree
+= i
;
926 byte_swap_cgout(cgp
,fs
);
927 #endif /* REV_ENDIAN_FS */
931 bno
= ffs_mapsearch(fs
, cgp
, bpref
, allocsiz
);
935 byte_swap_cgout(cgp
,fs
);
936 #endif /* REV_ENDIAN_FS */
940 for (i
= 0; i
< frags
; i
++)
941 clrbit(cg_blksfree(cgp
), bno
+ i
);
942 cgp
->cg_cs
.cs_nffree
-= frags
;
943 fs
->fs_cstotal
.cs_nffree
-= frags
;
944 fs
->fs_cs(fs
, cg
).cs_nffree
-= frags
;
946 cgp
->cg_frsum
[allocsiz
]--;
947 if (frags
!= allocsiz
)
948 cgp
->cg_frsum
[allocsiz
- frags
]++;
951 byte_swap_cgout(cgp
,fs
);
952 #endif /* REV_ENDIAN_FS */
954 return (cg
* fs
->fs_fpg
+ bno
);
958 * Allocate a block in a cylinder group.
960 * This algorithm implements the following policy:
961 * 1) allocate the requested block.
962 * 2) allocate a rotationally optimal block in the same cylinder.
963 * 3) allocate the next available block on the block rotor for the
964 * specified cylinder group.
965 * Note that this routine only allocates fs_bsize blocks; these
966 * blocks may be fragmented by the routine that allocates them.
969 ffs_alloccgblk(fs
, cgp
, bpref
)
970 register struct fs
*fs
;
971 register struct cg
*cgp
;
974 ufs_daddr_t bno
, blkno
;
975 int cylno
, pos
, delta
;
979 if (bpref
== 0 || dtog(fs
, bpref
) != cgp
->cg_cgx
) {
980 bpref
= cgp
->cg_rotor
;
983 bpref
= blknum(fs
, bpref
);
984 bpref
= dtogd(fs
, bpref
);
986 * if the requested block is available, use it
988 if (ffs_isblock(fs
, cg_blksfree(cgp
), fragstoblks(fs
, bpref
))) {
992 if (fs
->fs_nrpos
<= 1 || fs
->fs_cpc
== 0) {
994 * Block layout information is not available.
995 * Leaving bpref unchanged means we take the
996 * next available free block following the one
997 * we just allocated. Hopefully this will at
998 * least hit a track cache on drives of unknown
999 * geometry (e.g. SCSI).
1004 * check for a block available on the same cylinder
1006 cylno
= cbtocylno(fs
, bpref
);
1007 if (cg_blktot(cgp
)[cylno
] == 0)
1010 * check the summary information to see if a block is
1011 * available in the requested cylinder starting at the
1012 * requested rotational position and proceeding around.
1014 cylbp
= cg_blks(fs
, cgp
, cylno
);
1015 pos
= cbtorpos(fs
, bpref
);
1016 for (i
= pos
; i
< fs
->fs_nrpos
; i
++)
1019 if (i
== fs
->fs_nrpos
)
1020 for (i
= 0; i
< pos
; i
++)
1025 * found a rotational position, now find the actual
1026 * block. A panic if none is actually there.
1028 pos
= cylno
% fs
->fs_cpc
;
1029 bno
= (cylno
- pos
) * fs
->fs_spc
/ NSPB(fs
);
1030 if (fs_postbl(fs
, pos
)[i
] == -1) {
1031 printf("pos = %d, i = %d, fs = %s\n",
1032 pos
, i
, fs
->fs_fsmnt
);
1033 panic("ffs_alloccgblk: cyl groups corrupted");
1035 for (i
= fs_postbl(fs
, pos
)[i
];; ) {
1036 if (ffs_isblock(fs
, cg_blksfree(cgp
), bno
+ i
)) {
1037 bno
= blkstofrags(fs
, (bno
+ i
));
1040 delta
= fs_rotbl(fs
)[i
];
1042 delta
+ i
> fragstoblks(fs
, fs
->fs_fpg
))
1046 printf("pos = %d, i = %d, fs = %s\n", pos
, i
, fs
->fs_fsmnt
);
1047 panic("ffs_alloccgblk: can't find blk in cyl");
1051 * no blocks in the requested cylinder, so take next
1052 * available one in this cylinder group.
1054 bno
= ffs_mapsearch(fs
, cgp
, bpref
, (int)fs
->fs_frag
);
1057 cgp
->cg_rotor
= bno
;
1059 blkno
= fragstoblks(fs
, bno
);
1060 ffs_clrblock(fs
, cg_blksfree(cgp
), (long)blkno
);
1061 ffs_clusteracct(fs
, cgp
, blkno
, -1);
1062 cgp
->cg_cs
.cs_nbfree
--;
1063 fs
->fs_cstotal
.cs_nbfree
--;
1064 fs
->fs_cs(fs
, cgp
->cg_cgx
).cs_nbfree
--;
1065 cylno
= cbtocylno(fs
, bno
);
1066 cg_blks(fs
, cgp
, cylno
)[cbtorpos(fs
, bno
)]--;
1067 cg_blktot(cgp
)[cylno
]--;
1069 return (cgp
->cg_cgx
* fs
->fs_fpg
+ bno
);
1073 * Determine whether a cluster can be allocated.
1075 * We do not currently check for optimal rotational layout if there
1076 * are multiple choices in the same cylinder group. Instead we just
1077 * take the first one that we find following bpref.
1080 ffs_clusteralloc(ip
, cg
, bpref
, len
)
1086 register struct fs
*fs
;
1087 register struct cg
*cgp
;
1089 int i
, got
, run
, bno
, bit
, map
;
1093 struct vnode
*vp
=ITOV(ip
);
1094 struct mount
*mp
=vp
->v_mount
;
1095 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1096 #endif /* REV_ENDIAN_FS */
1099 if (fs
->fs_maxcluster
[cg
] < len
)
1101 if (buf_bread(ip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, cg
))), (int)fs
->fs_cgsize
,
1104 cgp
= (struct cg
*)buf_dataptr(bp
);
1107 byte_swap_cgin(cgp
,fs
);
1108 #endif /* REV_ENDIAN_FS */
1109 if (!cg_chkmagic(cgp
)) {
1112 byte_swap_cgout(cgp
,fs
);
1113 #endif /* REV_ENDIAN_FS */
1117 * Check to see if a cluster of the needed size (or bigger) is
1118 * available in this cylinder group.
1120 lp
= &cg_clustersum(cgp
)[len
];
1121 for (i
= len
; i
<= fs
->fs_contigsumsize
; i
++)
1124 if (i
> fs
->fs_contigsumsize
) {
1126 * This is the first time looking for a cluster in this
1127 * cylinder group. Update the cluster summary information
1128 * to reflect the true maximum sized cluster so that
1129 * future cluster allocation requests can avoid reading
1130 * the cylinder group map only to find no clusters.
1132 lp
= &cg_clustersum(cgp
)[len
- 1];
1133 for (i
= len
- 1; i
> 0; i
--)
1136 fs
->fs_maxcluster
[cg
] = i
;
1139 byte_swap_cgout(cgp
,fs
);
1140 #endif /* REV_ENDIAN_FS */
1144 * Search the cluster map to find a big enough cluster.
1145 * We take the first one that we find, even if it is larger
1146 * than we need as we prefer to get one close to the previous
1147 * block allocation. We do not search before the current
1148 * preference point as we do not want to allocate a block
1149 * that is allocated before the previous one (as we will
1150 * then have to wait for another pass of the elevator
1151 * algorithm before it will be read). We prefer to fail and
1152 * be recalled to try an allocation in the next cylinder group.
1154 if (dtog(fs
, bpref
) != cg
)
1157 bpref
= fragstoblks(fs
, dtogd(fs
, blknum(fs
, bpref
)));
1158 mapp
= &cg_clustersfree(cgp
)[bpref
/ NBBY
];
1160 bit
= 1 << (bpref
% NBBY
);
1161 for (run
= 0, got
= bpref
; got
< cgp
->cg_nclusterblks
; got
++) {
1162 if ((map
& bit
) == 0) {
1169 if ((got
& (NBBY
- 1)) != (NBBY
- 1)) {
1176 if (got
== cgp
->cg_nclusterblks
) {
1179 byte_swap_cgout(cgp
,fs
);
1180 #endif /* REV_ENDIAN_FS */
1184 * Allocate the cluster that we have found.
1186 for (i
= 1; i
<= len
; i
++)
1187 if (!ffs_isblock(fs
, cg_blksfree(cgp
), got
- run
+ i
))
1188 panic("ffs_clusteralloc: map mismatch");
1189 bno
= cg
* fs
->fs_fpg
+ blkstofrags(fs
, got
- run
+ 1);
1190 if (dtog(fs
, bno
) != cg
)
1191 panic("ffs_clusteralloc: allocated out of group");
1192 len
= blkstofrags(fs
, len
);
1193 for (i
= 0; i
< len
; i
+= fs
->fs_frag
)
1194 if ((got
= ffs_alloccgblk(fs
, cgp
, bno
+ i
)) != bno
+ i
)
1195 panic("ffs_clusteralloc: lost block");
1198 byte_swap_cgout(cgp
,fs
);
1199 #endif /* REV_ENDIAN_FS */
1209 * Determine whether an inode can be allocated.
1211 * Check to see if an inode is available, and if it is,
1212 * allocate it using the following policy:
1213 * 1) allocate the requested inode.
1214 * 2) allocate the next available inode after the requested
1215 * inode in the specified cylinder group.
1218 ffs_nodealloccg(ip
, cg
, ipref
, mode
)
1224 register struct fs
*fs
;
1225 register struct cg
*cgp
;
1228 int error
, start
, len
, loc
, map
, i
;
1230 struct vnode
*vp
=ITOV(ip
);
1231 struct mount
*mp
=vp
->v_mount
;
1232 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1233 #endif /* REV_ENDIAN_FS */
1236 if (fs
->fs_cs(fs
, cg
).cs_nifree
== 0)
1238 error
= (int)buf_bread(ip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, cg
))),
1239 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
1244 cgp
= (struct cg
*)buf_dataptr(bp
);
1247 byte_swap_cgin(cgp
,fs
);
1248 #endif /* REV_ENDIAN_FS */
1249 if (!cg_chkmagic(cgp
) || cgp
->cg_cs
.cs_nifree
== 0) {
1252 byte_swap_cgout(cgp
,fs
);
1253 #endif /* REV_ENDIAN_FS */
1259 cgp
->cg_time
= tv
.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.
1318 ffs_blkfree(ip
, bno
, size
)
1319 register struct inode
*ip
;
1323 register struct fs
*fs
;
1324 register struct cg
*cgp
;
1328 int i
, error
, cg
, blk
, frags
, bbase
;
1330 struct vnode
*vp
=ITOV(ip
);
1331 struct mount
*mp
=vp
->v_mount
;
1332 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1333 #endif /* REV_ENDIAN_FS */
1336 if ((u_int
)size
> fs
->fs_bsize
|| fragoff(fs
, size
) != 0) {
1337 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
1338 ip
->i_dev
, fs
->fs_bsize
, size
, fs
->fs_fsmnt
);
1339 panic("blkfree: bad size");
1342 if ((u_int
)bno
>= fs
->fs_size
) {
1343 printf("bad block %d, ino %d\n", bno
, ip
->i_number
);
1344 ffs_fserr(fs
, ip
->i_uid
, "bad block");
1347 error
= (int)buf_bread(ip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, cg
))),
1348 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
1353 cgp
= (struct cg
*)buf_dataptr(bp
);
1356 byte_swap_cgin(cgp
,fs
);
1357 #endif /* REV_ENDIAN_FS */
1358 if (!cg_chkmagic(cgp
)) {
1361 byte_swap_cgout(cgp
,fs
);
1362 #endif /* REV_ENDIAN_FS */
1367 cgp
->cg_time
= tv
.tv_sec
;
1368 bno
= dtogd(fs
, bno
);
1369 if (size
== fs
->fs_bsize
) {
1370 blkno
= fragstoblks(fs
, bno
);
1371 if (ffs_isblock(fs
, cg_blksfree(cgp
), blkno
)) {
1372 printf("dev = 0x%x, block = %d, fs = %s\n",
1373 ip
->i_dev
, bno
, fs
->fs_fsmnt
);
1374 panic("blkfree: freeing free block");
1376 ffs_setblock(fs
, cg_blksfree(cgp
), blkno
);
1377 ffs_clusteracct(fs
, cgp
, blkno
, 1);
1378 cgp
->cg_cs
.cs_nbfree
++;
1379 fs
->fs_cstotal
.cs_nbfree
++;
1380 fs
->fs_cs(fs
, cg
).cs_nbfree
++;
1381 i
= cbtocylno(fs
, bno
);
1382 cg_blks(fs
, cgp
, i
)[cbtorpos(fs
, bno
)]++;
1383 cg_blktot(cgp
)[i
]++;
1385 bbase
= bno
- fragnum(fs
, bno
);
1387 * decrement the counts associated with the old frags
1389 blk
= blkmap(fs
, cg_blksfree(cgp
), bbase
);
1390 ffs_fragacct(fs
, blk
, cgp
->cg_frsum
, -1);
1392 * deallocate the fragment
1394 frags
= numfrags(fs
, size
);
1395 for (i
= 0; i
< frags
; i
++) {
1396 if (isset(cg_blksfree(cgp
), bno
+ i
)) {
1397 printf("dev = 0x%x, block = %d, fs = %s\n",
1398 ip
->i_dev
, bno
+ i
, fs
->fs_fsmnt
);
1399 panic("blkfree: freeing free frag");
1401 setbit(cg_blksfree(cgp
), bno
+ i
);
1403 cgp
->cg_cs
.cs_nffree
+= i
;
1404 fs
->fs_cstotal
.cs_nffree
+= i
;
1405 fs
->fs_cs(fs
, cg
).cs_nffree
+= i
;
1407 * add back in counts associated with the new frags
1409 blk
= blkmap(fs
, cg_blksfree(cgp
), bbase
);
1410 ffs_fragacct(fs
, blk
, cgp
->cg_frsum
, 1);
1412 * if a complete block has been reassembled, account for it
1414 blkno
= fragstoblks(fs
, bbase
);
1415 if (ffs_isblock(fs
, cg_blksfree(cgp
), blkno
)) {
1416 cgp
->cg_cs
.cs_nffree
-= fs
->fs_frag
;
1417 fs
->fs_cstotal
.cs_nffree
-= fs
->fs_frag
;
1418 fs
->fs_cs(fs
, cg
).cs_nffree
-= fs
->fs_frag
;
1419 ffs_clusteracct(fs
, cgp
, blkno
, 1);
1420 cgp
->cg_cs
.cs_nbfree
++;
1421 fs
->fs_cstotal
.cs_nbfree
++;
1422 fs
->fs_cs(fs
, cg
).cs_nbfree
++;
1423 i
= cbtocylno(fs
, bbase
);
1424 cg_blks(fs
, cgp
, i
)[cbtorpos(fs
, bbase
)]++;
1425 cg_blktot(cgp
)[i
]++;
1431 byte_swap_cgout(cgp
,fs
);
1432 #endif /* REV_ENDIAN_FS */
1438 * Verify allocation of a block or fragment. Returns true if block or
1439 * fragment is allocated, false if it is free.
1441 ffs_checkblk(ip
, bno
, size
)
1449 int i
, error
, frags
, free
;
1451 struct vnode
*vp
=ITOV(ip
);
1452 struct mount
*mp
=vp
->v_mount
;
1453 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1454 #endif /* REV_ENDIAN_FS */
1457 if ((u_int
)size
> fs
->fs_bsize
|| fragoff(fs
, size
) != 0) {
1458 printf("bsize = %d, size = %d, fs = %s\n",
1459 fs
->fs_bsize
, size
, fs
->fs_fsmnt
);
1460 panic("checkblk: bad size");
1462 if ((u_int
)bno
>= fs
->fs_size
)
1463 panic("checkblk: bad block %d", bno
);
1464 error
= (int)buf_bread(ip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, dtog(fs
, bno
)))),
1465 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
1470 cgp
= (struct cg
*)buf_dataptr(bp
);
1473 byte_swap_cgin(cgp
,fs
);
1474 #endif /* REV_ENDIAN_FS */
1475 if (!cg_chkmagic(cgp
)) {
1478 byte_swap_cgout(cgp
,fs
);
1479 #endif /* REV_ENDIAN_FS */
1483 bno
= dtogd(fs
, bno
);
1484 if (size
== fs
->fs_bsize
) {
1485 free
= ffs_isblock(fs
, cg_blksfree(cgp
), fragstoblks(fs
, bno
));
1487 frags
= numfrags(fs
, size
);
1488 for (free
= 0, i
= 0; i
< frags
; i
++)
1489 if (isset(cg_blksfree(cgp
), bno
+ i
))
1491 if (free
!= 0 && free
!= frags
)
1492 panic("checkblk: partially free fragment");
1496 byte_swap_cgout(cgp
,fs
);
1497 #endif /* REV_ENDIAN_FS */
1501 #endif /* DIAGNOSTIC */
1506 * The specified inode is placed back in the free map.
1509 ffs_vfree(struct vnode
*vp
, ino_t ino
, int mode
)
1511 register struct fs
*fs
;
1512 register struct cg
*cgp
;
1513 register struct inode
*pip
;
1518 struct mount
*mp
=vp
->v_mount
;
1519 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1520 #endif /* REV_ENDIAN_FS */
1524 if ((u_int
)ino
>= fs
->fs_ipg
* fs
->fs_ncg
)
1525 panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n",
1526 pip
->i_dev
, ino
, fs
->fs_fsmnt
);
1527 cg
= ino_to_cg(fs
, ino
);
1528 error
= (int)buf_bread(pip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, cg
))),
1529 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
1534 cgp
= (struct cg
*)buf_dataptr(bp
);
1537 byte_swap_cgin(cgp
,fs
);
1538 #endif /* REV_ENDIAN_FS */
1539 if (!cg_chkmagic(cgp
)) {
1542 byte_swap_cgout(cgp
,fs
);
1543 #endif /* REV_ENDIAN_FS */
1548 cgp
->cg_time
= tv
.tv_sec
;
1550 if (isclr(cg_inosused(cgp
), ino
)) {
1551 printf("dev = 0x%x, ino = %d, fs = %s\n",
1552 pip
->i_dev
, ino
, fs
->fs_fsmnt
);
1553 if (fs
->fs_ronly
== 0)
1554 panic("ifree: freeing free inode");
1556 clrbit(cg_inosused(cgp
), ino
);
1557 if (ino
< cgp
->cg_irotor
)
1558 cgp
->cg_irotor
= ino
;
1559 cgp
->cg_cs
.cs_nifree
++;
1560 fs
->fs_cstotal
.cs_nifree
++;
1561 fs
->fs_cs(fs
, cg
).cs_nifree
++;
1562 if ((mode
& IFMT
) == IFDIR
) {
1563 cgp
->cg_cs
.cs_ndir
--;
1564 fs
->fs_cstotal
.cs_ndir
--;
1565 fs
->fs_cs(fs
, cg
).cs_ndir
--;
1570 byte_swap_cgout(cgp
,fs
);
1571 #endif /* REV_ENDIAN_FS */
1577 * Find a block of the specified size in the specified cylinder group.
1579 * It is a panic if a request is made to find a block if none are
1583 ffs_mapsearch(fs
, cgp
, bpref
, allocsiz
)
1584 register struct fs
*fs
;
1585 register struct cg
*cgp
;
1590 int start
, len
, loc
, i
;
1591 int blk
, field
, subfield
, pos
;
1594 * find the fragment by searching through the free block
1595 * map for an appropriate bit pattern
1598 start
= dtogd(fs
, bpref
) / NBBY
;
1600 start
= cgp
->cg_frotor
/ NBBY
;
1601 len
= howmany(fs
->fs_fpg
, NBBY
) - start
;
1602 loc
= scanc((u_int
)len
, (u_char
*)&cg_blksfree(cgp
)[start
],
1603 (u_char
*)fragtbl
[fs
->fs_frag
],
1604 (u_char
)(1 << (allocsiz
- 1 + (fs
->fs_frag
% NBBY
))));
1608 loc
= scanc((u_int
)len
, (u_char
*)&cg_blksfree(cgp
)[0],
1609 (u_char
*)fragtbl
[fs
->fs_frag
],
1610 (u_char
)(1 << (allocsiz
- 1 + (fs
->fs_frag
% NBBY
))));
1612 printf("start = %d, len = %d, fs = %s\n",
1613 start
, len
, fs
->fs_fsmnt
);
1614 panic("ffs_alloccg: map corrupted");
1618 bno
= (start
+ len
- loc
) * NBBY
;
1619 cgp
->cg_frotor
= bno
;
1621 * found the byte in the map
1622 * sift through the bits to find the selected frag
1624 for (i
= bno
+ NBBY
; bno
< i
; bno
+= fs
->fs_frag
) {
1625 blk
= blkmap(fs
, cg_blksfree(cgp
), bno
);
1627 field
= around
[allocsiz
];
1628 subfield
= inside
[allocsiz
];
1629 for (pos
= 0; pos
<= fs
->fs_frag
- allocsiz
; pos
++) {
1630 if ((blk
& field
) == subfield
)
1636 printf("bno = %d, fs = %s\n", bno
, fs
->fs_fsmnt
);
1637 panic("ffs_alloccg: block not in map");
1642 * Update the cluster map because of an allocation or free.
1644 * Cnt == 1 means free; cnt == -1 means allocating.
1647 ffs_clusteracct(struct fs
*fs
, struct cg
*cgp
, ufs_daddr_t blkno
, int cnt
)
1651 u_char
*freemapp
, *mapp
;
1652 int i
, start
, end
, forw
, back
, map
, bit
;
1654 if (fs
->fs_contigsumsize
<= 0)
1656 freemapp
= cg_clustersfree(cgp
);
1657 sump
= cg_clustersum(cgp
);
1659 * Allocate or clear the actual block.
1662 setbit(freemapp
, blkno
);
1664 clrbit(freemapp
, blkno
);
1666 * Find the size of the cluster going forward.
1669 end
= start
+ fs
->fs_contigsumsize
;
1670 if (end
>= cgp
->cg_nclusterblks
)
1671 end
= cgp
->cg_nclusterblks
;
1672 mapp
= &freemapp
[start
/ NBBY
];
1674 bit
= 1 << (start
% NBBY
);
1675 for (i
= start
; i
< end
; i
++) {
1676 if ((map
& bit
) == 0)
1678 if ((i
& (NBBY
- 1)) != (NBBY
- 1)) {
1687 * Find the size of the cluster going backward.
1690 end
= start
- fs
->fs_contigsumsize
;
1693 mapp
= &freemapp
[start
/ NBBY
];
1695 bit
= 1 << (start
% NBBY
);
1696 for (i
= start
; i
> end
; i
--) {
1697 if ((map
& bit
) == 0)
1699 if ((i
& (NBBY
- 1)) != 0) {
1703 bit
= 1 << (NBBY
- 1);
1708 * Account for old cluster and the possibly new forward and
1711 i
= back
+ forw
+ 1;
1712 if (i
> fs
->fs_contigsumsize
)
1713 i
= fs
->fs_contigsumsize
;
1720 * Update cluster summary information.
1722 lp
= &sump
[fs
->fs_contigsumsize
];
1723 for (i
= fs
->fs_contigsumsize
; i
> 0; i
--)
1726 fs
->fs_maxcluster
[cgp
->cg_cgx
] = i
;
1730 * Fserr prints the name of a file system with an error diagnostic.
1732 * The form of the error message is:
1736 ffs_fserr(fs
, uid
, cp
)
1742 log(LOG_ERR
, "uid %d on %s: %s\n", uid
, fs
->fs_fsmnt
, cp
);