ffs_alloc.c revision 1.85 1 /* $NetBSD: ffs_alloc.c,v 1.85 2005/07/15 05:01:16 thorpej 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.85 2005/07/15 05:01:16 thorpej 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 %u ipref %u\n", ino, ipref);
704 #if 0
705 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
706 (int)fs->fs_bsize, NOCRED, &bp);
707 #endif
708
709 #endif
710 panic("ffs_valloc: dup alloc");
711 }
712 if (DIP(ip, blocks)) { /* XXX */
713 printf("free inode %s/%d had %" PRId64 " blocks\n",
714 fs->fs_fsmnt, ino, DIP(ip, blocks));
715 DIP_ASSIGN(ip, blocks, 0);
716 }
717 ip->i_flag &= ~IN_SPACECOUNTED;
718 ip->i_flags = 0;
719 DIP_ASSIGN(ip, flags, 0);
720 /*
721 * Set up a new generation number for this inode.
722 */
723 ip->i_gen++;
724 DIP_ASSIGN(ip, gen, ip->i_gen);
725 if (fs->fs_magic == FS_UFS2_MAGIC) {
726 TIMEVAL_TO_TIMESPEC(&time, &ts);
727 ip->i_ffs2_birthtime = ts.tv_sec;
728 ip->i_ffs2_birthnsec = ts.tv_nsec;
729 }
730 return (0);
731 noinodes:
732 ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes");
733 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
734 return (ENOSPC);
735 }
736
737 /*
738 * Find a cylinder group in which to place a directory.
739 *
740 * The policy implemented by this algorithm is to allocate a
741 * directory inode in the same cylinder group as its parent
742 * directory, but also to reserve space for its files inodes
743 * and data. Restrict the number of directories which may be
744 * allocated one after another in the same cylinder group
745 * without intervening allocation of files.
746 *
747 * If we allocate a first level directory then force allocation
748 * in another cylinder group.
749 */
750 static ino_t
751 ffs_dirpref(struct inode *pip)
752 {
753 register struct fs *fs;
754 int cg, prefcg;
755 int64_t dirsize, cgsize;
756 int avgifree, avgbfree, avgndir, curdirsize;
757 int minifree, minbfree, maxndir;
758 int mincg, minndir;
759 int maxcontigdirs;
760
761 fs = pip->i_fs;
762
763 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
764 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
765 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
766
767 /*
768 * Force allocation in another cg if creating a first level dir.
769 */
770 if (ITOV(pip)->v_flag & VROOT) {
771 prefcg = random() % fs->fs_ncg;
772 mincg = prefcg;
773 minndir = fs->fs_ipg;
774 for (cg = prefcg; cg < fs->fs_ncg; cg++)
775 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
776 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
777 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
778 mincg = cg;
779 minndir = fs->fs_cs(fs, cg).cs_ndir;
780 }
781 for (cg = 0; cg < prefcg; cg++)
782 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
783 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
784 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
785 mincg = cg;
786 minndir = fs->fs_cs(fs, cg).cs_ndir;
787 }
788 return ((ino_t)(fs->fs_ipg * mincg));
789 }
790
791 /*
792 * Count various limits which used for
793 * optimal allocation of a directory inode.
794 */
795 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
796 minifree = avgifree - fs->fs_ipg / 4;
797 if (minifree < 0)
798 minifree = 0;
799 minbfree = avgbfree - fragstoblks(fs, fs->fs_fpg) / 4;
800 if (minbfree < 0)
801 minbfree = 0;
802 cgsize = fs->fs_fsize * fs->fs_fpg;
803 dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir;
804 curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
805 if (dirsize < curdirsize)
806 dirsize = curdirsize;
807 maxcontigdirs = min(cgsize / dirsize, 255);
808 if (fs->fs_avgfpdir > 0)
809 maxcontigdirs = min(maxcontigdirs,
810 fs->fs_ipg / fs->fs_avgfpdir);
811 if (maxcontigdirs == 0)
812 maxcontigdirs = 1;
813
814 /*
815 * Limit number of dirs in one cg and reserve space for
816 * regular files, but only if we have no deficit in
817 * inodes or space.
818 */
819 prefcg = ino_to_cg(fs, pip->i_number);
820 for (cg = prefcg; cg < fs->fs_ncg; cg++)
821 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
822 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
823 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
824 if (fs->fs_contigdirs[cg] < maxcontigdirs)
825 return ((ino_t)(fs->fs_ipg * cg));
826 }
827 for (cg = 0; cg < prefcg; cg++)
828 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
829 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
830 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
831 if (fs->fs_contigdirs[cg] < maxcontigdirs)
832 return ((ino_t)(fs->fs_ipg * cg));
833 }
834 /*
835 * This is a backstop when we are deficient in space.
836 */
837 for (cg = prefcg; cg < fs->fs_ncg; cg++)
838 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
839 return ((ino_t)(fs->fs_ipg * cg));
840 for (cg = 0; cg < prefcg; cg++)
841 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
842 break;
843 return ((ino_t)(fs->fs_ipg * cg));
844 }
845
846 /*
847 * Select the desired position for the next block in a file. The file is
848 * logically divided into sections. The first section is composed of the
849 * direct blocks. Each additional section contains fs_maxbpg blocks.
850 *
851 * If no blocks have been allocated in the first section, the policy is to
852 * request a block in the same cylinder group as the inode that describes
853 * the file. If no blocks have been allocated in any other section, the
854 * policy is to place the section in a cylinder group with a greater than
855 * average number of free blocks. An appropriate cylinder group is found
856 * by using a rotor that sweeps the cylinder groups. When a new group of
857 * blocks is needed, the sweep begins in the cylinder group following the
858 * cylinder group from which the previous allocation was made. The sweep
859 * continues until a cylinder group with greater than the average number
860 * of free blocks is found. If the allocation is for the first block in an
861 * indirect block, the information on the previous allocation is unavailable;
862 * here a best guess is made based upon the logical block number being
863 * allocated.
864 *
865 * If a section is already partially allocated, the policy is to
866 * contiguously allocate fs_maxcontig blocks. The end of one of these
867 * contiguous blocks and the beginning of the next is laid out
868 * contigously if possible.
869 */
870 daddr_t
871 ffs_blkpref_ufs1(struct inode *ip, daddr_t lbn, int indx,
872 int32_t *bap /* XXX ondisk32 */)
873 {
874 struct fs *fs;
875 int cg;
876 int avgbfree, startcg;
877
878 fs = ip->i_fs;
879 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
880 if (lbn < NDADDR + NINDIR(fs)) {
881 cg = ino_to_cg(fs, ip->i_number);
882 return (fs->fs_fpg * cg + fs->fs_frag);
883 }
884 /*
885 * Find a cylinder with greater than average number of
886 * unused data blocks.
887 */
888 if (indx == 0 || bap[indx - 1] == 0)
889 startcg =
890 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
891 else
892 startcg = dtog(fs,
893 ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
894 startcg %= fs->fs_ncg;
895 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
896 for (cg = startcg; cg < fs->fs_ncg; cg++)
897 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
898 return (fs->fs_fpg * cg + fs->fs_frag);
899 }
900 for (cg = 0; cg < startcg; cg++)
901 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
902 return (fs->fs_fpg * cg + fs->fs_frag);
903 }
904 return (0);
905 }
906 /*
907 * We just always try to lay things out contiguously.
908 */
909 return ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
910 }
911
912 daddr_t
913 ffs_blkpref_ufs2(struct inode *ip, daddr_t lbn, int indx, int64_t *bap)
914 {
915 struct fs *fs;
916 int cg;
917 int avgbfree, startcg;
918
919 fs = ip->i_fs;
920 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
921 if (lbn < NDADDR + NINDIR(fs)) {
922 cg = ino_to_cg(fs, ip->i_number);
923 return (fs->fs_fpg * cg + fs->fs_frag);
924 }
925 /*
926 * Find a cylinder with greater than average number of
927 * unused data blocks.
928 */
929 if (indx == 0 || bap[indx - 1] == 0)
930 startcg =
931 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
932 else
933 startcg = dtog(fs,
934 ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
935 startcg %= fs->fs_ncg;
936 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
937 for (cg = startcg; cg < fs->fs_ncg; cg++)
938 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
939 return (fs->fs_fpg * cg + fs->fs_frag);
940 }
941 for (cg = 0; cg < startcg; cg++)
942 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
943 return (fs->fs_fpg * cg + fs->fs_frag);
944 }
945 return (0);
946 }
947 /*
948 * We just always try to lay things out contiguously.
949 */
950 return ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
951 }
952
953
954 /*
955 * Implement the cylinder overflow algorithm.
956 *
957 * The policy implemented by this algorithm is:
958 * 1) allocate the block in its requested cylinder group.
959 * 2) quadradically rehash on the cylinder group number.
960 * 3) brute force search for a free block.
961 */
962 /*VARARGS5*/
963 static daddr_t
964 ffs_hashalloc(struct inode *ip, int cg, daddr_t pref,
965 int size /* size for data blocks, mode for inodes */,
966 daddr_t (*allocator)(struct inode *, int, daddr_t, int))
967 {
968 struct fs *fs;
969 daddr_t result;
970 int i, icg = cg;
971
972 fs = ip->i_fs;
973 /*
974 * 1: preferred cylinder group
975 */
976 result = (*allocator)(ip, cg, pref, size);
977 if (result)
978 return (result);
979 /*
980 * 2: quadratic rehash
981 */
982 for (i = 1; i < fs->fs_ncg; i *= 2) {
983 cg += i;
984 if (cg >= fs->fs_ncg)
985 cg -= fs->fs_ncg;
986 result = (*allocator)(ip, cg, 0, size);
987 if (result)
988 return (result);
989 }
990 /*
991 * 3: brute force search
992 * Note that we start at i == 2, since 0 was checked initially,
993 * and 1 is always checked in the quadratic rehash.
994 */
995 cg = (icg + 2) % fs->fs_ncg;
996 for (i = 2; i < fs->fs_ncg; i++) {
997 result = (*allocator)(ip, cg, 0, size);
998 if (result)
999 return (result);
1000 cg++;
1001 if (cg == fs->fs_ncg)
1002 cg = 0;
1003 }
1004 return (0);
1005 }
1006
1007 /*
1008 * Determine whether a fragment can be extended.
1009 *
1010 * Check to see if the necessary fragments are available, and
1011 * if they are, allocate them.
1012 */
1013 static daddr_t
1014 ffs_fragextend(struct inode *ip, int cg, daddr_t bprev, int osize, int nsize)
1015 {
1016 struct fs *fs;
1017 struct cg *cgp;
1018 struct buf *bp;
1019 daddr_t bno;
1020 int frags, bbase;
1021 int i, error;
1022 u_int8_t *blksfree;
1023
1024 fs = ip->i_fs;
1025 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
1026 return (0);
1027 frags = numfrags(fs, nsize);
1028 bbase = fragnum(fs, bprev);
1029 if (bbase > fragnum(fs, (bprev + frags - 1))) {
1030 /* cannot extend across a block boundary */
1031 return (0);
1032 }
1033 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1034 (int)fs->fs_cgsize, NOCRED, &bp);
1035 if (error) {
1036 brelse(bp);
1037 return (0);
1038 }
1039 cgp = (struct cg *)bp->b_data;
1040 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
1041 brelse(bp);
1042 return (0);
1043 }
1044 cgp->cg_old_time = ufs_rw32(time.tv_sec, UFS_FSNEEDSWAP(fs));
1045 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1046 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1047 cgp->cg_time = ufs_rw64(time.tv_sec, UFS_FSNEEDSWAP(fs));
1048 bno = dtogd(fs, bprev);
1049 blksfree = cg_blksfree(cgp, UFS_FSNEEDSWAP(fs));
1050 for (i = numfrags(fs, osize); i < frags; i++)
1051 if (isclr(blksfree, bno + i)) {
1052 brelse(bp);
1053 return (0);
1054 }
1055 /*
1056 * the current fragment can be extended
1057 * deduct the count on fragment being extended into
1058 * increase the count on the remaining fragment (if any)
1059 * allocate the extended piece
1060 */
1061 for (i = frags; i < fs->fs_frag - bbase; i++)
1062 if (isclr(blksfree, bno + i))
1063 break;
1064 ufs_add32(cgp->cg_frsum[i - numfrags(fs, osize)], -1, UFS_FSNEEDSWAP(fs));
1065 if (i != frags)
1066 ufs_add32(cgp->cg_frsum[i - frags], 1, UFS_FSNEEDSWAP(fs));
1067 for (i = numfrags(fs, osize); i < frags; i++) {
1068 clrbit(blksfree, bno + i);
1069 ufs_add32(cgp->cg_cs.cs_nffree, -1, UFS_FSNEEDSWAP(fs));
1070 fs->fs_cstotal.cs_nffree--;
1071 fs->fs_cs(fs, cg).cs_nffree--;
1072 }
1073 fs->fs_fmod = 1;
1074 if (DOINGSOFTDEP(ITOV(ip)))
1075 softdep_setup_blkmapdep(bp, fs, bprev);
1076 ACTIVECG_CLR(fs, cg);
1077 bdwrite(bp);
1078 return (bprev);
1079 }
1080
1081 /*
1082 * Determine whether a block can be allocated.
1083 *
1084 * Check to see if a block of the appropriate size is available,
1085 * and if it is, allocate it.
1086 */
1087 static daddr_t
1088 ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
1089 {
1090 struct fs *fs = ip->i_fs;
1091 struct cg *cgp;
1092 struct buf *bp;
1093 int32_t bno;
1094 daddr_t blkno;
1095 int error, frags, allocsiz, i;
1096 u_int8_t *blksfree;
1097 #ifdef FFS_EI
1098 const int needswap = UFS_FSNEEDSWAP(fs);
1099 #endif
1100
1101 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
1102 return (0);
1103 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1104 (int)fs->fs_cgsize, NOCRED, &bp);
1105 if (error) {
1106 brelse(bp);
1107 return (0);
1108 }
1109 cgp = (struct cg *)bp->b_data;
1110 if (!cg_chkmagic(cgp, needswap) ||
1111 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
1112 brelse(bp);
1113 return (0);
1114 }
1115 cgp->cg_old_time = ufs_rw32(time.tv_sec, needswap);
1116 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1117 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1118 cgp->cg_time = ufs_rw64(time.tv_sec, needswap);
1119 if (size == fs->fs_bsize) {
1120 blkno = ffs_alloccgblk(ip, bp, bpref);
1121 ACTIVECG_CLR(fs, cg);
1122 bdwrite(bp);
1123 return (blkno);
1124 }
1125 /*
1126 * check to see if any fragments are already available
1127 * allocsiz is the size which will be allocated, hacking
1128 * it down to a smaller size if necessary
1129 */
1130 blksfree = cg_blksfree(cgp, needswap);
1131 frags = numfrags(fs, size);
1132 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1133 if (cgp->cg_frsum[allocsiz] != 0)
1134 break;
1135 if (allocsiz == fs->fs_frag) {
1136 /*
1137 * no fragments were available, so a block will be
1138 * allocated, and hacked up
1139 */
1140 if (cgp->cg_cs.cs_nbfree == 0) {
1141 brelse(bp);
1142 return (0);
1143 }
1144 blkno = ffs_alloccgblk(ip, bp, bpref);
1145 bno = dtogd(fs, blkno);
1146 for (i = frags; i < fs->fs_frag; i++)
1147 setbit(blksfree, bno + i);
1148 i = fs->fs_frag - frags;
1149 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1150 fs->fs_cstotal.cs_nffree += i;
1151 fs->fs_cs(fs, cg).cs_nffree += i;
1152 fs->fs_fmod = 1;
1153 ufs_add32(cgp->cg_frsum[i], 1, needswap);
1154 ACTIVECG_CLR(fs, cg);
1155 bdwrite(bp);
1156 return (blkno);
1157 }
1158 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1159 #if 0
1160 /*
1161 * XXX fvdl mapsearch will panic, and never return -1
1162 * also: returning NULL as daddr_t ?
1163 */
1164 if (bno < 0) {
1165 brelse(bp);
1166 return (0);
1167 }
1168 #endif
1169 for (i = 0; i < frags; i++)
1170 clrbit(blksfree, bno + i);
1171 ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
1172 fs->fs_cstotal.cs_nffree -= frags;
1173 fs->fs_cs(fs, cg).cs_nffree -= frags;
1174 fs->fs_fmod = 1;
1175 ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
1176 if (frags != allocsiz)
1177 ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
1178 blkno = cg * fs->fs_fpg + bno;
1179 if (DOINGSOFTDEP(ITOV(ip)))
1180 softdep_setup_blkmapdep(bp, fs, blkno);
1181 ACTIVECG_CLR(fs, cg);
1182 bdwrite(bp);
1183 return blkno;
1184 }
1185
1186 /*
1187 * Allocate a block in a cylinder group.
1188 *
1189 * This algorithm implements the following policy:
1190 * 1) allocate the requested block.
1191 * 2) allocate a rotationally optimal block in the same cylinder.
1192 * 3) allocate the next available block on the block rotor for the
1193 * specified cylinder group.
1194 * Note that this routine only allocates fs_bsize blocks; these
1195 * blocks may be fragmented by the routine that allocates them.
1196 */
1197 static daddr_t
1198 ffs_alloccgblk(struct inode *ip, struct buf *bp, daddr_t bpref)
1199 {
1200 struct fs *fs = ip->i_fs;
1201 struct cg *cgp;
1202 daddr_t blkno;
1203 int32_t bno;
1204 u_int8_t *blksfree;
1205 #ifdef FFS_EI
1206 const int needswap = UFS_FSNEEDSWAP(fs);
1207 #endif
1208
1209 cgp = (struct cg *)bp->b_data;
1210 blksfree = cg_blksfree(cgp, needswap);
1211 if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
1212 bpref = ufs_rw32(cgp->cg_rotor, needswap);
1213 } else {
1214 bpref = blknum(fs, bpref);
1215 bno = dtogd(fs, bpref);
1216 /*
1217 * if the requested block is available, use it
1218 */
1219 if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno)))
1220 goto gotit;
1221 }
1222 /*
1223 * Take the next available block in this cylinder group.
1224 */
1225 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1226 if (bno < 0)
1227 return (0);
1228 cgp->cg_rotor = ufs_rw32(bno, needswap);
1229 gotit:
1230 blkno = fragstoblks(fs, bno);
1231 ffs_clrblock(fs, blksfree, blkno);
1232 ffs_clusteracct(fs, cgp, blkno, -1);
1233 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
1234 fs->fs_cstotal.cs_nbfree--;
1235 fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
1236 if ((fs->fs_magic == FS_UFS1_MAGIC) &&
1237 ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
1238 int cylno;
1239 cylno = old_cbtocylno(fs, bno);
1240 KASSERT(cylno >= 0);
1241 KASSERT(cylno < fs->fs_old_ncyl);
1242 KASSERT(old_cbtorpos(fs, bno) >= 0);
1243 KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, bno) < fs->fs_old_nrpos);
1244 ufs_add16(old_cg_blks(fs, cgp, cylno, needswap)[old_cbtorpos(fs, bno)], -1,
1245 needswap);
1246 ufs_add32(old_cg_blktot(cgp, needswap)[cylno], -1, needswap);
1247 }
1248 fs->fs_fmod = 1;
1249 blkno = ufs_rw32(cgp->cg_cgx, needswap) * fs->fs_fpg + bno;
1250 if (DOINGSOFTDEP(ITOV(ip)))
1251 softdep_setup_blkmapdep(bp, fs, blkno);
1252 return (blkno);
1253 }
1254
1255 #ifdef XXXUBC
1256 /*
1257 * Determine whether a cluster can be allocated.
1258 *
1259 * We do not currently check for optimal rotational layout if there
1260 * are multiple choices in the same cylinder group. Instead we just
1261 * take the first one that we find following bpref.
1262 */
1263
1264 /*
1265 * This function must be fixed for UFS2 if re-enabled.
1266 */
1267 static daddr_t
1268 ffs_clusteralloc(struct inode *ip, int cg, daddr_t bpref, int len)
1269 {
1270 struct fs *fs;
1271 struct cg *cgp;
1272 struct buf *bp;
1273 int i, got, run, bno, bit, map;
1274 u_char *mapp;
1275 int32_t *lp;
1276
1277 fs = ip->i_fs;
1278 if (fs->fs_maxcluster[cg] < len)
1279 return (0);
1280 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1281 NOCRED, &bp))
1282 goto fail;
1283 cgp = (struct cg *)bp->b_data;
1284 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs)))
1285 goto fail;
1286 /*
1287 * Check to see if a cluster of the needed size (or bigger) is
1288 * available in this cylinder group.
1289 */
1290 lp = &cg_clustersum(cgp, UFS_FSNEEDSWAP(fs))[len];
1291 for (i = len; i <= fs->fs_contigsumsize; i++)
1292 if (ufs_rw32(*lp++, UFS_FSNEEDSWAP(fs)) > 0)
1293 break;
1294 if (i > fs->fs_contigsumsize) {
1295 /*
1296 * This is the first time looking for a cluster in this
1297 * cylinder group. Update the cluster summary information
1298 * to reflect the true maximum sized cluster so that
1299 * future cluster allocation requests can avoid reading
1300 * the cylinder group map only to find no clusters.
1301 */
1302 lp = &cg_clustersum(cgp, UFS_FSNEEDSWAP(fs))[len - 1];
1303 for (i = len - 1; i > 0; i--)
1304 if (ufs_rw32(*lp--, UFS_FSNEEDSWAP(fs)) > 0)
1305 break;
1306 fs->fs_maxcluster[cg] = i;
1307 goto fail;
1308 }
1309 /*
1310 * Search the cluster map to find a big enough cluster.
1311 * We take the first one that we find, even if it is larger
1312 * than we need as we prefer to get one close to the previous
1313 * block allocation. We do not search before the current
1314 * preference point as we do not want to allocate a block
1315 * that is allocated before the previous one (as we will
1316 * then have to wait for another pass of the elevator
1317 * algorithm before it will be read). We prefer to fail and
1318 * be recalled to try an allocation in the next cylinder group.
1319 */
1320 if (dtog(fs, bpref) != cg)
1321 bpref = 0;
1322 else
1323 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1324 mapp = &cg_clustersfree(cgp, UFS_FSNEEDSWAP(fs))[bpref / NBBY];
1325 map = *mapp++;
1326 bit = 1 << (bpref % NBBY);
1327 for (run = 0, got = bpref;
1328 got < ufs_rw32(cgp->cg_nclusterblks, UFS_FSNEEDSWAP(fs)); got++) {
1329 if ((map & bit) == 0) {
1330 run = 0;
1331 } else {
1332 run++;
1333 if (run == len)
1334 break;
1335 }
1336 if ((got & (NBBY - 1)) != (NBBY - 1)) {
1337 bit <<= 1;
1338 } else {
1339 map = *mapp++;
1340 bit = 1;
1341 }
1342 }
1343 if (got == ufs_rw32(cgp->cg_nclusterblks, UFS_FSNEEDSWAP(fs)))
1344 goto fail;
1345 /*
1346 * Allocate the cluster that we have found.
1347 */
1348 #ifdef DIAGNOSTIC
1349 for (i = 1; i <= len; i++)
1350 if (!ffs_isblock(fs, cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)),
1351 got - run + i))
1352 panic("ffs_clusteralloc: map mismatch");
1353 #endif
1354 bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1);
1355 if (dtog(fs, bno) != cg)
1356 panic("ffs_clusteralloc: allocated out of group");
1357 len = blkstofrags(fs, len);
1358 for (i = 0; i < len; i += fs->fs_frag)
1359 if ((got = ffs_alloccgblk(ip, bp, bno + i)) != bno + i)
1360 panic("ffs_clusteralloc: lost block");
1361 ACTIVECG_CLR(fs, cg);
1362 bdwrite(bp);
1363 return (bno);
1364
1365 fail:
1366 brelse(bp);
1367 return (0);
1368 }
1369 #endif /* XXXUBC */
1370
1371 /*
1372 * Determine whether an inode can be allocated.
1373 *
1374 * Check to see if an inode is available, and if it is,
1375 * allocate it using the following policy:
1376 * 1) allocate the requested inode.
1377 * 2) allocate the next available inode after the requested
1378 * inode in the specified cylinder group.
1379 */
1380 static daddr_t
1381 ffs_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode)
1382 {
1383 struct fs *fs = ip->i_fs;
1384 struct cg *cgp;
1385 struct buf *bp, *ibp;
1386 u_int8_t *inosused;
1387 int error, start, len, loc, map, i;
1388 int32_t initediblk;
1389 struct ufs2_dinode *dp2;
1390 #ifdef FFS_EI
1391 const int needswap = UFS_FSNEEDSWAP(fs);
1392 #endif
1393
1394 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1395 return (0);
1396 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1397 (int)fs->fs_cgsize, NOCRED, &bp);
1398 if (error) {
1399 brelse(bp);
1400 return (0);
1401 }
1402 cgp = (struct cg *)bp->b_data;
1403 if (!cg_chkmagic(cgp, needswap) || cgp->cg_cs.cs_nifree == 0) {
1404 brelse(bp);
1405 return (0);
1406 }
1407 cgp->cg_old_time = ufs_rw32(time.tv_sec, needswap);
1408 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1409 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1410 cgp->cg_time = ufs_rw64(time.tv_sec, needswap);
1411 inosused = cg_inosused(cgp, needswap);
1412 if (ipref) {
1413 ipref %= fs->fs_ipg;
1414 if (isclr(inosused, ipref))
1415 goto gotit;
1416 }
1417 start = ufs_rw32(cgp->cg_irotor, needswap) / NBBY;
1418 len = howmany(fs->fs_ipg - ufs_rw32(cgp->cg_irotor, needswap),
1419 NBBY);
1420 loc = skpc(0xff, len, &inosused[start]);
1421 if (loc == 0) {
1422 len = start + 1;
1423 start = 0;
1424 loc = skpc(0xff, len, &inosused[0]);
1425 if (loc == 0) {
1426 printf("cg = %d, irotor = %d, fs = %s\n",
1427 cg, ufs_rw32(cgp->cg_irotor, needswap),
1428 fs->fs_fsmnt);
1429 panic("ffs_nodealloccg: map corrupted");
1430 /* NOTREACHED */
1431 }
1432 }
1433 i = start + len - loc;
1434 map = inosused[i];
1435 ipref = i * NBBY;
1436 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1437 if ((map & i) == 0) {
1438 cgp->cg_irotor = ufs_rw32(ipref, needswap);
1439 goto gotit;
1440 }
1441 }
1442 printf("fs = %s\n", fs->fs_fsmnt);
1443 panic("ffs_nodealloccg: block not in map");
1444 /* NOTREACHED */
1445 gotit:
1446 if (DOINGSOFTDEP(ITOV(ip)))
1447 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1448 setbit(inosused, ipref);
1449 ufs_add32(cgp->cg_cs.cs_nifree, -1, needswap);
1450 fs->fs_cstotal.cs_nifree--;
1451 fs->fs_cs(fs, cg).cs_nifree--;
1452 fs->fs_fmod = 1;
1453 if ((mode & IFMT) == IFDIR) {
1454 ufs_add32(cgp->cg_cs.cs_ndir, 1, needswap);
1455 fs->fs_cstotal.cs_ndir++;
1456 fs->fs_cs(fs, cg).cs_ndir++;
1457 }
1458 /*
1459 * Check to see if we need to initialize more inodes.
1460 */
1461 initediblk = ufs_rw32(cgp->cg_initediblk, needswap);
1462 if (fs->fs_magic == FS_UFS2_MAGIC &&
1463 ipref + INOPB(fs) > initediblk &&
1464 initediblk < ufs_rw32(cgp->cg_niblk, needswap)) {
1465 ibp = getblk(ip->i_devvp, fsbtodb(fs,
1466 ino_to_fsba(fs, cg * fs->fs_ipg + initediblk)),
1467 (int)fs->fs_bsize, 0, 0);
1468 memset(ibp->b_data, 0, fs->fs_bsize);
1469 dp2 = (struct ufs2_dinode *)(ibp->b_data);
1470 for (i = 0; i < INOPB(fs); i++) {
1471 /*
1472 * Don't bother to swap, it's supposed to be
1473 * random, after all.
1474 */
1475 dp2->di_gen = (arc4random() & INT32_MAX) / 2 + 1;
1476 dp2++;
1477 }
1478 bawrite(ibp);
1479 initediblk += INOPB(fs);
1480 cgp->cg_initediblk = ufs_rw32(initediblk, needswap);
1481 }
1482
1483 ACTIVECG_CLR(fs, cg);
1484 bdwrite(bp);
1485 return (cg * fs->fs_ipg + ipref);
1486 }
1487
1488 /*
1489 * Free a block or fragment.
1490 *
1491 * The specified block or fragment is placed back in the
1492 * free map. If a fragment is deallocated, a possible
1493 * block reassembly is checked.
1494 */
1495 void
1496 ffs_blkfree(struct fs *fs, struct vnode *devvp, daddr_t bno, long size,
1497 ino_t inum)
1498 {
1499 struct cg *cgp;
1500 struct buf *bp;
1501 struct ufsmount *ump;
1502 int32_t fragno, cgbno;
1503 daddr_t cgblkno;
1504 int i, error, cg, blk, frags, bbase;
1505 u_int8_t *blksfree;
1506 dev_t dev;
1507 const int needswap = UFS_FSNEEDSWAP(fs);
1508
1509 cg = dtog(fs, bno);
1510 if (devvp->v_type != VBLK) {
1511 /* devvp is a snapshot */
1512 dev = VTOI(devvp)->i_devvp->v_rdev;
1513 cgblkno = fragstoblks(fs, cgtod(fs, cg));
1514 } else {
1515 dev = devvp->v_rdev;
1516 ump = VFSTOUFS(devvp->v_specmountpoint);
1517 cgblkno = fsbtodb(fs, cgtod(fs, cg));
1518 if (TAILQ_FIRST(&ump->um_snapshots) != NULL &&
1519 ffs_snapblkfree(fs, devvp, bno, size, inum))
1520 return;
1521 }
1522 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1523 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1524 printf("dev = 0x%x, bno = %" PRId64 " bsize = %d, "
1525 "size = %ld, fs = %s\n",
1526 dev, bno, fs->fs_bsize, size, fs->fs_fsmnt);
1527 panic("blkfree: bad size");
1528 }
1529
1530 if (bno >= fs->fs_size) {
1531 printf("bad block %" PRId64 ", ino %d\n", bno, inum);
1532 ffs_fserr(fs, inum, "bad block");
1533 return;
1534 }
1535 error = bread(devvp, cgblkno, (int)fs->fs_cgsize, NOCRED, &bp);
1536 if (error) {
1537 brelse(bp);
1538 return;
1539 }
1540 cgp = (struct cg *)bp->b_data;
1541 if (!cg_chkmagic(cgp, needswap)) {
1542 brelse(bp);
1543 return;
1544 }
1545 cgp->cg_old_time = ufs_rw32(time.tv_sec, needswap);
1546 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1547 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1548 cgp->cg_time = ufs_rw64(time.tv_sec, needswap);
1549 cgbno = dtogd(fs, bno);
1550 blksfree = cg_blksfree(cgp, needswap);
1551 if (size == fs->fs_bsize) {
1552 fragno = fragstoblks(fs, cgbno);
1553 if (!ffs_isfreeblock(fs, blksfree, fragno)) {
1554 if (devvp->v_type != VBLK) {
1555 /* devvp is a snapshot */
1556 brelse(bp);
1557 return;
1558 }
1559 printf("dev = 0x%x, block = %" PRId64 ", fs = %s\n",
1560 dev, bno, fs->fs_fsmnt);
1561 panic("blkfree: freeing free block");
1562 }
1563 ffs_setblock(fs, blksfree, fragno);
1564 ffs_clusteracct(fs, cgp, fragno, 1);
1565 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1566 fs->fs_cstotal.cs_nbfree++;
1567 fs->fs_cs(fs, cg).cs_nbfree++;
1568 if ((fs->fs_magic == FS_UFS1_MAGIC) &&
1569 ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
1570 i = old_cbtocylno(fs, cgbno);
1571 KASSERT(i >= 0);
1572 KASSERT(i < fs->fs_old_ncyl);
1573 KASSERT(old_cbtorpos(fs, cgbno) >= 0);
1574 KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, cgbno) < fs->fs_old_nrpos);
1575 ufs_add16(old_cg_blks(fs, cgp, i, needswap)[old_cbtorpos(fs, cgbno)], 1,
1576 needswap);
1577 ufs_add32(old_cg_blktot(cgp, needswap)[i], 1, needswap);
1578 }
1579 } else {
1580 bbase = cgbno - fragnum(fs, cgbno);
1581 /*
1582 * decrement the counts associated with the old frags
1583 */
1584 blk = blkmap(fs, blksfree, bbase);
1585 ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
1586 /*
1587 * deallocate the fragment
1588 */
1589 frags = numfrags(fs, size);
1590 for (i = 0; i < frags; i++) {
1591 if (isset(blksfree, cgbno + i)) {
1592 printf("dev = 0x%x, block = %" PRId64
1593 ", fs = %s\n",
1594 dev, bno + i, fs->fs_fsmnt);
1595 panic("blkfree: freeing free frag");
1596 }
1597 setbit(blksfree, cgbno + i);
1598 }
1599 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1600 fs->fs_cstotal.cs_nffree += i;
1601 fs->fs_cs(fs, cg).cs_nffree += i;
1602 /*
1603 * add back in counts associated with the new frags
1604 */
1605 blk = blkmap(fs, blksfree, bbase);
1606 ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
1607 /*
1608 * if a complete block has been reassembled, account for it
1609 */
1610 fragno = fragstoblks(fs, bbase);
1611 if (ffs_isblock(fs, blksfree, fragno)) {
1612 ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
1613 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1614 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1615 ffs_clusteracct(fs, cgp, fragno, 1);
1616 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1617 fs->fs_cstotal.cs_nbfree++;
1618 fs->fs_cs(fs, cg).cs_nbfree++;
1619 if ((fs->fs_magic == FS_UFS1_MAGIC) &&
1620 ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
1621 i = old_cbtocylno(fs, bbase);
1622 KASSERT(i >= 0);
1623 KASSERT(i < fs->fs_old_ncyl);
1624 KASSERT(old_cbtorpos(fs, bbase) >= 0);
1625 KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, bbase) < fs->fs_old_nrpos);
1626 ufs_add16(old_cg_blks(fs, cgp, i, needswap)[old_cbtorpos(fs,
1627 bbase)], 1, needswap);
1628 ufs_add32(old_cg_blktot(cgp, needswap)[i], 1, needswap);
1629 }
1630 }
1631 }
1632 fs->fs_fmod = 1;
1633 ACTIVECG_CLR(fs, cg);
1634 bdwrite(bp);
1635 }
1636
1637 #if defined(DIAGNOSTIC) || defined(DEBUG)
1638 #ifdef XXXUBC
1639 /*
1640 * Verify allocation of a block or fragment. Returns true if block or
1641 * fragment is allocated, false if it is free.
1642 */
1643 static int
1644 ffs_checkblk(struct inode *ip, daddr_t bno, long size)
1645 {
1646 struct fs *fs;
1647 struct cg *cgp;
1648 struct buf *bp;
1649 int i, error, frags, free;
1650
1651 fs = ip->i_fs;
1652 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1653 printf("bsize = %d, size = %ld, fs = %s\n",
1654 fs->fs_bsize, size, fs->fs_fsmnt);
1655 panic("checkblk: bad size");
1656 }
1657 if (bno >= fs->fs_size)
1658 panic("checkblk: bad block %d", bno);
1659 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
1660 (int)fs->fs_cgsize, NOCRED, &bp);
1661 if (error) {
1662 brelse(bp);
1663 return 0;
1664 }
1665 cgp = (struct cg *)bp->b_data;
1666 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
1667 brelse(bp);
1668 return 0;
1669 }
1670 bno = dtogd(fs, bno);
1671 if (size == fs->fs_bsize) {
1672 free = ffs_isblock(fs, cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)),
1673 fragstoblks(fs, bno));
1674 } else {
1675 frags = numfrags(fs, size);
1676 for (free = 0, i = 0; i < frags; i++)
1677 if (isset(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i))
1678 free++;
1679 if (free != 0 && free != frags)
1680 panic("checkblk: partially free fragment");
1681 }
1682 brelse(bp);
1683 return (!free);
1684 }
1685 #endif /* XXXUBC */
1686 #endif /* DIAGNOSTIC */
1687
1688 /*
1689 * Free an inode.
1690 */
1691 int
1692 ffs_vfree(void *v)
1693 {
1694 struct vop_vfree_args /* {
1695 struct vnode *a_pvp;
1696 ino_t a_ino;
1697 int a_mode;
1698 } */ *ap = v;
1699
1700 if (DOINGSOFTDEP(ap->a_pvp)) {
1701 softdep_freefile(ap);
1702 return (0);
1703 }
1704 return (ffs_freefile(VTOI(ap->a_pvp)->i_fs, VTOI(ap->a_pvp)->i_devvp,
1705 ap->a_ino, ap->a_mode));
1706 }
1707
1708 /*
1709 * Do the actual free operation.
1710 * The specified inode is placed back in the free map.
1711 */
1712 int
1713 ffs_freefile(struct fs *fs, struct vnode *devvp, ino_t ino, int mode)
1714 {
1715 struct cg *cgp;
1716 struct buf *bp;
1717 int error, cg;
1718 daddr_t cgbno;
1719 u_int8_t *inosused;
1720 dev_t dev;
1721 #ifdef FFS_EI
1722 const int needswap = UFS_FSNEEDSWAP(fs);
1723 #endif
1724
1725 cg = ino_to_cg(fs, ino);
1726 if (devvp->v_type != VBLK) {
1727 /* devvp is a snapshot */
1728 dev = VTOI(devvp)->i_devvp->v_rdev;
1729 cgbno = fragstoblks(fs, cgtod(fs, cg));
1730 } else {
1731 dev = devvp->v_rdev;
1732 cgbno = fsbtodb(fs, cgtod(fs, cg));
1733 }
1734 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1735 panic("ifree: range: dev = 0x%x, ino = %d, fs = %s",
1736 dev, ino, fs->fs_fsmnt);
1737 error = bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp);
1738 if (error) {
1739 brelse(bp);
1740 return (error);
1741 }
1742 cgp = (struct cg *)bp->b_data;
1743 if (!cg_chkmagic(cgp, needswap)) {
1744 brelse(bp);
1745 return (0);
1746 }
1747 cgp->cg_old_time = ufs_rw32(time.tv_sec, needswap);
1748 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1749 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1750 cgp->cg_time = ufs_rw64(time.tv_sec, needswap);
1751 inosused = cg_inosused(cgp, needswap);
1752 ino %= fs->fs_ipg;
1753 if (isclr(inosused, ino)) {
1754 printf("ifree: dev = 0x%x, ino = %d, fs = %s\n",
1755 dev, ino + cg * fs->fs_ipg, fs->fs_fsmnt);
1756 if (fs->fs_ronly == 0)
1757 panic("ifree: freeing free inode");
1758 }
1759 clrbit(inosused, ino);
1760 if (ino < ufs_rw32(cgp->cg_irotor, needswap))
1761 cgp->cg_irotor = ufs_rw32(ino, needswap);
1762 ufs_add32(cgp->cg_cs.cs_nifree, 1, needswap);
1763 fs->fs_cstotal.cs_nifree++;
1764 fs->fs_cs(fs, cg).cs_nifree++;
1765 if ((mode & IFMT) == IFDIR) {
1766 ufs_add32(cgp->cg_cs.cs_ndir, -1, needswap);
1767 fs->fs_cstotal.cs_ndir--;
1768 fs->fs_cs(fs, cg).cs_ndir--;
1769 }
1770 fs->fs_fmod = 1;
1771 ACTIVECG_CLR(fs, cg);
1772 bdwrite(bp);
1773 return (0);
1774 }
1775
1776 /*
1777 * Check to see if a file is free.
1778 */
1779 int
1780 ffs_checkfreefile(struct fs *fs, struct vnode *devvp, ino_t ino)
1781 {
1782 struct cg *cgp;
1783 struct buf *bp;
1784 daddr_t cgbno;
1785 int ret, cg;
1786 u_int8_t *inosused;
1787
1788 cg = ino_to_cg(fs, ino);
1789 if (devvp->v_type != VBLK) {
1790 /* devvp is a snapshot */
1791 cgbno = fragstoblks(fs, cgtod(fs, cg));
1792 } else
1793 cgbno = fsbtodb(fs, cgtod(fs, cg));
1794 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1795 return 1;
1796 if (bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp)) {
1797 brelse(bp);
1798 return 1;
1799 }
1800 cgp = (struct cg *)bp->b_data;
1801 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
1802 brelse(bp);
1803 return 1;
1804 }
1805 inosused = cg_inosused(cgp, UFS_FSNEEDSWAP(fs));
1806 ino %= fs->fs_ipg;
1807 ret = isclr(inosused, ino);
1808 brelse(bp);
1809 return ret;
1810 }
1811
1812 /*
1813 * Find a block of the specified size in the specified cylinder group.
1814 *
1815 * It is a panic if a request is made to find a block if none are
1816 * available.
1817 */
1818 static int32_t
1819 ffs_mapsearch(struct fs *fs, struct cg *cgp, daddr_t bpref, int allocsiz)
1820 {
1821 int32_t bno;
1822 int start, len, loc, i;
1823 int blk, field, subfield, pos;
1824 int ostart, olen;
1825 u_int8_t *blksfree;
1826 #ifdef FFS_EI
1827 const int needswap = UFS_FSNEEDSWAP(fs);
1828 #endif
1829
1830 /*
1831 * find the fragment by searching through the free block
1832 * map for an appropriate bit pattern
1833 */
1834 if (bpref)
1835 start = dtogd(fs, bpref) / NBBY;
1836 else
1837 start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
1838 blksfree = cg_blksfree(cgp, needswap);
1839 len = howmany(fs->fs_fpg, NBBY) - start;
1840 ostart = start;
1841 olen = len;
1842 loc = scanc((u_int)len,
1843 (const u_char *)&blksfree[start],
1844 (const u_char *)fragtbl[fs->fs_frag],
1845 (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
1846 if (loc == 0) {
1847 len = start + 1;
1848 start = 0;
1849 loc = scanc((u_int)len,
1850 (const u_char *)&blksfree[0],
1851 (const u_char *)fragtbl[fs->fs_frag],
1852 (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
1853 if (loc == 0) {
1854 printf("start = %d, len = %d, fs = %s\n",
1855 ostart, olen, fs->fs_fsmnt);
1856 printf("offset=%d %ld\n",
1857 ufs_rw32(cgp->cg_freeoff, needswap),
1858 (long)blksfree - (long)cgp);
1859 printf("cg %d\n", cgp->cg_cgx);
1860 panic("ffs_alloccg: map corrupted");
1861 /* NOTREACHED */
1862 }
1863 }
1864 bno = (start + len - loc) * NBBY;
1865 cgp->cg_frotor = ufs_rw32(bno, needswap);
1866 /*
1867 * found the byte in the map
1868 * sift through the bits to find the selected frag
1869 */
1870 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1871 blk = blkmap(fs, blksfree, bno);
1872 blk <<= 1;
1873 field = around[allocsiz];
1874 subfield = inside[allocsiz];
1875 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
1876 if ((blk & field) == subfield)
1877 return (bno + pos);
1878 field <<= 1;
1879 subfield <<= 1;
1880 }
1881 }
1882 printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
1883 panic("ffs_alloccg: block not in map");
1884 /* return (-1); */
1885 }
1886
1887 /*
1888 * Update the cluster map because of an allocation or free.
1889 *
1890 * Cnt == 1 means free; cnt == -1 means allocating.
1891 */
1892 void
1893 ffs_clusteracct(struct fs *fs, struct cg *cgp, int32_t blkno, int cnt)
1894 {
1895 int32_t *sump;
1896 int32_t *lp;
1897 u_char *freemapp, *mapp;
1898 int i, start, end, forw, back, map, bit;
1899 #ifdef FFS_EI
1900 const int needswap = UFS_FSNEEDSWAP(fs);
1901 #endif
1902
1903 if (fs->fs_contigsumsize <= 0)
1904 return;
1905 freemapp = cg_clustersfree(cgp, needswap);
1906 sump = cg_clustersum(cgp, needswap);
1907 /*
1908 * Allocate or clear the actual block.
1909 */
1910 if (cnt > 0)
1911 setbit(freemapp, blkno);
1912 else
1913 clrbit(freemapp, blkno);
1914 /*
1915 * Find the size of the cluster going forward.
1916 */
1917 start = blkno + 1;
1918 end = start + fs->fs_contigsumsize;
1919 if (end >= ufs_rw32(cgp->cg_nclusterblks, needswap))
1920 end = ufs_rw32(cgp->cg_nclusterblks, needswap);
1921 mapp = &freemapp[start / NBBY];
1922 map = *mapp++;
1923 bit = 1 << (start % NBBY);
1924 for (i = start; i < end; i++) {
1925 if ((map & bit) == 0)
1926 break;
1927 if ((i & (NBBY - 1)) != (NBBY - 1)) {
1928 bit <<= 1;
1929 } else {
1930 map = *mapp++;
1931 bit = 1;
1932 }
1933 }
1934 forw = i - start;
1935 /*
1936 * Find the size of the cluster going backward.
1937 */
1938 start = blkno - 1;
1939 end = start - fs->fs_contigsumsize;
1940 if (end < 0)
1941 end = -1;
1942 mapp = &freemapp[start / NBBY];
1943 map = *mapp--;
1944 bit = 1 << (start % NBBY);
1945 for (i = start; i > end; i--) {
1946 if ((map & bit) == 0)
1947 break;
1948 if ((i & (NBBY - 1)) != 0) {
1949 bit >>= 1;
1950 } else {
1951 map = *mapp--;
1952 bit = 1 << (NBBY - 1);
1953 }
1954 }
1955 back = start - i;
1956 /*
1957 * Account for old cluster and the possibly new forward and
1958 * back clusters.
1959 */
1960 i = back + forw + 1;
1961 if (i > fs->fs_contigsumsize)
1962 i = fs->fs_contigsumsize;
1963 ufs_add32(sump[i], cnt, needswap);
1964 if (back > 0)
1965 ufs_add32(sump[back], -cnt, needswap);
1966 if (forw > 0)
1967 ufs_add32(sump[forw], -cnt, needswap);
1968
1969 /*
1970 * Update cluster summary information.
1971 */
1972 lp = &sump[fs->fs_contigsumsize];
1973 for (i = fs->fs_contigsumsize; i > 0; i--)
1974 if (ufs_rw32(*lp--, needswap) > 0)
1975 break;
1976 fs->fs_maxcluster[ufs_rw32(cgp->cg_cgx, needswap)] = i;
1977 }
1978
1979 /*
1980 * Fserr prints the name of a file system with an error diagnostic.
1981 *
1982 * The form of the error message is:
1983 * fs: error message
1984 */
1985 static void
1986 ffs_fserr(struct fs *fs, u_int uid, const char *cp)
1987 {
1988
1989 log(LOG_ERR, "uid %d, pid %d, command %s, on %s: %s\n",
1990 uid, curproc->p_pid, curproc->p_comm, fs->fs_fsmnt, cp);
1991 }
1992