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