xref: /minix/sys/ufs/ffs/ffs_alloc.c (revision 0a6a1f1d)
1 /*	$NetBSD: ffs_alloc.c,v 1.151 2015/08/12 14:52:35 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Wasabi Systems, Inc.
9  *
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.
18  *
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.
30  */
31 
32 /*
33  * Copyright (c) 2002 Networks Associates Technology, Inc.
34  * All rights reserved.
35  *
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
41  *
42  * Copyright (c) 1982, 1986, 1989, 1993
43  *	The Regents of the University of California.  All rights reserved.
44  *
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.
56  *
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.
68  *
69  *	@(#)ffs_alloc.c	8.19 (Berkeley) 7/13/95
70  */
71 
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: ffs_alloc.c,v 1.151 2015/08/12 14:52:35 riastradh Exp $");
74 
75 #if defined(_KERNEL_OPT)
76 #include "opt_ffs.h"
77 #include "opt_quota.h"
78 #include "opt_uvm_page_trkown.h"
79 #endif
80 
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>
94 
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>
102 
103 #include <ufs/ffs/fs.h>
104 #include <ufs/ffs/ffs_extern.h>
105 
106 #ifdef UVM_PAGE_TRKOWN
107 #include <uvm/uvm.h>
108 #endif
109 
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);
124 
125 /* if 1, changes in optimalization strategy are logged */
126 int ffs_log_changeopt = 0;
127 
128 /* in ffs_tables.c */
129 extern const int inside[], around[];
130 extern const u_char * const fragtbl[];
131 
132 /* Basic consistency check for block allocations */
133 static int
ffs_check_bad_allocation(const char * func,struct fs * fs,daddr_t bno,long size,dev_t dev,ino_t inum)134 ffs_check_bad_allocation(const char *func, struct fs *fs, daddr_t bno,
135     long size, dev_t dev, ino_t inum)
136 {
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);
143 	}
144 
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;
150 	}
151 	return 0;
152 }
153 
154 /*
155  * Allocate a block in the file system.
156  *
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.
172  *
173  * => called with um_lock held
174  * => releases um_lock before returning
175  */
176 int
ffs_alloc(struct inode * ip,daddr_t lbn,daddr_t bpref,int size,int flags,kauth_cred_t cred,daddr_t * bnp)177 ffs_alloc(struct inode *ip, daddr_t lbn, daddr_t bpref, int size, int flags,
178     kauth_cred_t cred, daddr_t *bnp)
179 {
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
187 
188 	fs = ip->i_fs;
189 	ump = ip->i_ump;
190 
191 	KASSERT(mutex_owned(&ump->um_lock));
192 
193 #ifdef UVM_PAGE_TRKOWN
194 
195 	/*
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.
206 	 */
207 
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);
215 
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;
225 		}
226 		mutex_exit(uobj->vmobjlock);
227 	}
228 #endif
229 
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");
237 	}
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
253 
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);
266 	}
267 #if defined(QUOTA) || defined(QUOTA2)
268 	/*
269 	 * Restore user's disk quota because allocation failed.
270 	 */
271 	(void) chkdq(ip, -btodb(size), cred, FORCE);
272 #endif
273 	if (flags & B_CONTIG) {
274 		/*
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.
280 		 *
281 		 *
282 		 * Fail silently - it's up to our caller to report
283 		 * errors.
284 		 */
285 		return (ENOSPC);
286 	}
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);
292 }
293 
294 /*
295  * Reallocate a fragment to a bigger size
296  *
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.
301  *
302  * => called with um_lock held
303  * => return with um_lock released
304  */
305 int
ffs_realloccg(struct inode * ip,daddr_t lbprev,daddr_t bpref,int osize,int nsize,kauth_cred_t cred,struct buf ** bpp,daddr_t * blknop)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)
308 {
309 	struct ufsmount *ump;
310 	struct fs *fs;
311 	struct buf *bp;
312 	int cg, request, error;
313 	daddr_t bprev, bno;
314 
315 	fs = ip->i_fs;
316 	ump = ip->i_ump;
317 
318 	KASSERT(mutex_owned(&ump->um_lock));
319 
320 #ifdef UVM_PAGE_TRKOWN
321 
322 	/*
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.
331 	 */
332 
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);
338 
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;
345 		}
346 		mutex_exit(uobj->vmobjlock);
347 	}
348 #endif
349 
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");
358 	}
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;
367 	}
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));
372 
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");
378 	}
379 	mutex_exit(&ump->um_lock);
380 
381 	/*
382 	 * Allocate the extra space in the buffer.
383 	 */
384 	if (bpp != NULL &&
385 	    (error = bread(ITOV(ip), lbprev, osize, 0, &bp)) != 0) {
386 		return (error);
387 	}
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);
392 		}
393 		return (error);
394 	}
395 #endif
396 	/*
397 	 * Check for extension in the existing location.
398 	 */
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;
404 
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;
415 		}
416 		if (blknop != NULL) {
417 			*blknop = bno;
418 		}
419 		return (0);
420 	}
421 	/*
422 	 * Allocate a new disk location.
423 	 */
424 	if (bpref >= fs->fs_size)
425 		bpref = 0;
426 	switch ((int)fs->fs_optim) {
427 	case FS_OPTSPACE:
428 		/*
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.
434 		 */
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;
440 
441 		if (ffs_log_changeopt) {
442 			log(LOG_NOTICE,
443 				"%s: optimization changed from SPACE to TIME\n",
444 				fs->fs_fsmnt);
445 		}
446 
447 		fs->fs_optim = FS_OPTTIME;
448 		break;
449 	case FS_OPTTIME:
450 		/*
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.
459 		 */
460 		request = fs->fs_bsize;
461 		if (fs->fs_cstotal.cs_nffree <
462 		    fs->fs_dsize * (fs->fs_minfree - 2) / 100)
463 			break;
464 
465 		if (ffs_log_changeopt) {
466 			log(LOG_NOTICE,
467 				"%s: optimization changed from TIME to SPACE\n",
468 				fs->fs_fsmnt);
469 		}
470 
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 */
478 	}
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);
489 		}
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);
501 		}
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;
513 		}
514 		if (blknop != NULL) {
515 			*blknop = bno;
516 		}
517 		return (0);
518 	}
519 	mutex_exit(&ump->um_lock);
520 
521 #if defined(QUOTA) || defined(QUOTA2)
522 	/*
523 	 * Restore user's disk quota because allocation failed.
524 	 */
525 	(void) chkdq(ip, -btodb(nsize - osize), cred, FORCE);
526 #endif
527 	if (bpp != NULL) {
528 		brelse(bp, 0);
529 	}
530 
531 nospace:
532 	/*
533 	 * no space available
534 	 */
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);
538 }
539 
540 /*
541  * Allocate an inode in the file system.
542  *
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.
554  *
555  * => um_lock not held upon entry or return
556  */
557 int
ffs_valloc(struct vnode * pvp,int mode,kauth_cred_t cred,ino_t * inop)558 ffs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred, ino_t *inop)
559 {
560 	struct ufsmount *ump;
561 	struct inode *pip;
562 	struct fs *fs;
563 	ino_t ino, ipref;
564 	int cg, error;
565 
566 	UFS_WAPBL_JUNLOCK_ASSERT(pvp->v_mount);
567 
568 	pip = VTOI(pvp);
569 	fs = pip->i_fs;
570 	ump = pip->i_ump;
571 
572 	error = UFS_WAPBL_BEGIN(pvp->v_mount);
573 	if (error) {
574 		return error;
575 	}
576 	mutex_enter(&ump->um_lock);
577 	if (fs->fs_cstotal.cs_nifree == 0)
578 		goto noinodes;
579 
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);
587 	/*
588 	 * Track number of dirs created one after another
589 	 * in a same cg without intervening by files.
590 	 */
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]--;
597 	}
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;
604 
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;
611 }
612 
613 /*
614  * Find a cylinder group in which to place a directory.
615  *
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.
622  *
623  * If we allocate a first level directory then force allocation
624  * in another cylinder group.
625  */
626 static ino_t
ffs_dirpref(struct inode * pip)627 ffs_dirpref(struct inode *pip)
628 {
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;
636 
637 	KASSERT(mutex_owned(&pip->i_ump->um_lock));
638 
639 	fs = pip->i_fs;
640 
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;
644 
645 	/*
646 	 * Force allocation in another cg if creating a first level dir.
647 	 */
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;
658 			}
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;
665 			}
666 		return ((ino_t)(fs->fs_ipg * mincg));
667 	}
668 
669 	/*
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.
675 	 */
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;
689 	}
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;
699 
700 	/*
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.
704 	 */
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));
712 		}
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));
719 		}
720 	/*
721 	 * This is a backstop when we are deficient in space.
722 	 */
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));
730 }
731 
732 /*
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.
736  *
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.
750  *
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.
755  *
756  * => um_lock held on entry and exit
757  */
758 daddr_t
ffs_blkpref_ufs1(struct inode * ip,daddr_t lbn,int indx,int flags,int32_t * bap)759 ffs_blkpref_ufs1(struct inode *ip, daddr_t lbn, int indx, int flags,
760     int32_t *bap /* XXX ondisk32 */)
761 {
762 	struct fs *fs;
763 	int cg;
764 	int avgbfree, startcg;
765 
766 	KASSERT(mutex_owned(&ip->i_ump->um_lock));
767 
768 	fs = ip->i_fs;
769 
770 	/*
771 	 * If allocating a contiguous file with B_CONTIG, use the hints
772 	 * in the inode extentions to return the desired block.
773 	 *
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.
779 	 */
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);
787 	}
788 
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);
793 		}
794 		/*
795 		 * Find a cylinder with greater than average number of
796 		 * unused data blocks.
797 		 */
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);
809 			}
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);
813 			}
814 		return (0);
815 	}
816 	/*
817 	 * We just always try to lay things out contiguously.
818 	 */
819 	return ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
820 }
821 
822 daddr_t
ffs_blkpref_ufs2(struct inode * ip,daddr_t lbn,int indx,int flags,int64_t * bap)823 ffs_blkpref_ufs2(struct inode *ip, daddr_t lbn, int indx, int flags,
824     int64_t *bap)
825 {
826 	struct fs *fs;
827 	int cg;
828 	int avgbfree, startcg;
829 
830 	KASSERT(mutex_owned(&ip->i_ump->um_lock));
831 
832 	fs = ip->i_fs;
833 
834 	/*
835 	 * If allocating a contiguous file with B_CONTIG, use the hints
836 	 * in the inode extentions to return the desired block.
837 	 *
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.
843 	 */
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);
851 	}
852 
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);
857 		}
858 		/*
859 		 * Find a cylinder with greater than average number of
860 		 * unused data blocks.
861 		 */
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);
873 			}
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);
877 			}
878 		return (0);
879 	}
880 	/*
881 	 * We just always try to lay things out contiguously.
882 	 */
883 	return ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
884 }
885 
886 
887 /*
888  * Implement the cylinder overflow algorithm.
889  *
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.
894  *
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)
898  */
899 /*VARARGS5*/
900 static daddr_t
ffs_hashalloc(struct inode * ip,int cg,daddr_t pref,int size,int flags,daddr_t (* allocator)(struct inode *,int,daddr_t,int,int))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))
904 {
905 	struct fs *fs;
906 	daddr_t result;
907 	int i, icg = cg;
908 
909 	fs = ip->i_fs;
910 	/*
911 	 * 1: preferred cylinder group
912 	 */
913 	result = (*allocator)(ip, cg, pref, size, flags);
914 	if (result)
915 		return (result);
916 
917 	if (flags & B_CONTIG)
918 		return (result);
919 	/*
920 	 * 2: quadratic rehash
921 	 */
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);
929 	}
930 	/*
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.
934 	 */
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;
943 	}
944 	return (0);
945 }
946 
947 /*
948  * Determine whether a fragment can be extended.
949  *
950  * Check to see if the necessary fragments are available, and
951  * if they are, allocate them.
952  *
953  * => called with um_lock held
954  * => returns with um_lock released on success, held on failure
955  */
956 static daddr_t
ffs_fragextend(struct inode * ip,int cg,daddr_t bprev,int osize,int nsize)957 ffs_fragextend(struct inode *ip, int cg, daddr_t bprev, int osize, int nsize)
958 {
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;
967 
968 	fs = ip->i_fs;
969 	ump = ip->i_ump;
970 
971 	KASSERT(mutex_owned(&ump->um_lock));
972 
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);
980 	}
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;
998 	/*
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
1003 	 */
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--;
1016 	}
1017 	fs->fs_fmod = 1;
1018 	ACTIVECG_CLR(fs, cg);
1019 	mutex_exit(&ump->um_lock);
1020 	bdwrite(bp);
1021 	return (bprev);
1022 
1023  fail:
1024  	if (bp != NULL)
1025 		brelse(bp, 0);
1026  	mutex_enter(&ump->um_lock);
1027  	return (0);
1028 }
1029 
1030 /*
1031  * Determine whether a block can be allocated.
1032  *
1033  * Check to see if a block of the appropriate size is available,
1034  * and if it is, allocate it.
1035  */
1036 static daddr_t
ffs_alloccg(struct inode * ip,int cg,daddr_t bpref,int size,int flags)1037 ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size, int flags)
1038 {
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);
1048 
1049 	ump = ip->i_ump;
1050 
1051 	KASSERT(mutex_owned(&ump->um_lock));
1052 
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);
1075 	}
1076 	/*
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
1080 	 */
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) {
1087 		/*
1088 		 * no fragments were available, so a block will be
1089 		 * allocated, and hacked up
1090 		 */
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);
1108 	}
1109 	bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1110 #if 0
1111 	/*
1112 	 * XXX fvdl mapsearch will panic, and never return -1
1113 	 *          also: returning NULL as daddr_t ?
1114 	 */
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;
1133 
1134  fail:
1135  	if (bp != NULL)
1136 		brelse(bp, 0);
1137  	mutex_enter(&ump->um_lock);
1138  	return (0);
1139 }
1140 
1141 /*
1142  * Allocate a block in a cylinder group.
1143  *
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.
1151  */
1152 static daddr_t
ffs_alloccgblk(struct inode * ip,struct buf * bp,daddr_t bpref,int flags)1153 ffs_alloccgblk(struct inode *ip, struct buf *bp, daddr_t bpref, int flags)
1154 {
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);
1162 
1163 	KASSERT(mutex_owned(&ip->i_ump->um_lock));
1164 
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);
1172 		/*
1173 		 * if the requested block is available, use it
1174 		 */
1175 		if (ffs_isblock(fs, blksfree, ffs_fragstoblks(fs, bno)))
1176 			goto gotit;
1177 		/*
1178 		 * if the requested data block isn't available and we are
1179 		 * trying to allocate a contiguous file, return an error.
1180 		 */
1181 		if ((flags & (B_CONTIG | B_METAONLY)) == B_CONTIG)
1182 			return (0);
1183 	}
1184 
1185 	/*
1186 	 * Take the next available block in this cylinder group.
1187 	 */
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);
1210 	}
1211 	fs->fs_fmod = 1;
1212 	cg = ufs_rw32(cgp->cg_cgx, needswap);
1213 	blkno = cgbase(fs, cg) + bno;
1214 	return (blkno);
1215 }
1216 
1217 /*
1218  * Determine whether an inode can be allocated.
1219  *
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.
1225  */
1226 static daddr_t
ffs_nodealloccg(struct inode * ip,int cg,daddr_t ipref,int mode,int flags)1227 ffs_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode, int flags)
1228 {
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);
1239 
1240 	KASSERT(mutex_owned(&ump->um_lock));
1241 	UFS_WAPBL_JLOCK_ASSERT(ip->i_ump->um_mountp);
1242 
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;
1256 
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;
1262 	}
1263 	/*
1264 	 * Check to see if we need to initialize more inodes.
1265 	 */
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)) {
1271 			/*
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.
1275 			 */
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;
1284 		}
1285 	}
1286 
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;
1296 	}
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 */
1311 		}
1312 	}
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");
1318 	}
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);
1324 	/*
1325 	 * Check to see if we need to initialize more inodes.
1326 	 */
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++) {
1332 			/*
1333 			 * Don't bother to swap, it's supposed to be
1334 			 * random, after all.
1335 			 */
1336 			dp2->di_gen = (cprng_fast32() & INT32_MAX) / 2 + 1;
1337 			dp2++;
1338 		}
1339 		initediblk += FFS_INOPB(fs);
1340 		cgp->cg_initediblk = ufs_rw32(initediblk, needswap);
1341 	}
1342 
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++;
1354 	}
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);
1369 }
1370 
1371 /*
1372  * Allocate a block or fragment.
1373  *
1374  * The specified block or fragment is removed from the
1375  * free map, possibly fragmenting a block in the process.
1376  *
1377  * This implementation should mirror fs_blkfree
1378  *
1379  * => um_lock not held on entry or exit
1380  */
1381 int
ffs_blkalloc(struct inode * ip,daddr_t bno,long size)1382 ffs_blkalloc(struct inode *ip, daddr_t bno, long size)
1383 {
1384 	int error;
1385 
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;
1390 
1391 	return ffs_blkalloc_ump(ip->i_ump, bno, size);
1392 }
1393 
1394 int
ffs_blkalloc_ump(struct ufsmount * ump,daddr_t bno,long size)1395 ffs_blkalloc_ump(struct ufsmount *ump, daddr_t bno, long size)
1396 {
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);
1404 
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);
1408 
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;
1414 	}
1415 	cgp = (struct cg *)bp->b_data;
1416 	if (!cg_chkmagic(cgp, needswap)) {
1417 		brelse(bp, 0);
1418 		return EIO;
1419 	}
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);
1424 
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;
1432 		}
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);
1440 
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;
1447 			}
1448 		}
1449 		/*
1450 		 * if a complete block is being split, account for it
1451 		 */
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--;
1461 		}
1462 		/*
1463 		 * decrement the counts associated with the old frags
1464 		 */
1465 		blk = blkmap(fs, blksfree, bbase);
1466 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
1467 		/*
1468 		 * allocate the fragment
1469 		 */
1470 		for (i = 0; i < frags; i++) {
1471 			clrbit(blksfree, cgbno + i);
1472 		}
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;
1476 		/*
1477 		 * add back in counts associated with the new frags
1478 		 */
1479 		blk = blkmap(fs, blksfree, bbase);
1480 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
1481 	}
1482 	fs->fs_fmod = 1;
1483 	ACTIVECG_CLR(fs, cg);
1484 	mutex_exit(&ump->um_lock);
1485 	bdwrite(bp);
1486 	return 0;
1487 }
1488 
1489 /*
1490  * Free a block or fragment.
1491  *
1492  * The specified block or fragment is placed back in the
1493  * free map. If a fragment is deallocated, a possible
1494  * block reassembly is checked.
1495  *
1496  * => um_lock not held on entry or exit
1497  */
1498 static void
ffs_blkfree_cg(struct fs * fs,struct vnode * devvp,daddr_t bno,long size)1499 ffs_blkfree_cg(struct fs *fs, struct vnode *devvp, daddr_t bno, long size)
1500 {
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);
1509 
1510 	KASSERT(!devvp_is_snapshot);
1511 
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));
1517 
1518 	error = bread(devvp, cgblkno, (int)fs->fs_cgsize,
1519 	    B_MODIFY, &bp);
1520 	if (error) {
1521 		return;
1522 	}
1523 	cgp = (struct cg *)bp->b_data;
1524 	if (!cg_chkmagic(cgp, needswap)) {
1525 		brelse(bp, 0);
1526 		return;
1527 	}
1528 
1529 	ffs_blkfree_common(ump, fs, dev, bp, bno, size, devvp_is_snapshot);
1530 
1531 	bdwrite(bp);
1532 }
1533 
1534 struct discardopdata {
1535 	struct work wk; /* must be first */
1536 	struct vnode *devvp;
1537 	daddr_t bno;
1538 	long size;
1539 };
1540 
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? */
1551 };
1552 
1553 static void
ffs_blkfree_td(struct fs * fs,struct discardopdata * td)1554 ffs_blkfree_td(struct fs *fs, struct discardopdata *td)
1555 {
1556 	struct mount *mp = spec_node_getmountedfs(td->devvp);
1557 	long todo;
1558 	int error;
1559 
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;
1568 		}
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;
1573 	}
1574 }
1575 
1576 static void
ffs_discardcb(struct work * wk,void * arg)1577 ffs_discardcb(struct work *wk, void *arg)
1578 {
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
1586 
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
1591 
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
1601 
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);
1609 }
1610 
1611 void *
ffs_discard_init(struct vnode * devvp,struct fs * fs)1612 ffs_discard_init(struct vnode *devvp, struct fs *fs)
1613 {
1614 	struct discarddata *ts;
1615 	int error;
1616 
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;
1623 	}
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;
1630 }
1631 
1632 void
ffs_discard_finish(void * vts,int flags)1633 ffs_discard_finish(void *vts, int flags)
1634 {
1635 	struct discarddata *ts = vts;
1636 	struct discardopdata *td = NULL;
1637 	int res = 0;
1638 
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));
1644 	}
1645 	mutex_exit(&ts->wqlk);
1646 	if (res)
1647 		printf("ffs_discarddata drain timeout\n");
1648 
1649 	mutex_enter(&ts->entrylk);
1650 	if (ts->entry) {
1651 		td = ts->entry;
1652 		ts->entry = NULL;
1653 	}
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));
1662 	}
1663 
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));
1669 }
1670 
1671 void
ffs_blkfree(struct fs * fs,struct vnode * devvp,daddr_t bno,long size,ino_t inum)1672 ffs_blkfree(struct fs *fs, struct vnode *devvp, daddr_t bno, long size,
1673     ino_t inum)
1674 {
1675 	struct ufsmount *ump;
1676 	int error;
1677 	dev_t dev;
1678 	struct discarddata *ts;
1679 	struct discardopdata *td;
1680 
1681 	dev = devvp->v_rdev;
1682 	ump = VFSTOUFS(spec_node_getmountedfs(devvp));
1683 	if (ffs_snapblkfree(fs, devvp, bno, size, inum))
1684 		return;
1685 
1686 	error = ffs_check_bad_allocation(__func__, fs, bno, size, dev, inum);
1687 	if (error)
1688 		return;
1689 
1690 	if (!ump->um_discarddata) {
1691 		ffs_blkfree_cg(fs, devvp, bno, size);
1692 		return;
1693 	}
1694 
1695 #ifdef TRIMDEBUG
1696 	printf("blkfree(%" PRId64 ",%ld)\n", bno, size);
1697 #endif
1698 	ts = ump->um_discarddata;
1699 	td = NULL;
1700 
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;
1715 			}
1716 			size = 0; /* mark done */
1717 		}
1718 		ts->entry = NULL;
1719 	}
1720 	mutex_exit(&ts->entrylk);
1721 
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);
1730 	}
1731 	if (!size)
1732 		return;
1733 
1734 	td = kmem_alloc(sizeof(*td), KM_SLEEP);
1735 	td->devvp = devvp;
1736 	td->bno = bno;
1737 	td->size = size;
1738 
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;
1747 		}
1748 		mutex_exit(&ts->entrylk);
1749 	}
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);
1758 	}
1759 }
1760 
1761 /*
1762  * Free a block or fragment from a snapshot cg copy.
1763  *
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.
1767  *
1768  * => um_lock not held on entry or exit
1769  */
1770 void
ffs_blkfree_snap(struct fs * fs,struct vnode * devvp,daddr_t bno,long size,ino_t inum)1771 ffs_blkfree_snap(struct fs *fs, struct vnode *devvp, daddr_t bno, long size,
1772     ino_t inum)
1773 {
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);
1782 
1783 	KASSERT(devvp_is_snapshot);
1784 
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));
1789 
1790 	error = ffs_check_bad_allocation(__func__, fs, bno, size, dev, inum);
1791 	if (error)
1792 		return;
1793 
1794 	error = bread(devvp, cgblkno, (int)fs->fs_cgsize,
1795 	    B_MODIFY, &bp);
1796 	if (error) {
1797 		return;
1798 	}
1799 	cgp = (struct cg *)bp->b_data;
1800 	if (!cg_chkmagic(cgp, needswap)) {
1801 		brelse(bp, 0);
1802 		return;
1803 	}
1804 
1805 	ffs_blkfree_common(ump, fs, dev, bp, bno, size, devvp_is_snapshot);
1806 
1807 	bdwrite(bp);
1808 }
1809 
1810 static void
ffs_blkfree_common(struct ufsmount * ump,struct fs * fs,dev_t dev,struct buf * bp,daddr_t bno,long size,bool devvp_is_snapshot)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)
1813 {
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);
1819 
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;
1835 			}
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");
1839 		}
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);
1855 		}
1856 	} else {
1857 		bbase = cgbno - ffs_fragnum(fs, cgbno);
1858 		/*
1859 		 * decrement the counts associated with the old frags
1860 		 */
1861 		blk = blkmap(fs, blksfree, bbase);
1862 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
1863 		/*
1864 		 * deallocate the fragment
1865 		 */
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");
1874 			}
1875 			setbit(blksfree, cgbno + i);
1876 		}
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;
1880 		/*
1881 		 * add back in counts associated with the new frags
1882 		 */
1883 		blk = blkmap(fs, blksfree, bbase);
1884 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
1885 		/*
1886 		 * if a complete block has been reassembled, account for it
1887 		 */
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);
1907 			}
1908 		}
1909 	}
1910 	fs->fs_fmod = 1;
1911 	ACTIVECG_CLR(fs, cg);
1912 	mutex_exit(&ump->um_lock);
1913 }
1914 
1915 /*
1916  * Free an inode.
1917  */
1918 int
ffs_vfree(struct vnode * vp,ino_t ino,int mode)1919 ffs_vfree(struct vnode *vp, ino_t ino, int mode)
1920 {
1921 
1922 	return ffs_freefile(vp->v_mount, ino, mode);
1923 }
1924 
1925 /*
1926  * Do the actual free operation.
1927  * The specified inode is placed back in the free map.
1928  *
1929  * => um_lock not held on entry or exit
1930  */
1931 int
ffs_freefile(struct mount * mp,ino_t ino,int mode)1932 ffs_freefile(struct mount *mp, ino_t ino, int mode)
1933 {
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);
1943 
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));
1948 
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);
1956 	}
1957 	cgp = (struct cg *)bp->b_data;
1958 	if (!cg_chkmagic(cgp, needswap)) {
1959 		brelse(bp, 0);
1960 		return (0);
1961 	}
1962 
1963 	ffs_freefile_common(ump, fs, dev, bp, ino, mode, false);
1964 
1965 	bdwrite(bp);
1966 
1967 	return 0;
1968 }
1969 
1970 int
ffs_freefile_snap(struct fs * fs,struct vnode * devvp,ino_t ino,int mode)1971 ffs_freefile_snap(struct fs *fs, struct vnode *devvp, ino_t ino, int mode)
1972 {
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);
1980 
1981 	KASSERT(devvp->v_type != VBLK);
1982 
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);
1995 	}
1996 	cgp = (struct cg *)bp->b_data;
1997 	if (!cg_chkmagic(cgp, needswap)) {
1998 		brelse(bp, 0);
1999 		return (0);
2000 	}
2001 	ffs_freefile_common(ump, fs, dev, bp, ino, mode, true);
2002 
2003 	bdwrite(bp);
2004 
2005 	return 0;
2006 }
2007 
2008 static void
ffs_freefile_common(struct ufsmount * ump,struct fs * fs,dev_t dev,struct buf * bp,ino_t ino,int mode,bool devvp_is_snapshot)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)
2011 {
2012 	int cg;
2013 	struct cg *cgp;
2014 	u_int8_t *inosused;
2015 	const int needswap = UFS_FSNEEDSWAP(fs);
2016 
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");
2031 	}
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--;
2046 	}
2047 	fs->fs_fmod = 1;
2048 	ACTIVECG_CLR(fs, cg);
2049 	mutex_exit(&ump->um_lock);
2050 }
2051 
2052 /*
2053  * Check to see if a file is free.
2054  */
2055 int
ffs_checkfreefile(struct fs * fs,struct vnode * devvp,ino_t ino)2056 ffs_checkfreefile(struct fs *fs, struct vnode *devvp, ino_t ino)
2057 {
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);
2064 
2065 	KASSERT(devvp_is_snapshot);
2066 
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;
2076 	}
2077 	cgp = (struct cg *)bp->b_data;
2078 	if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
2079 		brelse(bp, 0);
2080 		return 1;
2081 	}
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;
2087 }
2088 
2089 /*
2090  * Find a block of the specified size in the specified cylinder group.
2091  *
2092  * It is a panic if a request is made to find a block if none are
2093  * available.
2094  */
2095 static int32_t
ffs_mapsearch(struct fs * fs,struct cg * cgp,daddr_t bpref,int allocsiz)2096 ffs_mapsearch(struct fs *fs, struct cg *cgp, daddr_t bpref, int allocsiz)
2097 {
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);
2104 
2105 	/* KASSERT(mutex_owned(&ump->um_lock)); */
2106 
2107 	/*
2108 	 * find the fragment by searching through the free block
2109 	 * map for an appropriate bit pattern
2110 	 */
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 */
2139 		}
2140 	}
2141 	bno = (start + len - loc) * NBBY;
2142 	cgp->cg_frotor = ufs_rw32(bno, needswap);
2143 	/*
2144 	 * found the byte in the map
2145 	 * sift through the bits to find the selected frag
2146 	 */
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;
2157 		}
2158 	}
2159 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
2160 	panic("ffs_alloccg: block not in map");
2161 	/* return (-1); */
2162 }
2163 
2164 /*
2165  * Fserr prints the name of a file system with an error diagnostic.
2166  *
2167  * The form of the error message is:
2168  *	fs: error message
2169  */
2170 static void
ffs_fserr(struct fs * fs,kauth_cred_t cred,const char * cp)2171 ffs_fserr(struct fs *fs, kauth_cred_t cred, const char *cp)
2172 {
2173 	KASSERT(cred != NULL);
2174 
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);
2183 	}
2184 }
2185