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