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