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