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