ffs_alloc.c revision 1.147 1 /* $NetBSD: ffs_alloc.c,v 1.147 2014/09/08 20:52:37 joerg 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.147 2014/09/08 20:52:37 joerg 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 *, u_int, 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
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, inum, "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
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, kauth_cred_geteuid(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
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, NOCRED, 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, kauth_cred_geteuid(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
558 ffs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred,
559 struct vnode **vpp)
560 {
561 struct ufsmount *ump;
562 struct inode *pip;
563 struct fs *fs;
564 struct inode *ip;
565 struct timespec ts;
566 ino_t ino, ipref;
567 int cg, error;
568
569 UFS_WAPBL_JUNLOCK_ASSERT(pvp->v_mount);
570
571 *vpp = NULL;
572 pip = VTOI(pvp);
573 fs = pip->i_fs;
574 ump = pip->i_ump;
575
576 error = UFS_WAPBL_BEGIN(pvp->v_mount);
577 if (error) {
578 return error;
579 }
580 mutex_enter(&ump->um_lock);
581 if (fs->fs_cstotal.cs_nifree == 0)
582 goto noinodes;
583
584 if ((mode & IFMT) == IFDIR)
585 ipref = ffs_dirpref(pip);
586 else
587 ipref = pip->i_number;
588 if (ipref >= fs->fs_ncg * fs->fs_ipg)
589 ipref = 0;
590 cg = ino_to_cg(fs, ipref);
591 /*
592 * Track number of dirs created one after another
593 * in a same cg without intervening by files.
594 */
595 if ((mode & IFMT) == IFDIR) {
596 if (fs->fs_contigdirs[cg] < 255)
597 fs->fs_contigdirs[cg]++;
598 } else {
599 if (fs->fs_contigdirs[cg] > 0)
600 fs->fs_contigdirs[cg]--;
601 }
602 ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode, 0, ffs_nodealloccg);
603 if (ino == 0)
604 goto noinodes;
605 UFS_WAPBL_END(pvp->v_mount);
606 error = VFS_VGET(pvp->v_mount, ino, vpp);
607 if (error) {
608 int err;
609 err = UFS_WAPBL_BEGIN(pvp->v_mount);
610 if (err == 0)
611 ffs_vfree(pvp, ino, mode);
612 if (err == 0)
613 UFS_WAPBL_END(pvp->v_mount);
614 return (error);
615 }
616 KASSERT((*vpp)->v_type == VNON);
617 ip = VTOI(*vpp);
618 if (ip->i_mode) {
619 #if 0
620 printf("mode = 0%o, inum = %d, fs = %s\n",
621 ip->i_mode, ip->i_number, fs->fs_fsmnt);
622 #else
623 printf("dmode %x mode %x dgen %x gen %x\n",
624 DIP(ip, mode), ip->i_mode,
625 DIP(ip, gen), ip->i_gen);
626 printf("size %llx blocks %llx\n",
627 (long long)DIP(ip, size), (long long)DIP(ip, blocks));
628 printf("ino %llu ipref %llu\n", (unsigned long long)ino,
629 (unsigned long long)ipref);
630 #if 0
631 error = bread(ump->um_devvp, FFS_FSBTODB(fs, ino_to_fsba(fs, ino)),
632 (int)fs->fs_bsize, NOCRED, 0, &bp);
633 #endif
634
635 #endif
636 panic("ffs_valloc: dup alloc");
637 }
638 if (DIP(ip, blocks)) { /* XXX */
639 printf("free inode %llu on %s had %" PRId64 " blocks\n",
640 (unsigned long long)ino, fs->fs_fsmnt, DIP(ip, blocks));
641 DIP_ASSIGN(ip, blocks, 0);
642 }
643 ip->i_flag &= ~IN_SPACECOUNTED;
644 ip->i_flags = 0;
645 DIP_ASSIGN(ip, flags, 0);
646 /*
647 * Set up a new generation number for this inode.
648 */
649 ip->i_gen++;
650 DIP_ASSIGN(ip, gen, ip->i_gen);
651 if (fs->fs_magic == FS_UFS2_MAGIC) {
652 vfs_timestamp(&ts);
653 ip->i_ffs2_birthtime = ts.tv_sec;
654 ip->i_ffs2_birthnsec = ts.tv_nsec;
655 }
656 return (0);
657 noinodes:
658 mutex_exit(&ump->um_lock);
659 UFS_WAPBL_END(pvp->v_mount);
660 ffs_fserr(fs, kauth_cred_geteuid(cred), "out of inodes");
661 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
662 return (ENOSPC);
663 }
664
665 /*
666 * Find a cylinder group in which to place a directory.
667 *
668 * The policy implemented by this algorithm is to allocate a
669 * directory inode in the same cylinder group as its parent
670 * directory, but also to reserve space for its files inodes
671 * and data. Restrict the number of directories which may be
672 * allocated one after another in the same cylinder group
673 * without intervening allocation of files.
674 *
675 * If we allocate a first level directory then force allocation
676 * in another cylinder group.
677 */
678 static ino_t
679 ffs_dirpref(struct inode *pip)
680 {
681 register struct fs *fs;
682 int cg, prefcg;
683 int64_t dirsize, cgsize, curdsz;
684 int avgifree, avgbfree, avgndir;
685 int minifree, minbfree, maxndir;
686 int mincg, minndir;
687 int maxcontigdirs;
688
689 KASSERT(mutex_owned(&pip->i_ump->um_lock));
690
691 fs = pip->i_fs;
692
693 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
694 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
695 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
696
697 /*
698 * Force allocation in another cg if creating a first level dir.
699 */
700 if (ITOV(pip)->v_vflag & VV_ROOT) {
701 prefcg = cprng_fast32() % fs->fs_ncg;
702 mincg = prefcg;
703 minndir = fs->fs_ipg;
704 for (cg = prefcg; cg < fs->fs_ncg; cg++)
705 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
706 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
707 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
708 mincg = cg;
709 minndir = fs->fs_cs(fs, cg).cs_ndir;
710 }
711 for (cg = 0; cg < prefcg; cg++)
712 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
713 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
714 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
715 mincg = cg;
716 minndir = fs->fs_cs(fs, cg).cs_ndir;
717 }
718 return ((ino_t)(fs->fs_ipg * mincg));
719 }
720
721 /*
722 * Count various limits which used for
723 * optimal allocation of a directory inode.
724 * Try cylinder groups with >75% avgifree and avgbfree.
725 * Avoid cylinder groups with no free blocks or inodes as that
726 * triggers an I/O-expensive cylinder group scan.
727 */
728 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
729 minifree = avgifree - avgifree / 4;
730 if (minifree < 1)
731 minifree = 1;
732 minbfree = avgbfree - avgbfree / 4;
733 if (minbfree < 1)
734 minbfree = 1;
735 cgsize = (int64_t)fs->fs_fsize * fs->fs_fpg;
736 dirsize = (int64_t)fs->fs_avgfilesize * fs->fs_avgfpdir;
737 if (avgndir != 0) {
738 curdsz = (cgsize - (int64_t)avgbfree * fs->fs_bsize) / avgndir;
739 if (dirsize < curdsz)
740 dirsize = curdsz;
741 }
742 if (cgsize < dirsize * 255)
743 maxcontigdirs = (avgbfree * fs->fs_bsize) / dirsize;
744 else
745 maxcontigdirs = 255;
746 if (fs->fs_avgfpdir > 0)
747 maxcontigdirs = min(maxcontigdirs,
748 fs->fs_ipg / fs->fs_avgfpdir);
749 if (maxcontigdirs == 0)
750 maxcontigdirs = 1;
751
752 /*
753 * Limit number of dirs in one cg and reserve space for
754 * regular files, but only if we have no deficit in
755 * inodes or space.
756 */
757 prefcg = ino_to_cg(fs, pip->i_number);
758 for (cg = prefcg; cg < fs->fs_ncg; cg++)
759 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
760 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
761 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
762 if (fs->fs_contigdirs[cg] < maxcontigdirs)
763 return ((ino_t)(fs->fs_ipg * cg));
764 }
765 for (cg = 0; cg < prefcg; cg++)
766 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
767 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
768 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
769 if (fs->fs_contigdirs[cg] < maxcontigdirs)
770 return ((ino_t)(fs->fs_ipg * cg));
771 }
772 /*
773 * This is a backstop when we are deficient in space.
774 */
775 for (cg = prefcg; cg < fs->fs_ncg; cg++)
776 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
777 return ((ino_t)(fs->fs_ipg * cg));
778 for (cg = 0; cg < prefcg; cg++)
779 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
780 break;
781 return ((ino_t)(fs->fs_ipg * cg));
782 }
783
784 /*
785 * Select the desired position for the next block in a file. The file is
786 * logically divided into sections. The first section is composed of the
787 * direct blocks. Each additional section contains fs_maxbpg blocks.
788 *
789 * If no blocks have been allocated in the first section, the policy is to
790 * request a block in the same cylinder group as the inode that describes
791 * the file. If no blocks have been allocated in any other section, the
792 * policy is to place the section in a cylinder group with a greater than
793 * average number of free blocks. An appropriate cylinder group is found
794 * by using a rotor that sweeps the cylinder groups. When a new group of
795 * blocks is needed, the sweep begins in the cylinder group following the
796 * cylinder group from which the previous allocation was made. The sweep
797 * continues until a cylinder group with greater than the average number
798 * of free blocks is found. If the allocation is for the first block in an
799 * indirect block, the information on the previous allocation is unavailable;
800 * here a best guess is made based upon the logical block number being
801 * allocated.
802 *
803 * If a section is already partially allocated, the policy is to
804 * contiguously allocate fs_maxcontig blocks. The end of one of these
805 * contiguous blocks and the beginning of the next is laid out
806 * contigously if possible.
807 *
808 * => um_lock held on entry and exit
809 */
810 daddr_t
811 ffs_blkpref_ufs1(struct inode *ip, daddr_t lbn, int indx, int flags,
812 int32_t *bap /* XXX ondisk32 */)
813 {
814 struct fs *fs;
815 int cg;
816 int avgbfree, startcg;
817
818 KASSERT(mutex_owned(&ip->i_ump->um_lock));
819
820 fs = ip->i_fs;
821
822 /*
823 * If allocating a contiguous file with B_CONTIG, use the hints
824 * in the inode extentions to return the desired block.
825 *
826 * For metadata (indirect blocks) return the address of where
827 * the first indirect block resides - we'll scan for the next
828 * available slot if we need to allocate more than one indirect
829 * block. For data, return the address of the actual block
830 * relative to the address of the first data block.
831 */
832 if (flags & B_CONTIG) {
833 KASSERT(ip->i_ffs_first_data_blk != 0);
834 KASSERT(ip->i_ffs_first_indir_blk != 0);
835 if (flags & B_METAONLY)
836 return ip->i_ffs_first_indir_blk;
837 else
838 return ip->i_ffs_first_data_blk + ffs_blkstofrags(fs, lbn);
839 }
840
841 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
842 if (lbn < UFS_NDADDR + FFS_NINDIR(fs)) {
843 cg = ino_to_cg(fs, ip->i_number);
844 return (cgbase(fs, cg) + fs->fs_frag);
845 }
846 /*
847 * Find a cylinder with greater than average number of
848 * unused data blocks.
849 */
850 if (indx == 0 || bap[indx - 1] == 0)
851 startcg =
852 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
853 else
854 startcg = dtog(fs,
855 ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
856 startcg %= fs->fs_ncg;
857 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
858 for (cg = startcg; cg < fs->fs_ncg; cg++)
859 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
860 return (cgbase(fs, cg) + fs->fs_frag);
861 }
862 for (cg = 0; cg < startcg; cg++)
863 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
864 return (cgbase(fs, cg) + fs->fs_frag);
865 }
866 return (0);
867 }
868 /*
869 * We just always try to lay things out contiguously.
870 */
871 return ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
872 }
873
874 daddr_t
875 ffs_blkpref_ufs2(struct inode *ip, daddr_t lbn, int indx, int flags,
876 int64_t *bap)
877 {
878 struct fs *fs;
879 int cg;
880 int avgbfree, startcg;
881
882 KASSERT(mutex_owned(&ip->i_ump->um_lock));
883
884 fs = ip->i_fs;
885
886 /*
887 * If allocating a contiguous file with B_CONTIG, use the hints
888 * in the inode extentions to return the desired block.
889 *
890 * For metadata (indirect blocks) return the address of where
891 * the first indirect block resides - we'll scan for the next
892 * available slot if we need to allocate more than one indirect
893 * block. For data, return the address of the actual block
894 * relative to the address of the first data block.
895 */
896 if (flags & B_CONTIG) {
897 KASSERT(ip->i_ffs_first_data_blk != 0);
898 KASSERT(ip->i_ffs_first_indir_blk != 0);
899 if (flags & B_METAONLY)
900 return ip->i_ffs_first_indir_blk;
901 else
902 return ip->i_ffs_first_data_blk + ffs_blkstofrags(fs, lbn);
903 }
904
905 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
906 if (lbn < UFS_NDADDR + FFS_NINDIR(fs)) {
907 cg = ino_to_cg(fs, ip->i_number);
908 return (cgbase(fs, cg) + fs->fs_frag);
909 }
910 /*
911 * Find a cylinder with greater than average number of
912 * unused data blocks.
913 */
914 if (indx == 0 || bap[indx - 1] == 0)
915 startcg =
916 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
917 else
918 startcg = dtog(fs,
919 ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
920 startcg %= fs->fs_ncg;
921 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
922 for (cg = startcg; cg < fs->fs_ncg; cg++)
923 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
924 return (cgbase(fs, cg) + fs->fs_frag);
925 }
926 for (cg = 0; cg < startcg; cg++)
927 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
928 return (cgbase(fs, cg) + fs->fs_frag);
929 }
930 return (0);
931 }
932 /*
933 * We just always try to lay things out contiguously.
934 */
935 return ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
936 }
937
938
939 /*
940 * Implement the cylinder overflow algorithm.
941 *
942 * The policy implemented by this algorithm is:
943 * 1) allocate the block in its requested cylinder group.
944 * 2) quadradically rehash on the cylinder group number.
945 * 3) brute force search for a free block.
946 *
947 * => called with um_lock held
948 * => returns with um_lock released on success, held on failure
949 * (*allocator releases lock on success, retains lock on failure)
950 */
951 /*VARARGS5*/
952 static daddr_t
953 ffs_hashalloc(struct inode *ip, int cg, daddr_t pref,
954 int size /* size for data blocks, mode for inodes */,
955 int flags, daddr_t (*allocator)(struct inode *, int, daddr_t, int, int))
956 {
957 struct fs *fs;
958 daddr_t result;
959 int i, icg = cg;
960
961 fs = ip->i_fs;
962 /*
963 * 1: preferred cylinder group
964 */
965 result = (*allocator)(ip, cg, pref, size, flags);
966 if (result)
967 return (result);
968
969 if (flags & B_CONTIG)
970 return (result);
971 /*
972 * 2: quadratic rehash
973 */
974 for (i = 1; i < fs->fs_ncg; i *= 2) {
975 cg += i;
976 if (cg >= fs->fs_ncg)
977 cg -= fs->fs_ncg;
978 result = (*allocator)(ip, cg, 0, size, flags);
979 if (result)
980 return (result);
981 }
982 /*
983 * 3: brute force search
984 * Note that we start at i == 2, since 0 was checked initially,
985 * and 1 is always checked in the quadratic rehash.
986 */
987 cg = (icg + 2) % fs->fs_ncg;
988 for (i = 2; i < fs->fs_ncg; i++) {
989 result = (*allocator)(ip, cg, 0, size, flags);
990 if (result)
991 return (result);
992 cg++;
993 if (cg == fs->fs_ncg)
994 cg = 0;
995 }
996 return (0);
997 }
998
999 /*
1000 * Determine whether a fragment can be extended.
1001 *
1002 * Check to see if the necessary fragments are available, and
1003 * if they are, allocate them.
1004 *
1005 * => called with um_lock held
1006 * => returns with um_lock released on success, held on failure
1007 */
1008 static daddr_t
1009 ffs_fragextend(struct inode *ip, int cg, daddr_t bprev, int osize, int nsize)
1010 {
1011 struct ufsmount *ump;
1012 struct fs *fs;
1013 struct cg *cgp;
1014 struct buf *bp;
1015 daddr_t bno;
1016 int frags, bbase;
1017 int i, error;
1018 u_int8_t *blksfree;
1019
1020 fs = ip->i_fs;
1021 ump = ip->i_ump;
1022
1023 KASSERT(mutex_owned(&ump->um_lock));
1024
1025 if (fs->fs_cs(fs, cg).cs_nffree < ffs_numfrags(fs, nsize - osize))
1026 return (0);
1027 frags = ffs_numfrags(fs, nsize);
1028 bbase = ffs_fragnum(fs, bprev);
1029 if (bbase > ffs_fragnum(fs, (bprev + frags - 1))) {
1030 /* cannot extend across a block boundary */
1031 return (0);
1032 }
1033 mutex_exit(&ump->um_lock);
1034 error = bread(ip->i_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
1035 (int)fs->fs_cgsize, NOCRED, B_MODIFY, &bp);
1036 if (error)
1037 goto fail;
1038 cgp = (struct cg *)bp->b_data;
1039 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs)))
1040 goto fail;
1041 cgp->cg_old_time = ufs_rw32(time_second, UFS_FSNEEDSWAP(fs));
1042 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1043 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1044 cgp->cg_time = ufs_rw64(time_second, UFS_FSNEEDSWAP(fs));
1045 bno = dtogd(fs, bprev);
1046 blksfree = cg_blksfree(cgp, UFS_FSNEEDSWAP(fs));
1047 for (i = ffs_numfrags(fs, osize); i < frags; i++)
1048 if (isclr(blksfree, bno + i))
1049 goto fail;
1050 /*
1051 * the current fragment can be extended
1052 * deduct the count on fragment being extended into
1053 * increase the count on the remaining fragment (if any)
1054 * allocate the extended piece
1055 */
1056 for (i = frags; i < fs->fs_frag - bbase; i++)
1057 if (isclr(blksfree, bno + i))
1058 break;
1059 ufs_add32(cgp->cg_frsum[i - ffs_numfrags(fs, osize)], -1, UFS_FSNEEDSWAP(fs));
1060 if (i != frags)
1061 ufs_add32(cgp->cg_frsum[i - frags], 1, UFS_FSNEEDSWAP(fs));
1062 mutex_enter(&ump->um_lock);
1063 for (i = ffs_numfrags(fs, osize); i < frags; i++) {
1064 clrbit(blksfree, bno + i);
1065 ufs_add32(cgp->cg_cs.cs_nffree, -1, UFS_FSNEEDSWAP(fs));
1066 fs->fs_cstotal.cs_nffree--;
1067 fs->fs_cs(fs, cg).cs_nffree--;
1068 }
1069 fs->fs_fmod = 1;
1070 ACTIVECG_CLR(fs, cg);
1071 mutex_exit(&ump->um_lock);
1072 bdwrite(bp);
1073 return (bprev);
1074
1075 fail:
1076 if (bp != NULL)
1077 brelse(bp, 0);
1078 mutex_enter(&ump->um_lock);
1079 return (0);
1080 }
1081
1082 /*
1083 * Determine whether a block can be allocated.
1084 *
1085 * Check to see if a block of the appropriate size is available,
1086 * and if it is, allocate it.
1087 */
1088 static daddr_t
1089 ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size, int flags)
1090 {
1091 struct ufsmount *ump;
1092 struct fs *fs = ip->i_fs;
1093 struct cg *cgp;
1094 struct buf *bp;
1095 int32_t bno;
1096 daddr_t blkno;
1097 int error, frags, allocsiz, i;
1098 u_int8_t *blksfree;
1099 const int needswap = UFS_FSNEEDSWAP(fs);
1100
1101 ump = ip->i_ump;
1102
1103 KASSERT(mutex_owned(&ump->um_lock));
1104
1105 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
1106 return (0);
1107 mutex_exit(&ump->um_lock);
1108 error = bread(ip->i_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
1109 (int)fs->fs_cgsize, NOCRED, B_MODIFY, &bp);
1110 if (error)
1111 goto fail;
1112 cgp = (struct cg *)bp->b_data;
1113 if (!cg_chkmagic(cgp, needswap) ||
1114 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize))
1115 goto fail;
1116 cgp->cg_old_time = ufs_rw32(time_second, needswap);
1117 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1118 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1119 cgp->cg_time = ufs_rw64(time_second, needswap);
1120 if (size == fs->fs_bsize) {
1121 mutex_enter(&ump->um_lock);
1122 blkno = ffs_alloccgblk(ip, bp, bpref, flags);
1123 ACTIVECG_CLR(fs, cg);
1124 mutex_exit(&ump->um_lock);
1125 bdwrite(bp);
1126 return (blkno);
1127 }
1128 /*
1129 * check to see if any fragments are already available
1130 * allocsiz is the size which will be allocated, hacking
1131 * it down to a smaller size if necessary
1132 */
1133 blksfree = cg_blksfree(cgp, needswap);
1134 frags = ffs_numfrags(fs, size);
1135 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1136 if (cgp->cg_frsum[allocsiz] != 0)
1137 break;
1138 if (allocsiz == fs->fs_frag) {
1139 /*
1140 * no fragments were available, so a block will be
1141 * allocated, and hacked up
1142 */
1143 if (cgp->cg_cs.cs_nbfree == 0)
1144 goto fail;
1145 mutex_enter(&ump->um_lock);
1146 blkno = ffs_alloccgblk(ip, bp, bpref, flags);
1147 bno = dtogd(fs, blkno);
1148 for (i = frags; i < fs->fs_frag; i++)
1149 setbit(blksfree, bno + i);
1150 i = fs->fs_frag - frags;
1151 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1152 fs->fs_cstotal.cs_nffree += i;
1153 fs->fs_cs(fs, cg).cs_nffree += i;
1154 fs->fs_fmod = 1;
1155 ufs_add32(cgp->cg_frsum[i], 1, needswap);
1156 ACTIVECG_CLR(fs, cg);
1157 mutex_exit(&ump->um_lock);
1158 bdwrite(bp);
1159 return (blkno);
1160 }
1161 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1162 #if 0
1163 /*
1164 * XXX fvdl mapsearch will panic, and never return -1
1165 * also: returning NULL as daddr_t ?
1166 */
1167 if (bno < 0)
1168 goto fail;
1169 #endif
1170 for (i = 0; i < frags; i++)
1171 clrbit(blksfree, bno + i);
1172 mutex_enter(&ump->um_lock);
1173 ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
1174 fs->fs_cstotal.cs_nffree -= frags;
1175 fs->fs_cs(fs, cg).cs_nffree -= frags;
1176 fs->fs_fmod = 1;
1177 ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
1178 if (frags != allocsiz)
1179 ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
1180 blkno = cgbase(fs, cg) + bno;
1181 ACTIVECG_CLR(fs, cg);
1182 mutex_exit(&ump->um_lock);
1183 bdwrite(bp);
1184 return blkno;
1185
1186 fail:
1187 if (bp != NULL)
1188 brelse(bp, 0);
1189 mutex_enter(&ump->um_lock);
1190 return (0);
1191 }
1192
1193 /*
1194 * Allocate a block in a cylinder group.
1195 *
1196 * This algorithm implements the following policy:
1197 * 1) allocate the requested block.
1198 * 2) allocate a rotationally optimal block in the same cylinder.
1199 * 3) allocate the next available block on the block rotor for the
1200 * specified cylinder group.
1201 * Note that this routine only allocates fs_bsize blocks; these
1202 * blocks may be fragmented by the routine that allocates them.
1203 */
1204 static daddr_t
1205 ffs_alloccgblk(struct inode *ip, struct buf *bp, daddr_t bpref, int flags)
1206 {
1207 struct fs *fs = ip->i_fs;
1208 struct cg *cgp;
1209 int cg;
1210 daddr_t blkno;
1211 int32_t bno;
1212 u_int8_t *blksfree;
1213 const int needswap = UFS_FSNEEDSWAP(fs);
1214
1215 KASSERT(mutex_owned(&ip->i_ump->um_lock));
1216
1217 cgp = (struct cg *)bp->b_data;
1218 blksfree = cg_blksfree(cgp, needswap);
1219 if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
1220 bpref = ufs_rw32(cgp->cg_rotor, needswap);
1221 } else {
1222 bpref = ffs_blknum(fs, bpref);
1223 bno = dtogd(fs, bpref);
1224 /*
1225 * if the requested block is available, use it
1226 */
1227 if (ffs_isblock(fs, blksfree, ffs_fragstoblks(fs, bno)))
1228 goto gotit;
1229 /*
1230 * if the requested data block isn't available and we are
1231 * trying to allocate a contiguous file, return an error.
1232 */
1233 if ((flags & (B_CONTIG | B_METAONLY)) == B_CONTIG)
1234 return (0);
1235 }
1236
1237 /*
1238 * Take the next available block in this cylinder group.
1239 */
1240 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1241 if (bno < 0)
1242 return (0);
1243 cgp->cg_rotor = ufs_rw32(bno, needswap);
1244 gotit:
1245 blkno = ffs_fragstoblks(fs, bno);
1246 ffs_clrblock(fs, blksfree, blkno);
1247 ffs_clusteracct(fs, cgp, blkno, -1);
1248 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
1249 fs->fs_cstotal.cs_nbfree--;
1250 fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
1251 if ((fs->fs_magic == FS_UFS1_MAGIC) &&
1252 ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
1253 int cylno;
1254 cylno = old_cbtocylno(fs, bno);
1255 KASSERT(cylno >= 0);
1256 KASSERT(cylno < fs->fs_old_ncyl);
1257 KASSERT(old_cbtorpos(fs, bno) >= 0);
1258 KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, bno) < fs->fs_old_nrpos);
1259 ufs_add16(old_cg_blks(fs, cgp, cylno, needswap)[old_cbtorpos(fs, bno)], -1,
1260 needswap);
1261 ufs_add32(old_cg_blktot(cgp, needswap)[cylno], -1, needswap);
1262 }
1263 fs->fs_fmod = 1;
1264 cg = ufs_rw32(cgp->cg_cgx, needswap);
1265 blkno = cgbase(fs, cg) + bno;
1266 return (blkno);
1267 }
1268
1269 /*
1270 * Determine whether an inode can be allocated.
1271 *
1272 * Check to see if an inode is available, and if it is,
1273 * allocate it using the following policy:
1274 * 1) allocate the requested inode.
1275 * 2) allocate the next available inode after the requested
1276 * inode in the specified cylinder group.
1277 */
1278 static daddr_t
1279 ffs_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode, int flags)
1280 {
1281 struct ufsmount *ump = ip->i_ump;
1282 struct fs *fs = ip->i_fs;
1283 struct cg *cgp;
1284 struct buf *bp, *ibp;
1285 u_int8_t *inosused;
1286 int error, start, len, loc, map, i;
1287 int32_t initediblk;
1288 daddr_t nalloc;
1289 struct ufs2_dinode *dp2;
1290 const int needswap = UFS_FSNEEDSWAP(fs);
1291
1292 KASSERT(mutex_owned(&ump->um_lock));
1293 UFS_WAPBL_JLOCK_ASSERT(ip->i_ump->um_mountp);
1294
1295 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1296 return (0);
1297 mutex_exit(&ump->um_lock);
1298 ibp = NULL;
1299 initediblk = -1;
1300 retry:
1301 error = bread(ip->i_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
1302 (int)fs->fs_cgsize, NOCRED, B_MODIFY, &bp);
1303 if (error)
1304 goto fail;
1305 cgp = (struct cg *)bp->b_data;
1306 if (!cg_chkmagic(cgp, needswap) || cgp->cg_cs.cs_nifree == 0)
1307 goto fail;
1308
1309 if (ibp != NULL &&
1310 initediblk != ufs_rw32(cgp->cg_initediblk, needswap)) {
1311 /* Another thread allocated more inodes so we retry the test. */
1312 brelse(ibp, 0);
1313 ibp = NULL;
1314 }
1315 /*
1316 * Check to see if we need to initialize more inodes.
1317 */
1318 if (fs->fs_magic == FS_UFS2_MAGIC && ibp == NULL) {
1319 initediblk = ufs_rw32(cgp->cg_initediblk, needswap);
1320 nalloc = fs->fs_ipg - ufs_rw32(cgp->cg_cs.cs_nifree, needswap);
1321 if (nalloc + FFS_INOPB(fs) > initediblk &&
1322 initediblk < ufs_rw32(cgp->cg_niblk, needswap)) {
1323 /*
1324 * We have to release the cg buffer here to prevent
1325 * a deadlock when reading the inode block will
1326 * run a copy-on-write that might use this cg.
1327 */
1328 brelse(bp, 0);
1329 bp = NULL;
1330 error = ffs_getblk(ip->i_devvp, FFS_FSBTODB(fs,
1331 ino_to_fsba(fs, cg * fs->fs_ipg + initediblk)),
1332 FFS_NOBLK, fs->fs_bsize, false, &ibp);
1333 if (error)
1334 goto fail;
1335 goto retry;
1336 }
1337 }
1338
1339 cgp->cg_old_time = ufs_rw32(time_second, needswap);
1340 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1341 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1342 cgp->cg_time = ufs_rw64(time_second, needswap);
1343 inosused = cg_inosused(cgp, needswap);
1344 if (ipref) {
1345 ipref %= fs->fs_ipg;
1346 if (isclr(inosused, ipref))
1347 goto gotit;
1348 }
1349 start = ufs_rw32(cgp->cg_irotor, needswap) / NBBY;
1350 len = howmany(fs->fs_ipg - ufs_rw32(cgp->cg_irotor, needswap),
1351 NBBY);
1352 loc = skpc(0xff, len, &inosused[start]);
1353 if (loc == 0) {
1354 len = start + 1;
1355 start = 0;
1356 loc = skpc(0xff, len, &inosused[0]);
1357 if (loc == 0) {
1358 printf("cg = %d, irotor = %d, fs = %s\n",
1359 cg, ufs_rw32(cgp->cg_irotor, needswap),
1360 fs->fs_fsmnt);
1361 panic("ffs_nodealloccg: map corrupted");
1362 /* NOTREACHED */
1363 }
1364 }
1365 i = start + len - loc;
1366 map = inosused[i] ^ 0xff;
1367 if (map == 0) {
1368 printf("fs = %s\n", fs->fs_fsmnt);
1369 panic("ffs_nodealloccg: block not in map");
1370 }
1371 ipref = i * NBBY + ffs(map) - 1;
1372 cgp->cg_irotor = ufs_rw32(ipref, needswap);
1373 gotit:
1374 UFS_WAPBL_REGISTER_INODE(ip->i_ump->um_mountp, cg * fs->fs_ipg + ipref,
1375 mode);
1376 /*
1377 * Check to see if we need to initialize more inodes.
1378 */
1379 if (ibp != NULL) {
1380 KASSERT(initediblk == ufs_rw32(cgp->cg_initediblk, needswap));
1381 memset(ibp->b_data, 0, fs->fs_bsize);
1382 dp2 = (struct ufs2_dinode *)(ibp->b_data);
1383 for (i = 0; i < FFS_INOPB(fs); i++) {
1384 /*
1385 * Don't bother to swap, it's supposed to be
1386 * random, after all.
1387 */
1388 dp2->di_gen = (cprng_fast32() & INT32_MAX) / 2 + 1;
1389 dp2++;
1390 }
1391 initediblk += FFS_INOPB(fs);
1392 cgp->cg_initediblk = ufs_rw32(initediblk, needswap);
1393 }
1394
1395 mutex_enter(&ump->um_lock);
1396 ACTIVECG_CLR(fs, cg);
1397 setbit(inosused, ipref);
1398 ufs_add32(cgp->cg_cs.cs_nifree, -1, needswap);
1399 fs->fs_cstotal.cs_nifree--;
1400 fs->fs_cs(fs, cg).cs_nifree--;
1401 fs->fs_fmod = 1;
1402 if ((mode & IFMT) == IFDIR) {
1403 ufs_add32(cgp->cg_cs.cs_ndir, 1, needswap);
1404 fs->fs_cstotal.cs_ndir++;
1405 fs->fs_cs(fs, cg).cs_ndir++;
1406 }
1407 mutex_exit(&ump->um_lock);
1408 if (ibp != NULL) {
1409 bwrite(bp);
1410 bawrite(ibp);
1411 } else
1412 bdwrite(bp);
1413 return (cg * fs->fs_ipg + ipref);
1414 fail:
1415 if (bp != NULL)
1416 brelse(bp, 0);
1417 if (ibp != NULL)
1418 brelse(ibp, 0);
1419 mutex_enter(&ump->um_lock);
1420 return (0);
1421 }
1422
1423 /*
1424 * Allocate a block or fragment.
1425 *
1426 * The specified block or fragment is removed from the
1427 * free map, possibly fragmenting a block in the process.
1428 *
1429 * This implementation should mirror fs_blkfree
1430 *
1431 * => um_lock not held on entry or exit
1432 */
1433 int
1434 ffs_blkalloc(struct inode *ip, daddr_t bno, long size)
1435 {
1436 int error;
1437
1438 error = ffs_check_bad_allocation(__func__, ip->i_fs, bno, size,
1439 ip->i_dev, ip->i_uid);
1440 if (error)
1441 return error;
1442
1443 return ffs_blkalloc_ump(ip->i_ump, bno, size);
1444 }
1445
1446 int
1447 ffs_blkalloc_ump(struct ufsmount *ump, daddr_t bno, long size)
1448 {
1449 struct fs *fs = ump->um_fs;
1450 struct cg *cgp;
1451 struct buf *bp;
1452 int32_t fragno, cgbno;
1453 int i, error, cg, blk, frags, bbase;
1454 u_int8_t *blksfree;
1455 const int needswap = UFS_FSNEEDSWAP(fs);
1456
1457 KASSERT((u_int)size <= fs->fs_bsize && ffs_fragoff(fs, size) == 0 &&
1458 ffs_fragnum(fs, bno) + ffs_numfrags(fs, size) <= fs->fs_frag);
1459 KASSERT(bno < fs->fs_size);
1460
1461 cg = dtog(fs, bno);
1462 error = bread(ump->um_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
1463 (int)fs->fs_cgsize, NOCRED, B_MODIFY, &bp);
1464 if (error) {
1465 return error;
1466 }
1467 cgp = (struct cg *)bp->b_data;
1468 if (!cg_chkmagic(cgp, needswap)) {
1469 brelse(bp, 0);
1470 return EIO;
1471 }
1472 cgp->cg_old_time = ufs_rw32(time_second, needswap);
1473 cgp->cg_time = ufs_rw64(time_second, needswap);
1474 cgbno = dtogd(fs, bno);
1475 blksfree = cg_blksfree(cgp, needswap);
1476
1477 mutex_enter(&ump->um_lock);
1478 if (size == fs->fs_bsize) {
1479 fragno = ffs_fragstoblks(fs, cgbno);
1480 if (!ffs_isblock(fs, blksfree, fragno)) {
1481 mutex_exit(&ump->um_lock);
1482 brelse(bp, 0);
1483 return EBUSY;
1484 }
1485 ffs_clrblock(fs, blksfree, fragno);
1486 ffs_clusteracct(fs, cgp, fragno, -1);
1487 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
1488 fs->fs_cstotal.cs_nbfree--;
1489 fs->fs_cs(fs, cg).cs_nbfree--;
1490 } else {
1491 bbase = cgbno - ffs_fragnum(fs, cgbno);
1492
1493 frags = ffs_numfrags(fs, size);
1494 for (i = 0; i < frags; i++) {
1495 if (isclr(blksfree, cgbno + i)) {
1496 mutex_exit(&ump->um_lock);
1497 brelse(bp, 0);
1498 return EBUSY;
1499 }
1500 }
1501 /*
1502 * if a complete block is being split, account for it
1503 */
1504 fragno = ffs_fragstoblks(fs, bbase);
1505 if (ffs_isblock(fs, blksfree, fragno)) {
1506 ufs_add32(cgp->cg_cs.cs_nffree, fs->fs_frag, needswap);
1507 fs->fs_cstotal.cs_nffree += fs->fs_frag;
1508 fs->fs_cs(fs, cg).cs_nffree += fs->fs_frag;
1509 ffs_clusteracct(fs, cgp, fragno, -1);
1510 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
1511 fs->fs_cstotal.cs_nbfree--;
1512 fs->fs_cs(fs, cg).cs_nbfree--;
1513 }
1514 /*
1515 * decrement the counts associated with the old frags
1516 */
1517 blk = blkmap(fs, blksfree, bbase);
1518 ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
1519 /*
1520 * allocate the fragment
1521 */
1522 for (i = 0; i < frags; i++) {
1523 clrbit(blksfree, cgbno + i);
1524 }
1525 ufs_add32(cgp->cg_cs.cs_nffree, -i, needswap);
1526 fs->fs_cstotal.cs_nffree -= i;
1527 fs->fs_cs(fs, cg).cs_nffree -= i;
1528 /*
1529 * add back in counts associated with the new frags
1530 */
1531 blk = blkmap(fs, blksfree, bbase);
1532 ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
1533 }
1534 fs->fs_fmod = 1;
1535 ACTIVECG_CLR(fs, cg);
1536 mutex_exit(&ump->um_lock);
1537 bdwrite(bp);
1538 return 0;
1539 }
1540
1541 /*
1542 * Free a block or fragment.
1543 *
1544 * The specified block or fragment is placed back in the
1545 * free map. If a fragment is deallocated, a possible
1546 * block reassembly is checked.
1547 *
1548 * => um_lock not held on entry or exit
1549 */
1550 static void
1551 ffs_blkfree_cg(struct fs *fs, struct vnode *devvp, daddr_t bno, long size)
1552 {
1553 struct cg *cgp;
1554 struct buf *bp;
1555 struct ufsmount *ump;
1556 daddr_t cgblkno;
1557 int error, cg;
1558 dev_t dev;
1559 const bool devvp_is_snapshot = (devvp->v_type != VBLK);
1560 const int needswap = UFS_FSNEEDSWAP(fs);
1561
1562 KASSERT(!devvp_is_snapshot);
1563
1564 cg = dtog(fs, bno);
1565 dev = devvp->v_rdev;
1566 ump = VFSTOUFS(spec_node_getmountedfs(devvp));
1567 KASSERT(fs == ump->um_fs);
1568 cgblkno = FFS_FSBTODB(fs, cgtod(fs, cg));
1569
1570 error = bread(devvp, cgblkno, (int)fs->fs_cgsize,
1571 NOCRED, B_MODIFY, &bp);
1572 if (error) {
1573 return;
1574 }
1575 cgp = (struct cg *)bp->b_data;
1576 if (!cg_chkmagic(cgp, needswap)) {
1577 brelse(bp, 0);
1578 return;
1579 }
1580
1581 ffs_blkfree_common(ump, fs, dev, bp, bno, size, devvp_is_snapshot);
1582
1583 bdwrite(bp);
1584 }
1585
1586 struct discardopdata {
1587 struct work wk; /* must be first */
1588 struct vnode *devvp;
1589 daddr_t bno;
1590 long size;
1591 };
1592
1593 struct discarddata {
1594 struct fs *fs;
1595 struct discardopdata *entry;
1596 long maxsize;
1597 kmutex_t entrylk;
1598 struct workqueue *wq;
1599 int wqcnt, wqdraining;
1600 kmutex_t wqlk;
1601 kcondvar_t wqcv;
1602 /* timer for flush? */
1603 };
1604
1605 static void
1606 ffs_blkfree_td(struct fs *fs, struct discardopdata *td)
1607 {
1608 long todo;
1609
1610 while (td->size) {
1611 todo = min(td->size,
1612 ffs_lfragtosize(fs, (fs->fs_frag - ffs_fragnum(fs, td->bno))));
1613 ffs_blkfree_cg(fs, td->devvp, td->bno, todo);
1614 td->bno += ffs_numfrags(fs, todo);
1615 td->size -= todo;
1616 }
1617 }
1618
1619 static void
1620 ffs_discardcb(struct work *wk, void *arg)
1621 {
1622 struct discardopdata *td = (void *)wk;
1623 struct discarddata *ts = arg;
1624 struct fs *fs = ts->fs;
1625 off_t start, len;
1626 #ifdef TRIMDEBUG
1627 int error;
1628 #endif
1629
1630 /* like FSBTODB but emits bytes; XXX move to fs.h */
1631 #ifndef FFS_FSBTOBYTES
1632 #define FFS_FSBTOBYTES(fs, b) ((b) << (fs)->fs_fshift)
1633 #endif
1634
1635 start = FFS_FSBTOBYTES(fs, td->bno);
1636 len = td->size;
1637 #ifdef TRIMDEBUG
1638 error =
1639 #endif
1640 VOP_FDISCARD(td->devvp, start, len);
1641 #ifdef TRIMDEBUG
1642 printf("trim(%" PRId64 ",%ld):%d\n", td->bno, td->size, error);
1643 #endif
1644
1645 ffs_blkfree_td(fs, td);
1646 kmem_free(td, sizeof(*td));
1647 mutex_enter(&ts->wqlk);
1648 ts->wqcnt--;
1649 if (ts->wqdraining && !ts->wqcnt)
1650 cv_signal(&ts->wqcv);
1651 mutex_exit(&ts->wqlk);
1652 }
1653
1654 void *
1655 ffs_discard_init(struct vnode *devvp, struct fs *fs)
1656 {
1657 struct discarddata *ts;
1658 int error;
1659
1660 ts = kmem_zalloc(sizeof (*ts), KM_SLEEP);
1661 error = workqueue_create(&ts->wq, "trimwq", ffs_discardcb, ts,
1662 0, 0, 0);
1663 if (error) {
1664 kmem_free(ts, sizeof (*ts));
1665 return NULL;
1666 }
1667 mutex_init(&ts->entrylk, MUTEX_DEFAULT, IPL_NONE);
1668 mutex_init(&ts->wqlk, MUTEX_DEFAULT, IPL_NONE);
1669 cv_init(&ts->wqcv, "trimwqcv");
1670 ts->maxsize = 100*1024; /* XXX */
1671 ts->fs = fs;
1672 return ts;
1673 }
1674
1675 void
1676 ffs_discard_finish(void *vts, int flags)
1677 {
1678 struct discarddata *ts = vts;
1679 struct discardopdata *td = NULL;
1680 int res = 0;
1681
1682 /* wait for workqueue to drain */
1683 mutex_enter(&ts->wqlk);
1684 if (ts->wqcnt) {
1685 ts->wqdraining = 1;
1686 res = cv_timedwait(&ts->wqcv, &ts->wqlk, mstohz(5000));
1687 }
1688 mutex_exit(&ts->wqlk);
1689 if (res)
1690 printf("ffs_discarddata drain timeout\n");
1691
1692 mutex_enter(&ts->entrylk);
1693 if (ts->entry) {
1694 td = ts->entry;
1695 ts->entry = NULL;
1696 }
1697 mutex_exit(&ts->entrylk);
1698 if (td) {
1699 /* XXX don't tell disk, its optional */
1700 ffs_blkfree_td(ts->fs, td);
1701 #ifdef TRIMDEBUG
1702 printf("finish(%" PRId64 ",%ld)\n", td->bno, td->size);
1703 #endif
1704 kmem_free(td, sizeof(*td));
1705 }
1706
1707 cv_destroy(&ts->wqcv);
1708 mutex_destroy(&ts->entrylk);
1709 mutex_destroy(&ts->wqlk);
1710 workqueue_destroy(ts->wq);
1711 kmem_free(ts, sizeof(*ts));
1712 }
1713
1714 void
1715 ffs_blkfree(struct fs *fs, struct vnode *devvp, daddr_t bno, long size,
1716 ino_t inum)
1717 {
1718 struct ufsmount *ump;
1719 int error;
1720 dev_t dev;
1721 struct discarddata *ts;
1722 struct discardopdata *td;
1723
1724 dev = devvp->v_rdev;
1725 ump = VFSTOUFS(spec_node_getmountedfs(devvp));
1726 if (ffs_snapblkfree(fs, devvp, bno, size, inum))
1727 return;
1728
1729 error = ffs_check_bad_allocation(__func__, fs, bno, size, dev, inum);
1730 if (error)
1731 return;
1732
1733 if (!ump->um_discarddata) {
1734 ffs_blkfree_cg(fs, devvp, bno, size);
1735 return;
1736 }
1737
1738 #ifdef TRIMDEBUG
1739 printf("blkfree(%" PRId64 ",%ld)\n", bno, size);
1740 #endif
1741 ts = ump->um_discarddata;
1742 td = NULL;
1743
1744 mutex_enter(&ts->entrylk);
1745 if (ts->entry) {
1746 td = ts->entry;
1747 /* ffs deallocs backwards, check for prepend only */
1748 if (td->bno == bno + ffs_numfrags(fs, size)
1749 && td->size + size <= ts->maxsize) {
1750 td->bno = bno;
1751 td->size += size;
1752 if (td->size < ts->maxsize) {
1753 #ifdef TRIMDEBUG
1754 printf("defer(%" PRId64 ",%ld)\n", td->bno, td->size);
1755 #endif
1756 mutex_exit(&ts->entrylk);
1757 return;
1758 }
1759 size = 0; /* mark done */
1760 }
1761 ts->entry = NULL;
1762 }
1763 mutex_exit(&ts->entrylk);
1764
1765 if (td) {
1766 #ifdef TRIMDEBUG
1767 printf("enq old(%" PRId64 ",%ld)\n", td->bno, td->size);
1768 #endif
1769 mutex_enter(&ts->wqlk);
1770 ts->wqcnt++;
1771 mutex_exit(&ts->wqlk);
1772 workqueue_enqueue(ts->wq, &td->wk, NULL);
1773 }
1774 if (!size)
1775 return;
1776
1777 td = kmem_alloc(sizeof(*td), KM_SLEEP);
1778 td->devvp = devvp;
1779 td->bno = bno;
1780 td->size = size;
1781
1782 if (td->size < ts->maxsize) { /* XXX always the case */
1783 mutex_enter(&ts->entrylk);
1784 if (!ts->entry) { /* possible race? */
1785 #ifdef TRIMDEBUG
1786 printf("defer(%" PRId64 ",%ld)\n", td->bno, td->size);
1787 #endif
1788 ts->entry = td;
1789 td = NULL;
1790 }
1791 mutex_exit(&ts->entrylk);
1792 }
1793 if (td) {
1794 #ifdef TRIMDEBUG
1795 printf("enq new(%" PRId64 ",%ld)\n", td->bno, td->size);
1796 #endif
1797 mutex_enter(&ts->wqlk);
1798 ts->wqcnt++;
1799 mutex_exit(&ts->wqlk);
1800 workqueue_enqueue(ts->wq, &td->wk, NULL);
1801 }
1802 }
1803
1804 /*
1805 * Free a block or fragment from a snapshot cg copy.
1806 *
1807 * The specified block or fragment is placed back in the
1808 * free map. If a fragment is deallocated, a possible
1809 * block reassembly is checked.
1810 *
1811 * => um_lock not held on entry or exit
1812 */
1813 void
1814 ffs_blkfree_snap(struct fs *fs, struct vnode *devvp, daddr_t bno, long size,
1815 ino_t inum)
1816 {
1817 struct cg *cgp;
1818 struct buf *bp;
1819 struct ufsmount *ump;
1820 daddr_t cgblkno;
1821 int error, cg;
1822 dev_t dev;
1823 const bool devvp_is_snapshot = (devvp->v_type != VBLK);
1824 const int needswap = UFS_FSNEEDSWAP(fs);
1825
1826 KASSERT(devvp_is_snapshot);
1827
1828 cg = dtog(fs, bno);
1829 dev = VTOI(devvp)->i_devvp->v_rdev;
1830 ump = VFSTOUFS(devvp->v_mount);
1831 cgblkno = ffs_fragstoblks(fs, cgtod(fs, cg));
1832
1833 error = ffs_check_bad_allocation(__func__, fs, bno, size, dev, inum);
1834 if (error)
1835 return;
1836
1837 error = bread(devvp, cgblkno, (int)fs->fs_cgsize,
1838 NOCRED, B_MODIFY, &bp);
1839 if (error) {
1840 return;
1841 }
1842 cgp = (struct cg *)bp->b_data;
1843 if (!cg_chkmagic(cgp, needswap)) {
1844 brelse(bp, 0);
1845 return;
1846 }
1847
1848 ffs_blkfree_common(ump, fs, dev, bp, bno, size, devvp_is_snapshot);
1849
1850 bdwrite(bp);
1851 }
1852
1853 static void
1854 ffs_blkfree_common(struct ufsmount *ump, struct fs *fs, dev_t dev,
1855 struct buf *bp, daddr_t bno, long size, bool devvp_is_snapshot)
1856 {
1857 struct cg *cgp;
1858 int32_t fragno, cgbno;
1859 int i, cg, blk, frags, bbase;
1860 u_int8_t *blksfree;
1861 const int needswap = UFS_FSNEEDSWAP(fs);
1862
1863 cg = dtog(fs, bno);
1864 cgp = (struct cg *)bp->b_data;
1865 cgp->cg_old_time = ufs_rw32(time_second, needswap);
1866 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1867 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1868 cgp->cg_time = ufs_rw64(time_second, needswap);
1869 cgbno = dtogd(fs, bno);
1870 blksfree = cg_blksfree(cgp, needswap);
1871 mutex_enter(&ump->um_lock);
1872 if (size == fs->fs_bsize) {
1873 fragno = ffs_fragstoblks(fs, cgbno);
1874 if (!ffs_isfreeblock(fs, blksfree, fragno)) {
1875 if (devvp_is_snapshot) {
1876 mutex_exit(&ump->um_lock);
1877 return;
1878 }
1879 printf("dev = 0x%llx, block = %" PRId64 ", fs = %s\n",
1880 (unsigned long long)dev, bno, fs->fs_fsmnt);
1881 panic("blkfree: freeing free block");
1882 }
1883 ffs_setblock(fs, blksfree, fragno);
1884 ffs_clusteracct(fs, cgp, fragno, 1);
1885 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1886 fs->fs_cstotal.cs_nbfree++;
1887 fs->fs_cs(fs, cg).cs_nbfree++;
1888 if ((fs->fs_magic == FS_UFS1_MAGIC) &&
1889 ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
1890 i = old_cbtocylno(fs, cgbno);
1891 KASSERT(i >= 0);
1892 KASSERT(i < fs->fs_old_ncyl);
1893 KASSERT(old_cbtorpos(fs, cgbno) >= 0);
1894 KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, cgbno) < fs->fs_old_nrpos);
1895 ufs_add16(old_cg_blks(fs, cgp, i, needswap)[old_cbtorpos(fs, cgbno)], 1,
1896 needswap);
1897 ufs_add32(old_cg_blktot(cgp, needswap)[i], 1, needswap);
1898 }
1899 } else {
1900 bbase = cgbno - ffs_fragnum(fs, cgbno);
1901 /*
1902 * decrement the counts associated with the old frags
1903 */
1904 blk = blkmap(fs, blksfree, bbase);
1905 ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
1906 /*
1907 * deallocate the fragment
1908 */
1909 frags = ffs_numfrags(fs, size);
1910 for (i = 0; i < frags; i++) {
1911 if (isset(blksfree, cgbno + i)) {
1912 printf("dev = 0x%llx, block = %" PRId64
1913 ", fs = %s\n",
1914 (unsigned long long)dev, bno + i,
1915 fs->fs_fsmnt);
1916 panic("blkfree: freeing free frag");
1917 }
1918 setbit(blksfree, cgbno + i);
1919 }
1920 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1921 fs->fs_cstotal.cs_nffree += i;
1922 fs->fs_cs(fs, cg).cs_nffree += i;
1923 /*
1924 * add back in counts associated with the new frags
1925 */
1926 blk = blkmap(fs, blksfree, bbase);
1927 ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
1928 /*
1929 * if a complete block has been reassembled, account for it
1930 */
1931 fragno = ffs_fragstoblks(fs, bbase);
1932 if (ffs_isblock(fs, blksfree, fragno)) {
1933 ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
1934 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1935 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1936 ffs_clusteracct(fs, cgp, fragno, 1);
1937 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1938 fs->fs_cstotal.cs_nbfree++;
1939 fs->fs_cs(fs, cg).cs_nbfree++;
1940 if ((fs->fs_magic == FS_UFS1_MAGIC) &&
1941 ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
1942 i = old_cbtocylno(fs, bbase);
1943 KASSERT(i >= 0);
1944 KASSERT(i < fs->fs_old_ncyl);
1945 KASSERT(old_cbtorpos(fs, bbase) >= 0);
1946 KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, bbase) < fs->fs_old_nrpos);
1947 ufs_add16(old_cg_blks(fs, cgp, i, needswap)[old_cbtorpos(fs,
1948 bbase)], 1, needswap);
1949 ufs_add32(old_cg_blktot(cgp, needswap)[i], 1, needswap);
1950 }
1951 }
1952 }
1953 fs->fs_fmod = 1;
1954 ACTIVECG_CLR(fs, cg);
1955 mutex_exit(&ump->um_lock);
1956 }
1957
1958 /*
1959 * Free an inode.
1960 */
1961 int
1962 ffs_vfree(struct vnode *vp, ino_t ino, int mode)
1963 {
1964
1965 return ffs_freefile(vp->v_mount, ino, mode);
1966 }
1967
1968 /*
1969 * Do the actual free operation.
1970 * The specified inode is placed back in the free map.
1971 *
1972 * => um_lock not held on entry or exit
1973 */
1974 int
1975 ffs_freefile(struct mount *mp, ino_t ino, int mode)
1976 {
1977 struct ufsmount *ump = VFSTOUFS(mp);
1978 struct fs *fs = ump->um_fs;
1979 struct vnode *devvp;
1980 struct cg *cgp;
1981 struct buf *bp;
1982 int error, cg;
1983 daddr_t cgbno;
1984 dev_t dev;
1985 const int needswap = UFS_FSNEEDSWAP(fs);
1986
1987 cg = ino_to_cg(fs, ino);
1988 devvp = ump->um_devvp;
1989 dev = devvp->v_rdev;
1990 cgbno = FFS_FSBTODB(fs, cgtod(fs, cg));
1991
1992 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1993 panic("ifree: range: dev = 0x%llx, ino = %llu, fs = %s",
1994 (long long)dev, (unsigned long long)ino, fs->fs_fsmnt);
1995 error = bread(devvp, cgbno, (int)fs->fs_cgsize,
1996 NOCRED, B_MODIFY, &bp);
1997 if (error) {
1998 return (error);
1999 }
2000 cgp = (struct cg *)bp->b_data;
2001 if (!cg_chkmagic(cgp, needswap)) {
2002 brelse(bp, 0);
2003 return (0);
2004 }
2005
2006 ffs_freefile_common(ump, fs, dev, bp, ino, mode, false);
2007
2008 bdwrite(bp);
2009
2010 return 0;
2011 }
2012
2013 int
2014 ffs_freefile_snap(struct fs *fs, struct vnode *devvp, ino_t ino, int mode)
2015 {
2016 struct ufsmount *ump;
2017 struct cg *cgp;
2018 struct buf *bp;
2019 int error, cg;
2020 daddr_t cgbno;
2021 dev_t dev;
2022 const int needswap = UFS_FSNEEDSWAP(fs);
2023
2024 KASSERT(devvp->v_type != VBLK);
2025
2026 cg = ino_to_cg(fs, ino);
2027 dev = VTOI(devvp)->i_devvp->v_rdev;
2028 ump = VFSTOUFS(devvp->v_mount);
2029 cgbno = ffs_fragstoblks(fs, cgtod(fs, cg));
2030 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
2031 panic("ifree: range: dev = 0x%llx, ino = %llu, fs = %s",
2032 (unsigned long long)dev, (unsigned long long)ino,
2033 fs->fs_fsmnt);
2034 error = bread(devvp, cgbno, (int)fs->fs_cgsize,
2035 NOCRED, B_MODIFY, &bp);
2036 if (error) {
2037 return (error);
2038 }
2039 cgp = (struct cg *)bp->b_data;
2040 if (!cg_chkmagic(cgp, needswap)) {
2041 brelse(bp, 0);
2042 return (0);
2043 }
2044 ffs_freefile_common(ump, fs, dev, bp, ino, mode, true);
2045
2046 bdwrite(bp);
2047
2048 return 0;
2049 }
2050
2051 static void
2052 ffs_freefile_common(struct ufsmount *ump, struct fs *fs, dev_t dev,
2053 struct buf *bp, ino_t ino, int mode, bool devvp_is_snapshot)
2054 {
2055 int cg;
2056 struct cg *cgp;
2057 u_int8_t *inosused;
2058 const int needswap = UFS_FSNEEDSWAP(fs);
2059
2060 cg = ino_to_cg(fs, ino);
2061 cgp = (struct cg *)bp->b_data;
2062 cgp->cg_old_time = ufs_rw32(time_second, needswap);
2063 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
2064 (fs->fs_old_flags & FS_FLAGS_UPDATED))
2065 cgp->cg_time = ufs_rw64(time_second, needswap);
2066 inosused = cg_inosused(cgp, needswap);
2067 ino %= fs->fs_ipg;
2068 if (isclr(inosused, ino)) {
2069 printf("ifree: dev = 0x%llx, ino = %llu, fs = %s\n",
2070 (unsigned long long)dev, (unsigned long long)ino +
2071 cg * fs->fs_ipg, fs->fs_fsmnt);
2072 if (fs->fs_ronly == 0)
2073 panic("ifree: freeing free inode");
2074 }
2075 clrbit(inosused, ino);
2076 if (!devvp_is_snapshot)
2077 UFS_WAPBL_UNREGISTER_INODE(ump->um_mountp,
2078 ino + cg * fs->fs_ipg, mode);
2079 if (ino < ufs_rw32(cgp->cg_irotor, needswap))
2080 cgp->cg_irotor = ufs_rw32(ino, needswap);
2081 ufs_add32(cgp->cg_cs.cs_nifree, 1, needswap);
2082 mutex_enter(&ump->um_lock);
2083 fs->fs_cstotal.cs_nifree++;
2084 fs->fs_cs(fs, cg).cs_nifree++;
2085 if ((mode & IFMT) == IFDIR) {
2086 ufs_add32(cgp->cg_cs.cs_ndir, -1, needswap);
2087 fs->fs_cstotal.cs_ndir--;
2088 fs->fs_cs(fs, cg).cs_ndir--;
2089 }
2090 fs->fs_fmod = 1;
2091 ACTIVECG_CLR(fs, cg);
2092 mutex_exit(&ump->um_lock);
2093 }
2094
2095 /*
2096 * Check to see if a file is free.
2097 */
2098 int
2099 ffs_checkfreefile(struct fs *fs, struct vnode *devvp, ino_t ino)
2100 {
2101 struct cg *cgp;
2102 struct buf *bp;
2103 daddr_t cgbno;
2104 int ret, cg;
2105 u_int8_t *inosused;
2106 const bool devvp_is_snapshot = (devvp->v_type != VBLK);
2107
2108 KASSERT(devvp_is_snapshot);
2109
2110 cg = ino_to_cg(fs, ino);
2111 if (devvp_is_snapshot)
2112 cgbno = ffs_fragstoblks(fs, cgtod(fs, cg));
2113 else
2114 cgbno = FFS_FSBTODB(fs, cgtod(fs, cg));
2115 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
2116 return 1;
2117 if (bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, 0, &bp)) {
2118 return 1;
2119 }
2120 cgp = (struct cg *)bp->b_data;
2121 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
2122 brelse(bp, 0);
2123 return 1;
2124 }
2125 inosused = cg_inosused(cgp, UFS_FSNEEDSWAP(fs));
2126 ino %= fs->fs_ipg;
2127 ret = isclr(inosused, ino);
2128 brelse(bp, 0);
2129 return ret;
2130 }
2131
2132 /*
2133 * Find a block of the specified size in the specified cylinder group.
2134 *
2135 * It is a panic if a request is made to find a block if none are
2136 * available.
2137 */
2138 static int32_t
2139 ffs_mapsearch(struct fs *fs, struct cg *cgp, daddr_t bpref, int allocsiz)
2140 {
2141 int32_t bno;
2142 int start, len, loc, i;
2143 int blk, field, subfield, pos;
2144 int ostart, olen;
2145 u_int8_t *blksfree;
2146 const int needswap = UFS_FSNEEDSWAP(fs);
2147
2148 /* KASSERT(mutex_owned(&ump->um_lock)); */
2149
2150 /*
2151 * find the fragment by searching through the free block
2152 * map for an appropriate bit pattern
2153 */
2154 if (bpref)
2155 start = dtogd(fs, bpref) / NBBY;
2156 else
2157 start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
2158 blksfree = cg_blksfree(cgp, needswap);
2159 len = howmany(fs->fs_fpg, NBBY) - start;
2160 ostart = start;
2161 olen = len;
2162 loc = scanc((u_int)len,
2163 (const u_char *)&blksfree[start],
2164 (const u_char *)fragtbl[fs->fs_frag],
2165 (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
2166 if (loc == 0) {
2167 len = start + 1;
2168 start = 0;
2169 loc = scanc((u_int)len,
2170 (const u_char *)&blksfree[0],
2171 (const u_char *)fragtbl[fs->fs_frag],
2172 (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
2173 if (loc == 0) {
2174 printf("start = %d, len = %d, fs = %s\n",
2175 ostart, olen, fs->fs_fsmnt);
2176 printf("offset=%d %ld\n",
2177 ufs_rw32(cgp->cg_freeoff, needswap),
2178 (long)blksfree - (long)cgp);
2179 printf("cg %d\n", cgp->cg_cgx);
2180 panic("ffs_alloccg: map corrupted");
2181 /* NOTREACHED */
2182 }
2183 }
2184 bno = (start + len - loc) * NBBY;
2185 cgp->cg_frotor = ufs_rw32(bno, needswap);
2186 /*
2187 * found the byte in the map
2188 * sift through the bits to find the selected frag
2189 */
2190 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
2191 blk = blkmap(fs, blksfree, bno);
2192 blk <<= 1;
2193 field = around[allocsiz];
2194 subfield = inside[allocsiz];
2195 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
2196 if ((blk & field) == subfield)
2197 return (bno + pos);
2198 field <<= 1;
2199 subfield <<= 1;
2200 }
2201 }
2202 printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
2203 panic("ffs_alloccg: block not in map");
2204 /* return (-1); */
2205 }
2206
2207 /*
2208 * Fserr prints the name of a file system with an error diagnostic.
2209 *
2210 * The form of the error message is:
2211 * fs: error message
2212 */
2213 static void
2214 ffs_fserr(struct fs *fs, u_int uid, const char *cp)
2215 {
2216
2217 log(LOG_ERR, "uid %d, pid %d, command %s, on %s: %s\n",
2218 uid, curproc->p_pid, curproc->p_comm, fs->fs_fsmnt, cp);
2219 }
2220