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