]> git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ffs/ffs_alloc.c
f4aa9630efdb89d43fc8eb5955ff21724d3a17b0
[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_OSREFERENCE_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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
31 /*
32 * Copyright (c) 1982, 1986, 1989, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * @(#)ffs_alloc.c 8.18 (Berkeley) 5/26/95
64 */
65 #include <rev_endian_fs.h>
66 #include <vm/vm_pager.h>
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/buf_internal.h>
71 #include <sys/proc.h>
72 #include <sys/kauth.h>
73 #include <sys/vnode_internal.h>
74 #include <sys/mount_internal.h>
75 #include <sys/kernel.h>
76 #include <sys/syslog.h>
77 #include <sys/quota.h>
78
79 #include <sys/vm.h>
80
81 #include <ufs/ufs/quota.h>
82 #include <ufs/ufs/inode.h>
83
84 #include <ufs/ffs/fs.h>
85 #include <ufs/ffs/ffs_extern.h>
86
87 #if REV_ENDIAN_FS
88 #include <ufs/ufs/ufs_byte_order.h>
89 #include <libkern/OSByteOrder.h>
90 #endif /* REV_ENDIAN_FS */
91
92 extern u_long nextgennumber;
93
94 static ufs_daddr_t ffs_alloccg(struct inode *, int, ufs_daddr_t, int);
95 static ufs_daddr_t ffs_alloccgblk(struct fs *, struct cg *, ufs_daddr_t);
96 static ufs_daddr_t ffs_clusteralloc(struct inode *, int, ufs_daddr_t, int);
97 static ino_t ffs_dirpref(struct inode *);
98 static ufs_daddr_t ffs_fragextend(struct inode *, int, long, int, int);
99 static void ffs_fserr(struct fs *, u_int, char *);
100 static u_long ffs_hashalloc
101 (struct inode *, int, long, int, u_int32_t (*)());
102 static ino_t ffs_nodealloccg(struct inode *, int, ufs_daddr_t, int);
103 static ufs_daddr_t ffs_mapsearch(struct fs *, struct cg *, ufs_daddr_t, int);
104 static void ffs_clusteracct
105 (struct fs *fs, struct cg *cgp, ufs_daddr_t blkno, int cnt);
106
107 /*
108 * Allocate a block in the file system.
109 *
110 * The size of the requested block is given, which must be some
111 * multiple of fs_fsize and <= fs_bsize.
112 * A preference may be optionally specified. If a preference is given
113 * the following hierarchy is used to allocate a block:
114 * 1) allocate the requested block.
115 * 2) allocate a rotationally optimal block in the same cylinder.
116 * 3) allocate a block in the same cylinder group.
117 * 4) quadradically rehash into other cylinder groups, until an
118 * available block is located.
119 * If no block preference is given the following heirarchy is used
120 * to allocate a block:
121 * 1) allocate a block in the cylinder group that contains the
122 * inode for the file.
123 * 2) quadradically rehash into other cylinder groups, until an
124 * available block is located.
125 */
126 ffs_alloc(ip, lbn, bpref, size, cred, bnp)
127 register struct inode *ip;
128 ufs_daddr_t lbn, bpref;
129 int size;
130 kauth_cred_t cred;
131 ufs_daddr_t *bnp;
132 {
133 register struct fs *fs;
134 ufs_daddr_t bno;
135 int cg, error;
136 int devBlockSize=0;
137 *bnp = 0;
138 fs = ip->i_fs;
139 #if DIAGNOSTIC
140 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
141 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
142 ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
143 panic("ffs_alloc: bad size");
144 }
145 if (cred == NOCRED)
146 panic("ffs_alloc: missing credential\n");
147 #endif /* DIAGNOSTIC */
148 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
149 goto nospace;
150 if (suser(cred, NULL) && freespace(fs, fs->fs_minfree) <= 0)
151 goto nospace;
152 devBlockSize = vfs_devblocksize(vnode_mount(ITOV(ip)));
153 #if QUOTA
154 if (error = chkdq(ip, (int64_t)size, cred, 0))
155 return (error);
156 #endif /* QUOTA */
157 if (bpref >= fs->fs_size)
158 bpref = 0;
159 if (bpref == 0)
160 cg = ino_to_cg(fs, ip->i_number);
161 else
162 cg = dtog(fs, bpref);
163 bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size,
164 (u_int32_t (*)())ffs_alloccg);
165 if (bno > 0) {
166 ip->i_blocks += btodb(size, devBlockSize);
167 ip->i_flag |= IN_CHANGE | IN_UPDATE;
168 *bnp = bno;
169 return (0);
170 }
171 #if QUOTA
172 /*
173 * Restore user's disk quota because allocation failed.
174 */
175 (void) chkdq(ip, (int64_t)-size, cred, FORCE);
176 #endif /* QUOTA */
177 nospace:
178 ffs_fserr(fs, kauth_cred_getuid(cred), "file system full");
179 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
180 return (ENOSPC);
181 }
182
183 /*
184 * Reallocate a fragment to a bigger size
185 *
186 * The number and size of the old block is given, and a preference
187 * and new size is also specified. The allocator attempts to extend
188 * the original block. Failing that, the regular block allocator is
189 * invoked to get an appropriate block.
190 */
191 ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp)
192 register struct inode *ip;
193 ufs_daddr_t lbprev;
194 ufs_daddr_t bpref;
195 int osize, nsize;
196 kauth_cred_t cred;
197 struct buf **bpp;
198 {
199 register struct fs *fs;
200 struct buf *bp;
201 int cg, request, error;
202 ufs_daddr_t bprev, bno;
203 int devBlockSize=0;
204
205 *bpp = 0;
206 fs = ip->i_fs;
207 #if DIAGNOSTIC
208 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
209 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
210 printf(
211 "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
212 ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
213 panic("ffs_realloccg: bad size");
214 }
215 if (cred == NOCRED)
216 panic("ffs_realloccg: missing credential\n");
217 #endif /* DIAGNOSTIC */
218 if (suser(cred, NULL) != 0 && freespace(fs, fs->fs_minfree) <= 0)
219 goto nospace;
220 if ((bprev = ip->i_db[lbprev]) == 0) {
221 printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
222 ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
223 panic("ffs_realloccg: bad bprev");
224 }
225 /*
226 * Allocate the extra space in the buffer.
227 */
228 if (error = (int)buf_bread(ITOV(ip), (daddr64_t)((unsigned)lbprev), osize, NOCRED, &bp)) {
229 buf_brelse(bp);
230 return (error);
231 }
232 devBlockSize = vfs_devblocksize(vnode_mount(ITOV(ip)));
233
234 #if QUOTA
235 if (error = chkdq(ip, (int64_t)(nsize - osize), cred, 0))
236 {
237 buf_brelse(bp);
238 return (error);
239 }
240 #endif /* QUOTA */
241 /*
242 * Check for extension in the existing location.
243 */
244 cg = dtog(fs, bprev);
245 if (bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) {
246 if ((ufs_daddr_t)buf_blkno(bp) != fsbtodb(fs, bno))
247 panic("bad blockno");
248 ip->i_blocks += btodb(nsize - osize, devBlockSize);
249 ip->i_flag |= IN_CHANGE | IN_UPDATE;
250 allocbuf(bp, nsize);
251 buf_setflags(bp, B_DONE);
252 bzero((char *)buf_dataptr(bp) + osize, (u_int)buf_size(bp) - osize);
253 *bpp = bp;
254 return (0);
255 }
256 /*
257 * Allocate a new disk location.
258 */
259 if (bpref >= fs->fs_size)
260 bpref = 0;
261 switch ((int)fs->fs_optim) {
262 case FS_OPTSPACE:
263 /*
264 * Allocate an exact sized fragment. Although this makes
265 * best use of space, we will waste time relocating it if
266 * the file continues to grow. If the fragmentation is
267 * less than half of the minimum free reserve, we choose
268 * to begin optimizing for time.
269 */
270 request = nsize;
271 if (fs->fs_minfree < 5 ||
272 fs->fs_cstotal.cs_nffree >
273 fs->fs_dsize * fs->fs_minfree / (2 * 100))
274 break;
275 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
276 fs->fs_fsmnt);
277 fs->fs_optim = FS_OPTTIME;
278 break;
279 case FS_OPTTIME:
280 /*
281 * At this point we have discovered a file that is trying to
282 * grow a small fragment to a larger fragment. To save time,
283 * we allocate a full sized block, then free the unused portion.
284 * If the file continues to grow, the `ffs_fragextend' call
285 * above will be able to grow it in place without further
286 * copying. If aberrant programs cause disk fragmentation to
287 * grow within 2% of the free reserve, we choose to begin
288 * optimizing for space.
289 */
290 request = fs->fs_bsize;
291 if (fs->fs_cstotal.cs_nffree <
292 fs->fs_dsize * (fs->fs_minfree - 2) / 100)
293 break;
294 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
295 fs->fs_fsmnt);
296 fs->fs_optim = FS_OPTSPACE;
297 break;
298 default:
299 printf("dev = 0x%x, optim = %d, fs = %s\n",
300 ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
301 panic("ffs_realloccg: bad optim");
302 /* NOTREACHED */
303 }
304 bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
305 (u_int32_t (*)())ffs_alloccg);
306 if (bno > 0) {
307 buf_setblkno(bp, (daddr64_t)((unsigned)fsbtodb(fs, bno)));
308 ffs_blkfree(ip, bprev, (long)osize);
309 if (nsize < request)
310 ffs_blkfree(ip, bno + numfrags(fs, nsize),
311 (long)(request - nsize));
312 ip->i_blocks += btodb(nsize - osize, devBlockSize);
313 ip->i_flag |= IN_CHANGE | IN_UPDATE;
314 allocbuf(bp, nsize);
315 buf_setflags(bp, B_DONE);
316 bzero((char *)buf_dataptr(bp) + osize, (u_int)buf_size(bp) - osize);
317 *bpp = bp;
318 return (0);
319 }
320 #if QUOTA
321 /*
322 * Restore user's disk quota because allocation failed.
323 */
324 (void) chkdq(ip, (int64_t)-(nsize - osize), cred, FORCE);
325 #endif /* QUOTA */
326 buf_brelse(bp);
327 nospace:
328 /*
329 * no space available
330 */
331 ffs_fserr(fs, kauth_cred_getuid(cred), "file system full");
332 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
333 return (ENOSPC);
334 }
335
336 /*
337 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
338 *
339 * The vnode and an array of buffer pointers for a range of sequential
340 * logical blocks to be made contiguous is given. The allocator attempts
341 * to find a range of sequential blocks starting as close as possible to
342 * an fs_rotdelay offset from the end of the allocation for the logical
343 * block immediately preceeding the current range. If successful, the
344 * physical block numbers in the buffer pointers and in the inode are
345 * changed to reflect the new allocation. If unsuccessful, the allocation
346 * is left unchanged. The success in doing the reallocation is returned.
347 * Note that the error return is not reflected back to the user. Rather
348 * the previous block allocation will be used.
349 */
350 /* Note: This routine is unused in UBC cluster I/O */
351
352 int doasyncfree = 1;
353 int doreallocblks = 1;
354
355
356 /*
357 * Allocate an inode in the file system.
358 *
359 * If allocating a directory, use ffs_dirpref to select the inode.
360 * If allocating in a directory, the following hierarchy is followed:
361 * 1) allocate the preferred inode.
362 * 2) allocate an inode in the same cylinder group.
363 * 3) quadradically rehash into other cylinder groups, until an
364 * available inode is located.
365 * If no inode preference is given the following heirarchy is used
366 * to allocate an inode:
367 * 1) allocate an inode in cylinder group 0.
368 * 2) quadradically rehash into other cylinder groups, until an
369 * available inode is located.
370 */
371 int
372 ffs_valloc(
373 struct vnode *pvp,
374 mode_t mode,
375 kauth_cred_t cred,
376 struct vnode **vpp)
377
378 {
379 register struct inode *pip;
380 register struct fs *fs;
381 register struct inode *ip;
382 struct timeval tv;
383 ino_t ino, ipref;
384 int cg, error;
385
386 *vpp = NULL;
387 pip = VTOI(pvp);
388 fs = pip->i_fs;
389 if (fs->fs_cstotal.cs_nifree == 0)
390 goto noinodes;
391
392 if ((mode & IFMT) == IFDIR)
393 ipref = ffs_dirpref(pip);
394 else
395 ipref = pip->i_number;
396 if (ipref >= fs->fs_ncg * fs->fs_ipg)
397 ipref = 0;
398 cg = ino_to_cg(fs, ipref);
399 /*
400 * Track the number of dirs created one after another
401 * in a cg without intervening files.
402 */
403 if ((mode & IFMT) == IFDIR) {
404 if (fs->fs_contigdirs[cg] < 255)
405 fs->fs_contigdirs[cg]++;
406 } else {
407 if (fs->fs_contigdirs[cg] > 0)
408 fs->fs_contigdirs[cg]--;
409 }
410 ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_nodealloccg);
411 if (ino == 0)
412 goto noinodes;
413
414 error = ffs_vget_internal(pvp->v_mount, ino, vpp, NULL, NULL, mode, 0);
415 if (error) {
416 ffs_vfree(pvp, ino, mode);
417 return (error);
418 }
419 ip = VTOI(*vpp);
420
421 if (ip->i_mode) {
422 printf("mode = 0%o, inum = %d, fs = %s\n",
423 ip->i_mode, ip->i_number, fs->fs_fsmnt);
424 panic("ffs_valloc: dup alloc");
425 }
426 if (ip->i_blocks) { /* XXX */
427 printf("free inode %s/%d had %d blocks\n",
428 fs->fs_fsmnt, ino, ip->i_blocks);
429 ip->i_blocks = 0;
430 }
431 ip->i_flags = 0;
432 /*
433 * Set up a new generation number for this inode.
434 */
435 microtime(&tv);
436 if (++nextgennumber < (u_long)tv.tv_sec)
437 nextgennumber = tv.tv_sec;
438 ip->i_gen = nextgennumber;
439 return (0);
440 noinodes:
441 ffs_fserr(fs, kauth_cred_getuid(cred), "out of inodes");
442 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
443 return (ENOSPC);
444 }
445
446 /*
447 * Find a cylinder group to place a directory.
448 *
449 * The policy implemented by this algorithm is to allocate a
450 * directory inode in the same cylinder group as its parent
451 * directory, but also to reserve space for its files inodes
452 * and data. Restrict the number of directories which may be
453 * allocated one after another in the same cylinder group
454 * without intervening allocation of files.
455 */
456 static ino_t
457 ffs_dirpref(pip)
458 struct inode *pip;
459 {
460 register struct fs *fs;
461 int cg, prefcg, dirsize, cgsize;
462 int avgifree, avgbfree, avgndir, curdirsize;
463 int minifree, minbfree, maxndir;
464 int mincg, minndir;
465 int maxcontigdirs;
466
467 fs = pip->i_fs;
468 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
469 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
470 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
471
472 /*
473 * Force allocation in another cg if creating a first level dir.
474 */
475 if (ITOV(pip)->v_flag & VROOT) {
476 #ifdef __APPLE__
477 prefcg = random() % fs->fs_ncg;
478 #else
479 prefcg = arc4random() % fs->fs_ncg;
480 #endif
481 mincg = prefcg;
482 minndir = fs->fs_ipg;
483 for (cg = prefcg; cg < fs->fs_ncg; cg++)
484 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
485 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
486 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
487 mincg = cg;
488 minndir = fs->fs_cs(fs, cg).cs_ndir;
489 }
490 for (cg = 0; cg < prefcg; cg++)
491 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
492 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
493 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
494 mincg = cg;
495 minndir = fs->fs_cs(fs, cg).cs_ndir;
496 }
497 return ((ino_t)(fs->fs_ipg * mincg));
498 }
499
500 /*
501 * Count various limits which used for
502 * optimal allocation of a directory inode.
503 */
504 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
505 minifree = avgifree - fs->fs_ipg / 4;
506 if (minifree < 0)
507 minifree = 0;
508 minbfree = avgbfree - fs->fs_fpg / fs->fs_frag / 4;
509 if (minbfree < 0)
510 minbfree = 0;
511 cgsize = fs->fs_fsize * fs->fs_fpg;
512 dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir;
513 curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
514 if (dirsize < curdirsize)
515 dirsize = curdirsize;
516 maxcontigdirs = min(cgsize / dirsize, 255);
517 if (fs->fs_avgfpdir > 0)
518 maxcontigdirs = min(maxcontigdirs,
519 fs->fs_ipg / fs->fs_avgfpdir);
520 if (maxcontigdirs == 0)
521 maxcontigdirs = 1;
522
523 /*
524 * Limit number of dirs in one cg and reserve space for
525 * regular files, but only if we have no deficit in
526 * inodes or space.
527 */
528 prefcg = ino_to_cg(fs, pip->i_number);
529 for (cg = prefcg; cg < fs->fs_ncg; cg++)
530 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
531 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
532 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
533 if (fs->fs_contigdirs[cg] < maxcontigdirs)
534 return ((ino_t)(fs->fs_ipg * cg));
535 }
536 for (cg = 0; cg < prefcg; cg++)
537 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
538 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
539 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
540 if (fs->fs_contigdirs[cg] < maxcontigdirs)
541 return ((ino_t)(fs->fs_ipg * cg));
542 }
543 /*
544 * This is a backstop when we have deficit in space.
545 */
546 for (cg = prefcg; cg < fs->fs_ncg; cg++)
547 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
548 return ((ino_t)(fs->fs_ipg * cg));
549 for (cg = 0; cg < prefcg; cg++)
550 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
551 break;
552 return ((ino_t)(fs->fs_ipg * cg));
553 }
554
555 /*
556 * Select the desired position for the next block in a file. The file is
557 * logically divided into sections. The first section is composed of the
558 * direct blocks. Each additional section contains fs_maxbpg blocks.
559 *
560 * If no blocks have been allocated in the first section, the policy is to
561 * request a block in the same cylinder group as the inode that describes
562 * the file. If no blocks have been allocated in any other section, the
563 * policy is to place the section in a cylinder group with a greater than
564 * average number of free blocks. An appropriate cylinder group is found
565 * by using a rotor that sweeps the cylinder groups. When a new group of
566 * blocks is needed, the sweep begins in the cylinder group following the
567 * cylinder group from which the previous allocation was made. The sweep
568 * continues until a cylinder group with greater than the average number
569 * of free blocks is found. If the allocation is for the first block in an
570 * indirect block, the information on the previous allocation is unavailable;
571 * here a best guess is made based upon the logical block number being
572 * allocated.
573 *
574 * If a section is already partially allocated, the policy is to
575 * contiguously allocate fs_maxcontig blocks. The end of one of these
576 * contiguous blocks and the beginning of the next is physically separated
577 * so that the disk head will be in transit between them for at least
578 * fs_rotdelay milliseconds. This is to allow time for the processor to
579 * schedule another I/O transfer.
580 */
581 ufs_daddr_t
582 ffs_blkpref(ip, lbn, indx, bap)
583 struct inode *ip;
584 ufs_daddr_t lbn;
585 int indx;
586 ufs_daddr_t *bap;
587 {
588 register struct fs *fs;
589 register int cg;
590 int avgbfree, startcg;
591 ufs_daddr_t nextblk;
592 #if REV_ENDIAN_FS
593 daddr_t prev=0;
594 struct vnode *vp=ITOV(ip);
595 struct mount *mp=vp->v_mount;
596 int rev_endian=(mp->mnt_flag & MNT_REVEND);
597 #endif /* REV_ENDIAN_FS */
598
599 fs = ip->i_fs;
600 #if REV_ENDIAN_FS
601 if (indx && bap) {
602 if (rev_endian) {
603 if (bap != &ip->i_db[0])
604 prev = OSSwapInt32(bap[indx - 1]);
605 else
606 prev = bap[indx - 1];
607 } else prev = bap[indx - 1];
608 }
609 if (indx % fs->fs_maxbpg == 0 || prev == 0)
610 #else /* REV_ENDIAN_FS */
611 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0)
612 #endif /* REV_ENDIAN_FS */
613 {
614 if (lbn < NDADDR) {
615 cg = ino_to_cg(fs, ip->i_number);
616 return (fs->fs_fpg * cg + fs->fs_frag);
617 }
618 /*
619 * Find a cylinder with greater than average number of
620 * unused data blocks.
621 */
622 #if REV_ENDIAN_FS
623 if (indx == 0 || prev == 0)
624 #else /* REV_ENDIAN_FS */
625 if (indx == 0 || bap[indx - 1] == 0)
626 #endif /* REV_ENDIAN_FS */
627 startcg =
628 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
629 else
630 #if REV_ENDIAN_FS
631 startcg = dtog(fs, prev) + 1;
632 #else /* REV_ENDIAN_FS */
633 startcg = dtog(fs, bap[indx - 1]) + 1;
634 #endif /* REV_ENDIAN_FS */
635 startcg %= fs->fs_ncg;
636 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
637 for (cg = startcg; cg < fs->fs_ncg; cg++)
638 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
639 fs->fs_cgrotor = cg;
640 return (fs->fs_fpg * cg + fs->fs_frag);
641 }
642 for (cg = 0; cg <= startcg; cg++)
643 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
644 fs->fs_cgrotor = cg;
645 return (fs->fs_fpg * cg + fs->fs_frag);
646 }
647 return (NULL);
648 }
649 /*
650 * One or more previous blocks have been laid out. If less
651 * than fs_maxcontig previous blocks are contiguous, the
652 * next block is requested contiguously, otherwise it is
653 * requested rotationally delayed by fs_rotdelay milliseconds.
654 */
655 #if REV_ENDIAN_FS
656 if (rev_endian) {
657 nextblk = prev + fs->fs_frag;
658 if (indx < fs->fs_maxcontig) {
659 return (nextblk);
660 }
661 if (bap != &ip->i_db[0])
662 prev = OSSwapInt32(bap[indx - fs->fs_maxcontig]);
663 else
664 prev = bap[indx - fs->fs_maxcontig];
665 if (prev + blkstofrags(fs, fs->fs_maxcontig) != nextblk)
666 return (nextblk);
667 } else {
668 #endif /* REV_ENDIAN_FS */
669 nextblk = bap[indx - 1] + fs->fs_frag;
670 if (indx < fs->fs_maxcontig || bap[indx - fs->fs_maxcontig] +
671 blkstofrags(fs, fs->fs_maxcontig) != nextblk)
672 return (nextblk);
673 #if REV_ENDIAN_FS
674 }
675 #endif /* REV_ENDIAN_FS */
676 if (fs->fs_rotdelay != 0)
677 /*
678 * Here we convert ms of delay to frags as:
679 * (frags) = (ms) * (rev/sec) * (sect/rev) /
680 * ((sect/frag) * (ms/sec))
681 * then round up to the next block.
682 */
683 nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
684 (NSPF(fs) * 1000), fs->fs_frag);
685 return (nextblk);
686 }
687
688 /*
689 * Implement the cylinder overflow algorithm.
690 *
691 * The policy implemented by this algorithm is:
692 * 1) allocate the block in its requested cylinder group.
693 * 2) quadradically rehash on the cylinder group number.
694 * 3) brute force search for a free block.
695 */
696 /*VARARGS5*/
697 static u_long
698 ffs_hashalloc(ip, cg, pref, size, allocator)
699 struct inode *ip;
700 int cg;
701 long pref;
702 int size; /* size for data blocks, mode for inodes */
703 u_int32_t (*allocator)();
704 {
705 register struct fs *fs;
706 long result;
707 int i, icg = cg;
708
709 fs = ip->i_fs;
710 /*
711 * 1: preferred cylinder group
712 */
713 result = (*allocator)(ip, cg, pref, size);
714 if (result)
715 return (result);
716 /*
717 * 2: quadratic rehash
718 */
719 for (i = 1; i < fs->fs_ncg; i *= 2) {
720 cg += i;
721 if (cg >= fs->fs_ncg)
722 cg -= fs->fs_ncg;
723 result = (*allocator)(ip, cg, 0, size);
724 if (result)
725 return (result);
726 }
727 /*
728 * 3: brute force search
729 * Note that we start at i == 2, since 0 was checked initially,
730 * and 1 is always checked in the quadratic rehash.
731 */
732 cg = (icg + 2) % fs->fs_ncg;
733 for (i = 2; i < fs->fs_ncg; i++) {
734 result = (*allocator)(ip, cg, 0, size);
735 if (result)
736 return (result);
737 cg++;
738 if (cg == fs->fs_ncg)
739 cg = 0;
740 }
741 return (NULL);
742 }
743
744 /*
745 * Determine whether a fragment can be extended.
746 *
747 * Check to see if the necessary fragments are available, and
748 * if they are, allocate them.
749 */
750 static ufs_daddr_t
751 ffs_fragextend(ip, cg, bprev, osize, nsize)
752 struct inode *ip;
753 int cg;
754 long bprev;
755 int osize, nsize;
756 {
757 register struct fs *fs;
758 register struct cg *cgp;
759 struct buf *bp;
760 struct timeval tv;
761 long bno;
762 int frags, bbase;
763 int i, error;
764 #if REV_ENDIAN_FS
765 struct vnode *vp=ITOV(ip);
766 struct mount *mp=vp->v_mount;
767 int rev_endian=(mp->mnt_flag & MNT_REVEND);
768 #endif /* REV_ENDIAN_FS */
769
770 fs = ip->i_fs;
771 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
772 return (NULL);
773 frags = numfrags(fs, nsize); /* number of fragments needed */
774 bbase = fragnum(fs, bprev); /* offset in a frag (it is mod fragsize */
775 if (bbase > fragnum(fs, (bprev + frags - 1))) {
776 /* cannot extend across a block boundary */
777 return (NULL);
778 }
779 /* read corresponding cylinder group info */
780 error = (int)buf_bread(ip->i_devvp, (daddr64_t)((unsigned)fsbtodb(fs, cgtod(fs, cg))),
781 (int)fs->fs_cgsize, NOCRED, &bp);
782 if (error) {
783 buf_brelse(bp);
784 return (NULL);
785 }
786 cgp = (struct cg *)buf_dataptr(bp);
787 #if REV_ENDIAN_FS
788 if (rev_endian) {
789 byte_swap_cgin(cgp, fs);
790 }
791 #endif /* REV_ENDIAN_FS */
792
793 if (!cg_chkmagic(cgp)) {
794 #if REV_ENDIAN_FS
795 if (rev_endian)
796 byte_swap_cgout(cgp,fs);
797 #endif /* REV_ENDIAN_FS */
798 buf_brelse(bp);
799 return (NULL);
800 }
801 microtime(&tv);
802 cgp->cg_time = tv.tv_sec;
803 bno = dtogd(fs, bprev);
804 for (i = numfrags(fs, osize); i < frags; i++)
805 if (isclr(cg_blksfree(cgp), bno + i)) {
806 #if REV_ENDIAN_FS
807 if (rev_endian)
808 byte_swap_cgout(cgp,fs);
809 #endif /* REV_ENDIAN_FS */
810 buf_brelse(bp);
811 return (NULL);
812 }
813 /*
814 * the current fragment can be extended
815 * deduct the count on fragment being extended into
816 * increase the count on the remaining fragment (if any)
817 * allocate the extended piece
818 */
819 for (i = frags; i < fs->fs_frag - bbase; i++)
820 if (isclr(cg_blksfree(cgp), bno + i))
821 break;
822 cgp->cg_frsum[i - numfrags(fs, osize)]--;
823 if (i != frags)
824 cgp->cg_frsum[i - frags]++;
825 for (i = numfrags(fs, osize); i < frags; i++) {
826 clrbit(cg_blksfree(cgp), bno + i);
827 cgp->cg_cs.cs_nffree--;
828 fs->fs_cstotal.cs_nffree--;
829 fs->fs_cs(fs, cg).cs_nffree--;
830 }
831 fs->fs_fmod = 1;
832 #if REV_ENDIAN_FS
833 if (rev_endian)
834 byte_swap_cgout(cgp,fs);
835 #endif /* REV_ENDIAN_FS */
836 buf_bdwrite(bp);
837 return (bprev);
838 }
839
840 /*
841 * Determine whether a block can be allocated.
842 *
843 * Check to see if a block of the appropriate size is available,
844 * and if it is, allocate it.
845 */
846 static ufs_daddr_t
847 ffs_alloccg(ip, cg, bpref, size)
848 struct inode *ip;
849 int cg;
850 ufs_daddr_t bpref;
851 int size;
852 {
853 register struct fs *fs;
854 register struct cg *cgp;
855 struct buf *bp;
856 struct timeval tv;
857 register int i;
858 int error, bno, frags, allocsiz;
859 #if REV_ENDIAN_FS
860 struct vnode *vp=ITOV(ip);
861 struct mount *mp=vp->v_mount;
862 int rev_endian=(mp->mnt_flag & MNT_REVEND);
863 #endif /* REV_ENDIAN_FS */
864
865 fs = ip->i_fs;
866 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
867 return (NULL);
868 error = (int)buf_bread(ip->i_devvp, (daddr64_t)((unsigned)fsbtodb(fs, cgtod(fs, cg))),
869 (int)fs->fs_cgsize, NOCRED, &bp);
870 if (error) {
871 buf_brelse(bp);
872 return (NULL);
873 }
874 cgp = (struct cg *)buf_dataptr(bp);
875 #if REV_ENDIAN_FS
876 if (rev_endian)
877 byte_swap_cgin(cgp,fs);
878 #endif /* REV_ENDIAN_FS */
879 if (!cg_chkmagic(cgp) ||
880 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
881 #if REV_ENDIAN_FS
882 if (rev_endian)
883 byte_swap_cgout(cgp,fs);
884 #endif /* REV_ENDIAN_FS */
885 buf_brelse(bp);
886 return (NULL);
887 }
888 microtime(&tv);
889 cgp->cg_time = tv.tv_sec;
890 if (size == fs->fs_bsize) {
891 bno = ffs_alloccgblk(fs, cgp, bpref);
892 #if REV_ENDIAN_FS
893 if (rev_endian)
894 byte_swap_cgout(cgp,fs);
895 #endif /* REV_ENDIAN_FS */
896 buf_bdwrite(bp);
897 return (bno);
898 }
899 /*
900 * check to see if any fragments are already available
901 * allocsiz is the size which will be allocated, hacking
902 * it down to a smaller size if necessary
903 */
904 frags = numfrags(fs, size);
905 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
906 if (cgp->cg_frsum[allocsiz] != 0)
907 break;
908 if (allocsiz == fs->fs_frag) {
909 /*
910 * no fragments were available, so a block will be
911 * allocated, and hacked up
912 */
913 if (cgp->cg_cs.cs_nbfree == 0) {
914 #if REV_ENDIAN_FS
915 if (rev_endian)
916 byte_swap_cgout(cgp,fs);
917 #endif /* REV_ENDIAN_FS */
918 buf_brelse(bp);
919 return (NULL);
920 }
921 bno = ffs_alloccgblk(fs, cgp, bpref);
922 bpref = dtogd(fs, bno);
923 for (i = frags; i < fs->fs_frag; i++)
924 setbit(cg_blksfree(cgp), bpref + i);
925 i = fs->fs_frag - frags;
926 cgp->cg_cs.cs_nffree += i;
927 fs->fs_cstotal.cs_nffree += i;
928 fs->fs_cs(fs, cg).cs_nffree += i;
929 fs->fs_fmod = 1;
930 cgp->cg_frsum[i]++;
931 #if REV_ENDIAN_FS
932 if (rev_endian)
933 byte_swap_cgout(cgp,fs);
934 #endif /* REV_ENDIAN_FS */
935 buf_bdwrite(bp);
936 return (bno);
937 }
938 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
939 if (bno < 0) {
940 #if REV_ENDIAN_FS
941 if (rev_endian)
942 byte_swap_cgout(cgp,fs);
943 #endif /* REV_ENDIAN_FS */
944 buf_brelse(bp);
945 return (NULL);
946 }
947 for (i = 0; i < frags; i++)
948 clrbit(cg_blksfree(cgp), bno + i);
949 cgp->cg_cs.cs_nffree -= frags;
950 fs->fs_cstotal.cs_nffree -= frags;
951 fs->fs_cs(fs, cg).cs_nffree -= frags;
952 fs->fs_fmod = 1;
953 cgp->cg_frsum[allocsiz]--;
954 if (frags != allocsiz)
955 cgp->cg_frsum[allocsiz - frags]++;
956 #if REV_ENDIAN_FS
957 if (rev_endian)
958 byte_swap_cgout(cgp,fs);
959 #endif /* REV_ENDIAN_FS */
960 buf_bdwrite(bp);
961 return (cg * fs->fs_fpg + bno);
962 }
963
964 /*
965 * Allocate a block in a cylinder group.
966 *
967 * This algorithm implements the following policy:
968 * 1) allocate the requested block.
969 * 2) allocate a rotationally optimal block in the same cylinder.
970 * 3) allocate the next available block on the block rotor for the
971 * specified cylinder group.
972 * Note that this routine only allocates fs_bsize blocks; these
973 * blocks may be fragmented by the routine that allocates them.
974 */
975 static ufs_daddr_t
976 ffs_alloccgblk(fs, cgp, bpref)
977 register struct fs *fs;
978 register struct cg *cgp;
979 ufs_daddr_t bpref;
980 {
981 ufs_daddr_t bno, blkno;
982 int cylno, pos, delta;
983 short *cylbp;
984 register int i;
985
986 if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
987 bpref = cgp->cg_rotor;
988 goto norot;
989 }
990 bpref = blknum(fs, bpref);
991 bpref = dtogd(fs, bpref);
992 /*
993 * if the requested block is available, use it
994 */
995 if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
996 bno = bpref;
997 goto gotit;
998 }
999 if (fs->fs_nrpos <= 1 || fs->fs_cpc == 0) {
1000 /*
1001 * Block layout information is not available.
1002 * Leaving bpref unchanged means we take the
1003 * next available free block following the one
1004 * we just allocated. Hopefully this will at
1005 * least hit a track cache on drives of unknown
1006 * geometry (e.g. SCSI).
1007 */
1008 goto norot;
1009 }
1010 /*
1011 * check for a block available on the same cylinder
1012 */
1013 cylno = cbtocylno(fs, bpref);
1014 if (cg_blktot(cgp)[cylno] == 0)
1015 goto norot;
1016 /*
1017 * check the summary information to see if a block is
1018 * available in the requested cylinder starting at the
1019 * requested rotational position and proceeding around.
1020 */
1021 cylbp = cg_blks(fs, cgp, cylno);
1022 pos = cbtorpos(fs, bpref);
1023 for (i = pos; i < fs->fs_nrpos; i++)
1024 if (cylbp[i] > 0)
1025 break;
1026 if (i == fs->fs_nrpos)
1027 for (i = 0; i < pos; i++)
1028 if (cylbp[i] > 0)
1029 break;
1030 if (cylbp[i] > 0) {
1031 /*
1032 * found a rotational position, now find the actual
1033 * block. A panic if none is actually there.
1034 */
1035 pos = cylno % fs->fs_cpc;
1036 bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
1037 if (fs_postbl(fs, pos)[i] == -1) {
1038 printf("pos = %d, i = %d, fs = %s\n",
1039 pos, i, fs->fs_fsmnt);
1040 panic("ffs_alloccgblk: cyl groups corrupted");
1041 }
1042 for (i = fs_postbl(fs, pos)[i];; ) {
1043 if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) {
1044 bno = blkstofrags(fs, (bno + i));
1045 goto gotit;
1046 }
1047 delta = fs_rotbl(fs)[i];
1048 if (delta <= 0 ||
1049 delta + i > fragstoblks(fs, fs->fs_fpg))
1050 break;
1051 i += delta;
1052 }
1053 printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
1054 panic("ffs_alloccgblk: can't find blk in cyl");
1055 }
1056 norot:
1057 /*
1058 * no blocks in the requested cylinder, so take next
1059 * available one in this cylinder group.
1060 */
1061 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1062 if (bno < 0)
1063 return (NULL);
1064 cgp->cg_rotor = bno;
1065 gotit:
1066 blkno = fragstoblks(fs, bno);
1067 ffs_clrblock(fs, cg_blksfree(cgp), (long)blkno);
1068 ffs_clusteracct(fs, cgp, blkno, -1);
1069 cgp->cg_cs.cs_nbfree--;
1070 fs->fs_cstotal.cs_nbfree--;
1071 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
1072 cylno = cbtocylno(fs, bno);
1073 cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
1074 cg_blktot(cgp)[cylno]--;
1075 fs->fs_fmod = 1;
1076 return (cgp->cg_cgx * fs->fs_fpg + bno);
1077 }
1078
1079 /*
1080 * Determine whether a cluster can be allocated.
1081 *
1082 * We do not currently check for optimal rotational layout if there
1083 * are multiple choices in the same cylinder group. Instead we just
1084 * take the first one that we find following bpref.
1085 */
1086 static ufs_daddr_t
1087 ffs_clusteralloc(ip, cg, bpref, len)
1088 struct inode *ip;
1089 int cg;
1090 ufs_daddr_t bpref;
1091 int len;
1092 {
1093 register struct fs *fs;
1094 register struct cg *cgp;
1095 struct buf *bp;
1096 int i, got, run, bno, bit, map;
1097 u_char *mapp;
1098 int32_t *lp;
1099 #if REV_ENDIAN_FS
1100 struct vnode *vp=ITOV(ip);
1101 struct mount *mp=vp->v_mount;
1102 int rev_endian=(mp->mnt_flag & MNT_REVEND);
1103 #endif /* REV_ENDIAN_FS */
1104
1105 fs = ip->i_fs;
1106 if (fs->fs_maxcluster[cg] < len)
1107 return (NULL);
1108 if (buf_bread(ip->i_devvp, (daddr64_t)((unsigned)fsbtodb(fs, cgtod(fs, cg))), (int)fs->fs_cgsize,
1109 NOCRED, &bp))
1110 goto fail;
1111 cgp = (struct cg *)buf_dataptr(bp);
1112 #if REV_ENDIAN_FS
1113 if (rev_endian)
1114 byte_swap_cgin(cgp,fs);
1115 #endif /* REV_ENDIAN_FS */
1116 if (!cg_chkmagic(cgp)) {
1117 #if REV_ENDIAN_FS
1118 if (rev_endian)
1119 byte_swap_cgout(cgp,fs);
1120 #endif /* REV_ENDIAN_FS */
1121 goto fail;
1122 }
1123 /*
1124 * Check to see if a cluster of the needed size (or bigger) is
1125 * available in this cylinder group.
1126 */
1127 lp = &cg_clustersum(cgp)[len];
1128 for (i = len; i <= fs->fs_contigsumsize; i++)
1129 if (*lp++ > 0)
1130 break;
1131 if (i > fs->fs_contigsumsize) {
1132 /*
1133 * This is the first time looking for a cluster in this
1134 * cylinder group. Update the cluster summary information
1135 * to reflect the true maximum sized cluster so that
1136 * future cluster allocation requests can avoid reading
1137 * the cylinder group map only to find no clusters.
1138 */
1139 lp = &cg_clustersum(cgp)[len - 1];
1140 for (i = len - 1; i > 0; i--)
1141 if (*lp-- > 0)
1142 break;
1143 fs->fs_maxcluster[cg] = i;
1144 #if REV_ENDIAN_FS
1145 if (rev_endian)
1146 byte_swap_cgout(cgp,fs);
1147 #endif /* REV_ENDIAN_FS */
1148 goto fail;
1149 }
1150 /*
1151 * Search the cluster map to find a big enough cluster.
1152 * We take the first one that we find, even if it is larger
1153 * than we need as we prefer to get one close to the previous
1154 * block allocation. We do not search before the current
1155 * preference point as we do not want to allocate a block
1156 * that is allocated before the previous one (as we will
1157 * then have to wait for another pass of the elevator
1158 * algorithm before it will be read). We prefer to fail and
1159 * be recalled to try an allocation in the next cylinder group.
1160 */
1161 if (dtog(fs, bpref) != cg)
1162 bpref = 0;
1163 else
1164 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1165 mapp = &cg_clustersfree(cgp)[bpref / NBBY];
1166 map = *mapp++;
1167 bit = 1 << (bpref % NBBY);
1168 for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
1169 if ((map & bit) == 0) {
1170 run = 0;
1171 } else {
1172 run++;
1173 if (run == len)
1174 break;
1175 }
1176 if ((got & (NBBY - 1)) != (NBBY - 1)) {
1177 bit <<= 1;
1178 } else {
1179 map = *mapp++;
1180 bit = 1;
1181 }
1182 }
1183 if (got == cgp->cg_nclusterblks) {
1184 #if REV_ENDIAN_FS
1185 if (rev_endian)
1186 byte_swap_cgout(cgp,fs);
1187 #endif /* REV_ENDIAN_FS */
1188 goto fail;
1189 }
1190 /*
1191 * Allocate the cluster that we have found.
1192 */
1193 for (i = 1; i <= len; i++)
1194 if (!ffs_isblock(fs, cg_blksfree(cgp), got - run + i))
1195 panic("ffs_clusteralloc: map mismatch");
1196 bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1);
1197 if (dtog(fs, bno) != cg)
1198 panic("ffs_clusteralloc: allocated out of group");
1199 len = blkstofrags(fs, len);
1200 for (i = 0; i < len; i += fs->fs_frag)
1201 if ((got = ffs_alloccgblk(fs, cgp, bno + i)) != bno + i)
1202 panic("ffs_clusteralloc: lost block");
1203 #if REV_ENDIAN_FS
1204 if (rev_endian)
1205 byte_swap_cgout(cgp,fs);
1206 #endif /* REV_ENDIAN_FS */
1207 buf_bdwrite(bp);
1208 return (bno);
1209
1210 fail:
1211 buf_brelse(bp);
1212 return (0);
1213 }
1214
1215 /*
1216 * Determine whether an inode can be allocated.
1217 *
1218 * Check to see if an inode is available, and if it is,
1219 * allocate it using the following policy:
1220 * 1) allocate the requested inode.
1221 * 2) allocate the next available inode after the requested
1222 * inode in the specified cylinder group.
1223 */
1224 static ino_t
1225 ffs_nodealloccg(ip, cg, ipref, mode)
1226 struct inode *ip;
1227 int cg;
1228 ufs_daddr_t ipref;
1229 int mode;
1230 {
1231 register struct fs *fs;
1232 register struct cg *cgp;
1233 struct buf *bp;
1234 struct timeval tv;
1235 int error, start, len, loc, map, i;
1236 #if REV_ENDIAN_FS
1237 struct vnode *vp=ITOV(ip);
1238 struct mount *mp=vp->v_mount;
1239 int rev_endian=(mp->mnt_flag & MNT_REVEND);
1240 #endif /* REV_ENDIAN_FS */
1241
1242 fs = ip->i_fs;
1243 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1244 return (NULL);
1245 error = (int)buf_bread(ip->i_devvp, (daddr64_t)((unsigned)fsbtodb(fs, cgtod(fs, cg))),
1246 (int)fs->fs_cgsize, NOCRED, &bp);
1247 if (error) {
1248 buf_brelse(bp);
1249 return (NULL);
1250 }
1251 cgp = (struct cg *)buf_dataptr(bp);
1252 #if REV_ENDIAN_FS
1253 if (rev_endian)
1254 byte_swap_cgin(cgp,fs);
1255 #endif /* REV_ENDIAN_FS */
1256 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
1257 #if REV_ENDIAN_FS
1258 if (rev_endian)
1259 byte_swap_cgout(cgp,fs);
1260 #endif /* REV_ENDIAN_FS */
1261 buf_brelse(bp);
1262 return (NULL);
1263 }
1264
1265 microtime(&tv);
1266 cgp->cg_time = tv.tv_sec;
1267 if (ipref) {
1268 ipref %= fs->fs_ipg;
1269 if (isclr(cg_inosused(cgp), ipref))
1270 goto gotit;
1271 }
1272 start = cgp->cg_irotor / NBBY;
1273 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
1274 loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
1275 if (loc == 0) {
1276 len = start + 1;
1277 start = 0;
1278 loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
1279 if (loc == 0) {
1280 printf("cg = %d, irotor = %d, fs = %s\n",
1281 cg, cgp->cg_irotor, fs->fs_fsmnt);
1282 panic("ffs_nodealloccg: map corrupted");
1283 /* NOTREACHED */
1284 }
1285 }
1286 i = start + len - loc;
1287 map = cg_inosused(cgp)[i];
1288 ipref = i * NBBY;
1289 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1290 if ((map & i) == 0) {
1291 cgp->cg_irotor = ipref;
1292 goto gotit;
1293 }
1294 }
1295 printf("fs = %s\n", fs->fs_fsmnt);
1296 panic("ffs_nodealloccg: block not in map");
1297 /* NOTREACHED */
1298 gotit:
1299 setbit(cg_inosused(cgp), ipref);
1300 cgp->cg_cs.cs_nifree--;
1301 fs->fs_cstotal.cs_nifree--;
1302 fs->fs_cs(fs, cg).cs_nifree--;
1303 fs->fs_fmod = 1;
1304 if ((mode & IFMT) == IFDIR) {
1305 cgp->cg_cs.cs_ndir++;
1306 fs->fs_cstotal.cs_ndir++;
1307 fs->fs_cs(fs, cg).cs_ndir++;
1308 }
1309 #if REV_ENDIAN_FS
1310 if (rev_endian)
1311 byte_swap_cgout(cgp,fs);
1312 #endif /* REV_ENDIAN_FS */
1313 buf_bdwrite(bp);
1314 return (cg * fs->fs_ipg + ipref);
1315 }
1316
1317 /*
1318 * Free a block or fragment.
1319 *
1320 * The specified block or fragment is placed back in the
1321 * free map. If a fragment is deallocated, a possible
1322 * block reassembly is checked.
1323 */
1324 void
1325 ffs_blkfree(ip, bno, size)
1326 register struct inode *ip;
1327 ufs_daddr_t bno;
1328 long size;
1329 {
1330 register struct fs *fs;
1331 register struct cg *cgp;
1332 struct buf *bp;
1333 struct timeval tv;
1334 ufs_daddr_t blkno;
1335 int i, error, cg, blk, frags, bbase;
1336 #if REV_ENDIAN_FS
1337 struct vnode *vp=ITOV(ip);
1338 struct mount *mp=vp->v_mount;
1339 int rev_endian=(mp->mnt_flag & MNT_REVEND);
1340 #endif /* REV_ENDIAN_FS */
1341
1342 fs = ip->i_fs;
1343 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1344 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
1345 ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
1346 panic("blkfree: bad size");
1347 }
1348 cg = dtog(fs, bno);
1349 if ((u_int)bno >= fs->fs_size) {
1350 printf("bad block %d, ino %d\n", bno, ip->i_number);
1351 ffs_fserr(fs, ip->i_uid, "bad block");
1352 return;
1353 }
1354 error = (int)buf_bread(ip->i_devvp, (daddr64_t)((unsigned)fsbtodb(fs, cgtod(fs, cg))),
1355 (int)fs->fs_cgsize, NOCRED, &bp);
1356 if (error) {
1357 buf_brelse(bp);
1358 return;
1359 }
1360 cgp = (struct cg *)buf_dataptr(bp);
1361 #if REV_ENDIAN_FS
1362 if (rev_endian)
1363 byte_swap_cgin(cgp,fs);
1364 #endif /* REV_ENDIAN_FS */
1365 if (!cg_chkmagic(cgp)) {
1366 #if REV_ENDIAN_FS
1367 if (rev_endian)
1368 byte_swap_cgout(cgp,fs);
1369 #endif /* REV_ENDIAN_FS */
1370 buf_brelse(bp);
1371 return;
1372 }
1373 microtime(&tv);
1374 cgp->cg_time = tv.tv_sec;
1375 bno = dtogd(fs, bno);
1376 if (size == fs->fs_bsize) {
1377 blkno = fragstoblks(fs, bno);
1378 if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) {
1379 printf("dev = 0x%x, block = %d, fs = %s\n",
1380 ip->i_dev, bno, fs->fs_fsmnt);
1381 panic("blkfree: freeing free block");
1382 }
1383 ffs_setblock(fs, cg_blksfree(cgp), blkno);
1384 ffs_clusteracct(fs, cgp, blkno, 1);
1385 cgp->cg_cs.cs_nbfree++;
1386 fs->fs_cstotal.cs_nbfree++;
1387 fs->fs_cs(fs, cg).cs_nbfree++;
1388 i = cbtocylno(fs, bno);
1389 cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
1390 cg_blktot(cgp)[i]++;
1391 } else {
1392 bbase = bno - fragnum(fs, bno);
1393 /*
1394 * decrement the counts associated with the old frags
1395 */
1396 blk = blkmap(fs, cg_blksfree(cgp), bbase);
1397 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
1398 /*
1399 * deallocate the fragment
1400 */
1401 frags = numfrags(fs, size);
1402 for (i = 0; i < frags; i++) {
1403 if (isset(cg_blksfree(cgp), bno + i)) {
1404 printf("dev = 0x%x, block = %d, fs = %s\n",
1405 ip->i_dev, bno + i, fs->fs_fsmnt);
1406 panic("blkfree: freeing free frag");
1407 }
1408 setbit(cg_blksfree(cgp), bno + i);
1409 }
1410 cgp->cg_cs.cs_nffree += i;
1411 fs->fs_cstotal.cs_nffree += i;
1412 fs->fs_cs(fs, cg).cs_nffree += i;
1413 /*
1414 * add back in counts associated with the new frags
1415 */
1416 blk = blkmap(fs, cg_blksfree(cgp), bbase);
1417 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
1418 /*
1419 * if a complete block has been reassembled, account for it
1420 */
1421 blkno = fragstoblks(fs, bbase);
1422 if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) {
1423 cgp->cg_cs.cs_nffree -= fs->fs_frag;
1424 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1425 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1426 ffs_clusteracct(fs, cgp, blkno, 1);
1427 cgp->cg_cs.cs_nbfree++;
1428 fs->fs_cstotal.cs_nbfree++;
1429 fs->fs_cs(fs, cg).cs_nbfree++;
1430 i = cbtocylno(fs, bbase);
1431 cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
1432 cg_blktot(cgp)[i]++;
1433 }
1434 }
1435 fs->fs_fmod = 1;
1436 #if REV_ENDIAN_FS
1437 if (rev_endian)
1438 byte_swap_cgout(cgp,fs);
1439 #endif /* REV_ENDIAN_FS */
1440 buf_bdwrite(bp);
1441 }
1442
1443 #if DIAGNOSTIC
1444 /*
1445 * Verify allocation of a block or fragment. Returns true if block or
1446 * fragment is allocated, false if it is free.
1447 */
1448 ffs_checkblk(ip, bno, size)
1449 struct inode *ip;
1450 ufs_daddr_t bno;
1451 long size;
1452 {
1453 struct fs *fs;
1454 struct cg *cgp;
1455 struct buf *bp;
1456 int i, error, frags, free;
1457 #if REV_ENDIAN_FS
1458 struct vnode *vp=ITOV(ip);
1459 struct mount *mp=vp->v_mount;
1460 int rev_endian=(mp->mnt_flag & MNT_REVEND);
1461 #endif /* REV_ENDIAN_FS */
1462
1463 fs = ip->i_fs;
1464 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1465 printf("bsize = %d, size = %d, fs = %s\n",
1466 fs->fs_bsize, size, fs->fs_fsmnt);
1467 panic("checkblk: bad size");
1468 }
1469 if ((u_int)bno >= fs->fs_size)
1470 panic("checkblk: bad block %d", bno);
1471 error = (int)buf_bread(ip->i_devvp, (daddr64_t)((unsigned)fsbtodb(fs, cgtod(fs, dtog(fs, bno)))),
1472 (int)fs->fs_cgsize, NOCRED, &bp);
1473 if (error) {
1474 buf_brelse(bp);
1475 return;
1476 }
1477 cgp = (struct cg *)buf_dataptr(bp);
1478 #if REV_ENDIAN_FS
1479 if (rev_endian)
1480 byte_swap_cgin(cgp,fs);
1481 #endif /* REV_ENDIAN_FS */
1482 if (!cg_chkmagic(cgp)) {
1483 #if REV_ENDIAN_FS
1484 if (rev_endian)
1485 byte_swap_cgout(cgp,fs);
1486 #endif /* REV_ENDIAN_FS */
1487 buf_brelse(bp);
1488 return;
1489 }
1490 bno = dtogd(fs, bno);
1491 if (size == fs->fs_bsize) {
1492 free = ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno));
1493 } else {
1494 frags = numfrags(fs, size);
1495 for (free = 0, i = 0; i < frags; i++)
1496 if (isset(cg_blksfree(cgp), bno + i))
1497 free++;
1498 if (free != 0 && free != frags)
1499 panic("checkblk: partially free fragment");
1500 }
1501 #if REV_ENDIAN_FS
1502 if (rev_endian)
1503 byte_swap_cgout(cgp,fs);
1504 #endif /* REV_ENDIAN_FS */
1505 buf_brelse(bp);
1506 return (!free);
1507 }
1508 #endif /* DIAGNOSTIC */
1509
1510 /*
1511 * Free an inode.
1512 *
1513 * The specified inode is placed back in the free map.
1514 */
1515 int
1516 ffs_vfree(struct vnode *vp, ino_t ino, int mode)
1517 {
1518 register struct fs *fs;
1519 register struct cg *cgp;
1520 register struct inode *pip;
1521 struct buf *bp;
1522 struct timeval tv;
1523 int error, cg;
1524 #if REV_ENDIAN_FS
1525 struct mount *mp=vp->v_mount;
1526 int rev_endian=(mp->mnt_flag & MNT_REVEND);
1527 #endif /* REV_ENDIAN_FS */
1528
1529 pip = VTOI(vp);
1530 fs = pip->i_fs;
1531 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1532 panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n",
1533 pip->i_dev, ino, fs->fs_fsmnt);
1534 cg = ino_to_cg(fs, ino);
1535 error = (int)buf_bread(pip->i_devvp, (daddr64_t)((unsigned)fsbtodb(fs, cgtod(fs, cg))),
1536 (int)fs->fs_cgsize, NOCRED, &bp);
1537 if (error) {
1538 buf_brelse(bp);
1539 return (0);
1540 }
1541 cgp = (struct cg *)buf_dataptr(bp);
1542 #if REV_ENDIAN_FS
1543 if (rev_endian)
1544 byte_swap_cgin(cgp,fs);
1545 #endif /* REV_ENDIAN_FS */
1546 if (!cg_chkmagic(cgp)) {
1547 #if REV_ENDIAN_FS
1548 if (rev_endian)
1549 byte_swap_cgout(cgp,fs);
1550 #endif /* REV_ENDIAN_FS */
1551 buf_brelse(bp);
1552 return (0);
1553 }
1554 microtime(&tv);
1555 cgp->cg_time = tv.tv_sec;
1556 ino %= fs->fs_ipg;
1557 if (isclr(cg_inosused(cgp), ino)) {
1558 printf("dev = 0x%x, ino = %d, fs = %s\n",
1559 pip->i_dev, ino, fs->fs_fsmnt);
1560 if (fs->fs_ronly == 0)
1561 panic("ifree: freeing free inode");
1562 }
1563 clrbit(cg_inosused(cgp), ino);
1564 if (ino < cgp->cg_irotor)
1565 cgp->cg_irotor = ino;
1566 cgp->cg_cs.cs_nifree++;
1567 fs->fs_cstotal.cs_nifree++;
1568 fs->fs_cs(fs, cg).cs_nifree++;
1569 if ((mode & IFMT) == IFDIR) {
1570 cgp->cg_cs.cs_ndir--;
1571 fs->fs_cstotal.cs_ndir--;
1572 fs->fs_cs(fs, cg).cs_ndir--;
1573 }
1574 fs->fs_fmod = 1;
1575 #if REV_ENDIAN_FS
1576 if (rev_endian)
1577 byte_swap_cgout(cgp,fs);
1578 #endif /* REV_ENDIAN_FS */
1579 buf_bdwrite(bp);
1580 return (0);
1581 }
1582
1583 /*
1584 * Find a block of the specified size in the specified cylinder group.
1585 *
1586 * It is a panic if a request is made to find a block if none are
1587 * available.
1588 */
1589 static ufs_daddr_t
1590 ffs_mapsearch(fs, cgp, bpref, allocsiz)
1591 register struct fs *fs;
1592 register struct cg *cgp;
1593 ufs_daddr_t bpref;
1594 int allocsiz;
1595 {
1596 ufs_daddr_t bno;
1597 int start, len, loc, i;
1598 int blk, field, subfield, pos;
1599
1600 /*
1601 * find the fragment by searching through the free block
1602 * map for an appropriate bit pattern
1603 */
1604 if (bpref)
1605 start = dtogd(fs, bpref) / NBBY;
1606 else
1607 start = cgp->cg_frotor / NBBY;
1608 len = howmany(fs->fs_fpg, NBBY) - start;
1609 loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[start],
1610 (u_char *)fragtbl[fs->fs_frag],
1611 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1612 if (loc == 0) {
1613 len = start + 1;
1614 start = 0;
1615 loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[0],
1616 (u_char *)fragtbl[fs->fs_frag],
1617 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1618 if (loc == 0) {
1619 printf("start = %d, len = %d, fs = %s\n",
1620 start, len, fs->fs_fsmnt);
1621 panic("ffs_alloccg: map corrupted");
1622 /* NOTREACHED */
1623 }
1624 }
1625 bno = (start + len - loc) * NBBY;
1626 cgp->cg_frotor = bno;
1627 /*
1628 * found the byte in the map
1629 * sift through the bits to find the selected frag
1630 */
1631 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1632 blk = blkmap(fs, cg_blksfree(cgp), bno);
1633 blk <<= 1;
1634 field = around[allocsiz];
1635 subfield = inside[allocsiz];
1636 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
1637 if ((blk & field) == subfield)
1638 return (bno + pos);
1639 field <<= 1;
1640 subfield <<= 1;
1641 }
1642 }
1643 printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
1644 panic("ffs_alloccg: block not in map");
1645 return (-1);
1646 }
1647
1648 /*
1649 * Update the cluster map because of an allocation or free.
1650 *
1651 * Cnt == 1 means free; cnt == -1 means allocating.
1652 */
1653 static void
1654 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs_daddr_t blkno, int cnt)
1655 {
1656 int32_t *sump;
1657 int32_t *lp;
1658 u_char *freemapp, *mapp;
1659 int i, start, end, forw, back, map, bit;
1660
1661 if (fs->fs_contigsumsize <= 0)
1662 return;
1663 freemapp = cg_clustersfree(cgp);
1664 sump = cg_clustersum(cgp);
1665 /*
1666 * Allocate or clear the actual block.
1667 */
1668 if (cnt > 0)
1669 setbit(freemapp, blkno);
1670 else
1671 clrbit(freemapp, blkno);
1672 /*
1673 * Find the size of the cluster going forward.
1674 */
1675 start = blkno + 1;
1676 end = start + fs->fs_contigsumsize;
1677 if (end >= cgp->cg_nclusterblks)
1678 end = cgp->cg_nclusterblks;
1679 mapp = &freemapp[start / NBBY];
1680 map = *mapp++;
1681 bit = 1 << (start % NBBY);
1682 for (i = start; i < end; i++) {
1683 if ((map & bit) == 0)
1684 break;
1685 if ((i & (NBBY - 1)) != (NBBY - 1)) {
1686 bit <<= 1;
1687 } else {
1688 map = *mapp++;
1689 bit = 1;
1690 }
1691 }
1692 forw = i - start;
1693 /*
1694 * Find the size of the cluster going backward.
1695 */
1696 start = blkno - 1;
1697 end = start - fs->fs_contigsumsize;
1698 if (end < 0)
1699 end = -1;
1700 mapp = &freemapp[start / NBBY];
1701 map = *mapp--;
1702 bit = 1 << (start % NBBY);
1703 for (i = start; i > end; i--) {
1704 if ((map & bit) == 0)
1705 break;
1706 if ((i & (NBBY - 1)) != 0) {
1707 bit >>= 1;
1708 } else {
1709 map = *mapp--;
1710 bit = 1 << (NBBY - 1);
1711 }
1712 }
1713 back = start - i;
1714 /*
1715 * Account for old cluster and the possibly new forward and
1716 * back clusters.
1717 */
1718 i = back + forw + 1;
1719 if (i > fs->fs_contigsumsize)
1720 i = fs->fs_contigsumsize;
1721 sump[i] += cnt;
1722 if (back > 0)
1723 sump[back] -= cnt;
1724 if (forw > 0)
1725 sump[forw] -= cnt;
1726 /*
1727 * Update cluster summary information.
1728 */
1729 lp = &sump[fs->fs_contigsumsize];
1730 for (i = fs->fs_contigsumsize; i > 0; i--)
1731 if (*lp-- > 0)
1732 break;
1733 fs->fs_maxcluster[cgp->cg_cgx] = i;
1734 }
1735
1736 /*
1737 * Fserr prints the name of a file system with an error diagnostic.
1738 *
1739 * The form of the error message is:
1740 * fs: error message
1741 */
1742 static void
1743 ffs_fserr(fs, uid, cp)
1744 struct fs *fs;
1745 u_int uid;
1746 char *cp;
1747 {
1748
1749 log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
1750 }