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