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