ffs_alloc.c revision 1.164 1 /* $NetBSD: ffs_alloc.c,v 1.164 2019/04/14 15:55:24 kardel 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.164 2019/04/14 15:55:24 kardel 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/kauth.h>
86 #include <sys/kernel.h>
87 #include <sys/mount.h>
88 #include <sys/proc.h>
89 #include <sys/syslog.h>
90 #include <sys/vnode.h>
91 #include <sys/wapbl.h>
92 #include <sys/cprng.h>
93
94 #include <miscfs/specfs/specdev.h>
95 #include <ufs/ufs/quota.h>
96 #include <ufs/ufs/ufsmount.h>
97 #include <ufs/ufs/inode.h>
98 #include <ufs/ufs/ufs_extern.h>
99 #include <ufs/ufs/ufs_bswap.h>
100 #include <ufs/ufs/ufs_wapbl.h>
101
102 #include <ufs/ffs/fs.h>
103 #include <ufs/ffs/ffs_extern.h>
104
105 #ifdef UVM_PAGE_TRKOWN
106 #include <uvm/uvm.h>
107 #endif
108
109 static daddr_t ffs_alloccg(struct inode *, int, daddr_t, int, int, int);
110 static daddr_t ffs_alloccgblk(struct inode *, struct buf *, daddr_t, int, int);
111 static ino_t ffs_dirpref(struct inode *);
112 static daddr_t ffs_fragextend(struct inode *, int, daddr_t, int, int);
113 static void ffs_fserr(struct fs *, kauth_cred_t, const char *);
114 static daddr_t ffs_hashalloc(struct inode *, int, daddr_t, int, int, int,
115 daddr_t (*)(struct inode *, int, daddr_t, int, int, int));
116 static daddr_t ffs_nodealloccg(struct inode *, int, daddr_t, int, int, int);
117 static int32_t ffs_mapsearch(struct fs *, struct cg *,
118 daddr_t, int);
119 static void ffs_blkfree_common(struct ufsmount *, struct fs *, dev_t, struct buf *,
120 daddr_t, long, bool);
121 static void ffs_freefile_common(struct ufsmount *, struct fs *, dev_t, struct buf *, ino_t,
122 int, bool);
123
124 /* if 1, changes in optimalization strategy are logged */
125 int ffs_log_changeopt = 0;
126
127 /* in ffs_tables.c */
128 extern const int inside[], around[];
129 extern const u_char * const fragtbl[];
130
131 /* Basic consistency check for block allocations */
132 static int
133 ffs_check_bad_allocation(const char *func, struct fs *fs, daddr_t bno,
134 long size, dev_t dev, ino_t inum)
135 {
136 if ((u_int)size > fs->fs_bsize || ffs_fragoff(fs, size) != 0 ||
137 ffs_fragnum(fs, bno) + ffs_numfrags(fs, size) > fs->fs_frag) {
138 panic("%s: bad size: dev = 0x%llx, bno = %" PRId64
139 " bsize = %d, size = %ld, fs = %s", func,
140 (long long)dev, bno, fs->fs_bsize, size, fs->fs_fsmnt);
141 }
142
143 if (bno >= fs->fs_size) {
144 printf("%s: bad block %" PRId64 ", ino %llu\n", func, bno,
145 (unsigned long long)inum);
146 ffs_fserr(fs, NOCRED, "bad block");
147 return EINVAL;
148 }
149 return 0;
150 }
151
152 /*
153 * Allocate a block in the file system.
154 *
155 * The size of the requested block is given, which must be some
156 * multiple of fs_fsize and <= fs_bsize.
157 * A preference may be optionally specified. If a preference is given
158 * the following hierarchy is used to allocate a block:
159 * 1) allocate the requested block.
160 * 2) allocate a rotationally optimal block in the same cylinder.
161 * 3) allocate a block in the same cylinder group.
162 * 4) quadradically rehash into other cylinder groups, until an
163 * available block is located.
164 * If no block preference is given the following hierarchy is used
165 * to allocate a block:
166 * 1) allocate a block in the cylinder group that contains the
167 * inode for the file.
168 * 2) quadradically rehash into other cylinder groups, until an
169 * available block is located.
170 *
171 * => called with um_lock held
172 * => releases um_lock before returning
173 */
174 int
175 ffs_alloc(struct inode *ip, daddr_t lbn, daddr_t bpref, int size,
176 int flags, kauth_cred_t cred, daddr_t *bnp)
177 {
178 struct ufsmount *ump;
179 struct fs *fs;
180 daddr_t bno;
181 int cg;
182 #if defined(QUOTA) || defined(QUOTA2)
183 int error;
184 #endif
185
186 fs = ip->i_fs;
187 ump = ip->i_ump;
188
189 KASSERT(mutex_owned(&ump->um_lock));
190
191 #ifdef UVM_PAGE_TRKOWN
192
193 /*
194 * Sanity-check that allocations within the file size
195 * do not allow other threads to read the stale contents
196 * of newly allocated blocks.
197 * Usually pages will exist to cover the new allocation.
198 * There is an optimization in ffs_write() where we skip
199 * creating pages if several conditions are met:
200 * - the file must not be mapped (in any user address space).
201 * - the write must cover whole pages and whole blocks.
202 * If those conditions are not met then pages must exist and
203 * be locked by the current thread.
204 */
205
206 struct vnode *vp = ITOV(ip);
207 if (vp->v_type == VREG &&
208 ffs_lblktosize(fs, (voff_t)lbn) < round_page(vp->v_size) &&
209 ((vp->v_vflag & VV_MAPPED) != 0 || (size & PAGE_MASK) != 0 ||
210 ffs_blkoff(fs, size) != 0)) {
211 struct vm_page *pg;
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 && pg->owner_tag != NULL &&
220 pg->owner == curproc->p_pid &&
221 pg->lowner == curlwp->l_lid));
222 off += PAGE_SIZE;
223 }
224 mutex_exit(uobj->vmobjlock);
225 }
226 #endif
227
228 *bnp = 0;
229
230 KASSERTMSG((cred != NOCRED), "missing credential");
231 KASSERTMSG(((u_int)size <= fs->fs_bsize),
232 "bad size: dev = 0x%llx, bsize = %d, size = %d, fs = %s",
233 (unsigned long long)ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
234 KASSERTMSG((ffs_fragoff(fs, size) == 0),
235 "bad size: dev = 0x%llx, bsize = %d, size = %d, fs = %s",
236 (unsigned long long)ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
237
238 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
239 goto nospace;
240 if (freespace(fs, fs->fs_minfree) <= 0 &&
241 kauth_authorize_system(cred, KAUTH_SYSTEM_FS_RESERVEDSPACE, 0, NULL,
242 NULL, NULL) != 0)
243 goto nospace;
244 #if defined(QUOTA) || defined(QUOTA2)
245 mutex_exit(&ump->um_lock);
246 if ((error = chkdq(ip, btodb(size), cred, 0)) != 0)
247 return (error);
248 mutex_enter(&ump->um_lock);
249 #endif
250
251 if (bpref >= fs->fs_size)
252 bpref = 0;
253 if (bpref == 0)
254 cg = ino_to_cg(fs, ip->i_number);
255 else
256 cg = dtog(fs, bpref);
257 bno = ffs_hashalloc(ip, cg, bpref, size, 0, flags, ffs_alloccg);
258 if (bno > 0) {
259 DIP_ADD(ip, blocks, btodb(size));
260 ip->i_flag |= IN_CHANGE | IN_UPDATE;
261 *bnp = bno;
262 return (0);
263 }
264 #if defined(QUOTA) || defined(QUOTA2)
265 /*
266 * Restore user's disk quota because allocation failed.
267 */
268 (void) chkdq(ip, -btodb(size), cred, FORCE);
269 #endif
270 if (flags & B_CONTIG) {
271 /*
272 * XXX ump->um_lock handling is "suspect" at best.
273 * For the case where ffs_hashalloc() fails early
274 * in the B_CONTIG case we reach here with um_lock
275 * already unlocked, so we can't release it again
276 * like in the normal error path. See kern/39206.
277 *
278 *
279 * Fail silently - it's up to our caller to report
280 * errors.
281 */
282 return (ENOSPC);
283 }
284 nospace:
285 mutex_exit(&ump->um_lock);
286 ffs_fserr(fs, cred, "file system full");
287 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
288 return (ENOSPC);
289 }
290
291 /*
292 * Reallocate a fragment to a bigger size
293 *
294 * The number and size of the old block is given, and a preference
295 * and new size is also specified. The allocator attempts to extend
296 * the original block. Failing that, the regular block allocator is
297 * invoked to get an appropriate block.
298 *
299 * => called with um_lock held
300 * => return with um_lock released
301 */
302 int
303 ffs_realloccg(struct inode *ip, daddr_t lbprev, daddr_t bpref, int osize,
304 int nsize, kauth_cred_t cred, struct buf **bpp, daddr_t *blknop)
305 {
306 struct ufsmount *ump;
307 struct fs *fs;
308 struct buf *bp;
309 int cg, request, error;
310 daddr_t bprev, bno;
311
312 fs = ip->i_fs;
313 ump = ip->i_ump;
314
315 KASSERT(mutex_owned(&ump->um_lock));
316
317 #ifdef UVM_PAGE_TRKOWN
318
319 /*
320 * Sanity-check that allocations within the file size
321 * do not allow other threads to read the stale contents
322 * of newly allocated blocks.
323 * Unlike in ffs_alloc(), here pages must always exist
324 * for such allocations, because only the last block of a file
325 * can be a fragment and ffs_write() will reallocate the
326 * fragment to the new size using ufs_balloc_range(),
327 * which always creates pages to cover blocks it allocates.
328 */
329
330 if (ITOV(ip)->v_type == VREG) {
331 struct vm_page *pg;
332 struct uvm_object *uobj = &ITOV(ip)->v_uobj;
333 voff_t off = trunc_page(ffs_lblktosize(fs, lbprev));
334 voff_t endoff = round_page(ffs_lblktosize(fs, lbprev) + osize);
335
336 mutex_enter(uobj->vmobjlock);
337 while (off < endoff) {
338 pg = uvm_pagelookup(uobj, off);
339 KASSERT(pg->owner == curproc->p_pid &&
340 pg->lowner == curlwp->l_lid);
341 off += PAGE_SIZE;
342 }
343 mutex_exit(uobj->vmobjlock);
344 }
345 #endif
346
347 KASSERTMSG((cred != NOCRED), "missing credential");
348 KASSERTMSG(((u_int)osize <= fs->fs_bsize),
349 "bad size: dev=0x%llx, bsize=%d, osize=%d, nsize=%d, fs=%s",
350 (unsigned long long)ip->i_dev, fs->fs_bsize, osize, nsize,
351 fs->fs_fsmnt);
352 KASSERTMSG((ffs_fragoff(fs, osize) == 0),
353 "bad size: dev=0x%llx, bsize=%d, osize=%d, nsize=%d, fs=%s",
354 (unsigned long long)ip->i_dev, fs->fs_bsize, osize, nsize,
355 fs->fs_fsmnt);
356 KASSERTMSG(((u_int)nsize <= fs->fs_bsize),
357 "bad size: dev=0x%llx, bsize=%d, osize=%d, nsize=%d, fs=%s",
358 (unsigned long long)ip->i_dev, fs->fs_bsize, osize, nsize,
359 fs->fs_fsmnt);
360 KASSERTMSG((ffs_fragoff(fs, nsize) == 0),
361 "bad size: dev=0x%llx, bsize=%d, osize=%d, nsize=%d, fs=%s",
362 (unsigned long long)ip->i_dev, fs->fs_bsize, osize, nsize,
363 fs->fs_fsmnt);
364
365 if (freespace(fs, fs->fs_minfree) <= 0 &&
366 kauth_authorize_system(cred, KAUTH_SYSTEM_FS_RESERVEDSPACE, 0, NULL,
367 NULL, NULL) != 0) {
368 mutex_exit(&ump->um_lock);
369 goto nospace;
370 }
371 if (fs->fs_magic == FS_UFS2_MAGIC)
372 bprev = ufs_rw64(ip->i_ffs2_db[lbprev], UFS_FSNEEDSWAP(fs));
373 else
374 bprev = ufs_rw32(ip->i_ffs1_db[lbprev], UFS_FSNEEDSWAP(fs));
375
376 if (bprev == 0) {
377 panic("%s: bad bprev: dev = 0x%llx, bsize = %d, bprev = %"
378 PRId64 ", fs = %s", __func__,
379 (unsigned long long)ip->i_dev, fs->fs_bsize, bprev,
380 fs->fs_fsmnt);
381 }
382 mutex_exit(&ump->um_lock);
383
384 /*
385 * Allocate the extra space in the buffer.
386 */
387 if (bpp != NULL &&
388 (error = bread(ITOV(ip), lbprev, osize, 0, &bp)) != 0) {
389 return (error);
390 }
391 #if defined(QUOTA) || defined(QUOTA2)
392 if ((error = chkdq(ip, btodb(nsize - osize), cred, 0)) != 0) {
393 if (bpp != NULL) {
394 brelse(bp, 0);
395 }
396 return (error);
397 }
398 #endif
399 /*
400 * Check for extension in the existing location.
401 */
402 cg = dtog(fs, bprev);
403 mutex_enter(&ump->um_lock);
404 if ((bno = ffs_fragextend(ip, cg, bprev, osize, nsize)) != 0) {
405 DIP_ADD(ip, blocks, btodb(nsize - osize));
406 ip->i_flag |= IN_CHANGE | IN_UPDATE;
407
408 if (bpp != NULL) {
409 if (bp->b_blkno != FFS_FSBTODB(fs, bno)) {
410 panic("%s: bad blockno %#llx != %#llx",
411 __func__, (unsigned long long) bp->b_blkno,
412 (unsigned long long)FFS_FSBTODB(fs, bno));
413 }
414 allocbuf(bp, nsize, 1);
415 memset((char *)bp->b_data + osize, 0, nsize - osize);
416 mutex_enter(bp->b_objlock);
417 KASSERT(!cv_has_waiters(&bp->b_done));
418 bp->b_oflags |= BO_DONE;
419 mutex_exit(bp->b_objlock);
420 *bpp = bp;
421 }
422 if (blknop != NULL) {
423 *blknop = bno;
424 }
425 return (0);
426 }
427 /*
428 * Allocate a new disk location.
429 */
430 if (bpref >= fs->fs_size)
431 bpref = 0;
432 switch ((int)fs->fs_optim) {
433 case FS_OPTSPACE:
434 /*
435 * Allocate an exact sized fragment. Although this makes
436 * best use of space, we will waste time relocating it if
437 * the file continues to grow. If the fragmentation is
438 * less than half of the minimum free reserve, we choose
439 * to begin optimizing for time.
440 */
441 request = nsize;
442 if (fs->fs_minfree < 5 ||
443 fs->fs_cstotal.cs_nffree >
444 fs->fs_dsize * fs->fs_minfree / (2 * 100))
445 break;
446
447 if (ffs_log_changeopt) {
448 log(LOG_NOTICE,
449 "%s: optimization changed from SPACE to TIME\n",
450 fs->fs_fsmnt);
451 }
452
453 fs->fs_optim = FS_OPTTIME;
454 break;
455 case FS_OPTTIME:
456 /*
457 * At this point we have discovered a file that is trying to
458 * grow a small fragment to a larger fragment. To save time,
459 * we allocate a full sized block, then free the unused portion.
460 * If the file continues to grow, the `ffs_fragextend' call
461 * above will be able to grow it in place without further
462 * copying. If aberrant programs cause disk fragmentation to
463 * grow within 2% of the free reserve, we choose to begin
464 * optimizing for space.
465 */
466 request = fs->fs_bsize;
467 if (fs->fs_cstotal.cs_nffree <
468 fs->fs_dsize * (fs->fs_minfree - 2) / 100)
469 break;
470
471 if (ffs_log_changeopt) {
472 log(LOG_NOTICE,
473 "%s: optimization changed from TIME to SPACE\n",
474 fs->fs_fsmnt);
475 }
476
477 fs->fs_optim = FS_OPTSPACE;
478 break;
479 default:
480 panic("%s: bad optim: dev = 0x%llx, optim = %d, fs = %s",
481 __func__, (unsigned long long)ip->i_dev, fs->fs_optim,
482 fs->fs_fsmnt);
483 /* NOTREACHED */
484 }
485 bno = ffs_hashalloc(ip, cg, bpref, request, nsize, 0, ffs_alloccg);
486 if (bno > 0) {
487 /*
488 * Use forced deallocation registration, we can't handle
489 * failure here. This is safe, as this place is ever hit
490 * maximum once per write operation, when fragment is extended
491 * to longer fragment, or a full block.
492 */
493 if ((ip->i_ump->um_mountp->mnt_wapbl) &&
494 (ITOV(ip)->v_type != VREG)) {
495 /* this should never fail */
496 error = UFS_WAPBL_REGISTER_DEALLOCATION_FORCE(
497 ip->i_ump->um_mountp, FFS_FSBTODB(fs, bprev),
498 osize);
499 if (error)
500 panic("ffs_realloccg: dealloc registration failed");
501 } else {
502 ffs_blkfree(fs, ip->i_devvp, bprev, (long)osize,
503 ip->i_number);
504 }
505 DIP_ADD(ip, blocks, btodb(nsize - osize));
506 ip->i_flag |= IN_CHANGE | IN_UPDATE;
507 if (bpp != NULL) {
508 bp->b_blkno = FFS_FSBTODB(fs, bno);
509 allocbuf(bp, nsize, 1);
510 memset((char *)bp->b_data + osize, 0, (u_int)nsize - osize);
511 mutex_enter(bp->b_objlock);
512 KASSERT(!cv_has_waiters(&bp->b_done));
513 bp->b_oflags |= BO_DONE;
514 mutex_exit(bp->b_objlock);
515 *bpp = bp;
516 }
517 if (blknop != NULL) {
518 *blknop = bno;
519 }
520 return (0);
521 }
522 mutex_exit(&ump->um_lock);
523
524 #if defined(QUOTA) || defined(QUOTA2)
525 /*
526 * Restore user's disk quota because allocation failed.
527 */
528 (void) chkdq(ip, -btodb(nsize - osize), cred, FORCE);
529 #endif
530 if (bpp != NULL) {
531 brelse(bp, 0);
532 }
533
534 nospace:
535 /*
536 * no space available
537 */
538 ffs_fserr(fs, cred, "file system full");
539 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
540 return (ENOSPC);
541 }
542
543 /*
544 * Allocate an inode in the file system.
545 *
546 * If allocating a directory, use ffs_dirpref to select the inode.
547 * If allocating in a directory, the following hierarchy is followed:
548 * 1) allocate the preferred inode.
549 * 2) allocate an inode in the same cylinder group.
550 * 3) quadradically rehash into other cylinder groups, until an
551 * available inode is located.
552 * If no inode preference is given the following hierarchy is used
553 * to allocate an inode:
554 * 1) allocate an inode in cylinder group 0.
555 * 2) quadradically rehash into other cylinder groups, until an
556 * available inode is located.
557 *
558 * => um_lock not held upon entry or return
559 */
560 int
561 ffs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred, ino_t *inop)
562 {
563 struct ufsmount *ump;
564 struct inode *pip;
565 struct fs *fs;
566 ino_t ino, ipref;
567 int cg, error;
568
569 UFS_WAPBL_JUNLOCK_ASSERT(pvp->v_mount);
570
571 pip = VTOI(pvp);
572 fs = pip->i_fs;
573 ump = pip->i_ump;
574
575 error = UFS_WAPBL_BEGIN(pvp->v_mount);
576 if (error) {
577 return error;
578 }
579 mutex_enter(&ump->um_lock);
580 if (fs->fs_cstotal.cs_nifree == 0)
581 goto noinodes;
582
583 if ((mode & IFMT) == IFDIR)
584 ipref = ffs_dirpref(pip);
585 else
586 ipref = pip->i_number;
587 if (ipref >= fs->fs_ncg * fs->fs_ipg)
588 ipref = 0;
589 cg = ino_to_cg(fs, ipref);
590 /*
591 * Track number of dirs created one after another
592 * in a same cg without intervening by files.
593 */
594 if ((mode & IFMT) == IFDIR) {
595 if (fs->fs_contigdirs[cg] < 255)
596 fs->fs_contigdirs[cg]++;
597 } else {
598 if (fs->fs_contigdirs[cg] > 0)
599 fs->fs_contigdirs[cg]--;
600 }
601 ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode, 0, 0, ffs_nodealloccg);
602 if (ino == 0)
603 goto noinodes;
604 UFS_WAPBL_END(pvp->v_mount);
605 *inop = ino;
606 return 0;
607
608 noinodes:
609 mutex_exit(&ump->um_lock);
610 UFS_WAPBL_END(pvp->v_mount);
611 ffs_fserr(fs, cred, "out of inodes");
612 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
613 return ENOSPC;
614 }
615
616 /*
617 * Find a cylinder group in which to place a directory.
618 *
619 * The policy implemented by this algorithm is to allocate a
620 * directory inode in the same cylinder group as its parent
621 * directory, but also to reserve space for its files inodes
622 * and data. Restrict the number of directories which may be
623 * allocated one after another in the same cylinder group
624 * without intervening allocation of files.
625 *
626 * If we allocate a first level directory then force allocation
627 * in another cylinder group.
628 */
629 static ino_t
630 ffs_dirpref(struct inode *pip)
631 {
632 register struct fs *fs;
633 int cg, prefcg;
634 int64_t dirsize, cgsize, curdsz;
635 int avgifree, avgbfree, avgndir;
636 int minifree, minbfree, maxndir;
637 int mincg, minndir;
638 int maxcontigdirs;
639
640 KASSERT(mutex_owned(&pip->i_ump->um_lock));
641
642 fs = pip->i_fs;
643
644 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
645 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
646 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
647
648 /*
649 * Force allocation in another cg if creating a first level dir.
650 */
651 if (ITOV(pip)->v_vflag & VV_ROOT) {
652 prefcg = cprng_fast32() % fs->fs_ncg;
653 mincg = prefcg;
654 minndir = fs->fs_ipg;
655 for (cg = prefcg; cg < fs->fs_ncg; cg++)
656 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
657 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
658 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
659 mincg = cg;
660 minndir = fs->fs_cs(fs, cg).cs_ndir;
661 }
662 for (cg = 0; cg < prefcg; cg++)
663 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
664 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
665 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
666 mincg = cg;
667 minndir = fs->fs_cs(fs, cg).cs_ndir;
668 }
669 return ((ino_t)(fs->fs_ipg * mincg));
670 }
671
672 /*
673 * Count various limits which used for
674 * optimal allocation of a directory inode.
675 * Try cylinder groups with >75% avgifree and avgbfree.
676 * Avoid cylinder groups with no free blocks or inodes as that
677 * triggers an I/O-expensive cylinder group scan.
678 */
679 maxndir = uimin(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
680 minifree = avgifree - avgifree / 4;
681 if (minifree < 1)
682 minifree = 1;
683 minbfree = avgbfree - avgbfree / 4;
684 if (minbfree < 1)
685 minbfree = 1;
686 cgsize = (int64_t)fs->fs_fsize * fs->fs_fpg;
687 dirsize = (int64_t)fs->fs_avgfilesize * fs->fs_avgfpdir;
688 if (avgndir != 0) {
689 curdsz = (cgsize - (int64_t)avgbfree * fs->fs_bsize) / avgndir;
690 if (dirsize < curdsz)
691 dirsize = curdsz;
692 }
693 if (cgsize < dirsize * 255)
694 maxcontigdirs = (avgbfree * fs->fs_bsize) / dirsize;
695 else
696 maxcontigdirs = 255;
697 if (fs->fs_avgfpdir > 0)
698 maxcontigdirs = uimin(maxcontigdirs,
699 fs->fs_ipg / fs->fs_avgfpdir);
700 if (maxcontigdirs == 0)
701 maxcontigdirs = 1;
702
703 /*
704 * Limit number of dirs in one cg and reserve space for
705 * regular files, but only if we have no deficit in
706 * inodes or space.
707 */
708 prefcg = ino_to_cg(fs, pip->i_number);
709 for (cg = prefcg; cg < fs->fs_ncg; cg++)
710 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
711 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
712 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
713 if (fs->fs_contigdirs[cg] < maxcontigdirs)
714 return ((ino_t)(fs->fs_ipg * cg));
715 }
716 for (cg = 0; cg < prefcg; cg++)
717 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
718 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
719 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
720 if (fs->fs_contigdirs[cg] < maxcontigdirs)
721 return ((ino_t)(fs->fs_ipg * cg));
722 }
723 /*
724 * This is a backstop when we are deficient in space.
725 */
726 for (cg = prefcg; cg < fs->fs_ncg; cg++)
727 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
728 return ((ino_t)(fs->fs_ipg * cg));
729 for (cg = 0; cg < prefcg; cg++)
730 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
731 break;
732 return ((ino_t)(fs->fs_ipg * cg));
733 }
734
735 /*
736 * Select the desired position for the next block in a file. The file is
737 * logically divided into sections. The first section is composed of the
738 * direct blocks. Each additional section contains fs_maxbpg blocks.
739 *
740 * If no blocks have been allocated in the first section, the policy is to
741 * request a block in the same cylinder group as the inode that describes
742 * the file. If no blocks have been allocated in any other section, the
743 * policy is to place the section in a cylinder group with a greater than
744 * average number of free blocks. An appropriate cylinder group is found
745 * by using a rotor that sweeps the cylinder groups. When a new group of
746 * blocks is needed, the sweep begins in the cylinder group following the
747 * cylinder group from which the previous allocation was made. The sweep
748 * continues until a cylinder group with greater than the average number
749 * of free blocks is found. If the allocation is for the first block in an
750 * indirect block, the information on the previous allocation is unavailable;
751 * here a best guess is made based upon the logical block number being
752 * allocated.
753 *
754 * If a section is already partially allocated, the policy is to
755 * contiguously allocate fs_maxcontig blocks. The end of one of these
756 * contiguous blocks and the beginning of the next is laid out
757 * contigously if possible.
758 *
759 * => um_lock held on entry and exit
760 */
761 daddr_t
762 ffs_blkpref_ufs1(struct inode *ip, daddr_t lbn, int indx, int flags,
763 int32_t *bap /* XXX ondisk32 */)
764 {
765 struct fs *fs;
766 int cg;
767 int avgbfree, startcg;
768
769 KASSERT(mutex_owned(&ip->i_ump->um_lock));
770
771 fs = ip->i_fs;
772
773 /*
774 * If allocating a contiguous file with B_CONTIG, use the hints
775 * in the inode extentions to return the desired block.
776 *
777 * For metadata (indirect blocks) return the address of where
778 * the first indirect block resides - we'll scan for the next
779 * available slot if we need to allocate more than one indirect
780 * block. For data, return the address of the actual block
781 * relative to the address of the first data block.
782 */
783 if (flags & B_CONTIG) {
784 KASSERT(ip->i_ffs_first_data_blk != 0);
785 KASSERT(ip->i_ffs_first_indir_blk != 0);
786 if (flags & B_METAONLY)
787 return ip->i_ffs_first_indir_blk;
788 else
789 return ip->i_ffs_first_data_blk + ffs_blkstofrags(fs, lbn);
790 }
791
792 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
793 if (lbn < UFS_NDADDR + FFS_NINDIR(fs)) {
794 cg = ino_to_cg(fs, ip->i_number);
795 return (cgbase(fs, cg) + fs->fs_frag);
796 }
797 /*
798 * Find a cylinder with greater than average number of
799 * unused data blocks.
800 */
801 if (indx == 0 || bap[indx - 1] == 0)
802 startcg =
803 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
804 else
805 startcg = dtog(fs,
806 ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
807 startcg %= fs->fs_ncg;
808 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
809 for (cg = startcg; cg < fs->fs_ncg; cg++)
810 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
811 return (cgbase(fs, cg) + fs->fs_frag);
812 }
813 for (cg = 0; cg < startcg; cg++)
814 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
815 return (cgbase(fs, cg) + fs->fs_frag);
816 }
817 return (0);
818 }
819 /*
820 * We just always try to lay things out contiguously.
821 */
822 return ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
823 }
824
825 daddr_t
826 ffs_blkpref_ufs2(struct inode *ip, daddr_t lbn, int indx, int flags,
827 int64_t *bap)
828 {
829 struct fs *fs;
830 int cg;
831 int avgbfree, startcg;
832
833 KASSERT(mutex_owned(&ip->i_ump->um_lock));
834
835 fs = ip->i_fs;
836
837 /*
838 * If allocating a contiguous file with B_CONTIG, use the hints
839 * in the inode extentions to return the desired block.
840 *
841 * For metadata (indirect blocks) return the address of where
842 * the first indirect block resides - we'll scan for the next
843 * available slot if we need to allocate more than one indirect
844 * block. For data, return the address of the actual block
845 * relative to the address of the first data block.
846 */
847 if (flags & B_CONTIG) {
848 KASSERT(ip->i_ffs_first_data_blk != 0);
849 KASSERT(ip->i_ffs_first_indir_blk != 0);
850 if (flags & B_METAONLY)
851 return ip->i_ffs_first_indir_blk;
852 else
853 return ip->i_ffs_first_data_blk + ffs_blkstofrags(fs, lbn);
854 }
855
856 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
857 if (lbn < UFS_NDADDR + FFS_NINDIR(fs)) {
858 cg = ino_to_cg(fs, ip->i_number);
859 return (cgbase(fs, cg) + fs->fs_frag);
860 }
861 /*
862 * Find a cylinder with greater than average number of
863 * unused data blocks.
864 */
865 if (indx == 0 || bap[indx - 1] == 0)
866 startcg =
867 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
868 else
869 startcg = dtog(fs,
870 ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
871 startcg %= fs->fs_ncg;
872 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
873 for (cg = startcg; cg < fs->fs_ncg; cg++)
874 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
875 return (cgbase(fs, cg) + fs->fs_frag);
876 }
877 for (cg = 0; cg < startcg; cg++)
878 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
879 return (cgbase(fs, cg) + fs->fs_frag);
880 }
881 return (0);
882 }
883 /*
884 * We just always try to lay things out contiguously.
885 */
886 return ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
887 }
888
889
890 /*
891 * Implement the cylinder overflow algorithm.
892 *
893 * The policy implemented by this algorithm is:
894 * 1) allocate the block in its requested cylinder group.
895 * 2) quadradically rehash on the cylinder group number.
896 * 3) brute force search for a free block.
897 *
898 * => called with um_lock held
899 * => returns with um_lock released on success, held on failure
900 * (*allocator releases lock on success, retains lock on failure)
901 */
902 /*VARARGS5*/
903 static daddr_t
904 ffs_hashalloc(struct inode *ip, int cg, daddr_t pref,
905 int size /* size for data blocks, mode for inodes */,
906 int realsize,
907 int flags,
908 daddr_t (*allocator)(struct inode *, int, daddr_t, int, int, int))
909 {
910 struct fs *fs;
911 daddr_t result;
912 int i, icg = cg;
913
914 fs = ip->i_fs;
915 /*
916 * 1: preferred cylinder group
917 */
918 result = (*allocator)(ip, cg, pref, size, realsize, flags);
919 if (result)
920 return (result);
921
922 if (flags & B_CONTIG)
923 return (result);
924 /*
925 * 2: quadratic rehash
926 */
927 for (i = 1; i < fs->fs_ncg; i *= 2) {
928 cg += i;
929 if (cg >= fs->fs_ncg)
930 cg -= fs->fs_ncg;
931 result = (*allocator)(ip, cg, 0, size, realsize, flags);
932 if (result)
933 return (result);
934 }
935 /*
936 * 3: brute force search
937 * Note that we start at i == 2, since 0 was checked initially,
938 * and 1 is always checked in the quadratic rehash.
939 */
940 cg = (icg + 2) % fs->fs_ncg;
941 for (i = 2; i < fs->fs_ncg; i++) {
942 result = (*allocator)(ip, cg, 0, size, realsize, flags);
943 if (result)
944 return (result);
945 cg++;
946 if (cg == fs->fs_ncg)
947 cg = 0;
948 }
949 return (0);
950 }
951
952 /*
953 * Determine whether a fragment can be extended.
954 *
955 * Check to see if the necessary fragments are available, and
956 * if they are, allocate them.
957 *
958 * => called with um_lock held
959 * => returns with um_lock released on success, held on failure
960 */
961 static daddr_t
962 ffs_fragextend(struct inode *ip, int cg, daddr_t bprev, int osize, int nsize)
963 {
964 struct ufsmount *ump;
965 struct fs *fs;
966 struct cg *cgp;
967 struct buf *bp;
968 daddr_t bno;
969 int frags, bbase;
970 int i, error;
971 u_int8_t *blksfree;
972
973 fs = ip->i_fs;
974 ump = ip->i_ump;
975
976 KASSERT(mutex_owned(&ump->um_lock));
977
978 if (fs->fs_cs(fs, cg).cs_nffree < ffs_numfrags(fs, nsize - osize))
979 return (0);
980 frags = ffs_numfrags(fs, nsize);
981 bbase = ffs_fragnum(fs, bprev);
982 if (bbase > ffs_fragnum(fs, (bprev + frags - 1))) {
983 /* cannot extend across a block boundary */
984 return (0);
985 }
986 mutex_exit(&ump->um_lock);
987 error = bread(ip->i_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
988 (int)fs->fs_cgsize, B_MODIFY, &bp);
989 if (error)
990 goto fail;
991 cgp = (struct cg *)bp->b_data;
992 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs)))
993 goto fail;
994 cgp->cg_old_time = ufs_rw32(time_second, UFS_FSNEEDSWAP(fs));
995 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
996 (fs->fs_old_flags & FS_FLAGS_UPDATED))
997 cgp->cg_time = ufs_rw64(time_second, UFS_FSNEEDSWAP(fs));
998 bno = dtogd(fs, bprev);
999 blksfree = cg_blksfree(cgp, UFS_FSNEEDSWAP(fs));
1000 for (i = ffs_numfrags(fs, osize); i < frags; i++)
1001 if (isclr(blksfree, bno + i))
1002 goto fail;
1003 /*
1004 * the current fragment can be extended
1005 * deduct the count on fragment being extended into
1006 * increase the count on the remaining fragment (if any)
1007 * allocate the extended piece
1008 */
1009 for (i = frags; i < fs->fs_frag - bbase; i++)
1010 if (isclr(blksfree, bno + i))
1011 break;
1012 ufs_add32(cgp->cg_frsum[i - ffs_numfrags(fs, osize)], -1, UFS_FSNEEDSWAP(fs));
1013 if (i != frags)
1014 ufs_add32(cgp->cg_frsum[i - frags], 1, UFS_FSNEEDSWAP(fs));
1015 mutex_enter(&ump->um_lock);
1016 for (i = ffs_numfrags(fs, osize); i < frags; i++) {
1017 clrbit(blksfree, bno + i);
1018 ufs_add32(cgp->cg_cs.cs_nffree, -1, UFS_FSNEEDSWAP(fs));
1019 fs->fs_cstotal.cs_nffree--;
1020 fs->fs_cs(fs, cg).cs_nffree--;
1021 }
1022 fs->fs_fmod = 1;
1023 ACTIVECG_CLR(fs, cg);
1024 mutex_exit(&ump->um_lock);
1025 bdwrite(bp);
1026 return (bprev);
1027
1028 fail:
1029 if (bp != NULL)
1030 brelse(bp, 0);
1031 mutex_enter(&ump->um_lock);
1032 return (0);
1033 }
1034
1035 /*
1036 * Determine whether a block can be allocated.
1037 *
1038 * Check to see if a block of the appropriate size is available,
1039 * and if it is, allocate it.
1040 */
1041 static daddr_t
1042 ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size, int realsize,
1043 int flags)
1044 {
1045 struct ufsmount *ump;
1046 struct fs *fs = ip->i_fs;
1047 struct cg *cgp;
1048 struct buf *bp;
1049 int32_t bno;
1050 daddr_t blkno;
1051 int error, frags, allocsiz, i;
1052 u_int8_t *blksfree;
1053 const int needswap = UFS_FSNEEDSWAP(fs);
1054
1055 ump = ip->i_ump;
1056
1057 KASSERT(mutex_owned(&ump->um_lock));
1058
1059 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
1060 return (0);
1061 mutex_exit(&ump->um_lock);
1062 error = bread(ip->i_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
1063 (int)fs->fs_cgsize, B_MODIFY, &bp);
1064 if (error)
1065 goto fail;
1066 cgp = (struct cg *)bp->b_data;
1067 if (!cg_chkmagic(cgp, needswap) ||
1068 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize))
1069 goto fail;
1070 cgp->cg_old_time = ufs_rw32(time_second, needswap);
1071 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1072 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1073 cgp->cg_time = ufs_rw64(time_second, needswap);
1074 if (size == fs->fs_bsize) {
1075 mutex_enter(&ump->um_lock);
1076 blkno = ffs_alloccgblk(ip, bp, bpref, realsize, flags);
1077 ACTIVECG_CLR(fs, cg);
1078 mutex_exit(&ump->um_lock);
1079
1080 /*
1081 * If actually needed size is lower, free the extra blocks now.
1082 * This is safe to call here, there is no outside reference
1083 * to this block yet. It is not necessary to keep um_lock
1084 * locked.
1085 */
1086 if (realsize != 0 && realsize < size) {
1087 ffs_blkfree_common(ip->i_ump, ip->i_fs,
1088 ip->i_devvp->v_rdev,
1089 bp, blkno + ffs_numfrags(fs, realsize),
1090 (long)(size - realsize), false);
1091 }
1092
1093 bdwrite(bp);
1094 return (blkno);
1095 }
1096 /*
1097 * check to see if any fragments are already available
1098 * allocsiz is the size which will be allocated, hacking
1099 * it down to a smaller size if necessary
1100 */
1101 blksfree = cg_blksfree(cgp, needswap);
1102 frags = ffs_numfrags(fs, size);
1103 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1104 if (cgp->cg_frsum[allocsiz] != 0)
1105 break;
1106 if (allocsiz == fs->fs_frag) {
1107 /*
1108 * no fragments were available, so a block will be
1109 * allocated, and hacked up
1110 */
1111 if (cgp->cg_cs.cs_nbfree == 0)
1112 goto fail;
1113 mutex_enter(&ump->um_lock);
1114 blkno = ffs_alloccgblk(ip, bp, bpref, realsize, flags);
1115 bno = dtogd(fs, blkno);
1116 for (i = frags; i < fs->fs_frag; i++)
1117 setbit(blksfree, bno + i);
1118 i = fs->fs_frag - frags;
1119 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1120 fs->fs_cstotal.cs_nffree += i;
1121 fs->fs_cs(fs, cg).cs_nffree += i;
1122 fs->fs_fmod = 1;
1123 ufs_add32(cgp->cg_frsum[i], 1, needswap);
1124 ACTIVECG_CLR(fs, cg);
1125 mutex_exit(&ump->um_lock);
1126 bdwrite(bp);
1127 return (blkno);
1128 }
1129 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1130 #if 0
1131 /*
1132 * XXX fvdl mapsearch will panic, and never return -1
1133 * also: returning NULL as daddr_t ?
1134 */
1135 if (bno < 0)
1136 goto fail;
1137 #endif
1138 for (i = 0; i < frags; i++)
1139 clrbit(blksfree, bno + i);
1140 mutex_enter(&ump->um_lock);
1141 ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
1142 fs->fs_cstotal.cs_nffree -= frags;
1143 fs->fs_cs(fs, cg).cs_nffree -= frags;
1144 fs->fs_fmod = 1;
1145 ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
1146 if (frags != allocsiz)
1147 ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
1148 blkno = cgbase(fs, cg) + bno;
1149 ACTIVECG_CLR(fs, cg);
1150 mutex_exit(&ump->um_lock);
1151 bdwrite(bp);
1152 return blkno;
1153
1154 fail:
1155 if (bp != NULL)
1156 brelse(bp, 0);
1157 mutex_enter(&ump->um_lock);
1158 return (0);
1159 }
1160
1161 /*
1162 * Allocate a block in a cylinder group.
1163 *
1164 * This algorithm implements the following policy:
1165 * 1) allocate the requested block.
1166 * 2) allocate a rotationally optimal block in the same cylinder.
1167 * 3) allocate the next available block on the block rotor for the
1168 * specified cylinder group.
1169 * Note that this routine only allocates fs_bsize blocks; these
1170 * blocks may be fragmented by the routine that allocates them.
1171 */
1172 static daddr_t
1173 ffs_alloccgblk(struct inode *ip, struct buf *bp, daddr_t bpref, int realsize,
1174 int flags)
1175 {
1176 struct fs *fs = ip->i_fs;
1177 struct cg *cgp;
1178 int cg;
1179 daddr_t blkno;
1180 int32_t bno;
1181 u_int8_t *blksfree;
1182 const int needswap = UFS_FSNEEDSWAP(fs);
1183
1184 KASSERT(mutex_owned(&ip->i_ump->um_lock));
1185
1186 cgp = (struct cg *)bp->b_data;
1187 blksfree = cg_blksfree(cgp, needswap);
1188 if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
1189 bpref = ufs_rw32(cgp->cg_rotor, needswap);
1190 } else {
1191 bpref = ffs_blknum(fs, bpref);
1192 bno = dtogd(fs, bpref);
1193 /*
1194 * if the requested block is available, use it
1195 */
1196 if (ffs_isblock(fs, blksfree, ffs_fragstoblks(fs, bno)))
1197 goto gotit;
1198 /*
1199 * if the requested data block isn't available and we are
1200 * trying to allocate a contiguous file, return an error.
1201 */
1202 if ((flags & (B_CONTIG | B_METAONLY)) == B_CONTIG)
1203 return (0);
1204 }
1205
1206 /*
1207 * Take the next available block in this cylinder group.
1208 */
1209 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1210 #if 0
1211 /*
1212 * XXX jdolecek ffs_mapsearch() succeeds or panics
1213 */
1214 if (bno < 0)
1215 return (0);
1216 #endif
1217 cgp->cg_rotor = ufs_rw32(bno, needswap);
1218 gotit:
1219 blkno = ffs_fragstoblks(fs, bno);
1220 ffs_clrblock(fs, blksfree, blkno);
1221 ffs_clusteracct(fs, cgp, blkno, -1);
1222 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
1223 fs->fs_cstotal.cs_nbfree--;
1224 fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
1225 if ((fs->fs_magic == FS_UFS1_MAGIC) &&
1226 ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
1227 int cylno;
1228 cylno = old_cbtocylno(fs, bno);
1229 KASSERT(cylno >= 0);
1230 KASSERT(cylno < fs->fs_old_ncyl);
1231 KASSERT(old_cbtorpos(fs, bno) >= 0);
1232 KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, bno) < fs->fs_old_nrpos);
1233 ufs_add16(old_cg_blks(fs, cgp, cylno, needswap)[old_cbtorpos(fs, bno)], -1,
1234 needswap);
1235 ufs_add32(old_cg_blktot(cgp, needswap)[cylno], -1, needswap);
1236 }
1237 fs->fs_fmod = 1;
1238 cg = ufs_rw32(cgp->cg_cgx, needswap);
1239 blkno = cgbase(fs, cg) + bno;
1240 return (blkno);
1241 }
1242
1243 /*
1244 * Determine whether an inode can be allocated.
1245 *
1246 * Check to see if an inode is available, and if it is,
1247 * allocate it using the following policy:
1248 * 1) allocate the requested inode.
1249 * 2) allocate the next available inode after the requested
1250 * inode in the specified cylinder group.
1251 */
1252 static daddr_t
1253 ffs_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode, int realsize,
1254 int flags)
1255 {
1256 struct ufsmount *ump = ip->i_ump;
1257 struct fs *fs = ip->i_fs;
1258 struct cg *cgp;
1259 struct buf *bp, *ibp;
1260 u_int8_t *inosused;
1261 int error, start, len, loc, map, i;
1262 int32_t initediblk, maxiblk, irotor;
1263 daddr_t nalloc;
1264 struct ufs2_dinode *dp2;
1265 const int needswap = UFS_FSNEEDSWAP(fs);
1266
1267 KASSERT(mutex_owned(&ump->um_lock));
1268 UFS_WAPBL_JLOCK_ASSERT(ip->i_ump->um_mountp);
1269
1270 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1271 return (0);
1272 mutex_exit(&ump->um_lock);
1273 ibp = NULL;
1274 if (fs->fs_magic == FS_UFS2_MAGIC) {
1275 initediblk = -1;
1276 } else {
1277 initediblk = fs->fs_ipg;
1278 }
1279 maxiblk = initediblk;
1280
1281 retry:
1282 error = bread(ip->i_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
1283 (int)fs->fs_cgsize, B_MODIFY, &bp);
1284 if (error)
1285 goto fail;
1286 cgp = (struct cg *)bp->b_data;
1287 if (!cg_chkmagic(cgp, needswap) || cgp->cg_cs.cs_nifree == 0)
1288 goto fail;
1289
1290 if (ibp != NULL &&
1291 initediblk != ufs_rw32(cgp->cg_initediblk, needswap)) {
1292 /* Another thread allocated more inodes so we retry the test. */
1293 brelse(ibp, 0);
1294 ibp = NULL;
1295 }
1296 /*
1297 * Check to see if we need to initialize more inodes.
1298 */
1299 if (fs->fs_magic == FS_UFS2_MAGIC && ibp == NULL) {
1300 initediblk = ufs_rw32(cgp->cg_initediblk, needswap);
1301 maxiblk = initediblk;
1302 nalloc = fs->fs_ipg - ufs_rw32(cgp->cg_cs.cs_nifree, needswap);
1303 if (nalloc + FFS_INOPB(fs) > initediblk &&
1304 initediblk < ufs_rw32(cgp->cg_niblk, needswap)) {
1305 /*
1306 * We have to release the cg buffer here to prevent
1307 * a deadlock when reading the inode block will
1308 * run a copy-on-write that might use this cg.
1309 */
1310 brelse(bp, 0);
1311 bp = NULL;
1312 error = ffs_getblk(ip->i_devvp, FFS_FSBTODB(fs,
1313 ino_to_fsba(fs, cg * fs->fs_ipg + initediblk)),
1314 FFS_NOBLK, fs->fs_bsize, false, &ibp);
1315 if (error)
1316 goto fail;
1317
1318 maxiblk += FFS_INOPB(fs);
1319
1320 goto retry;
1321 }
1322 }
1323
1324 cgp->cg_old_time = ufs_rw32(time_second, needswap);
1325 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1326 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1327 cgp->cg_time = ufs_rw64(time_second, needswap);
1328 inosused = cg_inosused(cgp, needswap);
1329
1330 if (ipref) {
1331 ipref %= fs->fs_ipg;
1332 /* safeguard to stay in (to be) allocated range */
1333 if (ipref < maxiblk && isclr(inosused, ipref))
1334 goto gotit;
1335 }
1336
1337 irotor = ufs_rw32(cgp->cg_irotor, needswap);
1338
1339 KASSERTMSG(irotor < initediblk, "%s: allocation botch: cg=%d, irotor %d"
1340 " out of bounds, initediblk=%d",
1341 __func__, cg, irotor, initediblk);
1342
1343 start = irotor / NBBY;
1344 len = howmany(maxiblk - irotor, NBBY);
1345 loc = skpc(0xff, len, &inosused[start]);
1346 if (loc == 0) {
1347 len = start + 1;
1348 start = 0;
1349 loc = skpc(0xff, len, &inosused[0]);
1350 if (loc == 0) {
1351 panic("%s: map corrupted: cg=%d, irotor=%d, fs=%s",
1352 __func__, cg, ufs_rw32(cgp->cg_irotor, needswap),
1353 fs->fs_fsmnt);
1354 /* NOTREACHED */
1355 }
1356 }
1357 i = start + len - loc;
1358 map = inosused[i] ^ 0xff;
1359 if (map == 0) {
1360 panic("%s: block not in map: fs=%s", __func__, fs->fs_fsmnt);
1361 }
1362
1363 ipref = i * NBBY + ffs(map) - 1;
1364
1365 cgp->cg_irotor = ufs_rw32(ipref, needswap);
1366
1367 gotit:
1368 KASSERTMSG(ipref < maxiblk, "%s: allocation botch: cg=%d attempt to "
1369 "allocate inode index %d beyond max allocated index %d"
1370 " of %d inodes/cg",
1371 __func__, cg, (int)ipref, maxiblk, cgp->cg_niblk);
1372
1373 UFS_WAPBL_REGISTER_INODE(ip->i_ump->um_mountp, cg * fs->fs_ipg + ipref,
1374 mode);
1375 /*
1376 * Check to see if we need to initialize more inodes.
1377 */
1378 if (ibp != NULL) {
1379 KASSERT(initediblk == ufs_rw32(cgp->cg_initediblk, needswap));
1380 memset(ibp->b_data, 0, fs->fs_bsize);
1381 dp2 = (struct ufs2_dinode *)(ibp->b_data);
1382 for (i = 0; i < FFS_INOPB(fs); i++) {
1383 /*
1384 * Don't bother to swap, it's supposed to be
1385 * random, after all.
1386 */
1387 dp2->di_gen = (cprng_fast32() & INT32_MAX) / 2 + 1;
1388 dp2++;
1389 }
1390 initediblk += FFS_INOPB(fs);
1391 cgp->cg_initediblk = ufs_rw32(initediblk, needswap);
1392 }
1393
1394 mutex_enter(&ump->um_lock);
1395 ACTIVECG_CLR(fs, cg);
1396 setbit(inosused, ipref);
1397 ufs_add32(cgp->cg_cs.cs_nifree, -1, needswap);
1398 fs->fs_cstotal.cs_nifree--;
1399 fs->fs_cs(fs, cg).cs_nifree--;
1400 fs->fs_fmod = 1;
1401 if ((mode & IFMT) == IFDIR) {
1402 ufs_add32(cgp->cg_cs.cs_ndir, 1, needswap);
1403 fs->fs_cstotal.cs_ndir++;
1404 fs->fs_cs(fs, cg).cs_ndir++;
1405 }
1406 mutex_exit(&ump->um_lock);
1407 if (ibp != NULL) {
1408 bwrite(ibp);
1409 bwrite(bp);
1410 } else
1411 bdwrite(bp);
1412 return (cg * fs->fs_ipg + ipref);
1413 fail:
1414 if (bp != NULL)
1415 brelse(bp, 0);
1416 if (ibp != NULL)
1417 brelse(ibp, 0);
1418 mutex_enter(&ump->um_lock);
1419 return (0);
1420 }
1421
1422 /*
1423 * Allocate a block or fragment.
1424 *
1425 * The specified block or fragment is removed from the
1426 * free map, possibly fragmenting a block in the process.
1427 *
1428 * This implementation should mirror fs_blkfree
1429 *
1430 * => um_lock not held on entry or exit
1431 */
1432 int
1433 ffs_blkalloc(struct inode *ip, daddr_t bno, long size)
1434 {
1435 int error;
1436
1437 error = ffs_check_bad_allocation(__func__, ip->i_fs, bno, size,
1438 ip->i_dev, ip->i_uid);
1439 if (error)
1440 return error;
1441
1442 return ffs_blkalloc_ump(ip->i_ump, bno, size);
1443 }
1444
1445 int
1446 ffs_blkalloc_ump(struct ufsmount *ump, daddr_t bno, long size)
1447 {
1448 struct fs *fs = ump->um_fs;
1449 struct cg *cgp;
1450 struct buf *bp;
1451 int32_t fragno, cgbno;
1452 int i, error, cg, blk, frags, bbase;
1453 u_int8_t *blksfree;
1454 const int needswap = UFS_FSNEEDSWAP(fs);
1455
1456 KASSERT((u_int)size <= fs->fs_bsize && ffs_fragoff(fs, size) == 0 &&
1457 ffs_fragnum(fs, bno) + ffs_numfrags(fs, size) <= fs->fs_frag);
1458 KASSERT(bno < fs->fs_size);
1459
1460 cg = dtog(fs, bno);
1461 error = bread(ump->um_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
1462 (int)fs->fs_cgsize, B_MODIFY, &bp);
1463 if (error) {
1464 return error;
1465 }
1466 cgp = (struct cg *)bp->b_data;
1467 if (!cg_chkmagic(cgp, needswap)) {
1468 brelse(bp, 0);
1469 return EIO;
1470 }
1471 cgp->cg_old_time = ufs_rw32(time_second, needswap);
1472 cgp->cg_time = ufs_rw64(time_second, needswap);
1473 cgbno = dtogd(fs, bno);
1474 blksfree = cg_blksfree(cgp, needswap);
1475
1476 mutex_enter(&ump->um_lock);
1477 if (size == fs->fs_bsize) {
1478 fragno = ffs_fragstoblks(fs, cgbno);
1479 if (!ffs_isblock(fs, blksfree, fragno)) {
1480 mutex_exit(&ump->um_lock);
1481 brelse(bp, 0);
1482 return EBUSY;
1483 }
1484 ffs_clrblock(fs, blksfree, fragno);
1485 ffs_clusteracct(fs, cgp, fragno, -1);
1486 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
1487 fs->fs_cstotal.cs_nbfree--;
1488 fs->fs_cs(fs, cg).cs_nbfree--;
1489 } else {
1490 bbase = cgbno - ffs_fragnum(fs, cgbno);
1491
1492 frags = ffs_numfrags(fs, size);
1493 for (i = 0; i < frags; i++) {
1494 if (isclr(blksfree, cgbno + i)) {
1495 mutex_exit(&ump->um_lock);
1496 brelse(bp, 0);
1497 return EBUSY;
1498 }
1499 }
1500 /*
1501 * if a complete block is being split, account for it
1502 */
1503 fragno = ffs_fragstoblks(fs, bbase);
1504 if (ffs_isblock(fs, blksfree, fragno)) {
1505 ufs_add32(cgp->cg_cs.cs_nffree, fs->fs_frag, needswap);
1506 fs->fs_cstotal.cs_nffree += fs->fs_frag;
1507 fs->fs_cs(fs, cg).cs_nffree += fs->fs_frag;
1508 ffs_clusteracct(fs, cgp, fragno, -1);
1509 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
1510 fs->fs_cstotal.cs_nbfree--;
1511 fs->fs_cs(fs, cg).cs_nbfree--;
1512 }
1513 /*
1514 * decrement the counts associated with the old frags
1515 */
1516 blk = blkmap(fs, blksfree, bbase);
1517 ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
1518 /*
1519 * allocate the fragment
1520 */
1521 for (i = 0; i < frags; i++) {
1522 clrbit(blksfree, cgbno + i);
1523 }
1524 ufs_add32(cgp->cg_cs.cs_nffree, -i, needswap);
1525 fs->fs_cstotal.cs_nffree -= i;
1526 fs->fs_cs(fs, cg).cs_nffree -= i;
1527 /*
1528 * add back in counts associated with the new frags
1529 */
1530 blk = blkmap(fs, blksfree, bbase);
1531 ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
1532 }
1533 fs->fs_fmod = 1;
1534 ACTIVECG_CLR(fs, cg);
1535 mutex_exit(&ump->um_lock);
1536 bdwrite(bp);
1537 return 0;
1538 }
1539
1540 /*
1541 * Free a block or fragment.
1542 *
1543 * The specified block or fragment is placed back in the
1544 * free map. If a fragment is deallocated, a possible
1545 * block reassembly is checked.
1546 *
1547 * => um_lock not held on entry or exit
1548 */
1549 static void
1550 ffs_blkfree_cg(struct fs *fs, struct vnode *devvp, daddr_t bno, long size)
1551 {
1552 struct cg *cgp;
1553 struct buf *bp;
1554 struct ufsmount *ump;
1555 daddr_t cgblkno;
1556 int error, cg;
1557 dev_t dev;
1558 const bool devvp_is_snapshot = (devvp->v_type != VBLK);
1559 const int needswap = UFS_FSNEEDSWAP(fs);
1560
1561 KASSERT(!devvp_is_snapshot);
1562
1563 cg = dtog(fs, bno);
1564 dev = devvp->v_rdev;
1565 ump = VFSTOUFS(spec_node_getmountedfs(devvp));
1566 KASSERT(fs == ump->um_fs);
1567 cgblkno = FFS_FSBTODB(fs, cgtod(fs, cg));
1568
1569 error = bread(devvp, cgblkno, (int)fs->fs_cgsize,
1570 B_MODIFY, &bp);
1571 if (error) {
1572 return;
1573 }
1574 cgp = (struct cg *)bp->b_data;
1575 if (!cg_chkmagic(cgp, needswap)) {
1576 brelse(bp, 0);
1577 return;
1578 }
1579
1580 ffs_blkfree_common(ump, fs, dev, bp, bno, size, devvp_is_snapshot);
1581
1582 bdwrite(bp);
1583 }
1584
1585 struct discardopdata {
1586 struct work wk; /* must be first */
1587 struct vnode *devvp;
1588 daddr_t bno;
1589 long size;
1590 };
1591
1592 struct discarddata {
1593 struct fs *fs;
1594 struct discardopdata *entry;
1595 long maxsize;
1596 kmutex_t entrylk;
1597 struct workqueue *wq;
1598 int wqcnt, wqdraining;
1599 kmutex_t wqlk;
1600 kcondvar_t wqcv;
1601 /* timer for flush? */
1602 };
1603
1604 static void
1605 ffs_blkfree_td(struct fs *fs, struct discardopdata *td)
1606 {
1607 struct mount *mp = spec_node_getmountedfs(td->devvp);
1608 long todo;
1609 int error;
1610
1611 while (td->size) {
1612 todo = uimin(td->size,
1613 ffs_lfragtosize(fs, (fs->fs_frag - ffs_fragnum(fs, td->bno))));
1614 error = UFS_WAPBL_BEGIN(mp);
1615 if (error) {
1616 printf("ffs: failed to begin wapbl transaction"
1617 " for discard: %d\n", error);
1618 break;
1619 }
1620 ffs_blkfree_cg(fs, td->devvp, td->bno, todo);
1621 UFS_WAPBL_END(mp);
1622 td->bno += ffs_numfrags(fs, todo);
1623 td->size -= todo;
1624 }
1625 }
1626
1627 static void
1628 ffs_discardcb(struct work *wk, void *arg)
1629 {
1630 struct discardopdata *td = (void *)wk;
1631 struct discarddata *ts = arg;
1632 struct fs *fs = ts->fs;
1633 off_t start, len;
1634 #ifdef TRIMDEBUG
1635 int error;
1636 #endif
1637
1638 /* like FSBTODB but emits bytes; XXX move to fs.h */
1639 #ifndef FFS_FSBTOBYTES
1640 #define FFS_FSBTOBYTES(fs, b) ((b) << (fs)->fs_fshift)
1641 #endif
1642
1643 start = FFS_FSBTOBYTES(fs, td->bno);
1644 len = td->size;
1645 #ifdef TRIMDEBUG
1646 error =
1647 #endif
1648 VOP_FDISCARD(td->devvp, start, len);
1649 #ifdef TRIMDEBUG
1650 printf("trim(%" PRId64 ",%ld):%d\n", td->bno, td->size, error);
1651 #endif
1652
1653 ffs_blkfree_td(fs, td);
1654 kmem_free(td, sizeof(*td));
1655 mutex_enter(&ts->wqlk);
1656 ts->wqcnt--;
1657 if (ts->wqdraining && !ts->wqcnt)
1658 cv_signal(&ts->wqcv);
1659 mutex_exit(&ts->wqlk);
1660 }
1661
1662 void *
1663 ffs_discard_init(struct vnode *devvp, struct fs *fs)
1664 {
1665 struct discarddata *ts;
1666 int error;
1667
1668 ts = kmem_zalloc(sizeof (*ts), KM_SLEEP);
1669 error = workqueue_create(&ts->wq, "trimwq", ffs_discardcb, ts,
1670 PRI_USER, IPL_NONE, 0);
1671 if (error) {
1672 kmem_free(ts, sizeof (*ts));
1673 return NULL;
1674 }
1675 mutex_init(&ts->entrylk, MUTEX_DEFAULT, IPL_NONE);
1676 mutex_init(&ts->wqlk, MUTEX_DEFAULT, IPL_NONE);
1677 cv_init(&ts->wqcv, "trimwqcv");
1678 ts->maxsize = 100*1024; /* XXX */
1679 ts->fs = fs;
1680 return ts;
1681 }
1682
1683 void
1684 ffs_discard_finish(void *vts, int flags)
1685 {
1686 struct discarddata *ts = vts;
1687 struct discardopdata *td = NULL;
1688
1689 /* wait for workqueue to drain */
1690 mutex_enter(&ts->wqlk);
1691 if (ts->wqcnt) {
1692 ts->wqdraining = 1;
1693 cv_wait(&ts->wqcv, &ts->wqlk);
1694 }
1695 mutex_exit(&ts->wqlk);
1696
1697 mutex_enter(&ts->entrylk);
1698 if (ts->entry) {
1699 td = ts->entry;
1700 ts->entry = NULL;
1701 }
1702 mutex_exit(&ts->entrylk);
1703 if (td) {
1704 /* XXX don't tell disk, its optional */
1705 ffs_blkfree_td(ts->fs, td);
1706 #ifdef TRIMDEBUG
1707 printf("finish(%" PRId64 ",%ld)\n", td->bno, td->size);
1708 #endif
1709 kmem_free(td, sizeof(*td));
1710 }
1711
1712 cv_destroy(&ts->wqcv);
1713 mutex_destroy(&ts->entrylk);
1714 mutex_destroy(&ts->wqlk);
1715 workqueue_destroy(ts->wq);
1716 kmem_free(ts, sizeof(*ts));
1717 }
1718
1719 void
1720 ffs_blkfree(struct fs *fs, struct vnode *devvp, daddr_t bno, long size,
1721 ino_t inum)
1722 {
1723 struct ufsmount *ump;
1724 int error;
1725 dev_t dev;
1726 struct discarddata *ts;
1727 struct discardopdata *td;
1728
1729 dev = devvp->v_rdev;
1730 ump = VFSTOUFS(spec_node_getmountedfs(devvp));
1731 if (ffs_snapblkfree(fs, devvp, bno, size, inum))
1732 return;
1733
1734 error = ffs_check_bad_allocation(__func__, fs, bno, size, dev, inum);
1735 if (error)
1736 return;
1737
1738 if (!ump->um_discarddata) {
1739 ffs_blkfree_cg(fs, devvp, bno, size);
1740 return;
1741 }
1742
1743 #ifdef TRIMDEBUG
1744 printf("blkfree(%" PRId64 ",%ld)\n", bno, size);
1745 #endif
1746 ts = ump->um_discarddata;
1747 td = NULL;
1748
1749 mutex_enter(&ts->entrylk);
1750 if (ts->entry) {
1751 td = ts->entry;
1752 /* ffs deallocs backwards, check for prepend only */
1753 if (td->bno == bno + ffs_numfrags(fs, size)
1754 && td->size + size <= ts->maxsize) {
1755 td->bno = bno;
1756 td->size += size;
1757 if (td->size < ts->maxsize) {
1758 #ifdef TRIMDEBUG
1759 printf("defer(%" PRId64 ",%ld)\n", td->bno, td->size);
1760 #endif
1761 mutex_exit(&ts->entrylk);
1762 return;
1763 }
1764 size = 0; /* mark done */
1765 }
1766 ts->entry = NULL;
1767 }
1768 mutex_exit(&ts->entrylk);
1769
1770 if (td) {
1771 #ifdef TRIMDEBUG
1772 printf("enq old(%" PRId64 ",%ld)\n", td->bno, td->size);
1773 #endif
1774 mutex_enter(&ts->wqlk);
1775 ts->wqcnt++;
1776 mutex_exit(&ts->wqlk);
1777 workqueue_enqueue(ts->wq, &td->wk, NULL);
1778 }
1779 if (!size)
1780 return;
1781
1782 td = kmem_alloc(sizeof(*td), KM_SLEEP);
1783 td->devvp = devvp;
1784 td->bno = bno;
1785 td->size = size;
1786
1787 if (td->size < ts->maxsize) { /* XXX always the case */
1788 mutex_enter(&ts->entrylk);
1789 if (!ts->entry) { /* possible race? */
1790 #ifdef TRIMDEBUG
1791 printf("defer(%" PRId64 ",%ld)\n", td->bno, td->size);
1792 #endif
1793 ts->entry = td;
1794 td = NULL;
1795 }
1796 mutex_exit(&ts->entrylk);
1797 }
1798 if (td) {
1799 #ifdef TRIMDEBUG
1800 printf("enq new(%" PRId64 ",%ld)\n", td->bno, td->size);
1801 #endif
1802 mutex_enter(&ts->wqlk);
1803 ts->wqcnt++;
1804 mutex_exit(&ts->wqlk);
1805 workqueue_enqueue(ts->wq, &td->wk, NULL);
1806 }
1807 }
1808
1809 /*
1810 * Free a block or fragment from a snapshot cg copy.
1811 *
1812 * The specified block or fragment is placed back in the
1813 * free map. If a fragment is deallocated, a possible
1814 * block reassembly is checked.
1815 *
1816 * => um_lock not held on entry or exit
1817 */
1818 void
1819 ffs_blkfree_snap(struct fs *fs, struct vnode *devvp, daddr_t bno, long size,
1820 ino_t inum)
1821 {
1822 struct cg *cgp;
1823 struct buf *bp;
1824 struct ufsmount *ump;
1825 daddr_t cgblkno;
1826 int error, cg;
1827 dev_t dev;
1828 const bool devvp_is_snapshot = (devvp->v_type != VBLK);
1829 const int needswap = UFS_FSNEEDSWAP(fs);
1830
1831 KASSERT(devvp_is_snapshot);
1832
1833 cg = dtog(fs, bno);
1834 dev = VTOI(devvp)->i_devvp->v_rdev;
1835 ump = VFSTOUFS(devvp->v_mount);
1836 cgblkno = ffs_fragstoblks(fs, cgtod(fs, cg));
1837
1838 error = ffs_check_bad_allocation(__func__, fs, bno, size, dev, inum);
1839 if (error)
1840 return;
1841
1842 error = bread(devvp, cgblkno, (int)fs->fs_cgsize,
1843 B_MODIFY, &bp);
1844 if (error) {
1845 return;
1846 }
1847 cgp = (struct cg *)bp->b_data;
1848 if (!cg_chkmagic(cgp, needswap)) {
1849 brelse(bp, 0);
1850 return;
1851 }
1852
1853 ffs_blkfree_common(ump, fs, dev, bp, bno, size, devvp_is_snapshot);
1854
1855 bdwrite(bp);
1856 }
1857
1858 static void
1859 ffs_blkfree_common(struct ufsmount *ump, struct fs *fs, dev_t dev,
1860 struct buf *bp, daddr_t bno, long size, bool devvp_is_snapshot)
1861 {
1862 struct cg *cgp;
1863 int32_t fragno, cgbno;
1864 int i, cg, blk, frags, bbase;
1865 u_int8_t *blksfree;
1866 const int needswap = UFS_FSNEEDSWAP(fs);
1867
1868 cg = dtog(fs, bno);
1869 cgp = (struct cg *)bp->b_data;
1870 cgp->cg_old_time = ufs_rw32(time_second, needswap);
1871 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1872 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1873 cgp->cg_time = ufs_rw64(time_second, needswap);
1874 cgbno = dtogd(fs, bno);
1875 blksfree = cg_blksfree(cgp, needswap);
1876 mutex_enter(&ump->um_lock);
1877 if (size == fs->fs_bsize) {
1878 fragno = ffs_fragstoblks(fs, cgbno);
1879 if (!ffs_isfreeblock(fs, blksfree, fragno)) {
1880 if (devvp_is_snapshot) {
1881 mutex_exit(&ump->um_lock);
1882 return;
1883 }
1884 panic("%s: freeing free block: dev = 0x%llx, block = %"
1885 PRId64 ", fs = %s", __func__,
1886 (unsigned long long)dev, bno, fs->fs_fsmnt);
1887 }
1888 ffs_setblock(fs, blksfree, fragno);
1889 ffs_clusteracct(fs, cgp, fragno, 1);
1890 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1891 fs->fs_cstotal.cs_nbfree++;
1892 fs->fs_cs(fs, cg).cs_nbfree++;
1893 if ((fs->fs_magic == FS_UFS1_MAGIC) &&
1894 ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
1895 i = old_cbtocylno(fs, cgbno);
1896 KASSERT(i >= 0);
1897 KASSERT(i < fs->fs_old_ncyl);
1898 KASSERT(old_cbtorpos(fs, cgbno) >= 0);
1899 KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, cgbno) < fs->fs_old_nrpos);
1900 ufs_add16(old_cg_blks(fs, cgp, i, needswap)[old_cbtorpos(fs, cgbno)], 1,
1901 needswap);
1902 ufs_add32(old_cg_blktot(cgp, needswap)[i], 1, needswap);
1903 }
1904 } else {
1905 bbase = cgbno - ffs_fragnum(fs, cgbno);
1906 /*
1907 * decrement the counts associated with the old frags
1908 */
1909 blk = blkmap(fs, blksfree, bbase);
1910 ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
1911 /*
1912 * deallocate the fragment
1913 */
1914 frags = ffs_numfrags(fs, size);
1915 for (i = 0; i < frags; i++) {
1916 if (isset(blksfree, cgbno + i)) {
1917 panic("%s: freeing free frag: "
1918 "dev = 0x%llx, block = %" PRId64
1919 ", fs = %s", __func__,
1920 (unsigned long long)dev, bno + i,
1921 fs->fs_fsmnt);
1922 }
1923 setbit(blksfree, cgbno + i);
1924 }
1925 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1926 fs->fs_cstotal.cs_nffree += i;
1927 fs->fs_cs(fs, cg).cs_nffree += i;
1928 /*
1929 * add back in counts associated with the new frags
1930 */
1931 blk = blkmap(fs, blksfree, bbase);
1932 ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
1933 /*
1934 * if a complete block has been reassembled, account for it
1935 */
1936 fragno = ffs_fragstoblks(fs, bbase);
1937 if (ffs_isblock(fs, blksfree, fragno)) {
1938 ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
1939 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1940 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1941 ffs_clusteracct(fs, cgp, fragno, 1);
1942 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1943 fs->fs_cstotal.cs_nbfree++;
1944 fs->fs_cs(fs, cg).cs_nbfree++;
1945 if ((fs->fs_magic == FS_UFS1_MAGIC) &&
1946 ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
1947 i = old_cbtocylno(fs, bbase);
1948 KASSERT(i >= 0);
1949 KASSERT(i < fs->fs_old_ncyl);
1950 KASSERT(old_cbtorpos(fs, bbase) >= 0);
1951 KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, bbase) < fs->fs_old_nrpos);
1952 ufs_add16(old_cg_blks(fs, cgp, i, needswap)[old_cbtorpos(fs,
1953 bbase)], 1, needswap);
1954 ufs_add32(old_cg_blktot(cgp, needswap)[i], 1, needswap);
1955 }
1956 }
1957 }
1958 fs->fs_fmod = 1;
1959 ACTIVECG_CLR(fs, cg);
1960 mutex_exit(&ump->um_lock);
1961 }
1962
1963 /*
1964 * Free an inode.
1965 */
1966 int
1967 ffs_vfree(struct vnode *vp, ino_t ino, int mode)
1968 {
1969
1970 return ffs_freefile(vp->v_mount, ino, mode);
1971 }
1972
1973 /*
1974 * Do the actual free operation.
1975 * The specified inode is placed back in the free map.
1976 *
1977 * => um_lock not held on entry or exit
1978 */
1979 int
1980 ffs_freefile(struct mount *mp, ino_t ino, int mode)
1981 {
1982 struct ufsmount *ump = VFSTOUFS(mp);
1983 struct fs *fs = ump->um_fs;
1984 struct vnode *devvp;
1985 struct cg *cgp;
1986 struct buf *bp;
1987 int error, cg;
1988 daddr_t cgbno;
1989 dev_t dev;
1990 const int needswap = UFS_FSNEEDSWAP(fs);
1991
1992 cg = ino_to_cg(fs, ino);
1993 devvp = ump->um_devvp;
1994 dev = devvp->v_rdev;
1995 cgbno = FFS_FSBTODB(fs, cgtod(fs, cg));
1996
1997 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1998 panic("%s: range: dev = 0x%llx, ino = %llu, fs = %s", __func__,
1999 (long long)dev, (unsigned long long)ino, fs->fs_fsmnt);
2000 error = bread(devvp, cgbno, (int)fs->fs_cgsize,
2001 B_MODIFY, &bp);
2002 if (error) {
2003 return (error);
2004 }
2005 cgp = (struct cg *)bp->b_data;
2006 if (!cg_chkmagic(cgp, needswap)) {
2007 brelse(bp, 0);
2008 return (0);
2009 }
2010
2011 ffs_freefile_common(ump, fs, dev, bp, ino, mode, false);
2012
2013 bdwrite(bp);
2014
2015 return 0;
2016 }
2017
2018 int
2019 ffs_freefile_snap(struct fs *fs, struct vnode *devvp, ino_t ino, int mode)
2020 {
2021 struct ufsmount *ump;
2022 struct cg *cgp;
2023 struct buf *bp;
2024 int error, cg;
2025 daddr_t cgbno;
2026 dev_t dev;
2027 const int needswap = UFS_FSNEEDSWAP(fs);
2028
2029 KASSERT(devvp->v_type != VBLK);
2030
2031 cg = ino_to_cg(fs, ino);
2032 dev = VTOI(devvp)->i_devvp->v_rdev;
2033 ump = VFSTOUFS(devvp->v_mount);
2034 cgbno = ffs_fragstoblks(fs, cgtod(fs, cg));
2035 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
2036 panic("%s: range: dev = 0x%llx, ino = %llu, fs = %s", __func__,
2037 (unsigned long long)dev, (unsigned long long)ino,
2038 fs->fs_fsmnt);
2039 error = bread(devvp, cgbno, (int)fs->fs_cgsize,
2040 B_MODIFY, &bp);
2041 if (error) {
2042 return (error);
2043 }
2044 cgp = (struct cg *)bp->b_data;
2045 if (!cg_chkmagic(cgp, needswap)) {
2046 brelse(bp, 0);
2047 return (0);
2048 }
2049 ffs_freefile_common(ump, fs, dev, bp, ino, mode, true);
2050
2051 bdwrite(bp);
2052
2053 return 0;
2054 }
2055
2056 static void
2057 ffs_freefile_common(struct ufsmount *ump, struct fs *fs, dev_t dev,
2058 struct buf *bp, ino_t ino, int mode, bool devvp_is_snapshot)
2059 {
2060 int cg;
2061 struct cg *cgp;
2062 u_int8_t *inosused;
2063 const int needswap = UFS_FSNEEDSWAP(fs);
2064
2065 cg = ino_to_cg(fs, ino);
2066 cgp = (struct cg *)bp->b_data;
2067 cgp->cg_old_time = ufs_rw32(time_second, needswap);
2068 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
2069 (fs->fs_old_flags & FS_FLAGS_UPDATED))
2070 cgp->cg_time = ufs_rw64(time_second, needswap);
2071 inosused = cg_inosused(cgp, needswap);
2072 ino %= fs->fs_ipg;
2073 if (isclr(inosused, ino)) {
2074 printf("ifree: dev = 0x%llx, ino = %llu, fs = %s\n",
2075 (unsigned long long)dev, (unsigned long long)ino +
2076 cg * fs->fs_ipg, fs->fs_fsmnt);
2077 if (fs->fs_ronly == 0)
2078 panic("%s: freeing free inode", __func__);
2079 }
2080 clrbit(inosused, ino);
2081 if (!devvp_is_snapshot)
2082 UFS_WAPBL_UNREGISTER_INODE(ump->um_mountp,
2083 ino + cg * fs->fs_ipg, mode);
2084 if (ino < ufs_rw32(cgp->cg_irotor, needswap))
2085 cgp->cg_irotor = ufs_rw32(ino, needswap);
2086 ufs_add32(cgp->cg_cs.cs_nifree, 1, needswap);
2087 mutex_enter(&ump->um_lock);
2088 fs->fs_cstotal.cs_nifree++;
2089 fs->fs_cs(fs, cg).cs_nifree++;
2090 if ((mode & IFMT) == IFDIR) {
2091 ufs_add32(cgp->cg_cs.cs_ndir, -1, needswap);
2092 fs->fs_cstotal.cs_ndir--;
2093 fs->fs_cs(fs, cg).cs_ndir--;
2094 }
2095 fs->fs_fmod = 1;
2096 ACTIVECG_CLR(fs, cg);
2097 mutex_exit(&ump->um_lock);
2098 }
2099
2100 /*
2101 * Check to see if a file is free.
2102 */
2103 int
2104 ffs_checkfreefile(struct fs *fs, struct vnode *devvp, ino_t ino)
2105 {
2106 struct cg *cgp;
2107 struct buf *bp;
2108 daddr_t cgbno;
2109 int ret, cg;
2110 u_int8_t *inosused;
2111 const bool devvp_is_snapshot = (devvp->v_type != VBLK);
2112
2113 KASSERT(devvp_is_snapshot);
2114
2115 cg = ino_to_cg(fs, ino);
2116 if (devvp_is_snapshot)
2117 cgbno = ffs_fragstoblks(fs, cgtod(fs, cg));
2118 else
2119 cgbno = FFS_FSBTODB(fs, cgtod(fs, cg));
2120 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
2121 return 1;
2122 if (bread(devvp, cgbno, (int)fs->fs_cgsize, 0, &bp)) {
2123 return 1;
2124 }
2125 cgp = (struct cg *)bp->b_data;
2126 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
2127 brelse(bp, 0);
2128 return 1;
2129 }
2130 inosused = cg_inosused(cgp, UFS_FSNEEDSWAP(fs));
2131 ino %= fs->fs_ipg;
2132 ret = isclr(inosused, ino);
2133 brelse(bp, 0);
2134 return ret;
2135 }
2136
2137 /*
2138 * Find a block of the specified size in the specified cylinder group.
2139 *
2140 * It is a panic if a request is made to find a block if none are
2141 * available.
2142 */
2143 static int32_t
2144 ffs_mapsearch(struct fs *fs, struct cg *cgp, daddr_t bpref, int allocsiz)
2145 {
2146 int32_t bno;
2147 int start, len, loc, i;
2148 int blk, field, subfield, pos;
2149 int ostart, olen;
2150 u_int8_t *blksfree;
2151 const int needswap = UFS_FSNEEDSWAP(fs);
2152
2153 /* KASSERT(mutex_owned(&ump->um_lock)); */
2154
2155 /*
2156 * find the fragment by searching through the free block
2157 * map for an appropriate bit pattern
2158 */
2159 if (bpref)
2160 start = dtogd(fs, bpref) / NBBY;
2161 else
2162 start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
2163 blksfree = cg_blksfree(cgp, needswap);
2164 len = howmany(fs->fs_fpg, NBBY) - start;
2165 ostart = start;
2166 olen = len;
2167 loc = scanc((u_int)len,
2168 (const u_char *)&blksfree[start],
2169 (const u_char *)fragtbl[fs->fs_frag],
2170 (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
2171 if (loc == 0) {
2172 len = start + 1;
2173 start = 0;
2174 loc = scanc((u_int)len,
2175 (const u_char *)&blksfree[0],
2176 (const u_char *)fragtbl[fs->fs_frag],
2177 (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
2178 if (loc == 0) {
2179 panic("%s: map corrupted: start=%d, len=%d, "
2180 "fs = %s, offset=%d/%ld, cg %d", __func__,
2181 ostart, olen, fs->fs_fsmnt,
2182 ufs_rw32(cgp->cg_freeoff, needswap),
2183 (long)blksfree - (long)cgp, cgp->cg_cgx);
2184 /* NOTREACHED */
2185 }
2186 }
2187 bno = (start + len - loc) * NBBY;
2188 cgp->cg_frotor = ufs_rw32(bno, needswap);
2189 /*
2190 * found the byte in the map
2191 * sift through the bits to find the selected frag
2192 */
2193 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
2194 blk = blkmap(fs, blksfree, bno);
2195 blk <<= 1;
2196 field = around[allocsiz];
2197 subfield = inside[allocsiz];
2198 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
2199 if ((blk & field) == subfield)
2200 return (bno + pos);
2201 field <<= 1;
2202 subfield <<= 1;
2203 }
2204 }
2205 panic("%s: block not in map: bno=%d, fs=%s", __func__,
2206 bno, fs->fs_fsmnt);
2207 /* return (-1); */
2208 }
2209
2210 /*
2211 * Fserr prints the name of a file system with an error diagnostic.
2212 *
2213 * The form of the error message is:
2214 * fs: error message
2215 */
2216 static void
2217 ffs_fserr(struct fs *fs, kauth_cred_t cred, const char *cp)
2218 {
2219 KASSERT(cred != NULL);
2220
2221 if (cred == NOCRED || cred == FSCRED) {
2222 log(LOG_ERR, "pid %d, command %s, on %s: %s\n",
2223 curproc->p_pid, curproc->p_comm,
2224 fs->fs_fsmnt, cp);
2225 } else {
2226 log(LOG_ERR, "uid %d, pid %d, command %s, on %s: %s\n",
2227 kauth_cred_getuid(cred), curproc->p_pid, curproc->p_comm,
2228 fs->fs_fsmnt, cp);
2229 }
2230 }
2231