]> git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ffs/ffs_alloc.c
xnu-792.6.56.tar.gz
[apple/xnu.git] / bsd / ufs / ffs / ffs_alloc.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 /*
25 * Copyright (c) 1982, 1986, 1989, 1993
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
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.
43 *
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
54 * SUCH DAMAGE.
55 *
56 * @(#)ffs_alloc.c 8.18 (Berkeley) 5/26/95
57 */
58 #include <rev_endian_fs.h>
59 #include <vm/vm_pager.h>
60
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/buf_internal.h>
64 #include <sys/proc.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>
71
72 #include <sys/vm.h>
73
74 #include <ufs/ufs/quota.h>
75 #include <ufs/ufs/inode.h>
76
77 #include <ufs/ffs/fs.h>
78 #include <ufs/ffs/ffs_extern.h>
79
80 #if REV_ENDIAN_FS
81 #include <ufs/ufs/ufs_byte_order.h>
82 #include <architecture/byte_order.h>
83 #endif /* REV_ENDIAN_FS */
84
85 extern u_long nextgennumber;
86
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);
99
100 /*
101 * Allocate a block in the file system.
102 *
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.
118 */
119 ffs_alloc(ip, lbn, bpref, size, cred, bnp)
120 register struct inode *ip;
121 ufs_daddr_t lbn, bpref;
122 int size;
123 kauth_cred_t cred;
124 ufs_daddr_t *bnp;
125 {
126 register struct fs *fs;
127 ufs_daddr_t bno;
128 int cg, error;
129 int devBlockSize=0;
130 *bnp = 0;
131 fs = ip->i_fs;
132 #if DIAGNOSTIC
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");
137 }
138 if (cred == NOCRED)
139 panic("ffs_alloc: missing credential\n");
140 #endif /* DIAGNOSTIC */
141 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
142 goto nospace;
143 if (suser(cred, NULL) && freespace(fs, fs->fs_minfree) <= 0)
144 goto nospace;
145 devBlockSize = vfs_devblocksize(vnode_mount(ITOV(ip)));
146 #if QUOTA
147 if (error = chkdq(ip, (int64_t)size, cred, 0))
148 return (error);
149 #endif /* QUOTA */
150 if (bpref >= fs->fs_size)
151 bpref = 0;
152 if (bpref == 0)
153 cg = ino_to_cg(fs, ip->i_number);
154 else
155 cg = dtog(fs, bpref);
156 bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size,
157 (u_int32_t (*)())ffs_alloccg);
158 if (bno > 0) {
159 ip->i_blocks += btodb(size, devBlockSize);
160 ip->i_flag |= IN_CHANGE | IN_UPDATE;
161 *bnp = bno;
162 return (0);
163 }
164 #if QUOTA
165 /*
166 * Restore user's disk quota because allocation failed.
167 */
168 (void) chkdq(ip, (int64_t)-size, cred, FORCE);
169 #endif /* QUOTA */
170 nospace:
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);
173 return (ENOSPC);
174 }
175
176 /*
177 * Reallocate a fragment to a bigger size
178 *
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.
183 */
184 ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp)
185 register struct inode *ip;
186 ufs_daddr_t lbprev;
187 ufs_daddr_t bpref;
188 int osize, nsize;
189 kauth_cred_t cred;
190 struct buf **bpp;
191 {
192 register struct fs *fs;
193 struct buf *bp;
194 int cg, request, error;
195 ufs_daddr_t bprev, bno;
196 int devBlockSize=0;
197
198 *bpp = 0;
199 fs = ip->i_fs;
200 #if DIAGNOSTIC
201 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
202 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
203 printf(
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");
207 }
208 if (cred == NOCRED)
209 panic("ffs_realloccg: missing credential\n");
210 #endif /* DIAGNOSTIC */
211 if (suser(cred, NULL) != 0 && freespace(fs, fs->fs_minfree) <= 0)
212 goto nospace;
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");
217 }
218 /*
219 * Allocate the extra space in the buffer.
220 */
221 if (error = (int)buf_bread(ITOV(ip), (daddr64_t)((unsigned)lbprev), osize, NOCRED, &bp)) {
222 buf_brelse(bp);
223 return (error);
224 }
225 devBlockSize = vfs_devblocksize(vnode_mount(ITOV(ip)));
226
227 #if QUOTA
228 if (error = chkdq(ip, (int64_t)(nsize - osize), cred, 0))
229 {
230 buf_brelse(bp);
231 return (error);
232 }
233 #endif /* QUOTA */
234 /*
235 * Check for extension in the existing location.
236 */
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;
243 allocbuf(bp, nsize);
244 buf_setflags(bp, B_DONE);
245 bzero((char *)buf_dataptr(bp) + osize, (u_int)buf_size(bp) - osize);
246 *bpp = bp;
247 return (0);
248 }
249 /*
250 * Allocate a new disk location.
251 */
252 if (bpref >= fs->fs_size)
253 bpref = 0;
254 switch ((int)fs->fs_optim) {
255 case FS_OPTSPACE:
256 /*
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.
262 */
263 request = nsize;
264 if (fs->fs_minfree < 5 ||
265 fs->fs_cstotal.cs_nffree >
266 fs->fs_dsize * fs->fs_minfree / (2 * 100))
267 break;
268 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
269 fs->fs_fsmnt);
270 fs->fs_optim = FS_OPTTIME;
271 break;
272 case FS_OPTTIME:
273 /*
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.
282 */
283 request = fs->fs_bsize;
284 if (fs->fs_cstotal.cs_nffree <
285 fs->fs_dsize * (fs->fs_minfree - 2) / 100)
286 break;
287 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
288 fs->fs_fsmnt);
289 fs->fs_optim = FS_OPTSPACE;
290 break;
291 default:
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");
295 /* NOTREACHED */
296 }
297 bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
298 (u_int32_t (*)())ffs_alloccg);
299 if (bno > 0) {
300 buf_setblkno(bp, (daddr64_t)((unsigned)fsbtodb(fs, bno)));
301 ffs_blkfree(ip, bprev, (long)osize);
302 if (nsize < request)
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;
307 allocbuf(bp, nsize);
308 buf_setflags(bp, B_DONE);
309 bzero((char *)buf_dataptr(bp) + osize, (u_int)buf_size(bp) - osize);
310 *bpp = bp;
311 return (0);
312 }
313 #if QUOTA
314 /*
315 * Restore user's disk quota because allocation failed.
316 */
317 (void) chkdq(ip, (int64_t)-(nsize - osize), cred, FORCE);
318 #endif /* QUOTA */
319 buf_brelse(bp);
320 nospace:
321 /*
322 * no space available
323 */
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);
326 return (ENOSPC);
327 }
328
329 /*
330 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
331 *
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.
342 */
343 /* Note: This routine is unused in UBC cluster I/O */
344
345 int doasyncfree = 1;
346 int doreallocblks = 1;
347
348
349 /*
350 * Allocate an inode in the file system.
351 *
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.
363 */
364 int
365 ffs_valloc(
366 struct vnode *pvp,
367 mode_t mode,
368 kauth_cred_t cred,
369 struct vnode **vpp)
370
371 {
372 register struct inode *pip;
373 register struct fs *fs;
374 register struct inode *ip;
375 struct timeval tv;
376 ino_t ino, ipref;
377 int cg, error;
378
379 *vpp = NULL;
380 pip = VTOI(pvp);
381 fs = pip->i_fs;
382 if (fs->fs_cstotal.cs_nifree == 0)
383 goto noinodes;
384
385 if ((mode & IFMT) == IFDIR)
386 ipref = ffs_dirpref(pip);
387 else
388 ipref = pip->i_number;
389 if (ipref >= fs->fs_ncg * fs->fs_ipg)
390 ipref = 0;
391 cg = ino_to_cg(fs, ipref);
392 /*
393 * Track the number of dirs created one after another
394 * in a cg without intervening files.
395 */
396 if ((mode & IFMT) == IFDIR) {
397 if (fs->fs_contigdirs[cg] < 255)
398 fs->fs_contigdirs[cg]++;
399 } else {
400 if (fs->fs_contigdirs[cg] > 0)
401 fs->fs_contigdirs[cg]--;
402 }
403 ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_nodealloccg);
404 if (ino == 0)
405 goto noinodes;
406
407 error = ffs_vget_internal(pvp->v_mount, ino, vpp, NULL, NULL, mode, 0);
408 if (error) {
409 ffs_vfree(pvp, ino, mode);
410 return (error);
411 }
412 ip = VTOI(*vpp);
413
414 if (ip->i_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");
418 }
419 if (ip->i_blocks) { /* XXX */
420 printf("free inode %s/%d had %d blocks\n",
421 fs->fs_fsmnt, ino, ip->i_blocks);
422 ip->i_blocks = 0;
423 }
424 ip->i_flags = 0;
425 /*
426 * Set up a new generation number for this inode.
427 */
428 microtime(&tv);
429 if (++nextgennumber < (u_long)tv.tv_sec)
430 nextgennumber = tv.tv_sec;
431 ip->i_gen = nextgennumber;
432 return (0);
433 noinodes:
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);
436 return (ENOSPC);
437 }
438
439 /*
440 * Find a cylinder group to place a directory.
441 *
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.
448 */
449 static ino_t
450 ffs_dirpref(pip)
451 struct inode *pip;
452 {
453 register struct fs *fs;
454 int cg, prefcg, dirsize, cgsize;
455 int avgifree, avgbfree, avgndir, curdirsize;
456 int minifree, minbfree, maxndir;
457 int mincg, minndir;
458 int maxcontigdirs;
459
460 fs = pip->i_fs;
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;
464
465 /*
466 * Force allocation in another cg if creating a first level dir.
467 */
468 if (ITOV(pip)->v_flag & VROOT) {
469 #ifdef __APPLE__
470 prefcg = random() % fs->fs_ncg;
471 #else
472 prefcg = arc4random() % fs->fs_ncg;
473 #endif
474 mincg = prefcg;
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) {
480 mincg = cg;
481 minndir = fs->fs_cs(fs, cg).cs_ndir;
482 }
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) {
487 mincg = cg;
488 minndir = fs->fs_cs(fs, cg).cs_ndir;
489 }
490 return ((ino_t)(fs->fs_ipg * mincg));
491 }
492
493 /*
494 * Count various limits which used for
495 * optimal allocation of a directory inode.
496 */
497 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
498 minifree = avgifree - fs->fs_ipg / 4;
499 if (minifree < 0)
500 minifree = 0;
501 minbfree = avgbfree - fs->fs_fpg / fs->fs_frag / 4;
502 if (minbfree < 0)
503 minbfree = 0;
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)
514 maxcontigdirs = 1;
515
516 /*
517 * Limit number of dirs in one cg and reserve space for
518 * regular files, but only if we have no deficit in
519 * inodes or space.
520 */
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));
528 }
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));
535 }
536 /*
537 * This is a backstop when we have deficit in space.
538 */
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)
544 break;
545 return ((ino_t)(fs->fs_ipg * cg));
546 }
547
548 /*
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.
552 *
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
565 * allocated.
566 *
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.
573 */
574 ufs_daddr_t
575 ffs_blkpref(ip, lbn, indx, bap)
576 struct inode *ip;
577 ufs_daddr_t lbn;
578 int indx;
579 ufs_daddr_t *bap;
580 {
581 register struct fs *fs;
582 register int cg;
583 int avgbfree, startcg;
584 ufs_daddr_t nextblk;
585 #if REV_ENDIAN_FS
586 daddr_t prev=0;
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 */
591
592 fs = ip->i_fs;
593 #if REV_ENDIAN_FS
594 if (indx && bap) {
595 if (rev_endian) {
596 if (bap != &ip->i_db[0])
597 prev = NXSwapLong(bap[indx - 1]);
598 else
599 prev = bap[indx - 1];
600 } else prev = bap[indx - 1];
601 }
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 */
606 {
607 if (lbn < NDADDR) {
608 cg = ino_to_cg(fs, ip->i_number);
609 return (fs->fs_fpg * cg + fs->fs_frag);
610 }
611 /*
612 * Find a cylinder with greater than average number of
613 * unused data blocks.
614 */
615 #if REV_ENDIAN_FS
616 if (indx == 0 || prev == 0)
617 #else /* REV_ENDIAN_FS */
618 if (indx == 0 || bap[indx - 1] == 0)
619 #endif /* REV_ENDIAN_FS */
620 startcg =
621 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
622 else
623 #if REV_ENDIAN_FS
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) {
632 fs->fs_cgrotor = cg;
633 return (fs->fs_fpg * cg + fs->fs_frag);
634 }
635 for (cg = 0; cg <= startcg; cg++)
636 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
637 fs->fs_cgrotor = cg;
638 return (fs->fs_fpg * cg + fs->fs_frag);
639 }
640 return (NULL);
641 }
642 /*
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.
647 */
648 #if REV_ENDIAN_FS
649 if (rev_endian) {
650 nextblk = prev + fs->fs_frag;
651 if (indx < fs->fs_maxcontig) {
652 return (nextblk);
653 }
654 if (bap != &ip->i_db[0])
655 prev = NXSwapLong(bap[indx - fs->fs_maxcontig]);
656 else
657 prev = bap[indx - fs->fs_maxcontig];
658 if (prev + blkstofrags(fs, fs->fs_maxcontig) != nextblk)
659 return (nextblk);
660 } else {
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)
665 return (nextblk);
666 #if REV_ENDIAN_FS
667 }
668 #endif /* REV_ENDIAN_FS */
669 if (fs->fs_rotdelay != 0)
670 /*
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.
675 */
676 nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
677 (NSPF(fs) * 1000), fs->fs_frag);
678 return (nextblk);
679 }
680
681 /*
682 * Implement the cylinder overflow algorithm.
683 *
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.
688 */
689 /*VARARGS5*/
690 static u_long
691 ffs_hashalloc(ip, cg, pref, size, allocator)
692 struct inode *ip;
693 int cg;
694 long pref;
695 int size; /* size for data blocks, mode for inodes */
696 u_int32_t (*allocator)();
697 {
698 register struct fs *fs;
699 long result;
700 int i, icg = cg;
701
702 fs = ip->i_fs;
703 /*
704 * 1: preferred cylinder group
705 */
706 result = (*allocator)(ip, cg, pref, size);
707 if (result)
708 return (result);
709 /*
710 * 2: quadratic rehash
711 */
712 for (i = 1; i < fs->fs_ncg; i *= 2) {
713 cg += i;
714 if (cg >= fs->fs_ncg)
715 cg -= fs->fs_ncg;
716 result = (*allocator)(ip, cg, 0, size);
717 if (result)
718 return (result);
719 }
720 /*
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.
724 */
725 cg = (icg + 2) % fs->fs_ncg;
726 for (i = 2; i < fs->fs_ncg; i++) {
727 result = (*allocator)(ip, cg, 0, size);
728 if (result)
729 return (result);
730 cg++;
731 if (cg == fs->fs_ncg)
732 cg = 0;
733 }
734 return (NULL);
735 }
736
737 /*
738 * Determine whether a fragment can be extended.
739 *
740 * Check to see if the necessary fragments are available, and
741 * if they are, allocate them.
742 */
743 static ufs_daddr_t
744 ffs_fragextend(ip, cg, bprev, osize, nsize)
745 struct inode *ip;
746 int cg;
747 long bprev;
748 int osize, nsize;
749 {
750 register struct fs *fs;
751 register struct cg *cgp;
752 struct buf *bp;
753 struct timeval tv;
754 long bno;
755 int frags, bbase;
756 int i, error;
757 #if REV_ENDIAN_FS
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 */
762
763 fs = ip->i_fs;
764 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
765 return (NULL);
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 */
770 return (NULL);
771 }
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);
775 if (error) {
776 buf_brelse(bp);
777 return (NULL);
778 }
779 cgp = (struct cg *)buf_dataptr(bp);
780 #if REV_ENDIAN_FS
781 if (rev_endian) {
782 byte_swap_cgin(cgp, fs);
783 }
784 #endif /* REV_ENDIAN_FS */
785
786 if (!cg_chkmagic(cgp)) {
787 #if REV_ENDIAN_FS
788 if (rev_endian)
789 byte_swap_cgout(cgp,fs);
790 #endif /* REV_ENDIAN_FS */
791 buf_brelse(bp);
792 return (NULL);
793 }
794 microtime(&tv);
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)) {
799 #if REV_ENDIAN_FS
800 if (rev_endian)
801 byte_swap_cgout(cgp,fs);
802 #endif /* REV_ENDIAN_FS */
803 buf_brelse(bp);
804 return (NULL);
805 }
806 /*
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
811 */
812 for (i = frags; i < fs->fs_frag - bbase; i++)
813 if (isclr(cg_blksfree(cgp), bno + i))
814 break;
815 cgp->cg_frsum[i - numfrags(fs, osize)]--;
816 if (i != frags)
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--;
823 }
824 fs->fs_fmod = 1;
825 #if REV_ENDIAN_FS
826 if (rev_endian)
827 byte_swap_cgout(cgp,fs);
828 #endif /* REV_ENDIAN_FS */
829 buf_bdwrite(bp);
830 return (bprev);
831 }
832
833 /*
834 * Determine whether a block can be allocated.
835 *
836 * Check to see if a block of the appropriate size is available,
837 * and if it is, allocate it.
838 */
839 static ufs_daddr_t
840 ffs_alloccg(ip, cg, bpref, size)
841 struct inode *ip;
842 int cg;
843 ufs_daddr_t bpref;
844 int size;
845 {
846 register struct fs *fs;
847 register struct cg *cgp;
848 struct buf *bp;
849 struct timeval tv;
850 register int i;
851 int error, bno, frags, allocsiz;
852 #if REV_ENDIAN_FS
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 */
857
858 fs = ip->i_fs;
859 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
860 return (NULL);
861 error = (int)buf_bread(ip->i_devvp, (daddr64_t)((unsigned)fsbtodb(fs, cgtod(fs, cg))),
862 (int)fs->fs_cgsize, NOCRED, &bp);
863 if (error) {
864 buf_brelse(bp);
865 return (NULL);
866 }
867 cgp = (struct cg *)buf_dataptr(bp);
868 #if REV_ENDIAN_FS
869 if (rev_endian)
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)) {
874 #if REV_ENDIAN_FS
875 if (rev_endian)
876 byte_swap_cgout(cgp,fs);
877 #endif /* REV_ENDIAN_FS */
878 buf_brelse(bp);
879 return (NULL);
880 }
881 microtime(&tv);
882 cgp->cg_time = tv.tv_sec;
883 if (size == fs->fs_bsize) {
884 bno = ffs_alloccgblk(fs, cgp, bpref);
885 #if REV_ENDIAN_FS
886 if (rev_endian)
887 byte_swap_cgout(cgp,fs);
888 #endif /* REV_ENDIAN_FS */
889 buf_bdwrite(bp);
890 return (bno);
891 }
892 /*
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
896 */
897 frags = numfrags(fs, size);
898 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
899 if (cgp->cg_frsum[allocsiz] != 0)
900 break;
901 if (allocsiz == fs->fs_frag) {
902 /*
903 * no fragments were available, so a block will be
904 * allocated, and hacked up
905 */
906 if (cgp->cg_cs.cs_nbfree == 0) {
907 #if REV_ENDIAN_FS
908 if (rev_endian)
909 byte_swap_cgout(cgp,fs);
910 #endif /* REV_ENDIAN_FS */
911 buf_brelse(bp);
912 return (NULL);
913 }
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;
922 fs->fs_fmod = 1;
923 cgp->cg_frsum[i]++;
924 #if REV_ENDIAN_FS
925 if (rev_endian)
926 byte_swap_cgout(cgp,fs);
927 #endif /* REV_ENDIAN_FS */
928 buf_bdwrite(bp);
929 return (bno);
930 }
931 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
932 if (bno < 0) {
933 #if REV_ENDIAN_FS
934 if (rev_endian)
935 byte_swap_cgout(cgp,fs);
936 #endif /* REV_ENDIAN_FS */
937 buf_brelse(bp);
938 return (NULL);
939 }
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;
945 fs->fs_fmod = 1;
946 cgp->cg_frsum[allocsiz]--;
947 if (frags != allocsiz)
948 cgp->cg_frsum[allocsiz - frags]++;
949 #if REV_ENDIAN_FS
950 if (rev_endian)
951 byte_swap_cgout(cgp,fs);
952 #endif /* REV_ENDIAN_FS */
953 buf_bdwrite(bp);
954 return (cg * fs->fs_fpg + bno);
955 }
956
957 /*
958 * Allocate a block in a cylinder group.
959 *
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.
967 */
968 static ufs_daddr_t
969 ffs_alloccgblk(fs, cgp, bpref)
970 register struct fs *fs;
971 register struct cg *cgp;
972 ufs_daddr_t bpref;
973 {
974 ufs_daddr_t bno, blkno;
975 int cylno, pos, delta;
976 short *cylbp;
977 register int i;
978
979 if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
980 bpref = cgp->cg_rotor;
981 goto norot;
982 }
983 bpref = blknum(fs, bpref);
984 bpref = dtogd(fs, bpref);
985 /*
986 * if the requested block is available, use it
987 */
988 if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
989 bno = bpref;
990 goto gotit;
991 }
992 if (fs->fs_nrpos <= 1 || fs->fs_cpc == 0) {
993 /*
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).
1000 */
1001 goto norot;
1002 }
1003 /*
1004 * check for a block available on the same cylinder
1005 */
1006 cylno = cbtocylno(fs, bpref);
1007 if (cg_blktot(cgp)[cylno] == 0)
1008 goto norot;
1009 /*
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.
1013 */
1014 cylbp = cg_blks(fs, cgp, cylno);
1015 pos = cbtorpos(fs, bpref);
1016 for (i = pos; i < fs->fs_nrpos; i++)
1017 if (cylbp[i] > 0)
1018 break;
1019 if (i == fs->fs_nrpos)
1020 for (i = 0; i < pos; i++)
1021 if (cylbp[i] > 0)
1022 break;
1023 if (cylbp[i] > 0) {
1024 /*
1025 * found a rotational position, now find the actual
1026 * block. A panic if none is actually there.
1027 */
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");
1034 }
1035 for (i = fs_postbl(fs, pos)[i];; ) {
1036 if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) {
1037 bno = blkstofrags(fs, (bno + i));
1038 goto gotit;
1039 }
1040 delta = fs_rotbl(fs)[i];
1041 if (delta <= 0 ||
1042 delta + i > fragstoblks(fs, fs->fs_fpg))
1043 break;
1044 i += delta;
1045 }
1046 printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
1047 panic("ffs_alloccgblk: can't find blk in cyl");
1048 }
1049 norot:
1050 /*
1051 * no blocks in the requested cylinder, so take next
1052 * available one in this cylinder group.
1053 */
1054 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1055 if (bno < 0)
1056 return (NULL);
1057 cgp->cg_rotor = bno;
1058 gotit:
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]--;
1068 fs->fs_fmod = 1;
1069 return (cgp->cg_cgx * fs->fs_fpg + bno);
1070 }
1071
1072 /*
1073 * Determine whether a cluster can be allocated.
1074 *
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.
1078 */
1079 static ufs_daddr_t
1080 ffs_clusteralloc(ip, cg, bpref, len)
1081 struct inode *ip;
1082 int cg;
1083 ufs_daddr_t bpref;
1084 int len;
1085 {
1086 register struct fs *fs;
1087 register struct cg *cgp;
1088 struct buf *bp;
1089 int i, got, run, bno, bit, map;
1090 u_char *mapp;
1091 int32_t *lp;
1092 #if REV_ENDIAN_FS
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 */
1097
1098 fs = ip->i_fs;
1099 if (fs->fs_maxcluster[cg] < len)
1100 return (NULL);
1101 if (buf_bread(ip->i_devvp, (daddr64_t)((unsigned)fsbtodb(fs, cgtod(fs, cg))), (int)fs->fs_cgsize,
1102 NOCRED, &bp))
1103 goto fail;
1104 cgp = (struct cg *)buf_dataptr(bp);
1105 #if REV_ENDIAN_FS
1106 if (rev_endian)
1107 byte_swap_cgin(cgp,fs);
1108 #endif /* REV_ENDIAN_FS */
1109 if (!cg_chkmagic(cgp)) {
1110 #if REV_ENDIAN_FS
1111 if (rev_endian)
1112 byte_swap_cgout(cgp,fs);
1113 #endif /* REV_ENDIAN_FS */
1114 goto fail;
1115 }
1116 /*
1117 * Check to see if a cluster of the needed size (or bigger) is
1118 * available in this cylinder group.
1119 */
1120 lp = &cg_clustersum(cgp)[len];
1121 for (i = len; i <= fs->fs_contigsumsize; i++)
1122 if (*lp++ > 0)
1123 break;
1124 if (i > fs->fs_contigsumsize) {
1125 /*
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.
1131 */
1132 lp = &cg_clustersum(cgp)[len - 1];
1133 for (i = len - 1; i > 0; i--)
1134 if (*lp-- > 0)
1135 break;
1136 fs->fs_maxcluster[cg] = i;
1137 #if REV_ENDIAN_FS
1138 if (rev_endian)
1139 byte_swap_cgout(cgp,fs);
1140 #endif /* REV_ENDIAN_FS */
1141 goto fail;
1142 }
1143 /*
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.
1153 */
1154 if (dtog(fs, bpref) != cg)
1155 bpref = 0;
1156 else
1157 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1158 mapp = &cg_clustersfree(cgp)[bpref / NBBY];
1159 map = *mapp++;
1160 bit = 1 << (bpref % NBBY);
1161 for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
1162 if ((map & bit) == 0) {
1163 run = 0;
1164 } else {
1165 run++;
1166 if (run == len)
1167 break;
1168 }
1169 if ((got & (NBBY - 1)) != (NBBY - 1)) {
1170 bit <<= 1;
1171 } else {
1172 map = *mapp++;
1173 bit = 1;
1174 }
1175 }
1176 if (got == cgp->cg_nclusterblks) {
1177 #if REV_ENDIAN_FS
1178 if (rev_endian)
1179 byte_swap_cgout(cgp,fs);
1180 #endif /* REV_ENDIAN_FS */
1181 goto fail;
1182 }
1183 /*
1184 * Allocate the cluster that we have found.
1185 */
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");
1196 #if REV_ENDIAN_FS
1197 if (rev_endian)
1198 byte_swap_cgout(cgp,fs);
1199 #endif /* REV_ENDIAN_FS */
1200 buf_bdwrite(bp);
1201 return (bno);
1202
1203 fail:
1204 buf_brelse(bp);
1205 return (0);
1206 }
1207
1208 /*
1209 * Determine whether an inode can be allocated.
1210 *
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.
1216 */
1217 static ino_t
1218 ffs_nodealloccg(ip, cg, ipref, mode)
1219 struct inode *ip;
1220 int cg;
1221 ufs_daddr_t ipref;
1222 int mode;
1223 {
1224 register struct fs *fs;
1225 register struct cg *cgp;
1226 struct buf *bp;
1227 struct timeval tv;
1228 int error, start, len, loc, map, i;
1229 #if REV_ENDIAN_FS
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 */
1234
1235 fs = ip->i_fs;
1236 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1237 return (NULL);
1238 error = (int)buf_bread(ip->i_devvp, (daddr64_t)((unsigned)fsbtodb(fs, cgtod(fs, cg))),
1239 (int)fs->fs_cgsize, NOCRED, &bp);
1240 if (error) {
1241 buf_brelse(bp);
1242 return (NULL);
1243 }
1244 cgp = (struct cg *)buf_dataptr(bp);
1245 #if REV_ENDIAN_FS
1246 if (rev_endian)
1247 byte_swap_cgin(cgp,fs);
1248 #endif /* REV_ENDIAN_FS */
1249 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
1250 #if REV_ENDIAN_FS
1251 if (rev_endian)
1252 byte_swap_cgout(cgp,fs);
1253 #endif /* REV_ENDIAN_FS */
1254 buf_brelse(bp);
1255 return (NULL);
1256 }
1257
1258 microtime(&tv);
1259 cgp->cg_time = tv.tv_sec;
1260 if (ipref) {
1261 ipref %= fs->fs_ipg;
1262 if (isclr(cg_inosused(cgp), ipref))
1263 goto gotit;
1264 }
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]);
1268 if (loc == 0) {
1269 len = start + 1;
1270 start = 0;
1271 loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
1272 if (loc == 0) {
1273 printf("cg = %d, irotor = %d, fs = %s\n",
1274 cg, cgp->cg_irotor, fs->fs_fsmnt);
1275 panic("ffs_nodealloccg: map corrupted");
1276 /* NOTREACHED */
1277 }
1278 }
1279 i = start + len - loc;
1280 map = cg_inosused(cgp)[i];
1281 ipref = i * NBBY;
1282 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1283 if ((map & i) == 0) {
1284 cgp->cg_irotor = ipref;
1285 goto gotit;
1286 }
1287 }
1288 printf("fs = %s\n", fs->fs_fsmnt);
1289 panic("ffs_nodealloccg: block not in map");
1290 /* NOTREACHED */
1291 gotit:
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--;
1296 fs->fs_fmod = 1;
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++;
1301 }
1302 #if REV_ENDIAN_FS
1303 if (rev_endian)
1304 byte_swap_cgout(cgp,fs);
1305 #endif /* REV_ENDIAN_FS */
1306 buf_bdwrite(bp);
1307 return (cg * fs->fs_ipg + ipref);
1308 }
1309
1310 /*
1311 * Free a block or fragment.
1312 *
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.
1316 */
1317 void
1318 ffs_blkfree(ip, bno, size)
1319 register struct inode *ip;
1320 ufs_daddr_t bno;
1321 long size;
1322 {
1323 register struct fs *fs;
1324 register struct cg *cgp;
1325 struct buf *bp;
1326 struct timeval tv;
1327 ufs_daddr_t blkno;
1328 int i, error, cg, blk, frags, bbase;
1329 #if REV_ENDIAN_FS
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 */
1334
1335 fs = ip->i_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");
1340 }
1341 cg = dtog(fs, bno);
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");
1345 return;
1346 }
1347 error = (int)buf_bread(ip->i_devvp, (daddr64_t)((unsigned)fsbtodb(fs, cgtod(fs, cg))),
1348 (int)fs->fs_cgsize, NOCRED, &bp);
1349 if (error) {
1350 buf_brelse(bp);
1351 return;
1352 }
1353 cgp = (struct cg *)buf_dataptr(bp);
1354 #if REV_ENDIAN_FS
1355 if (rev_endian)
1356 byte_swap_cgin(cgp,fs);
1357 #endif /* REV_ENDIAN_FS */
1358 if (!cg_chkmagic(cgp)) {
1359 #if REV_ENDIAN_FS
1360 if (rev_endian)
1361 byte_swap_cgout(cgp,fs);
1362 #endif /* REV_ENDIAN_FS */
1363 buf_brelse(bp);
1364 return;
1365 }
1366 microtime(&tv);
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");
1375 }
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]++;
1384 } else {
1385 bbase = bno - fragnum(fs, bno);
1386 /*
1387 * decrement the counts associated with the old frags
1388 */
1389 blk = blkmap(fs, cg_blksfree(cgp), bbase);
1390 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
1391 /*
1392 * deallocate the fragment
1393 */
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");
1400 }
1401 setbit(cg_blksfree(cgp), bno + i);
1402 }
1403 cgp->cg_cs.cs_nffree += i;
1404 fs->fs_cstotal.cs_nffree += i;
1405 fs->fs_cs(fs, cg).cs_nffree += i;
1406 /*
1407 * add back in counts associated with the new frags
1408 */
1409 blk = blkmap(fs, cg_blksfree(cgp), bbase);
1410 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
1411 /*
1412 * if a complete block has been reassembled, account for it
1413 */
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]++;
1426 }
1427 }
1428 fs->fs_fmod = 1;
1429 #if REV_ENDIAN_FS
1430 if (rev_endian)
1431 byte_swap_cgout(cgp,fs);
1432 #endif /* REV_ENDIAN_FS */
1433 buf_bdwrite(bp);
1434 }
1435
1436 #if DIAGNOSTIC
1437 /*
1438 * Verify allocation of a block or fragment. Returns true if block or
1439 * fragment is allocated, false if it is free.
1440 */
1441 ffs_checkblk(ip, bno, size)
1442 struct inode *ip;
1443 ufs_daddr_t bno;
1444 long size;
1445 {
1446 struct fs *fs;
1447 struct cg *cgp;
1448 struct buf *bp;
1449 int i, error, frags, free;
1450 #if REV_ENDIAN_FS
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 */
1455
1456 fs = ip->i_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");
1461 }
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);
1466 if (error) {
1467 buf_brelse(bp);
1468 return;
1469 }
1470 cgp = (struct cg *)buf_dataptr(bp);
1471 #if REV_ENDIAN_FS
1472 if (rev_endian)
1473 byte_swap_cgin(cgp,fs);
1474 #endif /* REV_ENDIAN_FS */
1475 if (!cg_chkmagic(cgp)) {
1476 #if REV_ENDIAN_FS
1477 if (rev_endian)
1478 byte_swap_cgout(cgp,fs);
1479 #endif /* REV_ENDIAN_FS */
1480 buf_brelse(bp);
1481 return;
1482 }
1483 bno = dtogd(fs, bno);
1484 if (size == fs->fs_bsize) {
1485 free = ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno));
1486 } else {
1487 frags = numfrags(fs, size);
1488 for (free = 0, i = 0; i < frags; i++)
1489 if (isset(cg_blksfree(cgp), bno + i))
1490 free++;
1491 if (free != 0 && free != frags)
1492 panic("checkblk: partially free fragment");
1493 }
1494 #if REV_ENDIAN_FS
1495 if (rev_endian)
1496 byte_swap_cgout(cgp,fs);
1497 #endif /* REV_ENDIAN_FS */
1498 buf_brelse(bp);
1499 return (!free);
1500 }
1501 #endif /* DIAGNOSTIC */
1502
1503 /*
1504 * Free an inode.
1505 *
1506 * The specified inode is placed back in the free map.
1507 */
1508 int
1509 ffs_vfree(struct vnode *vp, ino_t ino, int mode)
1510 {
1511 register struct fs *fs;
1512 register struct cg *cgp;
1513 register struct inode *pip;
1514 struct buf *bp;
1515 struct timeval tv;
1516 int error, cg;
1517 #if REV_ENDIAN_FS
1518 struct mount *mp=vp->v_mount;
1519 int rev_endian=(mp->mnt_flag & MNT_REVEND);
1520 #endif /* REV_ENDIAN_FS */
1521
1522 pip = VTOI(vp);
1523 fs = pip->i_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);
1530 if (error) {
1531 buf_brelse(bp);
1532 return (0);
1533 }
1534 cgp = (struct cg *)buf_dataptr(bp);
1535 #if REV_ENDIAN_FS
1536 if (rev_endian)
1537 byte_swap_cgin(cgp,fs);
1538 #endif /* REV_ENDIAN_FS */
1539 if (!cg_chkmagic(cgp)) {
1540 #if REV_ENDIAN_FS
1541 if (rev_endian)
1542 byte_swap_cgout(cgp,fs);
1543 #endif /* REV_ENDIAN_FS */
1544 buf_brelse(bp);
1545 return (0);
1546 }
1547 microtime(&tv);
1548 cgp->cg_time = tv.tv_sec;
1549 ino %= fs->fs_ipg;
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");
1555 }
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--;
1566 }
1567 fs->fs_fmod = 1;
1568 #if REV_ENDIAN_FS
1569 if (rev_endian)
1570 byte_swap_cgout(cgp,fs);
1571 #endif /* REV_ENDIAN_FS */
1572 buf_bdwrite(bp);
1573 return (0);
1574 }
1575
1576 /*
1577 * Find a block of the specified size in the specified cylinder group.
1578 *
1579 * It is a panic if a request is made to find a block if none are
1580 * available.
1581 */
1582 static ufs_daddr_t
1583 ffs_mapsearch(fs, cgp, bpref, allocsiz)
1584 register struct fs *fs;
1585 register struct cg *cgp;
1586 ufs_daddr_t bpref;
1587 int allocsiz;
1588 {
1589 ufs_daddr_t bno;
1590 int start, len, loc, i;
1591 int blk, field, subfield, pos;
1592
1593 /*
1594 * find the fragment by searching through the free block
1595 * map for an appropriate bit pattern
1596 */
1597 if (bpref)
1598 start = dtogd(fs, bpref) / NBBY;
1599 else
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))));
1605 if (loc == 0) {
1606 len = start + 1;
1607 start = 0;
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))));
1611 if (loc == 0) {
1612 printf("start = %d, len = %d, fs = %s\n",
1613 start, len, fs->fs_fsmnt);
1614 panic("ffs_alloccg: map corrupted");
1615 /* NOTREACHED */
1616 }
1617 }
1618 bno = (start + len - loc) * NBBY;
1619 cgp->cg_frotor = bno;
1620 /*
1621 * found the byte in the map
1622 * sift through the bits to find the selected frag
1623 */
1624 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1625 blk = blkmap(fs, cg_blksfree(cgp), bno);
1626 blk <<= 1;
1627 field = around[allocsiz];
1628 subfield = inside[allocsiz];
1629 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
1630 if ((blk & field) == subfield)
1631 return (bno + pos);
1632 field <<= 1;
1633 subfield <<= 1;
1634 }
1635 }
1636 printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
1637 panic("ffs_alloccg: block not in map");
1638 return (-1);
1639 }
1640
1641 /*
1642 * Update the cluster map because of an allocation or free.
1643 *
1644 * Cnt == 1 means free; cnt == -1 means allocating.
1645 */
1646 static void
1647 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs_daddr_t blkno, int cnt)
1648 {
1649 int32_t *sump;
1650 int32_t *lp;
1651 u_char *freemapp, *mapp;
1652 int i, start, end, forw, back, map, bit;
1653
1654 if (fs->fs_contigsumsize <= 0)
1655 return;
1656 freemapp = cg_clustersfree(cgp);
1657 sump = cg_clustersum(cgp);
1658 /*
1659 * Allocate or clear the actual block.
1660 */
1661 if (cnt > 0)
1662 setbit(freemapp, blkno);
1663 else
1664 clrbit(freemapp, blkno);
1665 /*
1666 * Find the size of the cluster going forward.
1667 */
1668 start = blkno + 1;
1669 end = start + fs->fs_contigsumsize;
1670 if (end >= cgp->cg_nclusterblks)
1671 end = cgp->cg_nclusterblks;
1672 mapp = &freemapp[start / NBBY];
1673 map = *mapp++;
1674 bit = 1 << (start % NBBY);
1675 for (i = start; i < end; i++) {
1676 if ((map & bit) == 0)
1677 break;
1678 if ((i & (NBBY - 1)) != (NBBY - 1)) {
1679 bit <<= 1;
1680 } else {
1681 map = *mapp++;
1682 bit = 1;
1683 }
1684 }
1685 forw = i - start;
1686 /*
1687 * Find the size of the cluster going backward.
1688 */
1689 start = blkno - 1;
1690 end = start - fs->fs_contigsumsize;
1691 if (end < 0)
1692 end = -1;
1693 mapp = &freemapp[start / NBBY];
1694 map = *mapp--;
1695 bit = 1 << (start % NBBY);
1696 for (i = start; i > end; i--) {
1697 if ((map & bit) == 0)
1698 break;
1699 if ((i & (NBBY - 1)) != 0) {
1700 bit >>= 1;
1701 } else {
1702 map = *mapp--;
1703 bit = 1 << (NBBY - 1);
1704 }
1705 }
1706 back = start - i;
1707 /*
1708 * Account for old cluster and the possibly new forward and
1709 * back clusters.
1710 */
1711 i = back + forw + 1;
1712 if (i > fs->fs_contigsumsize)
1713 i = fs->fs_contigsumsize;
1714 sump[i] += cnt;
1715 if (back > 0)
1716 sump[back] -= cnt;
1717 if (forw > 0)
1718 sump[forw] -= cnt;
1719 /*
1720 * Update cluster summary information.
1721 */
1722 lp = &sump[fs->fs_contigsumsize];
1723 for (i = fs->fs_contigsumsize; i > 0; i--)
1724 if (*lp-- > 0)
1725 break;
1726 fs->fs_maxcluster[cgp->cg_cgx] = i;
1727 }
1728
1729 /*
1730 * Fserr prints the name of a file system with an error diagnostic.
1731 *
1732 * The form of the error message is:
1733 * fs: error message
1734 */
1735 static void
1736 ffs_fserr(fs, uid, cp)
1737 struct fs *fs;
1738 u_int uid;
1739 char *cp;
1740 {
1741
1742 log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
1743 }