Remove building with NOCRYPTO option
[minix3.git] / sys / ufs / ffs / ffs_alloc.c
blobf81a345f985c91ebbee5602c165529cde76af763
1 /* $NetBSD: ffs_alloc.c,v 1.151 2015/08/12 14:52:35 riastradh Exp $ */
3 /*-
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Wasabi Systems, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 2002 Networks Associates Technology, Inc.
34 * All rights reserved.
36 * This software was developed for the FreeBSD Project by Marshall
37 * Kirk McKusick and Network Associates Laboratories, the Security
38 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
39 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
40 * research program
42 * Copyright (c) 1982, 1986, 1989, 1993
43 * The Regents of the University of California. All rights reserved.
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * SUCH DAMAGE.
69 * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: ffs_alloc.c,v 1.151 2015/08/12 14:52:35 riastradh Exp $");
75 #if defined(_KERNEL_OPT)
76 #include "opt_ffs.h"
77 #include "opt_quota.h"
78 #include "opt_uvm_page_trkown.h"
79 #endif
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/buf.h>
84 #include <sys/cprng.h>
85 #include <sys/fstrans.h>
86 #include <sys/kauth.h>
87 #include <sys/kernel.h>
88 #include <sys/mount.h>
89 #include <sys/proc.h>
90 #include <sys/syslog.h>
91 #include <sys/vnode.h>
92 #include <sys/wapbl.h>
93 #include <sys/cprng.h>
95 #include <miscfs/specfs/specdev.h>
96 #include <ufs/ufs/quota.h>
97 #include <ufs/ufs/ufsmount.h>
98 #include <ufs/ufs/inode.h>
99 #include <ufs/ufs/ufs_extern.h>
100 #include <ufs/ufs/ufs_bswap.h>
101 #include <ufs/ufs/ufs_wapbl.h>
103 #include <ufs/ffs/fs.h>
104 #include <ufs/ffs/ffs_extern.h>
106 #ifdef UVM_PAGE_TRKOWN
107 #include <uvm/uvm.h>
108 #endif
110 static daddr_t ffs_alloccg(struct inode *, int, daddr_t, int, int);
111 static daddr_t ffs_alloccgblk(struct inode *, struct buf *, daddr_t, int);
112 static ino_t ffs_dirpref(struct inode *);
113 static daddr_t ffs_fragextend(struct inode *, int, daddr_t, int, int);
114 static void ffs_fserr(struct fs *, kauth_cred_t, const char *);
115 static daddr_t ffs_hashalloc(struct inode *, int, daddr_t, int, int,
116 daddr_t (*)(struct inode *, int, daddr_t, int, int));
117 static daddr_t ffs_nodealloccg(struct inode *, int, daddr_t, int, int);
118 static int32_t ffs_mapsearch(struct fs *, struct cg *,
119 daddr_t, int);
120 static void ffs_blkfree_common(struct ufsmount *, struct fs *, dev_t, struct buf *,
121 daddr_t, long, bool);
122 static void ffs_freefile_common(struct ufsmount *, struct fs *, dev_t, struct buf *, ino_t,
123 int, bool);
125 /* if 1, changes in optimalization strategy are logged */
126 int ffs_log_changeopt = 0;
128 /* in ffs_tables.c */
129 extern const int inside[], around[];
130 extern const u_char * const fragtbl[];
132 /* Basic consistency check for block allocations */
133 static int
134 ffs_check_bad_allocation(const char *func, struct fs *fs, daddr_t bno,
135 long size, dev_t dev, ino_t inum)
137 if ((u_int)size > fs->fs_bsize || ffs_fragoff(fs, size) != 0 ||
138 ffs_fragnum(fs, bno) + ffs_numfrags(fs, size) > fs->fs_frag) {
139 printf("dev = 0x%llx, bno = %" PRId64 " bsize = %d, "
140 "size = %ld, fs = %s\n",
141 (long long)dev, bno, fs->fs_bsize, size, fs->fs_fsmnt);
142 panic("%s: bad size", func);
145 if (bno >= fs->fs_size) {
146 printf("bad block %" PRId64 ", ino %llu\n", bno,
147 (unsigned long long)inum);
148 ffs_fserr(fs, NOCRED, "bad block");
149 return EINVAL;
151 return 0;
155 * Allocate a block in the file system.
157 * The size of the requested block is given, which must be some
158 * multiple of fs_fsize and <= fs_bsize.
159 * A preference may be optionally specified. If a preference is given
160 * the following hierarchy is used to allocate a block:
161 * 1) allocate the requested block.
162 * 2) allocate a rotationally optimal block in the same cylinder.
163 * 3) allocate a block in the same cylinder group.
164 * 4) quadradically rehash into other cylinder groups, until an
165 * available block is located.
166 * If no block preference is given the following hierarchy is used
167 * to allocate a block:
168 * 1) allocate a block in the cylinder group that contains the
169 * inode for the file.
170 * 2) quadradically rehash into other cylinder groups, until an
171 * available block is located.
173 * => called with um_lock held
174 * => releases um_lock before returning
177 ffs_alloc(struct inode *ip, daddr_t lbn, daddr_t bpref, int size, int flags,
178 kauth_cred_t cred, daddr_t *bnp)
180 struct ufsmount *ump;
181 struct fs *fs;
182 daddr_t bno;
183 int cg;
184 #if defined(QUOTA) || defined(QUOTA2)
185 int error;
186 #endif
188 fs = ip->i_fs;
189 ump = ip->i_ump;
191 KASSERT(mutex_owned(&ump->um_lock));
193 #ifdef UVM_PAGE_TRKOWN
196 * Sanity-check that allocations within the file size
197 * do not allow other threads to read the stale contents
198 * of newly allocated blocks.
199 * Usually pages will exist to cover the new allocation.
200 * There is an optimization in ffs_write() where we skip
201 * creating pages if several conditions are met:
202 * - the file must not be mapped (in any user address space).
203 * - the write must cover whole pages and whole blocks.
204 * If those conditions are not met then pages must exist and
205 * be locked by the current thread.
208 if (ITOV(ip)->v_type == VREG &&
209 ffs_lblktosize(fs, (voff_t)lbn) < round_page(ITOV(ip)->v_size)) {
210 struct vm_page *pg;
211 struct vnode *vp = ITOV(ip);
212 struct uvm_object *uobj = &vp->v_uobj;
213 voff_t off = trunc_page(ffs_lblktosize(fs, lbn));
214 voff_t endoff = round_page(ffs_lblktosize(fs, lbn) + size);
216 mutex_enter(uobj->vmobjlock);
217 while (off < endoff) {
218 pg = uvm_pagelookup(uobj, off);
219 KASSERT((pg == NULL && (vp->v_vflag & VV_MAPPED) == 0 &&
220 (size & PAGE_MASK) == 0 &&
221 ffs_blkoff(fs, size) == 0) ||
222 (pg != NULL && pg->owner == curproc->p_pid &&
223 pg->lowner == curlwp->l_lid));
224 off += PAGE_SIZE;
226 mutex_exit(uobj->vmobjlock);
228 #endif
230 *bnp = 0;
231 #ifdef DIAGNOSTIC
232 if ((u_int)size > fs->fs_bsize || ffs_fragoff(fs, size) != 0) {
233 printf("dev = 0x%llx, bsize = %d, size = %d, fs = %s\n",
234 (unsigned long long)ip->i_dev, fs->fs_bsize, size,
235 fs->fs_fsmnt);
236 panic("ffs_alloc: bad size");
238 if (cred == NOCRED)
239 panic("ffs_alloc: missing credential");
240 #endif /* DIAGNOSTIC */
241 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
242 goto nospace;
243 if (freespace(fs, fs->fs_minfree) <= 0 &&
244 kauth_authorize_system(cred, KAUTH_SYSTEM_FS_RESERVEDSPACE, 0, NULL,
245 NULL, NULL) != 0)
246 goto nospace;
247 #if defined(QUOTA) || defined(QUOTA2)
248 mutex_exit(&ump->um_lock);
249 if ((error = chkdq(ip, btodb(size), cred, 0)) != 0)
250 return (error);
251 mutex_enter(&ump->um_lock);
252 #endif
254 if (bpref >= fs->fs_size)
255 bpref = 0;
256 if (bpref == 0)
257 cg = ino_to_cg(fs, ip->i_number);
258 else
259 cg = dtog(fs, bpref);
260 bno = ffs_hashalloc(ip, cg, bpref, size, flags, ffs_alloccg);
261 if (bno > 0) {
262 DIP_ADD(ip, blocks, btodb(size));
263 ip->i_flag |= IN_CHANGE | IN_UPDATE;
264 *bnp = bno;
265 return (0);
267 #if defined(QUOTA) || defined(QUOTA2)
269 * Restore user's disk quota because allocation failed.
271 (void) chkdq(ip, -btodb(size), cred, FORCE);
272 #endif
273 if (flags & B_CONTIG) {
275 * XXX ump->um_lock handling is "suspect" at best.
276 * For the case where ffs_hashalloc() fails early
277 * in the B_CONTIG case we reach here with um_lock
278 * already unlocked, so we can't release it again
279 * like in the normal error path. See kern/39206.
282 * Fail silently - it's up to our caller to report
283 * errors.
285 return (ENOSPC);
287 nospace:
288 mutex_exit(&ump->um_lock);
289 ffs_fserr(fs, cred, "file system full");
290 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
291 return (ENOSPC);
295 * Reallocate a fragment to a bigger size
297 * The number and size of the old block is given, and a preference
298 * and new size is also specified. The allocator attempts to extend
299 * the original block. Failing that, the regular block allocator is
300 * invoked to get an appropriate block.
302 * => called with um_lock held
303 * => return with um_lock released
306 ffs_realloccg(struct inode *ip, daddr_t lbprev, daddr_t bpref, int osize,
307 int nsize, kauth_cred_t cred, struct buf **bpp, daddr_t *blknop)
309 struct ufsmount *ump;
310 struct fs *fs;
311 struct buf *bp;
312 int cg, request, error;
313 daddr_t bprev, bno;
315 fs = ip->i_fs;
316 ump = ip->i_ump;
318 KASSERT(mutex_owned(&ump->um_lock));
320 #ifdef UVM_PAGE_TRKOWN
323 * Sanity-check that allocations within the file size
324 * do not allow other threads to read the stale contents
325 * of newly allocated blocks.
326 * Unlike in ffs_alloc(), here pages must always exist
327 * for such allocations, because only the last block of a file
328 * can be a fragment and ffs_write() will reallocate the
329 * fragment to the new size using ufs_balloc_range(),
330 * which always creates pages to cover blocks it allocates.
333 if (ITOV(ip)->v_type == VREG) {
334 struct vm_page *pg;
335 struct uvm_object *uobj = &ITOV(ip)->v_uobj;
336 voff_t off = trunc_page(ffs_lblktosize(fs, lbprev));
337 voff_t endoff = round_page(ffs_lblktosize(fs, lbprev) + osize);
339 mutex_enter(uobj->vmobjlock);
340 while (off < endoff) {
341 pg = uvm_pagelookup(uobj, off);
342 KASSERT(pg->owner == curproc->p_pid &&
343 pg->lowner == curlwp->l_lid);
344 off += PAGE_SIZE;
346 mutex_exit(uobj->vmobjlock);
348 #endif
350 #ifdef DIAGNOSTIC
351 if ((u_int)osize > fs->fs_bsize || ffs_fragoff(fs, osize) != 0 ||
352 (u_int)nsize > fs->fs_bsize || ffs_fragoff(fs, nsize) != 0) {
353 printf(
354 "dev = 0x%llx, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
355 (unsigned long long)ip->i_dev, fs->fs_bsize, osize, nsize,
356 fs->fs_fsmnt);
357 panic("ffs_realloccg: bad size");
359 if (cred == NOCRED)
360 panic("ffs_realloccg: missing credential");
361 #endif /* DIAGNOSTIC */
362 if (freespace(fs, fs->fs_minfree) <= 0 &&
363 kauth_authorize_system(cred, KAUTH_SYSTEM_FS_RESERVEDSPACE, 0, NULL,
364 NULL, NULL) != 0) {
365 mutex_exit(&ump->um_lock);
366 goto nospace;
368 if (fs->fs_magic == FS_UFS2_MAGIC)
369 bprev = ufs_rw64(ip->i_ffs2_db[lbprev], UFS_FSNEEDSWAP(fs));
370 else
371 bprev = ufs_rw32(ip->i_ffs1_db[lbprev], UFS_FSNEEDSWAP(fs));
373 if (bprev == 0) {
374 printf("dev = 0x%llx, bsize = %d, bprev = %" PRId64 ", fs = %s\n",
375 (unsigned long long)ip->i_dev, fs->fs_bsize, bprev,
376 fs->fs_fsmnt);
377 panic("ffs_realloccg: bad bprev");
379 mutex_exit(&ump->um_lock);
382 * Allocate the extra space in the buffer.
384 if (bpp != NULL &&
385 (error = bread(ITOV(ip), lbprev, osize, 0, &bp)) != 0) {
386 return (error);
388 #if defined(QUOTA) || defined(QUOTA2)
389 if ((error = chkdq(ip, btodb(nsize - osize), cred, 0)) != 0) {
390 if (bpp != NULL) {
391 brelse(bp, 0);
393 return (error);
395 #endif
397 * Check for extension in the existing location.
399 cg = dtog(fs, bprev);
400 mutex_enter(&ump->um_lock);
401 if ((bno = ffs_fragextend(ip, cg, bprev, osize, nsize)) != 0) {
402 DIP_ADD(ip, blocks, btodb(nsize - osize));
403 ip->i_flag |= IN_CHANGE | IN_UPDATE;
405 if (bpp != NULL) {
406 if (bp->b_blkno != FFS_FSBTODB(fs, bno))
407 panic("bad blockno");
408 allocbuf(bp, nsize, 1);
409 memset((char *)bp->b_data + osize, 0, nsize - osize);
410 mutex_enter(bp->b_objlock);
411 KASSERT(!cv_has_waiters(&bp->b_done));
412 bp->b_oflags |= BO_DONE;
413 mutex_exit(bp->b_objlock);
414 *bpp = bp;
416 if (blknop != NULL) {
417 *blknop = bno;
419 return (0);
422 * Allocate a new disk location.
424 if (bpref >= fs->fs_size)
425 bpref = 0;
426 switch ((int)fs->fs_optim) {
427 case FS_OPTSPACE:
429 * Allocate an exact sized fragment. Although this makes
430 * best use of space, we will waste time relocating it if
431 * the file continues to grow. If the fragmentation is
432 * less than half of the minimum free reserve, we choose
433 * to begin optimizing for time.
435 request = nsize;
436 if (fs->fs_minfree < 5 ||
437 fs->fs_cstotal.cs_nffree >
438 fs->fs_dsize * fs->fs_minfree / (2 * 100))
439 break;
441 if (ffs_log_changeopt) {
442 log(LOG_NOTICE,
443 "%s: optimization changed from SPACE to TIME\n",
444 fs->fs_fsmnt);
447 fs->fs_optim = FS_OPTTIME;
448 break;
449 case FS_OPTTIME:
451 * At this point we have discovered a file that is trying to
452 * grow a small fragment to a larger fragment. To save time,
453 * we allocate a full sized block, then free the unused portion.
454 * If the file continues to grow, the `ffs_fragextend' call
455 * above will be able to grow it in place without further
456 * copying. If aberrant programs cause disk fragmentation to
457 * grow within 2% of the free reserve, we choose to begin
458 * optimizing for space.
460 request = fs->fs_bsize;
461 if (fs->fs_cstotal.cs_nffree <
462 fs->fs_dsize * (fs->fs_minfree - 2) / 100)
463 break;
465 if (ffs_log_changeopt) {
466 log(LOG_NOTICE,
467 "%s: optimization changed from TIME to SPACE\n",
468 fs->fs_fsmnt);
471 fs->fs_optim = FS_OPTSPACE;
472 break;
473 default:
474 printf("dev = 0x%llx, optim = %d, fs = %s\n",
475 (unsigned long long)ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
476 panic("ffs_realloccg: bad optim");
477 /* NOTREACHED */
479 bno = ffs_hashalloc(ip, cg, bpref, request, 0, ffs_alloccg);
480 if (bno > 0) {
481 if ((ip->i_ump->um_mountp->mnt_wapbl) &&
482 (ITOV(ip)->v_type != VREG)) {
483 UFS_WAPBL_REGISTER_DEALLOCATION(
484 ip->i_ump->um_mountp, FFS_FSBTODB(fs, bprev),
485 osize);
486 } else {
487 ffs_blkfree(fs, ip->i_devvp, bprev, (long)osize,
488 ip->i_number);
490 if (nsize < request) {
491 if ((ip->i_ump->um_mountp->mnt_wapbl) &&
492 (ITOV(ip)->v_type != VREG)) {
493 UFS_WAPBL_REGISTER_DEALLOCATION(
494 ip->i_ump->um_mountp,
495 FFS_FSBTODB(fs, (bno + ffs_numfrags(fs, nsize))),
496 request - nsize);
497 } else
498 ffs_blkfree(fs, ip->i_devvp,
499 bno + ffs_numfrags(fs, nsize),
500 (long)(request - nsize), ip->i_number);
502 DIP_ADD(ip, blocks, btodb(nsize - osize));
503 ip->i_flag |= IN_CHANGE | IN_UPDATE;
504 if (bpp != NULL) {
505 bp->b_blkno = FFS_FSBTODB(fs, bno);
506 allocbuf(bp, nsize, 1);
507 memset((char *)bp->b_data + osize, 0, (u_int)nsize - osize);
508 mutex_enter(bp->b_objlock);
509 KASSERT(!cv_has_waiters(&bp->b_done));
510 bp->b_oflags |= BO_DONE;
511 mutex_exit(bp->b_objlock);
512 *bpp = bp;
514 if (blknop != NULL) {
515 *blknop = bno;
517 return (0);
519 mutex_exit(&ump->um_lock);
521 #if defined(QUOTA) || defined(QUOTA2)
523 * Restore user's disk quota because allocation failed.
525 (void) chkdq(ip, -btodb(nsize - osize), cred, FORCE);
526 #endif
527 if (bpp != NULL) {
528 brelse(bp, 0);
531 nospace:
533 * no space available
535 ffs_fserr(fs, cred, "file system full");
536 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
537 return (ENOSPC);
541 * Allocate an inode in the file system.
543 * If allocating a directory, use ffs_dirpref to select the inode.
544 * If allocating in a directory, the following hierarchy is followed:
545 * 1) allocate the preferred inode.
546 * 2) allocate an inode in the same cylinder group.
547 * 3) quadradically rehash into other cylinder groups, until an
548 * available inode is located.
549 * If no inode preference is given the following hierarchy is used
550 * to allocate an inode:
551 * 1) allocate an inode in cylinder group 0.
552 * 2) quadradically rehash into other cylinder groups, until an
553 * available inode is located.
555 * => um_lock not held upon entry or return
558 ffs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred, ino_t *inop)
560 struct ufsmount *ump;
561 struct inode *pip;
562 struct fs *fs;
563 ino_t ino, ipref;
564 int cg, error;
566 UFS_WAPBL_JUNLOCK_ASSERT(pvp->v_mount);
568 pip = VTOI(pvp);
569 fs = pip->i_fs;
570 ump = pip->i_ump;
572 error = UFS_WAPBL_BEGIN(pvp->v_mount);
573 if (error) {
574 return error;
576 mutex_enter(&ump->um_lock);
577 if (fs->fs_cstotal.cs_nifree == 0)
578 goto noinodes;
580 if ((mode & IFMT) == IFDIR)
581 ipref = ffs_dirpref(pip);
582 else
583 ipref = pip->i_number;
584 if (ipref >= fs->fs_ncg * fs->fs_ipg)
585 ipref = 0;
586 cg = ino_to_cg(fs, ipref);
588 * Track number of dirs created one after another
589 * in a same cg without intervening by files.
591 if ((mode & IFMT) == IFDIR) {
592 if (fs->fs_contigdirs[cg] < 255)
593 fs->fs_contigdirs[cg]++;
594 } else {
595 if (fs->fs_contigdirs[cg] > 0)
596 fs->fs_contigdirs[cg]--;
598 ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode, 0, ffs_nodealloccg);
599 if (ino == 0)
600 goto noinodes;
601 UFS_WAPBL_END(pvp->v_mount);
602 *inop = ino;
603 return 0;
605 noinodes:
606 mutex_exit(&ump->um_lock);
607 UFS_WAPBL_END(pvp->v_mount);
608 ffs_fserr(fs, cred, "out of inodes");
609 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
610 return ENOSPC;
614 * Find a cylinder group in which to place a directory.
616 * The policy implemented by this algorithm is to allocate a
617 * directory inode in the same cylinder group as its parent
618 * directory, but also to reserve space for its files inodes
619 * and data. Restrict the number of directories which may be
620 * allocated one after another in the same cylinder group
621 * without intervening allocation of files.
623 * If we allocate a first level directory then force allocation
624 * in another cylinder group.
626 static ino_t
627 ffs_dirpref(struct inode *pip)
629 register struct fs *fs;
630 int cg, prefcg;
631 int64_t dirsize, cgsize, curdsz;
632 int avgifree, avgbfree, avgndir;
633 int minifree, minbfree, maxndir;
634 int mincg, minndir;
635 int maxcontigdirs;
637 KASSERT(mutex_owned(&pip->i_ump->um_lock));
639 fs = pip->i_fs;
641 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
642 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
643 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
646 * Force allocation in another cg if creating a first level dir.
648 if (ITOV(pip)->v_vflag & VV_ROOT) {
649 prefcg = cprng_fast32() % fs->fs_ncg;
650 mincg = prefcg;
651 minndir = fs->fs_ipg;
652 for (cg = prefcg; cg < fs->fs_ncg; cg++)
653 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
654 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
655 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
656 mincg = cg;
657 minndir = fs->fs_cs(fs, cg).cs_ndir;
659 for (cg = 0; cg < prefcg; cg++)
660 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
661 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
662 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
663 mincg = cg;
664 minndir = fs->fs_cs(fs, cg).cs_ndir;
666 return ((ino_t)(fs->fs_ipg * mincg));
670 * Count various limits which used for
671 * optimal allocation of a directory inode.
672 * Try cylinder groups with >75% avgifree and avgbfree.
673 * Avoid cylinder groups with no free blocks or inodes as that
674 * triggers an I/O-expensive cylinder group scan.
676 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
677 minifree = avgifree - avgifree / 4;
678 if (minifree < 1)
679 minifree = 1;
680 minbfree = avgbfree - avgbfree / 4;
681 if (minbfree < 1)
682 minbfree = 1;
683 cgsize = (int64_t)fs->fs_fsize * fs->fs_fpg;
684 dirsize = (int64_t)fs->fs_avgfilesize * fs->fs_avgfpdir;
685 if (avgndir != 0) {
686 curdsz = (cgsize - (int64_t)avgbfree * fs->fs_bsize) / avgndir;
687 if (dirsize < curdsz)
688 dirsize = curdsz;
690 if (cgsize < dirsize * 255)
691 maxcontigdirs = (avgbfree * fs->fs_bsize) / dirsize;
692 else
693 maxcontigdirs = 255;
694 if (fs->fs_avgfpdir > 0)
695 maxcontigdirs = min(maxcontigdirs,
696 fs->fs_ipg / fs->fs_avgfpdir);
697 if (maxcontigdirs == 0)
698 maxcontigdirs = 1;
701 * Limit number of dirs in one cg and reserve space for
702 * regular files, but only if we have no deficit in
703 * inodes or space.
705 prefcg = ino_to_cg(fs, pip->i_number);
706 for (cg = prefcg; cg < fs->fs_ncg; cg++)
707 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
708 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
709 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
710 if (fs->fs_contigdirs[cg] < maxcontigdirs)
711 return ((ino_t)(fs->fs_ipg * cg));
713 for (cg = 0; cg < prefcg; cg++)
714 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
715 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
716 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
717 if (fs->fs_contigdirs[cg] < maxcontigdirs)
718 return ((ino_t)(fs->fs_ipg * cg));
721 * This is a backstop when we are deficient in space.
723 for (cg = prefcg; cg < fs->fs_ncg; cg++)
724 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
725 return ((ino_t)(fs->fs_ipg * cg));
726 for (cg = 0; cg < prefcg; cg++)
727 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
728 break;
729 return ((ino_t)(fs->fs_ipg * cg));
733 * Select the desired position for the next block in a file. The file is
734 * logically divided into sections. The first section is composed of the
735 * direct blocks. Each additional section contains fs_maxbpg blocks.
737 * If no blocks have been allocated in the first section, the policy is to
738 * request a block in the same cylinder group as the inode that describes
739 * the file. If no blocks have been allocated in any other section, the
740 * policy is to place the section in a cylinder group with a greater than
741 * average number of free blocks. An appropriate cylinder group is found
742 * by using a rotor that sweeps the cylinder groups. When a new group of
743 * blocks is needed, the sweep begins in the cylinder group following the
744 * cylinder group from which the previous allocation was made. The sweep
745 * continues until a cylinder group with greater than the average number
746 * of free blocks is found. If the allocation is for the first block in an
747 * indirect block, the information on the previous allocation is unavailable;
748 * here a best guess is made based upon the logical block number being
749 * allocated.
751 * If a section is already partially allocated, the policy is to
752 * contiguously allocate fs_maxcontig blocks. The end of one of these
753 * contiguous blocks and the beginning of the next is laid out
754 * contigously if possible.
756 * => um_lock held on entry and exit
758 daddr_t
759 ffs_blkpref_ufs1(struct inode *ip, daddr_t lbn, int indx, int flags,
760 int32_t *bap /* XXX ondisk32 */)
762 struct fs *fs;
763 int cg;
764 int avgbfree, startcg;
766 KASSERT(mutex_owned(&ip->i_ump->um_lock));
768 fs = ip->i_fs;
771 * If allocating a contiguous file with B_CONTIG, use the hints
772 * in the inode extentions to return the desired block.
774 * For metadata (indirect blocks) return the address of where
775 * the first indirect block resides - we'll scan for the next
776 * available slot if we need to allocate more than one indirect
777 * block. For data, return the address of the actual block
778 * relative to the address of the first data block.
780 if (flags & B_CONTIG) {
781 KASSERT(ip->i_ffs_first_data_blk != 0);
782 KASSERT(ip->i_ffs_first_indir_blk != 0);
783 if (flags & B_METAONLY)
784 return ip->i_ffs_first_indir_blk;
785 else
786 return ip->i_ffs_first_data_blk + ffs_blkstofrags(fs, lbn);
789 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
790 if (lbn < UFS_NDADDR + FFS_NINDIR(fs)) {
791 cg = ino_to_cg(fs, ip->i_number);
792 return (cgbase(fs, cg) + fs->fs_frag);
795 * Find a cylinder with greater than average number of
796 * unused data blocks.
798 if (indx == 0 || bap[indx - 1] == 0)
799 startcg =
800 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
801 else
802 startcg = dtog(fs,
803 ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
804 startcg %= fs->fs_ncg;
805 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
806 for (cg = startcg; cg < fs->fs_ncg; cg++)
807 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
808 return (cgbase(fs, cg) + fs->fs_frag);
810 for (cg = 0; cg < startcg; cg++)
811 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
812 return (cgbase(fs, cg) + fs->fs_frag);
814 return (0);
817 * We just always try to lay things out contiguously.
819 return ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
822 daddr_t
823 ffs_blkpref_ufs2(struct inode *ip, daddr_t lbn, int indx, int flags,
824 int64_t *bap)
826 struct fs *fs;
827 int cg;
828 int avgbfree, startcg;
830 KASSERT(mutex_owned(&ip->i_ump->um_lock));
832 fs = ip->i_fs;
835 * If allocating a contiguous file with B_CONTIG, use the hints
836 * in the inode extentions to return the desired block.
838 * For metadata (indirect blocks) return the address of where
839 * the first indirect block resides - we'll scan for the next
840 * available slot if we need to allocate more than one indirect
841 * block. For data, return the address of the actual block
842 * relative to the address of the first data block.
844 if (flags & B_CONTIG) {
845 KASSERT(ip->i_ffs_first_data_blk != 0);
846 KASSERT(ip->i_ffs_first_indir_blk != 0);
847 if (flags & B_METAONLY)
848 return ip->i_ffs_first_indir_blk;
849 else
850 return ip->i_ffs_first_data_blk + ffs_blkstofrags(fs, lbn);
853 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
854 if (lbn < UFS_NDADDR + FFS_NINDIR(fs)) {
855 cg = ino_to_cg(fs, ip->i_number);
856 return (cgbase(fs, cg) + fs->fs_frag);
859 * Find a cylinder with greater than average number of
860 * unused data blocks.
862 if (indx == 0 || bap[indx - 1] == 0)
863 startcg =
864 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
865 else
866 startcg = dtog(fs,
867 ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
868 startcg %= fs->fs_ncg;
869 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
870 for (cg = startcg; cg < fs->fs_ncg; cg++)
871 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
872 return (cgbase(fs, cg) + fs->fs_frag);
874 for (cg = 0; cg < startcg; cg++)
875 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
876 return (cgbase(fs, cg) + fs->fs_frag);
878 return (0);
881 * We just always try to lay things out contiguously.
883 return ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
888 * Implement the cylinder overflow algorithm.
890 * The policy implemented by this algorithm is:
891 * 1) allocate the block in its requested cylinder group.
892 * 2) quadradically rehash on the cylinder group number.
893 * 3) brute force search for a free block.
895 * => called with um_lock held
896 * => returns with um_lock released on success, held on failure
897 * (*allocator releases lock on success, retains lock on failure)
899 /*VARARGS5*/
900 static daddr_t
901 ffs_hashalloc(struct inode *ip, int cg, daddr_t pref,
902 int size /* size for data blocks, mode for inodes */,
903 int flags, daddr_t (*allocator)(struct inode *, int, daddr_t, int, int))
905 struct fs *fs;
906 daddr_t result;
907 int i, icg = cg;
909 fs = ip->i_fs;
911 * 1: preferred cylinder group
913 result = (*allocator)(ip, cg, pref, size, flags);
914 if (result)
915 return (result);
917 if (flags & B_CONTIG)
918 return (result);
920 * 2: quadratic rehash
922 for (i = 1; i < fs->fs_ncg; i *= 2) {
923 cg += i;
924 if (cg >= fs->fs_ncg)
925 cg -= fs->fs_ncg;
926 result = (*allocator)(ip, cg, 0, size, flags);
927 if (result)
928 return (result);
931 * 3: brute force search
932 * Note that we start at i == 2, since 0 was checked initially,
933 * and 1 is always checked in the quadratic rehash.
935 cg = (icg + 2) % fs->fs_ncg;
936 for (i = 2; i < fs->fs_ncg; i++) {
937 result = (*allocator)(ip, cg, 0, size, flags);
938 if (result)
939 return (result);
940 cg++;
941 if (cg == fs->fs_ncg)
942 cg = 0;
944 return (0);
948 * Determine whether a fragment can be extended.
950 * Check to see if the necessary fragments are available, and
951 * if they are, allocate them.
953 * => called with um_lock held
954 * => returns with um_lock released on success, held on failure
956 static daddr_t
957 ffs_fragextend(struct inode *ip, int cg, daddr_t bprev, int osize, int nsize)
959 struct ufsmount *ump;
960 struct fs *fs;
961 struct cg *cgp;
962 struct buf *bp;
963 daddr_t bno;
964 int frags, bbase;
965 int i, error;
966 u_int8_t *blksfree;
968 fs = ip->i_fs;
969 ump = ip->i_ump;
971 KASSERT(mutex_owned(&ump->um_lock));
973 if (fs->fs_cs(fs, cg).cs_nffree < ffs_numfrags(fs, nsize - osize))
974 return (0);
975 frags = ffs_numfrags(fs, nsize);
976 bbase = ffs_fragnum(fs, bprev);
977 if (bbase > ffs_fragnum(fs, (bprev + frags - 1))) {
978 /* cannot extend across a block boundary */
979 return (0);
981 mutex_exit(&ump->um_lock);
982 error = bread(ip->i_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
983 (int)fs->fs_cgsize, B_MODIFY, &bp);
984 if (error)
985 goto fail;
986 cgp = (struct cg *)bp->b_data;
987 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs)))
988 goto fail;
989 cgp->cg_old_time = ufs_rw32(time_second, UFS_FSNEEDSWAP(fs));
990 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
991 (fs->fs_old_flags & FS_FLAGS_UPDATED))
992 cgp->cg_time = ufs_rw64(time_second, UFS_FSNEEDSWAP(fs));
993 bno = dtogd(fs, bprev);
994 blksfree = cg_blksfree(cgp, UFS_FSNEEDSWAP(fs));
995 for (i = ffs_numfrags(fs, osize); i < frags; i++)
996 if (isclr(blksfree, bno + i))
997 goto fail;
999 * the current fragment can be extended
1000 * deduct the count on fragment being extended into
1001 * increase the count on the remaining fragment (if any)
1002 * allocate the extended piece
1004 for (i = frags; i < fs->fs_frag - bbase; i++)
1005 if (isclr(blksfree, bno + i))
1006 break;
1007 ufs_add32(cgp->cg_frsum[i - ffs_numfrags(fs, osize)], -1, UFS_FSNEEDSWAP(fs));
1008 if (i != frags)
1009 ufs_add32(cgp->cg_frsum[i - frags], 1, UFS_FSNEEDSWAP(fs));
1010 mutex_enter(&ump->um_lock);
1011 for (i = ffs_numfrags(fs, osize); i < frags; i++) {
1012 clrbit(blksfree, bno + i);
1013 ufs_add32(cgp->cg_cs.cs_nffree, -1, UFS_FSNEEDSWAP(fs));
1014 fs->fs_cstotal.cs_nffree--;
1015 fs->fs_cs(fs, cg).cs_nffree--;
1017 fs->fs_fmod = 1;
1018 ACTIVECG_CLR(fs, cg);
1019 mutex_exit(&ump->um_lock);
1020 bdwrite(bp);
1021 return (bprev);
1023 fail:
1024 if (bp != NULL)
1025 brelse(bp, 0);
1026 mutex_enter(&ump->um_lock);
1027 return (0);
1031 * Determine whether a block can be allocated.
1033 * Check to see if a block of the appropriate size is available,
1034 * and if it is, allocate it.
1036 static daddr_t
1037 ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size, int flags)
1039 struct ufsmount *ump;
1040 struct fs *fs = ip->i_fs;
1041 struct cg *cgp;
1042 struct buf *bp;
1043 int32_t bno;
1044 daddr_t blkno;
1045 int error, frags, allocsiz, i;
1046 u_int8_t *blksfree;
1047 const int needswap = UFS_FSNEEDSWAP(fs);
1049 ump = ip->i_ump;
1051 KASSERT(mutex_owned(&ump->um_lock));
1053 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
1054 return (0);
1055 mutex_exit(&ump->um_lock);
1056 error = bread(ip->i_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
1057 (int)fs->fs_cgsize, B_MODIFY, &bp);
1058 if (error)
1059 goto fail;
1060 cgp = (struct cg *)bp->b_data;
1061 if (!cg_chkmagic(cgp, needswap) ||
1062 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize))
1063 goto fail;
1064 cgp->cg_old_time = ufs_rw32(time_second, needswap);
1065 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1066 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1067 cgp->cg_time = ufs_rw64(time_second, needswap);
1068 if (size == fs->fs_bsize) {
1069 mutex_enter(&ump->um_lock);
1070 blkno = ffs_alloccgblk(ip, bp, bpref, flags);
1071 ACTIVECG_CLR(fs, cg);
1072 mutex_exit(&ump->um_lock);
1073 bdwrite(bp);
1074 return (blkno);
1077 * check to see if any fragments are already available
1078 * allocsiz is the size which will be allocated, hacking
1079 * it down to a smaller size if necessary
1081 blksfree = cg_blksfree(cgp, needswap);
1082 frags = ffs_numfrags(fs, size);
1083 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1084 if (cgp->cg_frsum[allocsiz] != 0)
1085 break;
1086 if (allocsiz == fs->fs_frag) {
1088 * no fragments were available, so a block will be
1089 * allocated, and hacked up
1091 if (cgp->cg_cs.cs_nbfree == 0)
1092 goto fail;
1093 mutex_enter(&ump->um_lock);
1094 blkno = ffs_alloccgblk(ip, bp, bpref, flags);
1095 bno = dtogd(fs, blkno);
1096 for (i = frags; i < fs->fs_frag; i++)
1097 setbit(blksfree, bno + i);
1098 i = fs->fs_frag - frags;
1099 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1100 fs->fs_cstotal.cs_nffree += i;
1101 fs->fs_cs(fs, cg).cs_nffree += i;
1102 fs->fs_fmod = 1;
1103 ufs_add32(cgp->cg_frsum[i], 1, needswap);
1104 ACTIVECG_CLR(fs, cg);
1105 mutex_exit(&ump->um_lock);
1106 bdwrite(bp);
1107 return (blkno);
1109 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1110 #if 0
1112 * XXX fvdl mapsearch will panic, and never return -1
1113 * also: returning NULL as daddr_t ?
1115 if (bno < 0)
1116 goto fail;
1117 #endif
1118 for (i = 0; i < frags; i++)
1119 clrbit(blksfree, bno + i);
1120 mutex_enter(&ump->um_lock);
1121 ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
1122 fs->fs_cstotal.cs_nffree -= frags;
1123 fs->fs_cs(fs, cg).cs_nffree -= frags;
1124 fs->fs_fmod = 1;
1125 ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
1126 if (frags != allocsiz)
1127 ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
1128 blkno = cgbase(fs, cg) + bno;
1129 ACTIVECG_CLR(fs, cg);
1130 mutex_exit(&ump->um_lock);
1131 bdwrite(bp);
1132 return blkno;
1134 fail:
1135 if (bp != NULL)
1136 brelse(bp, 0);
1137 mutex_enter(&ump->um_lock);
1138 return (0);
1142 * Allocate a block in a cylinder group.
1144 * This algorithm implements the following policy:
1145 * 1) allocate the requested block.
1146 * 2) allocate a rotationally optimal block in the same cylinder.
1147 * 3) allocate the next available block on the block rotor for the
1148 * specified cylinder group.
1149 * Note that this routine only allocates fs_bsize blocks; these
1150 * blocks may be fragmented by the routine that allocates them.
1152 static daddr_t
1153 ffs_alloccgblk(struct inode *ip, struct buf *bp, daddr_t bpref, int flags)
1155 struct fs *fs = ip->i_fs;
1156 struct cg *cgp;
1157 int cg;
1158 daddr_t blkno;
1159 int32_t bno;
1160 u_int8_t *blksfree;
1161 const int needswap = UFS_FSNEEDSWAP(fs);
1163 KASSERT(mutex_owned(&ip->i_ump->um_lock));
1165 cgp = (struct cg *)bp->b_data;
1166 blksfree = cg_blksfree(cgp, needswap);
1167 if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
1168 bpref = ufs_rw32(cgp->cg_rotor, needswap);
1169 } else {
1170 bpref = ffs_blknum(fs, bpref);
1171 bno = dtogd(fs, bpref);
1173 * if the requested block is available, use it
1175 if (ffs_isblock(fs, blksfree, ffs_fragstoblks(fs, bno)))
1176 goto gotit;
1178 * if the requested data block isn't available and we are
1179 * trying to allocate a contiguous file, return an error.
1181 if ((flags & (B_CONTIG | B_METAONLY)) == B_CONTIG)
1182 return (0);
1186 * Take the next available block in this cylinder group.
1188 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1189 if (bno < 0)
1190 return (0);
1191 cgp->cg_rotor = ufs_rw32(bno, needswap);
1192 gotit:
1193 blkno = ffs_fragstoblks(fs, bno);
1194 ffs_clrblock(fs, blksfree, blkno);
1195 ffs_clusteracct(fs, cgp, blkno, -1);
1196 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
1197 fs->fs_cstotal.cs_nbfree--;
1198 fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
1199 if ((fs->fs_magic == FS_UFS1_MAGIC) &&
1200 ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
1201 int cylno;
1202 cylno = old_cbtocylno(fs, bno);
1203 KASSERT(cylno >= 0);
1204 KASSERT(cylno < fs->fs_old_ncyl);
1205 KASSERT(old_cbtorpos(fs, bno) >= 0);
1206 KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, bno) < fs->fs_old_nrpos);
1207 ufs_add16(old_cg_blks(fs, cgp, cylno, needswap)[old_cbtorpos(fs, bno)], -1,
1208 needswap);
1209 ufs_add32(old_cg_blktot(cgp, needswap)[cylno], -1, needswap);
1211 fs->fs_fmod = 1;
1212 cg = ufs_rw32(cgp->cg_cgx, needswap);
1213 blkno = cgbase(fs, cg) + bno;
1214 return (blkno);
1218 * Determine whether an inode can be allocated.
1220 * Check to see if an inode is available, and if it is,
1221 * allocate it using the following policy:
1222 * 1) allocate the requested inode.
1223 * 2) allocate the next available inode after the requested
1224 * inode in the specified cylinder group.
1226 static daddr_t
1227 ffs_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode, int flags)
1229 struct ufsmount *ump = ip->i_ump;
1230 struct fs *fs = ip->i_fs;
1231 struct cg *cgp;
1232 struct buf *bp, *ibp;
1233 u_int8_t *inosused;
1234 int error, start, len, loc, map, i;
1235 int32_t initediblk;
1236 daddr_t nalloc;
1237 struct ufs2_dinode *dp2;
1238 const int needswap = UFS_FSNEEDSWAP(fs);
1240 KASSERT(mutex_owned(&ump->um_lock));
1241 UFS_WAPBL_JLOCK_ASSERT(ip->i_ump->um_mountp);
1243 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1244 return (0);
1245 mutex_exit(&ump->um_lock);
1246 ibp = NULL;
1247 initediblk = -1;
1248 retry:
1249 error = bread(ip->i_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
1250 (int)fs->fs_cgsize, B_MODIFY, &bp);
1251 if (error)
1252 goto fail;
1253 cgp = (struct cg *)bp->b_data;
1254 if (!cg_chkmagic(cgp, needswap) || cgp->cg_cs.cs_nifree == 0)
1255 goto fail;
1257 if (ibp != NULL &&
1258 initediblk != ufs_rw32(cgp->cg_initediblk, needswap)) {
1259 /* Another thread allocated more inodes so we retry the test. */
1260 brelse(ibp, 0);
1261 ibp = NULL;
1264 * Check to see if we need to initialize more inodes.
1266 if (fs->fs_magic == FS_UFS2_MAGIC && ibp == NULL) {
1267 initediblk = ufs_rw32(cgp->cg_initediblk, needswap);
1268 nalloc = fs->fs_ipg - ufs_rw32(cgp->cg_cs.cs_nifree, needswap);
1269 if (nalloc + FFS_INOPB(fs) > initediblk &&
1270 initediblk < ufs_rw32(cgp->cg_niblk, needswap)) {
1272 * We have to release the cg buffer here to prevent
1273 * a deadlock when reading the inode block will
1274 * run a copy-on-write that might use this cg.
1276 brelse(bp, 0);
1277 bp = NULL;
1278 error = ffs_getblk(ip->i_devvp, FFS_FSBTODB(fs,
1279 ino_to_fsba(fs, cg * fs->fs_ipg + initediblk)),
1280 FFS_NOBLK, fs->fs_bsize, false, &ibp);
1281 if (error)
1282 goto fail;
1283 goto retry;
1287 cgp->cg_old_time = ufs_rw32(time_second, needswap);
1288 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1289 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1290 cgp->cg_time = ufs_rw64(time_second, needswap);
1291 inosused = cg_inosused(cgp, needswap);
1292 if (ipref) {
1293 ipref %= fs->fs_ipg;
1294 if (isclr(inosused, ipref))
1295 goto gotit;
1297 start = ufs_rw32(cgp->cg_irotor, needswap) / NBBY;
1298 len = howmany(fs->fs_ipg - ufs_rw32(cgp->cg_irotor, needswap),
1299 NBBY);
1300 loc = skpc(0xff, len, &inosused[start]);
1301 if (loc == 0) {
1302 len = start + 1;
1303 start = 0;
1304 loc = skpc(0xff, len, &inosused[0]);
1305 if (loc == 0) {
1306 printf("cg = %d, irotor = %d, fs = %s\n",
1307 cg, ufs_rw32(cgp->cg_irotor, needswap),
1308 fs->fs_fsmnt);
1309 panic("ffs_nodealloccg: map corrupted");
1310 /* NOTREACHED */
1313 i = start + len - loc;
1314 map = inosused[i] ^ 0xff;
1315 if (map == 0) {
1316 printf("fs = %s\n", fs->fs_fsmnt);
1317 panic("ffs_nodealloccg: block not in map");
1319 ipref = i * NBBY + ffs(map) - 1;
1320 cgp->cg_irotor = ufs_rw32(ipref, needswap);
1321 gotit:
1322 UFS_WAPBL_REGISTER_INODE(ip->i_ump->um_mountp, cg * fs->fs_ipg + ipref,
1323 mode);
1325 * Check to see if we need to initialize more inodes.
1327 if (ibp != NULL) {
1328 KASSERT(initediblk == ufs_rw32(cgp->cg_initediblk, needswap));
1329 memset(ibp->b_data, 0, fs->fs_bsize);
1330 dp2 = (struct ufs2_dinode *)(ibp->b_data);
1331 for (i = 0; i < FFS_INOPB(fs); i++) {
1333 * Don't bother to swap, it's supposed to be
1334 * random, after all.
1336 dp2->di_gen = (cprng_fast32() & INT32_MAX) / 2 + 1;
1337 dp2++;
1339 initediblk += FFS_INOPB(fs);
1340 cgp->cg_initediblk = ufs_rw32(initediblk, needswap);
1343 mutex_enter(&ump->um_lock);
1344 ACTIVECG_CLR(fs, cg);
1345 setbit(inosused, ipref);
1346 ufs_add32(cgp->cg_cs.cs_nifree, -1, needswap);
1347 fs->fs_cstotal.cs_nifree--;
1348 fs->fs_cs(fs, cg).cs_nifree--;
1349 fs->fs_fmod = 1;
1350 if ((mode & IFMT) == IFDIR) {
1351 ufs_add32(cgp->cg_cs.cs_ndir, 1, needswap);
1352 fs->fs_cstotal.cs_ndir++;
1353 fs->fs_cs(fs, cg).cs_ndir++;
1355 mutex_exit(&ump->um_lock);
1356 if (ibp != NULL) {
1357 bwrite(bp);
1358 bawrite(ibp);
1359 } else
1360 bdwrite(bp);
1361 return (cg * fs->fs_ipg + ipref);
1362 fail:
1363 if (bp != NULL)
1364 brelse(bp, 0);
1365 if (ibp != NULL)
1366 brelse(ibp, 0);
1367 mutex_enter(&ump->um_lock);
1368 return (0);
1372 * Allocate a block or fragment.
1374 * The specified block or fragment is removed from the
1375 * free map, possibly fragmenting a block in the process.
1377 * This implementation should mirror fs_blkfree
1379 * => um_lock not held on entry or exit
1382 ffs_blkalloc(struct inode *ip, daddr_t bno, long size)
1384 int error;
1386 error = ffs_check_bad_allocation(__func__, ip->i_fs, bno, size,
1387 ip->i_dev, ip->i_uid);
1388 if (error)
1389 return error;
1391 return ffs_blkalloc_ump(ip->i_ump, bno, size);
1395 ffs_blkalloc_ump(struct ufsmount *ump, daddr_t bno, long size)
1397 struct fs *fs = ump->um_fs;
1398 struct cg *cgp;
1399 struct buf *bp;
1400 int32_t fragno, cgbno;
1401 int i, error, cg, blk, frags, bbase;
1402 u_int8_t *blksfree;
1403 const int needswap = UFS_FSNEEDSWAP(fs);
1405 KASSERT((u_int)size <= fs->fs_bsize && ffs_fragoff(fs, size) == 0 &&
1406 ffs_fragnum(fs, bno) + ffs_numfrags(fs, size) <= fs->fs_frag);
1407 KASSERT(bno < fs->fs_size);
1409 cg = dtog(fs, bno);
1410 error = bread(ump->um_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
1411 (int)fs->fs_cgsize, B_MODIFY, &bp);
1412 if (error) {
1413 return error;
1415 cgp = (struct cg *)bp->b_data;
1416 if (!cg_chkmagic(cgp, needswap)) {
1417 brelse(bp, 0);
1418 return EIO;
1420 cgp->cg_old_time = ufs_rw32(time_second, needswap);
1421 cgp->cg_time = ufs_rw64(time_second, needswap);
1422 cgbno = dtogd(fs, bno);
1423 blksfree = cg_blksfree(cgp, needswap);
1425 mutex_enter(&ump->um_lock);
1426 if (size == fs->fs_bsize) {
1427 fragno = ffs_fragstoblks(fs, cgbno);
1428 if (!ffs_isblock(fs, blksfree, fragno)) {
1429 mutex_exit(&ump->um_lock);
1430 brelse(bp, 0);
1431 return EBUSY;
1433 ffs_clrblock(fs, blksfree, fragno);
1434 ffs_clusteracct(fs, cgp, fragno, -1);
1435 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
1436 fs->fs_cstotal.cs_nbfree--;
1437 fs->fs_cs(fs, cg).cs_nbfree--;
1438 } else {
1439 bbase = cgbno - ffs_fragnum(fs, cgbno);
1441 frags = ffs_numfrags(fs, size);
1442 for (i = 0; i < frags; i++) {
1443 if (isclr(blksfree, cgbno + i)) {
1444 mutex_exit(&ump->um_lock);
1445 brelse(bp, 0);
1446 return EBUSY;
1450 * if a complete block is being split, account for it
1452 fragno = ffs_fragstoblks(fs, bbase);
1453 if (ffs_isblock(fs, blksfree, fragno)) {
1454 ufs_add32(cgp->cg_cs.cs_nffree, fs->fs_frag, needswap);
1455 fs->fs_cstotal.cs_nffree += fs->fs_frag;
1456 fs->fs_cs(fs, cg).cs_nffree += fs->fs_frag;
1457 ffs_clusteracct(fs, cgp, fragno, -1);
1458 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
1459 fs->fs_cstotal.cs_nbfree--;
1460 fs->fs_cs(fs, cg).cs_nbfree--;
1463 * decrement the counts associated with the old frags
1465 blk = blkmap(fs, blksfree, bbase);
1466 ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
1468 * allocate the fragment
1470 for (i = 0; i < frags; i++) {
1471 clrbit(blksfree, cgbno + i);
1473 ufs_add32(cgp->cg_cs.cs_nffree, -i, needswap);
1474 fs->fs_cstotal.cs_nffree -= i;
1475 fs->fs_cs(fs, cg).cs_nffree -= i;
1477 * add back in counts associated with the new frags
1479 blk = blkmap(fs, blksfree, bbase);
1480 ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
1482 fs->fs_fmod = 1;
1483 ACTIVECG_CLR(fs, cg);
1484 mutex_exit(&ump->um_lock);
1485 bdwrite(bp);
1486 return 0;
1490 * Free a block or fragment.
1492 * The specified block or fragment is placed back in the
1493 * free map. If a fragment is deallocated, a possible
1494 * block reassembly is checked.
1496 * => um_lock not held on entry or exit
1498 static void
1499 ffs_blkfree_cg(struct fs *fs, struct vnode *devvp, daddr_t bno, long size)
1501 struct cg *cgp;
1502 struct buf *bp;
1503 struct ufsmount *ump;
1504 daddr_t cgblkno;
1505 int error, cg;
1506 dev_t dev;
1507 const bool devvp_is_snapshot = (devvp->v_type != VBLK);
1508 const int needswap = UFS_FSNEEDSWAP(fs);
1510 KASSERT(!devvp_is_snapshot);
1512 cg = dtog(fs, bno);
1513 dev = devvp->v_rdev;
1514 ump = VFSTOUFS(spec_node_getmountedfs(devvp));
1515 KASSERT(fs == ump->um_fs);
1516 cgblkno = FFS_FSBTODB(fs, cgtod(fs, cg));
1518 error = bread(devvp, cgblkno, (int)fs->fs_cgsize,
1519 B_MODIFY, &bp);
1520 if (error) {
1521 return;
1523 cgp = (struct cg *)bp->b_data;
1524 if (!cg_chkmagic(cgp, needswap)) {
1525 brelse(bp, 0);
1526 return;
1529 ffs_blkfree_common(ump, fs, dev, bp, bno, size, devvp_is_snapshot);
1531 bdwrite(bp);
1534 struct discardopdata {
1535 struct work wk; /* must be first */
1536 struct vnode *devvp;
1537 daddr_t bno;
1538 long size;
1541 struct discarddata {
1542 struct fs *fs;
1543 struct discardopdata *entry;
1544 long maxsize;
1545 kmutex_t entrylk;
1546 struct workqueue *wq;
1547 int wqcnt, wqdraining;
1548 kmutex_t wqlk;
1549 kcondvar_t wqcv;
1550 /* timer for flush? */
1553 static void
1554 ffs_blkfree_td(struct fs *fs, struct discardopdata *td)
1556 struct mount *mp = spec_node_getmountedfs(td->devvp);
1557 long todo;
1558 int error;
1560 while (td->size) {
1561 todo = min(td->size,
1562 ffs_lfragtosize(fs, (fs->fs_frag - ffs_fragnum(fs, td->bno))));
1563 error = UFS_WAPBL_BEGIN(mp);
1564 if (error) {
1565 printf("ffs: failed to begin wapbl transaction"
1566 " for discard: %d\n", error);
1567 break;
1569 ffs_blkfree_cg(fs, td->devvp, td->bno, todo);
1570 UFS_WAPBL_END(mp);
1571 td->bno += ffs_numfrags(fs, todo);
1572 td->size -= todo;
1576 static void
1577 ffs_discardcb(struct work *wk, void *arg)
1579 struct discardopdata *td = (void *)wk;
1580 struct discarddata *ts = arg;
1581 struct fs *fs = ts->fs;
1582 off_t start, len;
1583 #ifdef TRIMDEBUG
1584 int error;
1585 #endif
1587 /* like FSBTODB but emits bytes; XXX move to fs.h */
1588 #ifndef FFS_FSBTOBYTES
1589 #define FFS_FSBTOBYTES(fs, b) ((b) << (fs)->fs_fshift)
1590 #endif
1592 start = FFS_FSBTOBYTES(fs, td->bno);
1593 len = td->size;
1594 #ifdef TRIMDEBUG
1595 error =
1596 #endif
1597 VOP_FDISCARD(td->devvp, start, len);
1598 #ifdef TRIMDEBUG
1599 printf("trim(%" PRId64 ",%ld):%d\n", td->bno, td->size, error);
1600 #endif
1602 ffs_blkfree_td(fs, td);
1603 kmem_free(td, sizeof(*td));
1604 mutex_enter(&ts->wqlk);
1605 ts->wqcnt--;
1606 if (ts->wqdraining && !ts->wqcnt)
1607 cv_signal(&ts->wqcv);
1608 mutex_exit(&ts->wqlk);
1611 void *
1612 ffs_discard_init(struct vnode *devvp, struct fs *fs)
1614 struct discarddata *ts;
1615 int error;
1617 ts = kmem_zalloc(sizeof (*ts), KM_SLEEP);
1618 error = workqueue_create(&ts->wq, "trimwq", ffs_discardcb, ts,
1619 0, 0, 0);
1620 if (error) {
1621 kmem_free(ts, sizeof (*ts));
1622 return NULL;
1624 mutex_init(&ts->entrylk, MUTEX_DEFAULT, IPL_NONE);
1625 mutex_init(&ts->wqlk, MUTEX_DEFAULT, IPL_NONE);
1626 cv_init(&ts->wqcv, "trimwqcv");
1627 ts->maxsize = 100*1024; /* XXX */
1628 ts->fs = fs;
1629 return ts;
1632 void
1633 ffs_discard_finish(void *vts, int flags)
1635 struct discarddata *ts = vts;
1636 struct discardopdata *td = NULL;
1637 int res = 0;
1639 /* wait for workqueue to drain */
1640 mutex_enter(&ts->wqlk);
1641 if (ts->wqcnt) {
1642 ts->wqdraining = 1;
1643 res = cv_timedwait(&ts->wqcv, &ts->wqlk, mstohz(5000));
1645 mutex_exit(&ts->wqlk);
1646 if (res)
1647 printf("ffs_discarddata drain timeout\n");
1649 mutex_enter(&ts->entrylk);
1650 if (ts->entry) {
1651 td = ts->entry;
1652 ts->entry = NULL;
1654 mutex_exit(&ts->entrylk);
1655 if (td) {
1656 /* XXX don't tell disk, its optional */
1657 ffs_blkfree_td(ts->fs, td);
1658 #ifdef TRIMDEBUG
1659 printf("finish(%" PRId64 ",%ld)\n", td->bno, td->size);
1660 #endif
1661 kmem_free(td, sizeof(*td));
1664 cv_destroy(&ts->wqcv);
1665 mutex_destroy(&ts->entrylk);
1666 mutex_destroy(&ts->wqlk);
1667 workqueue_destroy(ts->wq);
1668 kmem_free(ts, sizeof(*ts));
1671 void
1672 ffs_blkfree(struct fs *fs, struct vnode *devvp, daddr_t bno, long size,
1673 ino_t inum)
1675 struct ufsmount *ump;
1676 int error;
1677 dev_t dev;
1678 struct discarddata *ts;
1679 struct discardopdata *td;
1681 dev = devvp->v_rdev;
1682 ump = VFSTOUFS(spec_node_getmountedfs(devvp));
1683 if (ffs_snapblkfree(fs, devvp, bno, size, inum))
1684 return;
1686 error = ffs_check_bad_allocation(__func__, fs, bno, size, dev, inum);
1687 if (error)
1688 return;
1690 if (!ump->um_discarddata) {
1691 ffs_blkfree_cg(fs, devvp, bno, size);
1692 return;
1695 #ifdef TRIMDEBUG
1696 printf("blkfree(%" PRId64 ",%ld)\n", bno, size);
1697 #endif
1698 ts = ump->um_discarddata;
1699 td = NULL;
1701 mutex_enter(&ts->entrylk);
1702 if (ts->entry) {
1703 td = ts->entry;
1704 /* ffs deallocs backwards, check for prepend only */
1705 if (td->bno == bno + ffs_numfrags(fs, size)
1706 && td->size + size <= ts->maxsize) {
1707 td->bno = bno;
1708 td->size += size;
1709 if (td->size < ts->maxsize) {
1710 #ifdef TRIMDEBUG
1711 printf("defer(%" PRId64 ",%ld)\n", td->bno, td->size);
1712 #endif
1713 mutex_exit(&ts->entrylk);
1714 return;
1716 size = 0; /* mark done */
1718 ts->entry = NULL;
1720 mutex_exit(&ts->entrylk);
1722 if (td) {
1723 #ifdef TRIMDEBUG
1724 printf("enq old(%" PRId64 ",%ld)\n", td->bno, td->size);
1725 #endif
1726 mutex_enter(&ts->wqlk);
1727 ts->wqcnt++;
1728 mutex_exit(&ts->wqlk);
1729 workqueue_enqueue(ts->wq, &td->wk, NULL);
1731 if (!size)
1732 return;
1734 td = kmem_alloc(sizeof(*td), KM_SLEEP);
1735 td->devvp = devvp;
1736 td->bno = bno;
1737 td->size = size;
1739 if (td->size < ts->maxsize) { /* XXX always the case */
1740 mutex_enter(&ts->entrylk);
1741 if (!ts->entry) { /* possible race? */
1742 #ifdef TRIMDEBUG
1743 printf("defer(%" PRId64 ",%ld)\n", td->bno, td->size);
1744 #endif
1745 ts->entry = td;
1746 td = NULL;
1748 mutex_exit(&ts->entrylk);
1750 if (td) {
1751 #ifdef TRIMDEBUG
1752 printf("enq new(%" PRId64 ",%ld)\n", td->bno, td->size);
1753 #endif
1754 mutex_enter(&ts->wqlk);
1755 ts->wqcnt++;
1756 mutex_exit(&ts->wqlk);
1757 workqueue_enqueue(ts->wq, &td->wk, NULL);
1762 * Free a block or fragment from a snapshot cg copy.
1764 * The specified block or fragment is placed back in the
1765 * free map. If a fragment is deallocated, a possible
1766 * block reassembly is checked.
1768 * => um_lock not held on entry or exit
1770 void
1771 ffs_blkfree_snap(struct fs *fs, struct vnode *devvp, daddr_t bno, long size,
1772 ino_t inum)
1774 struct cg *cgp;
1775 struct buf *bp;
1776 struct ufsmount *ump;
1777 daddr_t cgblkno;
1778 int error, cg;
1779 dev_t dev;
1780 const bool devvp_is_snapshot = (devvp->v_type != VBLK);
1781 const int needswap = UFS_FSNEEDSWAP(fs);
1783 KASSERT(devvp_is_snapshot);
1785 cg = dtog(fs, bno);
1786 dev = VTOI(devvp)->i_devvp->v_rdev;
1787 ump = VFSTOUFS(devvp->v_mount);
1788 cgblkno = ffs_fragstoblks(fs, cgtod(fs, cg));
1790 error = ffs_check_bad_allocation(__func__, fs, bno, size, dev, inum);
1791 if (error)
1792 return;
1794 error = bread(devvp, cgblkno, (int)fs->fs_cgsize,
1795 B_MODIFY, &bp);
1796 if (error) {
1797 return;
1799 cgp = (struct cg *)bp->b_data;
1800 if (!cg_chkmagic(cgp, needswap)) {
1801 brelse(bp, 0);
1802 return;
1805 ffs_blkfree_common(ump, fs, dev, bp, bno, size, devvp_is_snapshot);
1807 bdwrite(bp);
1810 static void
1811 ffs_blkfree_common(struct ufsmount *ump, struct fs *fs, dev_t dev,
1812 struct buf *bp, daddr_t bno, long size, bool devvp_is_snapshot)
1814 struct cg *cgp;
1815 int32_t fragno, cgbno;
1816 int i, cg, blk, frags, bbase;
1817 u_int8_t *blksfree;
1818 const int needswap = UFS_FSNEEDSWAP(fs);
1820 cg = dtog(fs, bno);
1821 cgp = (struct cg *)bp->b_data;
1822 cgp->cg_old_time = ufs_rw32(time_second, needswap);
1823 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1824 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1825 cgp->cg_time = ufs_rw64(time_second, needswap);
1826 cgbno = dtogd(fs, bno);
1827 blksfree = cg_blksfree(cgp, needswap);
1828 mutex_enter(&ump->um_lock);
1829 if (size == fs->fs_bsize) {
1830 fragno = ffs_fragstoblks(fs, cgbno);
1831 if (!ffs_isfreeblock(fs, blksfree, fragno)) {
1832 if (devvp_is_snapshot) {
1833 mutex_exit(&ump->um_lock);
1834 return;
1836 printf("dev = 0x%llx, block = %" PRId64 ", fs = %s\n",
1837 (unsigned long long)dev, bno, fs->fs_fsmnt);
1838 panic("blkfree: freeing free block");
1840 ffs_setblock(fs, blksfree, fragno);
1841 ffs_clusteracct(fs, cgp, fragno, 1);
1842 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1843 fs->fs_cstotal.cs_nbfree++;
1844 fs->fs_cs(fs, cg).cs_nbfree++;
1845 if ((fs->fs_magic == FS_UFS1_MAGIC) &&
1846 ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
1847 i = old_cbtocylno(fs, cgbno);
1848 KASSERT(i >= 0);
1849 KASSERT(i < fs->fs_old_ncyl);
1850 KASSERT(old_cbtorpos(fs, cgbno) >= 0);
1851 KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, cgbno) < fs->fs_old_nrpos);
1852 ufs_add16(old_cg_blks(fs, cgp, i, needswap)[old_cbtorpos(fs, cgbno)], 1,
1853 needswap);
1854 ufs_add32(old_cg_blktot(cgp, needswap)[i], 1, needswap);
1856 } else {
1857 bbase = cgbno - ffs_fragnum(fs, cgbno);
1859 * decrement the counts associated with the old frags
1861 blk = blkmap(fs, blksfree, bbase);
1862 ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
1864 * deallocate the fragment
1866 frags = ffs_numfrags(fs, size);
1867 for (i = 0; i < frags; i++) {
1868 if (isset(blksfree, cgbno + i)) {
1869 printf("dev = 0x%llx, block = %" PRId64
1870 ", fs = %s\n",
1871 (unsigned long long)dev, bno + i,
1872 fs->fs_fsmnt);
1873 panic("blkfree: freeing free frag");
1875 setbit(blksfree, cgbno + i);
1877 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1878 fs->fs_cstotal.cs_nffree += i;
1879 fs->fs_cs(fs, cg).cs_nffree += i;
1881 * add back in counts associated with the new frags
1883 blk = blkmap(fs, blksfree, bbase);
1884 ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
1886 * if a complete block has been reassembled, account for it
1888 fragno = ffs_fragstoblks(fs, bbase);
1889 if (ffs_isblock(fs, blksfree, fragno)) {
1890 ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
1891 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1892 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1893 ffs_clusteracct(fs, cgp, fragno, 1);
1894 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1895 fs->fs_cstotal.cs_nbfree++;
1896 fs->fs_cs(fs, cg).cs_nbfree++;
1897 if ((fs->fs_magic == FS_UFS1_MAGIC) &&
1898 ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
1899 i = old_cbtocylno(fs, bbase);
1900 KASSERT(i >= 0);
1901 KASSERT(i < fs->fs_old_ncyl);
1902 KASSERT(old_cbtorpos(fs, bbase) >= 0);
1903 KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, bbase) < fs->fs_old_nrpos);
1904 ufs_add16(old_cg_blks(fs, cgp, i, needswap)[old_cbtorpos(fs,
1905 bbase)], 1, needswap);
1906 ufs_add32(old_cg_blktot(cgp, needswap)[i], 1, needswap);
1910 fs->fs_fmod = 1;
1911 ACTIVECG_CLR(fs, cg);
1912 mutex_exit(&ump->um_lock);
1916 * Free an inode.
1919 ffs_vfree(struct vnode *vp, ino_t ino, int mode)
1922 return ffs_freefile(vp->v_mount, ino, mode);
1926 * Do the actual free operation.
1927 * The specified inode is placed back in the free map.
1929 * => um_lock not held on entry or exit
1932 ffs_freefile(struct mount *mp, ino_t ino, int mode)
1934 struct ufsmount *ump = VFSTOUFS(mp);
1935 struct fs *fs = ump->um_fs;
1936 struct vnode *devvp;
1937 struct cg *cgp;
1938 struct buf *bp;
1939 int error, cg;
1940 daddr_t cgbno;
1941 dev_t dev;
1942 const int needswap = UFS_FSNEEDSWAP(fs);
1944 cg = ino_to_cg(fs, ino);
1945 devvp = ump->um_devvp;
1946 dev = devvp->v_rdev;
1947 cgbno = FFS_FSBTODB(fs, cgtod(fs, cg));
1949 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1950 panic("ifree: range: dev = 0x%llx, ino = %llu, fs = %s",
1951 (long long)dev, (unsigned long long)ino, fs->fs_fsmnt);
1952 error = bread(devvp, cgbno, (int)fs->fs_cgsize,
1953 B_MODIFY, &bp);
1954 if (error) {
1955 return (error);
1957 cgp = (struct cg *)bp->b_data;
1958 if (!cg_chkmagic(cgp, needswap)) {
1959 brelse(bp, 0);
1960 return (0);
1963 ffs_freefile_common(ump, fs, dev, bp, ino, mode, false);
1965 bdwrite(bp);
1967 return 0;
1971 ffs_freefile_snap(struct fs *fs, struct vnode *devvp, ino_t ino, int mode)
1973 struct ufsmount *ump;
1974 struct cg *cgp;
1975 struct buf *bp;
1976 int error, cg;
1977 daddr_t cgbno;
1978 dev_t dev;
1979 const int needswap = UFS_FSNEEDSWAP(fs);
1981 KASSERT(devvp->v_type != VBLK);
1983 cg = ino_to_cg(fs, ino);
1984 dev = VTOI(devvp)->i_devvp->v_rdev;
1985 ump = VFSTOUFS(devvp->v_mount);
1986 cgbno = ffs_fragstoblks(fs, cgtod(fs, cg));
1987 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1988 panic("ifree: range: dev = 0x%llx, ino = %llu, fs = %s",
1989 (unsigned long long)dev, (unsigned long long)ino,
1990 fs->fs_fsmnt);
1991 error = bread(devvp, cgbno, (int)fs->fs_cgsize,
1992 B_MODIFY, &bp);
1993 if (error) {
1994 return (error);
1996 cgp = (struct cg *)bp->b_data;
1997 if (!cg_chkmagic(cgp, needswap)) {
1998 brelse(bp, 0);
1999 return (0);
2001 ffs_freefile_common(ump, fs, dev, bp, ino, mode, true);
2003 bdwrite(bp);
2005 return 0;
2008 static void
2009 ffs_freefile_common(struct ufsmount *ump, struct fs *fs, dev_t dev,
2010 struct buf *bp, ino_t ino, int mode, bool devvp_is_snapshot)
2012 int cg;
2013 struct cg *cgp;
2014 u_int8_t *inosused;
2015 const int needswap = UFS_FSNEEDSWAP(fs);
2017 cg = ino_to_cg(fs, ino);
2018 cgp = (struct cg *)bp->b_data;
2019 cgp->cg_old_time = ufs_rw32(time_second, needswap);
2020 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
2021 (fs->fs_old_flags & FS_FLAGS_UPDATED))
2022 cgp->cg_time = ufs_rw64(time_second, needswap);
2023 inosused = cg_inosused(cgp, needswap);
2024 ino %= fs->fs_ipg;
2025 if (isclr(inosused, ino)) {
2026 printf("ifree: dev = 0x%llx, ino = %llu, fs = %s\n",
2027 (unsigned long long)dev, (unsigned long long)ino +
2028 cg * fs->fs_ipg, fs->fs_fsmnt);
2029 if (fs->fs_ronly == 0)
2030 panic("ifree: freeing free inode");
2032 clrbit(inosused, ino);
2033 if (!devvp_is_snapshot)
2034 UFS_WAPBL_UNREGISTER_INODE(ump->um_mountp,
2035 ino + cg * fs->fs_ipg, mode);
2036 if (ino < ufs_rw32(cgp->cg_irotor, needswap))
2037 cgp->cg_irotor = ufs_rw32(ino, needswap);
2038 ufs_add32(cgp->cg_cs.cs_nifree, 1, needswap);
2039 mutex_enter(&ump->um_lock);
2040 fs->fs_cstotal.cs_nifree++;
2041 fs->fs_cs(fs, cg).cs_nifree++;
2042 if ((mode & IFMT) == IFDIR) {
2043 ufs_add32(cgp->cg_cs.cs_ndir, -1, needswap);
2044 fs->fs_cstotal.cs_ndir--;
2045 fs->fs_cs(fs, cg).cs_ndir--;
2047 fs->fs_fmod = 1;
2048 ACTIVECG_CLR(fs, cg);
2049 mutex_exit(&ump->um_lock);
2053 * Check to see if a file is free.
2056 ffs_checkfreefile(struct fs *fs, struct vnode *devvp, ino_t ino)
2058 struct cg *cgp;
2059 struct buf *bp;
2060 daddr_t cgbno;
2061 int ret, cg;
2062 u_int8_t *inosused;
2063 const bool devvp_is_snapshot = (devvp->v_type != VBLK);
2065 KASSERT(devvp_is_snapshot);
2067 cg = ino_to_cg(fs, ino);
2068 if (devvp_is_snapshot)
2069 cgbno = ffs_fragstoblks(fs, cgtod(fs, cg));
2070 else
2071 cgbno = FFS_FSBTODB(fs, cgtod(fs, cg));
2072 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
2073 return 1;
2074 if (bread(devvp, cgbno, (int)fs->fs_cgsize, 0, &bp)) {
2075 return 1;
2077 cgp = (struct cg *)bp->b_data;
2078 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
2079 brelse(bp, 0);
2080 return 1;
2082 inosused = cg_inosused(cgp, UFS_FSNEEDSWAP(fs));
2083 ino %= fs->fs_ipg;
2084 ret = isclr(inosused, ino);
2085 brelse(bp, 0);
2086 return ret;
2090 * Find a block of the specified size in the specified cylinder group.
2092 * It is a panic if a request is made to find a block if none are
2093 * available.
2095 static int32_t
2096 ffs_mapsearch(struct fs *fs, struct cg *cgp, daddr_t bpref, int allocsiz)
2098 int32_t bno;
2099 int start, len, loc, i;
2100 int blk, field, subfield, pos;
2101 int ostart, olen;
2102 u_int8_t *blksfree;
2103 const int needswap = UFS_FSNEEDSWAP(fs);
2105 /* KASSERT(mutex_owned(&ump->um_lock)); */
2108 * find the fragment by searching through the free block
2109 * map for an appropriate bit pattern
2111 if (bpref)
2112 start = dtogd(fs, bpref) / NBBY;
2113 else
2114 start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
2115 blksfree = cg_blksfree(cgp, needswap);
2116 len = howmany(fs->fs_fpg, NBBY) - start;
2117 ostart = start;
2118 olen = len;
2119 loc = scanc((u_int)len,
2120 (const u_char *)&blksfree[start],
2121 (const u_char *)fragtbl[fs->fs_frag],
2122 (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
2123 if (loc == 0) {
2124 len = start + 1;
2125 start = 0;
2126 loc = scanc((u_int)len,
2127 (const u_char *)&blksfree[0],
2128 (const u_char *)fragtbl[fs->fs_frag],
2129 (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
2130 if (loc == 0) {
2131 printf("start = %d, len = %d, fs = %s\n",
2132 ostart, olen, fs->fs_fsmnt);
2133 printf("offset=%d %ld\n",
2134 ufs_rw32(cgp->cg_freeoff, needswap),
2135 (long)blksfree - (long)cgp);
2136 printf("cg %d\n", cgp->cg_cgx);
2137 panic("ffs_alloccg: map corrupted");
2138 /* NOTREACHED */
2141 bno = (start + len - loc) * NBBY;
2142 cgp->cg_frotor = ufs_rw32(bno, needswap);
2144 * found the byte in the map
2145 * sift through the bits to find the selected frag
2147 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
2148 blk = blkmap(fs, blksfree, bno);
2149 blk <<= 1;
2150 field = around[allocsiz];
2151 subfield = inside[allocsiz];
2152 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
2153 if ((blk & field) == subfield)
2154 return (bno + pos);
2155 field <<= 1;
2156 subfield <<= 1;
2159 printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
2160 panic("ffs_alloccg: block not in map");
2161 /* return (-1); */
2165 * Fserr prints the name of a file system with an error diagnostic.
2167 * The form of the error message is:
2168 * fs: error message
2170 static void
2171 ffs_fserr(struct fs *fs, kauth_cred_t cred, const char *cp)
2173 KASSERT(cred != NULL);
2175 if (cred == NOCRED || cred == FSCRED) {
2176 log(LOG_ERR, "pid %d, command %s, on %s: %s\n",
2177 curproc->p_pid, curproc->p_comm,
2178 fs->fs_fsmnt, cp);
2179 } else {
2180 log(LOG_ERR, "uid %d, pid %d, command %s, on %s: %s\n",
2181 kauth_cred_getuid(cred), curproc->p_pid, curproc->p_comm,
2182 fs->fs_fsmnt, cp);