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