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