ffs_alloc.c revision 1.89 1 /* $NetBSD: ffs_alloc.c,v 1.89 2005/11/27 11:45:56 dsl 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.89 2005/11/27 11:45:56 dsl 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 #if 0
388 /*
389 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
390 *
391 * The vnode and an array of buffer pointers for a range of sequential
392 * logical blocks to be made contiguous is given. The allocator attempts
393 * to find a range of sequential blocks starting as close as possible
394 * from the end of the allocation for the logical block immediately
395 * preceding the current range. If successful, the physical block numbers
396 * in the buffer pointers and in the inode are changed to reflect the new
397 * allocation. If unsuccessful, the allocation is left unchanged. The
398 * success in doing the reallocation is returned. Note that the error
399 * return is not reflected back to the user. Rather the previous block
400 * allocation will be used.
401
402 */
403 #ifdef XXXUBC
404 #ifdef DEBUG
405 #include <sys/sysctl.h>
406 int prtrealloc = 0;
407 struct ctldebug debug15 = { "prtrealloc", &prtrealloc };
408 #endif
409 #endif
410
411 /*
412 * NOTE: when re-enabling this, it must be updated for UFS2.
413 */
414
415 int doasyncfree = 1;
416
417 int
418 ffs_reallocblks(void *v)
419 {
420 #ifdef XXXUBC
421 struct vop_reallocblks_args /* {
422 struct vnode *a_vp;
423 struct cluster_save *a_buflist;
424 } */ *ap = v;
425 struct fs *fs;
426 struct inode *ip;
427 struct vnode *vp;
428 struct buf *sbp, *ebp;
429 int32_t *bap, *ebap = NULL, *sbap; /* XXX ondisk32 */
430 struct cluster_save *buflist;
431 daddr_t start_lbn, end_lbn, soff, newblk, blkno;
432 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
433 int i, len, start_lvl, end_lvl, pref, ssize;
434 #endif /* XXXUBC */
435
436 /* XXXUBC don't reallocblks for now */
437 return ENOSPC;
438
439 #ifdef XXXUBC
440 vp = ap->a_vp;
441 ip = VTOI(vp);
442 fs = ip->i_fs;
443 if (fs->fs_contigsumsize <= 0)
444 return (ENOSPC);
445 buflist = ap->a_buflist;
446 len = buflist->bs_nchildren;
447 start_lbn = buflist->bs_children[0]->b_lblkno;
448 end_lbn = start_lbn + len - 1;
449 #ifdef DIAGNOSTIC
450 for (i = 0; i < len; i++)
451 if (!ffs_checkblk(ip,
452 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
453 panic("ffs_reallocblks: unallocated block 1");
454 for (i = 1; i < len; i++)
455 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
456 panic("ffs_reallocblks: non-logical cluster");
457 blkno = buflist->bs_children[0]->b_blkno;
458 ssize = fsbtodb(fs, fs->fs_frag);
459 for (i = 1; i < len - 1; i++)
460 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
461 panic("ffs_reallocblks: non-physical cluster %d", i);
462 #endif
463 /*
464 * If the latest allocation is in a new cylinder group, assume that
465 * the filesystem has decided to move and do not force it back to
466 * the previous cylinder group.
467 */
468 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
469 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
470 return (ENOSPC);
471 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
472 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
473 return (ENOSPC);
474 /*
475 * Get the starting offset and block map for the first block.
476 */
477 if (start_lvl == 0) {
478 sbap = &ip->i_ffs1_db[0];
479 soff = start_lbn;
480 } else {
481 idp = &start_ap[start_lvl - 1];
482 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
483 brelse(sbp);
484 return (ENOSPC);
485 }
486 sbap = (int32_t *)sbp->b_data;
487 soff = idp->in_off;
488 }
489 /*
490 * Find the preferred location for the cluster.
491 */
492 pref = ffs_blkpref(ip, start_lbn, soff, sbap);
493 /*
494 * If the block range spans two block maps, get the second map.
495 */
496 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
497 ssize = len;
498 } else {
499 #ifdef DIAGNOSTIC
500 if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
501 panic("ffs_reallocblk: start == end");
502 #endif
503 ssize = len - (idp->in_off + 1);
504 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
505 goto fail;
506 ebap = (int32_t *)ebp->b_data; /* XXX ondisk32 */
507 }
508 /*
509 * Search the block map looking for an allocation of the desired size.
510 */
511 if ((newblk = (daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref,
512 len, ffs_clusteralloc)) == 0)
513 goto fail;
514 /*
515 * We have found a new contiguous block.
516 *
517 * First we have to replace the old block pointers with the new
518 * block pointers in the inode and indirect blocks associated
519 * with the file.
520 */
521 #ifdef DEBUG
522 if (prtrealloc)
523 printf("realloc: ino %d, lbns %d-%d\n\told:", ip->i_number,
524 start_lbn, end_lbn);
525 #endif
526 blkno = newblk;
527 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
528 daddr_t ba;
529
530 if (i == ssize) {
531 bap = ebap;
532 soff = -i;
533 }
534 /* XXX ondisk32 */
535 ba = ufs_rw32(*bap, UFS_FSNEEDSWAP(fs));
536 #ifdef DIAGNOSTIC
537 if (!ffs_checkblk(ip,
538 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
539 panic("ffs_reallocblks: unallocated block 2");
540 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != ba)
541 panic("ffs_reallocblks: alloc mismatch");
542 #endif
543 #ifdef DEBUG
544 if (prtrealloc)
545 printf(" %d,", ba);
546 #endif
547 if (DOINGSOFTDEP(vp)) {
548 if (sbap == &ip->i_ffs1_db[0] && i < ssize)
549 softdep_setup_allocdirect(ip, start_lbn + i,
550 blkno, ba, fs->fs_bsize, fs->fs_bsize,
551 buflist->bs_children[i]);
552 else
553 softdep_setup_allocindir_page(ip, start_lbn + i,
554 i < ssize ? sbp : ebp, soff + i, blkno,
555 ba, buflist->bs_children[i]);
556 }
557 /* XXX ondisk32 */
558 *bap++ = ufs_rw32((u_int32_t)blkno, UFS_FSNEEDSWAP(fs));
559 }
560 /*
561 * Next we must write out the modified inode and indirect blocks.
562 * For strict correctness, the writes should be synchronous since
563 * the old block values may have been written to disk. In practise
564 * they are almost never written, but if we are concerned about
565 * strict correctness, the `doasyncfree' flag should be set to zero.
566 *
567 * The test on `doasyncfree' should be changed to test a flag
568 * that shows whether the associated buffers and inodes have
569 * been written. The flag should be set when the cluster is
570 * started and cleared whenever the buffer or inode is flushed.
571 * We can then check below to see if it is set, and do the
572 * synchronous write only when it has been cleared.
573 */
574 if (sbap != &ip->i_ffs1_db[0]) {
575 if (doasyncfree)
576 bdwrite(sbp);
577 else
578 bwrite(sbp);
579 } else {
580 ip->i_flag |= IN_CHANGE | IN_UPDATE;
581 if (!doasyncfree)
582 ffs_update(vp, NULL, NULL, 1);
583 }
584 if (ssize < len) {
585 if (doasyncfree)
586 bdwrite(ebp);
587 else
588 bwrite(ebp);
589 }
590 /*
591 * Last, free the old blocks and assign the new blocks to the buffers.
592 */
593 #ifdef DEBUG
594 if (prtrealloc)
595 printf("\n\tnew:");
596 #endif
597 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
598 if (!DOINGSOFTDEP(vp))
599 ffs_blkfree(fs, ip->i_devvp,
600 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
601 fs->fs_bsize, ip->i_number);
602 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
603 #ifdef DEBUG
604 if (!ffs_checkblk(ip,
605 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
606 panic("ffs_reallocblks: unallocated block 3");
607 if (prtrealloc)
608 printf(" %d,", blkno);
609 #endif
610 }
611 #ifdef DEBUG
612 if (prtrealloc) {
613 prtrealloc--;
614 printf("\n");
615 }
616 #endif
617 return (0);
618
619 fail:
620 if (ssize < len)
621 brelse(ebp);
622 if (sbap != &ip->i_ffs1_db[0])
623 brelse(sbp);
624 return (ENOSPC);
625 #endif /* XXXUBC */
626 }
627 #endif /* 0 */
628
629 /*
630 * Allocate an inode in the file system.
631 *
632 * If allocating a directory, use ffs_dirpref to select the inode.
633 * If allocating in a directory, the following hierarchy is followed:
634 * 1) allocate the preferred inode.
635 * 2) allocate an inode in the same cylinder group.
636 * 3) quadradically rehash into other cylinder groups, until an
637 * available inode is located.
638 * If no inode preference is given the following hierarchy is used
639 * to allocate an inode:
640 * 1) allocate an inode in cylinder group 0.
641 * 2) quadradically rehash into other cylinder groups, until an
642 * available inode is located.
643 */
644 int
645 ffs_valloc(struct vnode *pvp, int mode, struct ucred *cred,
646 struct vnode **vpp)
647 {
648 struct inode *pip;
649 struct fs *fs;
650 struct inode *ip;
651 struct timespec ts;
652 ino_t ino, ipref;
653 int cg, error;
654
655 *vpp = NULL;
656 pip = VTOI(pvp);
657 fs = pip->i_fs;
658 if (fs->fs_cstotal.cs_nifree == 0)
659 goto noinodes;
660
661 if ((mode & IFMT) == IFDIR)
662 ipref = ffs_dirpref(pip);
663 else
664 ipref = pip->i_number;
665 if (ipref >= fs->fs_ncg * fs->fs_ipg)
666 ipref = 0;
667 cg = ino_to_cg(fs, ipref);
668 /*
669 * Track number of dirs created one after another
670 * in a same cg without intervening by files.
671 */
672 if ((mode & IFMT) == IFDIR) {
673 if (fs->fs_contigdirs[cg] < 255)
674 fs->fs_contigdirs[cg]++;
675 } else {
676 if (fs->fs_contigdirs[cg] > 0)
677 fs->fs_contigdirs[cg]--;
678 }
679 ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode, ffs_nodealloccg);
680 if (ino == 0)
681 goto noinodes;
682 error = VFS_VGET(pvp->v_mount, ino, vpp);
683 if (error) {
684 ffs_vfree(pvp, ino, mode);
685 return (error);
686 }
687 ip = VTOI(*vpp);
688 if (ip->i_mode) {
689 #if 0
690 printf("mode = 0%o, inum = %d, fs = %s\n",
691 ip->i_mode, ip->i_number, fs->fs_fsmnt);
692 #else
693 printf("dmode %x mode %x dgen %x gen %x\n",
694 DIP(ip, mode), ip->i_mode,
695 DIP(ip, gen), ip->i_gen);
696 printf("size %llx blocks %llx\n",
697 (long long)DIP(ip, size), (long long)DIP(ip, blocks));
698 printf("ino %llu ipref %llu\n", (unsigned long long)ino,
699 (unsigned long long)ipref);
700 #if 0
701 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
702 (int)fs->fs_bsize, NOCRED, &bp);
703 #endif
704
705 #endif
706 panic("ffs_valloc: dup alloc");
707 }
708 if (DIP(ip, blocks)) { /* XXX */
709 printf("free inode %s/%llu had %" PRId64 " blocks\n",
710 fs->fs_fsmnt, (unsigned long long)ino, DIP(ip, blocks));
711 DIP_ASSIGN(ip, blocks, 0);
712 }
713 ip->i_flag &= ~IN_SPACECOUNTED;
714 ip->i_flags = 0;
715 DIP_ASSIGN(ip, flags, 0);
716 /*
717 * Set up a new generation number for this inode.
718 */
719 ip->i_gen++;
720 DIP_ASSIGN(ip, gen, ip->i_gen);
721 if (fs->fs_magic == FS_UFS2_MAGIC) {
722 nanotime(&ts);
723 ip->i_ffs2_birthtime = ts.tv_sec;
724 ip->i_ffs2_birthnsec = ts.tv_nsec;
725 }
726 return (0);
727 noinodes:
728 ffs_fserr(fs, cred->cr_uid, "out of inodes");
729 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
730 return (ENOSPC);
731 }
732
733 /*
734 * Find a cylinder group in which to place a directory.
735 *
736 * The policy implemented by this algorithm is to allocate a
737 * directory inode in the same cylinder group as its parent
738 * directory, but also to reserve space for its files inodes
739 * and data. Restrict the number of directories which may be
740 * allocated one after another in the same cylinder group
741 * without intervening allocation of files.
742 *
743 * If we allocate a first level directory then force allocation
744 * in another cylinder group.
745 */
746 static ino_t
747 ffs_dirpref(struct inode *pip)
748 {
749 register struct fs *fs;
750 int cg, prefcg;
751 int64_t dirsize, cgsize, curdsz;
752 int avgifree, avgbfree, avgndir;
753 int minifree, minbfree, maxndir;
754 int mincg, minndir;
755 int maxcontigdirs;
756
757 fs = pip->i_fs;
758
759 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
760 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
761 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
762
763 /*
764 * Force allocation in another cg if creating a first level dir.
765 */
766 if (ITOV(pip)->v_flag & VROOT) {
767 prefcg = random() % fs->fs_ncg;
768 mincg = prefcg;
769 minndir = fs->fs_ipg;
770 for (cg = prefcg; cg < fs->fs_ncg; cg++)
771 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
772 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
773 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
774 mincg = cg;
775 minndir = fs->fs_cs(fs, cg).cs_ndir;
776 }
777 for (cg = 0; cg < prefcg; cg++)
778 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
779 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
780 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
781 mincg = cg;
782 minndir = fs->fs_cs(fs, cg).cs_ndir;
783 }
784 return ((ino_t)(fs->fs_ipg * mincg));
785 }
786
787 /*
788 * Count various limits which used for
789 * optimal allocation of a directory inode.
790 */
791 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
792 minifree = avgifree - fs->fs_ipg / 4;
793 if (minifree < 0)
794 minifree = 0;
795 minbfree = avgbfree - fragstoblks(fs, fs->fs_fpg) / 4;
796 if (minbfree < 0)
797 minbfree = 0;
798 cgsize = (int64_t)fs->fs_fsize * fs->fs_fpg;
799 dirsize = (int64_t)fs->fs_avgfilesize * fs->fs_avgfpdir;
800 if (avgndir != 0) {
801 curdsz = (cgsize - (int64_t)avgbfree * fs->fs_bsize) / avgndir;
802 if (dirsize < curdsz)
803 dirsize = curdsz;
804 }
805 if (cgsize < dirsize * 255)
806 maxcontigdirs = cgsize / dirsize;
807 else
808 maxcontigdirs = 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(struct vnode *vp, ino_t ino, int mode)
1695 {
1696
1697 if (DOINGSOFTDEP(vp)) {
1698 softdep_freefile(vp, ino, mode);
1699 return (0);
1700 }
1701 return ffs_freefile(VTOI(vp)->i_fs, VTOI(vp)->i_devvp, ino, mode);
1702 }
1703
1704 /*
1705 * Do the actual free operation.
1706 * The specified inode is placed back in the free map.
1707 */
1708 int
1709 ffs_freefile(struct fs *fs, struct vnode *devvp, ino_t ino, int mode)
1710 {
1711 struct cg *cgp;
1712 struct buf *bp;
1713 int error, cg;
1714 daddr_t cgbno;
1715 u_int8_t *inosused;
1716 dev_t dev;
1717 #ifdef FFS_EI
1718 const int needswap = UFS_FSNEEDSWAP(fs);
1719 #endif
1720
1721 cg = ino_to_cg(fs, ino);
1722 if (devvp->v_type != VBLK) {
1723 /* devvp is a snapshot */
1724 dev = VTOI(devvp)->i_devvp->v_rdev;
1725 cgbno = fragstoblks(fs, cgtod(fs, cg));
1726 } else {
1727 dev = devvp->v_rdev;
1728 cgbno = fsbtodb(fs, cgtod(fs, cg));
1729 }
1730 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1731 panic("ifree: range: dev = 0x%x, ino = %llu, fs = %s",
1732 dev, (unsigned long long)ino, fs->fs_fsmnt);
1733 error = bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp);
1734 if (error) {
1735 brelse(bp);
1736 return (error);
1737 }
1738 cgp = (struct cg *)bp->b_data;
1739 if (!cg_chkmagic(cgp, needswap)) {
1740 brelse(bp);
1741 return (0);
1742 }
1743 cgp->cg_old_time = ufs_rw32(time.tv_sec, needswap);
1744 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1745 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1746 cgp->cg_time = ufs_rw64(time.tv_sec, needswap);
1747 inosused = cg_inosused(cgp, needswap);
1748 ino %= fs->fs_ipg;
1749 if (isclr(inosused, ino)) {
1750 printf("ifree: dev = 0x%x, ino = %llu, fs = %s\n",
1751 dev, (unsigned long long)ino + cg * fs->fs_ipg,
1752 fs->fs_fsmnt);
1753 if (fs->fs_ronly == 0)
1754 panic("ifree: freeing free inode");
1755 }
1756 clrbit(inosused, ino);
1757 if (ino < ufs_rw32(cgp->cg_irotor, needswap))
1758 cgp->cg_irotor = ufs_rw32(ino, needswap);
1759 ufs_add32(cgp->cg_cs.cs_nifree, 1, needswap);
1760 fs->fs_cstotal.cs_nifree++;
1761 fs->fs_cs(fs, cg).cs_nifree++;
1762 if ((mode & IFMT) == IFDIR) {
1763 ufs_add32(cgp->cg_cs.cs_ndir, -1, needswap);
1764 fs->fs_cstotal.cs_ndir--;
1765 fs->fs_cs(fs, cg).cs_ndir--;
1766 }
1767 fs->fs_fmod = 1;
1768 ACTIVECG_CLR(fs, cg);
1769 bdwrite(bp);
1770 return (0);
1771 }
1772
1773 /*
1774 * Check to see if a file is free.
1775 */
1776 int
1777 ffs_checkfreefile(struct fs *fs, struct vnode *devvp, ino_t ino)
1778 {
1779 struct cg *cgp;
1780 struct buf *bp;
1781 daddr_t cgbno;
1782 int ret, cg;
1783 u_int8_t *inosused;
1784
1785 cg = ino_to_cg(fs, ino);
1786 if (devvp->v_type != VBLK) {
1787 /* devvp is a snapshot */
1788 cgbno = fragstoblks(fs, cgtod(fs, cg));
1789 } else
1790 cgbno = fsbtodb(fs, cgtod(fs, cg));
1791 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1792 return 1;
1793 if (bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp)) {
1794 brelse(bp);
1795 return 1;
1796 }
1797 cgp = (struct cg *)bp->b_data;
1798 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
1799 brelse(bp);
1800 return 1;
1801 }
1802 inosused = cg_inosused(cgp, UFS_FSNEEDSWAP(fs));
1803 ino %= fs->fs_ipg;
1804 ret = isclr(inosused, ino);
1805 brelse(bp);
1806 return ret;
1807 }
1808
1809 /*
1810 * Find a block of the specified size in the specified cylinder group.
1811 *
1812 * It is a panic if a request is made to find a block if none are
1813 * available.
1814 */
1815 static int32_t
1816 ffs_mapsearch(struct fs *fs, struct cg *cgp, daddr_t bpref, int allocsiz)
1817 {
1818 int32_t bno;
1819 int start, len, loc, i;
1820 int blk, field, subfield, pos;
1821 int ostart, olen;
1822 u_int8_t *blksfree;
1823 #ifdef FFS_EI
1824 const int needswap = UFS_FSNEEDSWAP(fs);
1825 #endif
1826
1827 /*
1828 * find the fragment by searching through the free block
1829 * map for an appropriate bit pattern
1830 */
1831 if (bpref)
1832 start = dtogd(fs, bpref) / NBBY;
1833 else
1834 start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
1835 blksfree = cg_blksfree(cgp, needswap);
1836 len = howmany(fs->fs_fpg, NBBY) - start;
1837 ostart = start;
1838 olen = len;
1839 loc = scanc((u_int)len,
1840 (const u_char *)&blksfree[start],
1841 (const u_char *)fragtbl[fs->fs_frag],
1842 (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
1843 if (loc == 0) {
1844 len = start + 1;
1845 start = 0;
1846 loc = scanc((u_int)len,
1847 (const u_char *)&blksfree[0],
1848 (const u_char *)fragtbl[fs->fs_frag],
1849 (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
1850 if (loc == 0) {
1851 printf("start = %d, len = %d, fs = %s\n",
1852 ostart, olen, fs->fs_fsmnt);
1853 printf("offset=%d %ld\n",
1854 ufs_rw32(cgp->cg_freeoff, needswap),
1855 (long)blksfree - (long)cgp);
1856 printf("cg %d\n", cgp->cg_cgx);
1857 panic("ffs_alloccg: map corrupted");
1858 /* NOTREACHED */
1859 }
1860 }
1861 bno = (start + len - loc) * NBBY;
1862 cgp->cg_frotor = ufs_rw32(bno, needswap);
1863 /*
1864 * found the byte in the map
1865 * sift through the bits to find the selected frag
1866 */
1867 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1868 blk = blkmap(fs, blksfree, bno);
1869 blk <<= 1;
1870 field = around[allocsiz];
1871 subfield = inside[allocsiz];
1872 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
1873 if ((blk & field) == subfield)
1874 return (bno + pos);
1875 field <<= 1;
1876 subfield <<= 1;
1877 }
1878 }
1879 printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
1880 panic("ffs_alloccg: block not in map");
1881 /* return (-1); */
1882 }
1883
1884 /*
1885 * Update the cluster map because of an allocation or free.
1886 *
1887 * Cnt == 1 means free; cnt == -1 means allocating.
1888 */
1889 void
1890 ffs_clusteracct(struct fs *fs, struct cg *cgp, int32_t blkno, int cnt)
1891 {
1892 int32_t *sump;
1893 int32_t *lp;
1894 u_char *freemapp, *mapp;
1895 int i, start, end, forw, back, map, bit;
1896 #ifdef FFS_EI
1897 const int needswap = UFS_FSNEEDSWAP(fs);
1898 #endif
1899
1900 if (fs->fs_contigsumsize <= 0)
1901 return;
1902 freemapp = cg_clustersfree(cgp, needswap);
1903 sump = cg_clustersum(cgp, needswap);
1904 /*
1905 * Allocate or clear the actual block.
1906 */
1907 if (cnt > 0)
1908 setbit(freemapp, blkno);
1909 else
1910 clrbit(freemapp, blkno);
1911 /*
1912 * Find the size of the cluster going forward.
1913 */
1914 start = blkno + 1;
1915 end = start + fs->fs_contigsumsize;
1916 if (end >= ufs_rw32(cgp->cg_nclusterblks, needswap))
1917 end = ufs_rw32(cgp->cg_nclusterblks, needswap);
1918 mapp = &freemapp[start / NBBY];
1919 map = *mapp++;
1920 bit = 1 << (start % NBBY);
1921 for (i = start; i < end; i++) {
1922 if ((map & bit) == 0)
1923 break;
1924 if ((i & (NBBY - 1)) != (NBBY - 1)) {
1925 bit <<= 1;
1926 } else {
1927 map = *mapp++;
1928 bit = 1;
1929 }
1930 }
1931 forw = i - start;
1932 /*
1933 * Find the size of the cluster going backward.
1934 */
1935 start = blkno - 1;
1936 end = start - fs->fs_contigsumsize;
1937 if (end < 0)
1938 end = -1;
1939 mapp = &freemapp[start / NBBY];
1940 map = *mapp--;
1941 bit = 1 << (start % NBBY);
1942 for (i = start; i > end; i--) {
1943 if ((map & bit) == 0)
1944 break;
1945 if ((i & (NBBY - 1)) != 0) {
1946 bit >>= 1;
1947 } else {
1948 map = *mapp--;
1949 bit = 1 << (NBBY - 1);
1950 }
1951 }
1952 back = start - i;
1953 /*
1954 * Account for old cluster and the possibly new forward and
1955 * back clusters.
1956 */
1957 i = back + forw + 1;
1958 if (i > fs->fs_contigsumsize)
1959 i = fs->fs_contigsumsize;
1960 ufs_add32(sump[i], cnt, needswap);
1961 if (back > 0)
1962 ufs_add32(sump[back], -cnt, needswap);
1963 if (forw > 0)
1964 ufs_add32(sump[forw], -cnt, needswap);
1965
1966 /*
1967 * Update cluster summary information.
1968 */
1969 lp = &sump[fs->fs_contigsumsize];
1970 for (i = fs->fs_contigsumsize; i > 0; i--)
1971 if (ufs_rw32(*lp--, needswap) > 0)
1972 break;
1973 fs->fs_maxcluster[ufs_rw32(cgp->cg_cgx, needswap)] = i;
1974 }
1975
1976 /*
1977 * Fserr prints the name of a file system with an error diagnostic.
1978 *
1979 * The form of the error message is:
1980 * fs: error message
1981 */
1982 static void
1983 ffs_fserr(struct fs *fs, u_int uid, const char *cp)
1984 {
1985
1986 log(LOG_ERR, "uid %d, pid %d, command %s, on %s: %s\n",
1987 uid, curproc->p_pid, curproc->p_comm, fs->fs_fsmnt, cp);
1988 }
1989