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