1 /*-
2 * Copyright (c) 2002 Networks Associates Technology, Inc.
3 * All rights reserved.
4 *
5 * This software was developed for the FreeBSD Project by Marshall
6 * Kirk McKusick and Network Associates Laboratories, the Security
7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
9 * research program
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * Copyright (c) 1982, 1986, 1989, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * @(#)ffs_alloc.c 8.18 (Berkeley) 5/26/95
60 */
61
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD: releng/8.2/sys/ufs/ffs/ffs_alloc.c 204375 2010-02-26 21:49:11Z mckusick $");
64
65 #include "opt_quota.h"
66
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/bio.h>
70 #include <sys/buf.h>
71 #include <sys/conf.h>
72 #include <sys/file.h>
73 #include <sys/filedesc.h>
74 #include <sys/priv.h>
75 #include <sys/proc.h>
76 #include <sys/vnode.h>
77 #include <sys/mount.h>
78 #include <sys/kernel.h>
79 #include <sys/sysctl.h>
80 #include <sys/syslog.h>
81
82 #include <ufs/ufs/extattr.h>
83 #include <ufs/ufs/quota.h>
84 #include <ufs/ufs/inode.h>
85 #include <ufs/ufs/ufs_extern.h>
86 #include <ufs/ufs/ufsmount.h>
87
88 #include <ufs/ffs/fs.h>
89 #include <ufs/ffs/ffs_extern.h>
90
91 typedef ufs2_daddr_t allocfcn_t(struct inode *ip, u_int cg, ufs2_daddr_t bpref,
92 int size);
93
94 static ufs2_daddr_t ffs_alloccg(struct inode *, u_int, ufs2_daddr_t, int);
95 static ufs2_daddr_t
96 ffs_alloccgblk(struct inode *, struct buf *, ufs2_daddr_t);
97 #ifdef INVARIANTS
98 static int ffs_checkblk(struct inode *, ufs2_daddr_t, long);
99 #endif
100 static ufs2_daddr_t ffs_clusteralloc(struct inode *, u_int, ufs2_daddr_t, int);
101 static void ffs_clusteracct(struct ufsmount *, struct fs *, struct cg *,
102 ufs1_daddr_t, int);
103 static ino_t ffs_dirpref(struct inode *);
104 static ufs2_daddr_t ffs_fragextend(struct inode *, u_int, ufs2_daddr_t,
105 int, int);
106 static void ffs_fserr(struct fs *, ino_t, char *);
107 static ufs2_daddr_t ffs_hashalloc
108 (struct inode *, u_int, ufs2_daddr_t, int, allocfcn_t *);
109 static ufs2_daddr_t ffs_nodealloccg(struct inode *, u_int, ufs2_daddr_t, int);
110 static ufs1_daddr_t ffs_mapsearch(struct fs *, struct cg *, ufs2_daddr_t, int);
111 static int ffs_reallocblks_ufs1(struct vop_reallocblks_args *);
112 static int ffs_reallocblks_ufs2(struct vop_reallocblks_args *);
113
114 /*
115 * Allocate a block in the filesystem.
116 *
117 * The size of the requested block is given, which must be some
118 * multiple of fs_fsize and <= fs_bsize.
119 * A preference may be optionally specified. If a preference is given
120 * the following hierarchy is used to allocate a block:
121 * 1) allocate the requested block.
122 * 2) allocate a rotationally optimal block in the same cylinder.
123 * 3) allocate a block in the same cylinder group.
124 * 4) quadradically rehash into other cylinder groups, until an
125 * available block is located.
126 * If no block preference is given the following hierarchy is used
127 * to allocate a block:
128 * 1) allocate a block in the cylinder group that contains the
129 * inode for the file.
130 * 2) quadradically rehash into other cylinder groups, until an
131 * available block is located.
132 */
133 int
134 ffs_alloc(ip, lbn, bpref, size, flags, cred, bnp)
135 struct inode *ip;
136 ufs2_daddr_t lbn, bpref;
137 int size, flags;
138 struct ucred *cred;
139 ufs2_daddr_t *bnp;
140 {
141 struct fs *fs;
142 struct ufsmount *ump;
143 ufs2_daddr_t bno;
144 u_int cg, reclaimed;
145 static struct timeval lastfail;
146 static int curfail;
147 int64_t delta;
148 #ifdef QUOTA
149 int error;
150 #endif
151
152 *bnp = 0;
153 fs = ip->i_fs;
154 ump = ip->i_ump;
155 mtx_assert(UFS_MTX(ump), MA_OWNED);
156 #ifdef INVARIANTS
157 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
158 printf("dev = %s, bsize = %ld, size = %d, fs = %s\n",
159 devtoname(ip->i_dev), (long)fs->fs_bsize, size,
160 fs->fs_fsmnt);
161 panic("ffs_alloc: bad size");
162 }
163 if (cred == NOCRED)
164 panic("ffs_alloc: missing credential");
165 #endif /* INVARIANTS */
166 reclaimed = 0;
167 retry:
168 #ifdef QUOTA
169 UFS_UNLOCK(ump);
170 error = chkdq(ip, btodb(size), cred, 0);
171 if (error)
172 return (error);
173 UFS_LOCK(ump);
174 #endif
175 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
176 goto nospace;
177 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0) &&
178 freespace(fs, fs->fs_minfree) - numfrags(fs, size) < 0)
179 goto nospace;
180 if (bpref >= fs->fs_size)
181 bpref = 0;
182 if (bpref == 0)
183 cg = ino_to_cg(fs, ip->i_number);
184 else
185 cg = dtog(fs, bpref);
186 bno = ffs_hashalloc(ip, cg, bpref, size, ffs_alloccg);
187 if (bno > 0) {
188 delta = btodb(size);
189 if (ip->i_flag & IN_SPACECOUNTED) {
190 UFS_LOCK(ump);
191 fs->fs_pendingblocks += delta;
192 UFS_UNLOCK(ump);
193 }
194 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
195 if (flags & IO_EXT)
196 ip->i_flag |= IN_CHANGE;
197 else
198 ip->i_flag |= IN_CHANGE | IN_UPDATE;
199 *bnp = bno;
200 return (0);
201 }
202 nospace:
203 #ifdef QUOTA
204 UFS_UNLOCK(ump);
205 /*
206 * Restore user's disk quota because allocation failed.
207 */
208 (void) chkdq(ip, -btodb(size), cred, FORCE);
209 UFS_LOCK(ump);
210 #endif
211 if (fs->fs_pendingblocks > 0 && reclaimed == 0) {
212 reclaimed = 1;
213 softdep_request_cleanup(fs, ITOV(ip));
214 goto retry;
215 }
216 UFS_UNLOCK(ump);
217 if (ppsratecheck(&lastfail, &curfail, 1)) {
218 ffs_fserr(fs, ip->i_number, "filesystem full");
219 uprintf("\n%s: write failed, filesystem is full\n",
220 fs->fs_fsmnt);
221 }
222 return (ENOSPC);
223 }
224
225 /*
226 * Reallocate a fragment to a bigger size
227 *
228 * The number and size of the old block is given, and a preference
229 * and new size is also specified. The allocator attempts to extend
230 * the original block. Failing that, the regular block allocator is
231 * invoked to get an appropriate block.
232 */
233 int
234 ffs_realloccg(ip, lbprev, bprev, bpref, osize, nsize, flags, cred, bpp)
235 struct inode *ip;
236 ufs2_daddr_t lbprev;
237 ufs2_daddr_t bprev;
238 ufs2_daddr_t bpref;
239 int osize, nsize, flags;
240 struct ucred *cred;
241 struct buf **bpp;
242 {
243 struct vnode *vp;
244 struct fs *fs;
245 struct buf *bp;
246 struct ufsmount *ump;
247 u_int cg, request, reclaimed;
248 int error;
249 ufs2_daddr_t bno;
250 static struct timeval lastfail;
251 static int curfail;
252 int64_t delta;
253
254 *bpp = 0;
255 vp = ITOV(ip);
256 fs = ip->i_fs;
257 bp = NULL;
258 ump = ip->i_ump;
259 mtx_assert(UFS_MTX(ump), MA_OWNED);
260 #ifdef INVARIANTS
261 if (vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
262 panic("ffs_realloccg: allocation on suspended filesystem");
263 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
264 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
265 printf(
266 "dev = %s, bsize = %ld, osize = %d, nsize = %d, fs = %s\n",
267 devtoname(ip->i_dev), (long)fs->fs_bsize, osize,
268 nsize, fs->fs_fsmnt);
269 panic("ffs_realloccg: bad size");
270 }
271 if (cred == NOCRED)
272 panic("ffs_realloccg: missing credential");
273 #endif /* INVARIANTS */
274 reclaimed = 0;
275 retry:
276 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0) &&
277 freespace(fs, fs->fs_minfree) - numfrags(fs, nsize - osize) < 0) {
278 goto nospace;
279 }
280 if (bprev == 0) {
281 printf("dev = %s, bsize = %ld, bprev = %jd, fs = %s\n",
282 devtoname(ip->i_dev), (long)fs->fs_bsize, (intmax_t)bprev,
283 fs->fs_fsmnt);
284 panic("ffs_realloccg: bad bprev");
285 }
286 UFS_UNLOCK(ump);
287 /*
288 * Allocate the extra space in the buffer.
289 */
290 error = bread(vp, lbprev, osize, NOCRED, &bp);
291 if (error) {
292 brelse(bp);
293 return (error);
294 }
295
296 if (bp->b_blkno == bp->b_lblkno) {
297 if (lbprev >= NDADDR)
298 panic("ffs_realloccg: lbprev out of range");
299 bp->b_blkno = fsbtodb(fs, bprev);
300 }
301
302 #ifdef QUOTA
303 error = chkdq(ip, btodb(nsize - osize), cred, 0);
304 if (error) {
305 brelse(bp);
306 return (error);
307 }
308 #endif
309 /*
310 * Check for extension in the existing location.
311 */
312 cg = dtog(fs, bprev);
313 UFS_LOCK(ump);
314 bno = ffs_fragextend(ip, cg, bprev, osize, nsize);
315 if (bno) {
316 if (bp->b_blkno != fsbtodb(fs, bno))
317 panic("ffs_realloccg: bad blockno");
318 delta = btodb(nsize - osize);
319 if (ip->i_flag & IN_SPACECOUNTED) {
320 UFS_LOCK(ump);
321 fs->fs_pendingblocks += delta;
322 UFS_UNLOCK(ump);
323 }
324 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
325 if (flags & IO_EXT)
326 ip->i_flag |= IN_CHANGE;
327 else
328 ip->i_flag |= IN_CHANGE | IN_UPDATE;
329 allocbuf(bp, nsize);
330 bp->b_flags |= B_DONE;
331 bzero(bp->b_data + osize, nsize - osize);
332 if ((bp->b_flags & (B_MALLOC | B_VMIO)) == B_VMIO)
333 vfs_bio_set_valid(bp, osize, nsize - osize);
334 *bpp = bp;
335 return (0);
336 }
337 /*
338 * Allocate a new disk location.
339 */
340 if (bpref >= fs->fs_size)
341 bpref = 0;
342 switch ((int)fs->fs_optim) {
343 case FS_OPTSPACE:
344 /*
345 * Allocate an exact sized fragment. Although this makes
346 * best use of space, we will waste time relocating it if
347 * the file continues to grow. If the fragmentation is
348 * less than half of the minimum free reserve, we choose
349 * to begin optimizing for time.
350 */
351 request = nsize;
352 if (fs->fs_minfree <= 5 ||
353 fs->fs_cstotal.cs_nffree >
354 (off_t)fs->fs_dsize * fs->fs_minfree / (2 * 100))
355 break;
356 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
357 fs->fs_fsmnt);
358 fs->fs_optim = FS_OPTTIME;
359 break;
360 case FS_OPTTIME:
361 /*
362 * At this point we have discovered a file that is trying to
363 * grow a small fragment to a larger fragment. To save time,
364 * we allocate a full sized block, then free the unused portion.
365 * If the file continues to grow, the `ffs_fragextend' call
366 * above will be able to grow it in place without further
367 * copying. If aberrant programs cause disk fragmentation to
368 * grow within 2% of the free reserve, we choose to begin
369 * optimizing for space.
370 */
371 request = fs->fs_bsize;
372 if (fs->fs_cstotal.cs_nffree <
373 (off_t)fs->fs_dsize * (fs->fs_minfree - 2) / 100)
374 break;
375 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
376 fs->fs_fsmnt);
377 fs->fs_optim = FS_OPTSPACE;
378 break;
379 default:
380 printf("dev = %s, optim = %ld, fs = %s\n",
381 devtoname(ip->i_dev), (long)fs->fs_optim, fs->fs_fsmnt);
382 panic("ffs_realloccg: bad optim");
383 /* NOTREACHED */
384 }
385 bno = ffs_hashalloc(ip, cg, bpref, request, ffs_alloccg);
386 if (bno > 0) {
387 bp->b_blkno = fsbtodb(fs, bno);
388 if (!DOINGSOFTDEP(vp))
389 ffs_blkfree(ump, fs, ip->i_devvp, bprev, (long)osize,
390 ip->i_number);
391 if (nsize < request)
392 ffs_blkfree(ump, fs, ip->i_devvp,
393 bno + numfrags(fs, nsize),
394 (long)(request - nsize), ip->i_number);
395 delta = btodb(nsize - osize);
396 if (ip->i_flag & IN_SPACECOUNTED) {
397 UFS_LOCK(ump);
398 fs->fs_pendingblocks += delta;
399 UFS_UNLOCK(ump);
400 }
401 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
402 if (flags & IO_EXT)
403 ip->i_flag |= IN_CHANGE;
404 else
405 ip->i_flag |= IN_CHANGE | IN_UPDATE;
406 allocbuf(bp, nsize);
407 bp->b_flags |= B_DONE;
408 bzero(bp->b_data + osize, nsize - osize);
409 if ((bp->b_flags & (B_MALLOC | B_VMIO)) == B_VMIO)
410 vfs_bio_set_valid(bp, osize, nsize - osize);
411 *bpp = bp;
412 return (0);
413 }
414 #ifdef QUOTA
415 UFS_UNLOCK(ump);
416 /*
417 * Restore user's disk quota because allocation failed.
418 */
419 (void) chkdq(ip, -btodb(nsize - osize), cred, FORCE);
420 UFS_LOCK(ump);
421 #endif
422 nospace:
423 /*
424 * no space available
425 */
426 if (fs->fs_pendingblocks > 0 && reclaimed == 0) {
427 reclaimed = 1;
428 softdep_request_cleanup(fs, vp);
429 UFS_UNLOCK(ump);
430 if (bp) {
431 brelse(bp);
432 bp = NULL;
433 }
434 UFS_LOCK(ump);
435 goto retry;
436 }
437 UFS_UNLOCK(ump);
438 if (bp)
439 brelse(bp);
440 if (ppsratecheck(&lastfail, &curfail, 1)) {
441 ffs_fserr(fs, ip->i_number, "filesystem full");
442 uprintf("\n%s: write failed, filesystem is full\n",
443 fs->fs_fsmnt);
444 }
445 return (ENOSPC);
446 }
447
448 /*
449 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
450 *
451 * The vnode and an array of buffer pointers for a range of sequential
452 * logical blocks to be made contiguous is given. The allocator attempts
453 * to find a range of sequential blocks starting as close as possible
454 * from the end of the allocation for the logical block immediately
455 * preceding the current range. If successful, the physical block numbers
456 * in the buffer pointers and in the inode are changed to reflect the new
457 * allocation. If unsuccessful, the allocation is left unchanged. The
458 * success in doing the reallocation is returned. Note that the error
459 * return is not reflected back to the user. Rather the previous block
460 * allocation will be used.
461 */
462
463 SYSCTL_NODE(_vfs, OID_AUTO, ffs, CTLFLAG_RW, 0, "FFS filesystem");
464
465 static int doasyncfree = 1;
466 SYSCTL_INT(_vfs_ffs, OID_AUTO, doasyncfree, CTLFLAG_RW, &doasyncfree, 0, "");
467
468 static int doreallocblks = 1;
469 SYSCTL_INT(_vfs_ffs, OID_AUTO, doreallocblks, CTLFLAG_RW, &doreallocblks, 0, "");
470
471 #ifdef DEBUG
472 static volatile int prtrealloc = 0;
473 #endif
474
475 int
476 ffs_reallocblks(ap)
477 struct vop_reallocblks_args /* {
478 struct vnode *a_vp;
479 struct cluster_save *a_buflist;
480 } */ *ap;
481 {
482
483 if (doreallocblks == 0)
484 return (ENOSPC);
485 if (VTOI(ap->a_vp)->i_ump->um_fstype == UFS1)
486 return (ffs_reallocblks_ufs1(ap));
487 return (ffs_reallocblks_ufs2(ap));
488 }
489
490 static int
491 ffs_reallocblks_ufs1(ap)
492 struct vop_reallocblks_args /* {
493 struct vnode *a_vp;
494 struct cluster_save *a_buflist;
495 } */ *ap;
496 {
497 struct fs *fs;
498 struct inode *ip;
499 struct vnode *vp;
500 struct buf *sbp, *ebp;
501 ufs1_daddr_t *bap, *sbap, *ebap = 0;
502 struct cluster_save *buflist;
503 struct ufsmount *ump;
504 ufs_lbn_t start_lbn, end_lbn;
505 ufs1_daddr_t soff, newblk, blkno;
506 ufs2_daddr_t pref;
507 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
508 int i, len, start_lvl, end_lvl, ssize;
509
510 vp = ap->a_vp;
511 ip = VTOI(vp);
512 fs = ip->i_fs;
513 ump = ip->i_ump;
514 if (fs->fs_contigsumsize <= 0)
515 return (ENOSPC);
516 buflist = ap->a_buflist;
517 len = buflist->bs_nchildren;
518 start_lbn = buflist->bs_children[0]->b_lblkno;
519 end_lbn = start_lbn + len - 1;
520 #ifdef INVARIANTS
521 for (i = 0; i < len; i++)
522 if (!ffs_checkblk(ip,
523 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
524 panic("ffs_reallocblks: unallocated block 1");
525 for (i = 1; i < len; i++)
526 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
527 panic("ffs_reallocblks: non-logical cluster");
528 blkno = buflist->bs_children[0]->b_blkno;
529 ssize = fsbtodb(fs, fs->fs_frag);
530 for (i = 1; i < len - 1; i++)
531 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
532 panic("ffs_reallocblks: non-physical cluster %d", i);
533 #endif
534 /*
535 * If the latest allocation is in a new cylinder group, assume that
536 * the filesystem has decided to move and do not force it back to
537 * the previous cylinder group.
538 */
539 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
540 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
541 return (ENOSPC);
542 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
543 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
544 return (ENOSPC);
545 /*
546 * Get the starting offset and block map for the first block.
547 */
548 if (start_lvl == 0) {
549 sbap = &ip->i_din1->di_db[0];
550 soff = start_lbn;
551 } else {
552 idp = &start_ap[start_lvl - 1];
553 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
554 brelse(sbp);
555 return (ENOSPC);
556 }
557 sbap = (ufs1_daddr_t *)sbp->b_data;
558 soff = idp->in_off;
559 }
560 /*
561 * If the block range spans two block maps, get the second map.
562 */
563 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
564 ssize = len;
565 } else {
566 #ifdef INVARIANTS
567 if (start_lvl > 0 &&
568 start_ap[start_lvl - 1].in_lbn == idp->in_lbn)
569 panic("ffs_reallocblk: start == end");
570 #endif
571 ssize = len - (idp->in_off + 1);
572 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
573 goto fail;
574 ebap = (ufs1_daddr_t *)ebp->b_data;
575 }
576 /*
577 * Find the preferred location for the cluster.
578 */
579 UFS_LOCK(ump);
580 pref = ffs_blkpref_ufs1(ip, start_lbn, soff, sbap);
581 /*
582 * Search the block map looking for an allocation of the desired size.
583 */
584 if ((newblk = ffs_hashalloc(ip, dtog(fs, pref), pref,
585 len, ffs_clusteralloc)) == 0) {
586 UFS_UNLOCK(ump);
587 goto fail;
588 }
589 /*
590 * We have found a new contiguous block.
591 *
592 * First we have to replace the old block pointers with the new
593 * block pointers in the inode and indirect blocks associated
594 * with the file.
595 */
596 #ifdef DEBUG
597 if (prtrealloc)
598 printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number,
599 (intmax_t)start_lbn, (intmax_t)end_lbn);
600 #endif
601 blkno = newblk;
602 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
603 if (i == ssize) {
604 bap = ebap;
605 soff = -i;
606 }
607 #ifdef INVARIANTS
608 if (!ffs_checkblk(ip,
609 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
610 panic("ffs_reallocblks: unallocated block 2");
611 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
612 panic("ffs_reallocblks: alloc mismatch");
613 #endif
614 #ifdef DEBUG
615 if (prtrealloc)
616 printf(" %d,", *bap);
617 #endif
618 if (DOINGSOFTDEP(vp)) {
619 if (sbap == &ip->i_din1->di_db[0] && i < ssize)
620 softdep_setup_allocdirect(ip, start_lbn + i,
621 blkno, *bap, fs->fs_bsize, fs->fs_bsize,
622 buflist->bs_children[i]);
623 else
624 softdep_setup_allocindir_page(ip, start_lbn + i,
625 i < ssize ? sbp : ebp, soff + i, blkno,
626 *bap, buflist->bs_children[i]);
627 }
628 *bap++ = blkno;
629 }
630 /*
631 * Next we must write out the modified inode and indirect blocks.
632 * For strict correctness, the writes should be synchronous since
633 * the old block values may have been written to disk. In practise
634 * they are almost never written, but if we are concerned about
635 * strict correctness, the `doasyncfree' flag should be set to zero.
636 *
637 * The test on `doasyncfree' should be changed to test a flag
638 * that shows whether the associated buffers and inodes have
639 * been written. The flag should be set when the cluster is
640 * started and cleared whenever the buffer or inode is flushed.
641 * We can then check below to see if it is set, and do the
642 * synchronous write only when it has been cleared.
643 */
644 if (sbap != &ip->i_din1->di_db[0]) {
645 if (doasyncfree)
646 bdwrite(sbp);
647 else
648 bwrite(sbp);
649 } else {
650 ip->i_flag |= IN_CHANGE | IN_UPDATE;
651 if (!doasyncfree)
652 ffs_update(vp, 1);
653 }
654 if (ssize < len) {
655 if (doasyncfree)
656 bdwrite(ebp);
657 else
658 bwrite(ebp);
659 }
660 /*
661 * Last, free the old blocks and assign the new blocks to the buffers.
662 */
663 #ifdef DEBUG
664 if (prtrealloc)
665 printf("\n\tnew:");
666 #endif
667 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
668 if (!DOINGSOFTDEP(vp))
669 ffs_blkfree(ump, fs, ip->i_devvp,
670 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
671 fs->fs_bsize, ip->i_number);
672 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
673 #ifdef INVARIANTS
674 if (!ffs_checkblk(ip,
675 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
676 panic("ffs_reallocblks: unallocated block 3");
677 #endif
678 #ifdef DEBUG
679 if (prtrealloc)
680 printf(" %d,", blkno);
681 #endif
682 }
683 #ifdef DEBUG
684 if (prtrealloc) {
685 prtrealloc--;
686 printf("\n");
687 }
688 #endif
689 return (0);
690
691 fail:
692 if (ssize < len)
693 brelse(ebp);
694 if (sbap != &ip->i_din1->di_db[0])
695 brelse(sbp);
696 return (ENOSPC);
697 }
698
699 static int
700 ffs_reallocblks_ufs2(ap)
701 struct vop_reallocblks_args /* {
702 struct vnode *a_vp;
703 struct cluster_save *a_buflist;
704 } */ *ap;
705 {
706 struct fs *fs;
707 struct inode *ip;
708 struct vnode *vp;
709 struct buf *sbp, *ebp;
710 ufs2_daddr_t *bap, *sbap, *ebap = 0;
711 struct cluster_save *buflist;
712 struct ufsmount *ump;
713 ufs_lbn_t start_lbn, end_lbn;
714 ufs2_daddr_t soff, newblk, blkno, pref;
715 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
716 int i, len, start_lvl, end_lvl, ssize;
717
718 vp = ap->a_vp;
719 ip = VTOI(vp);
720 fs = ip->i_fs;
721 ump = ip->i_ump;
722 if (fs->fs_contigsumsize <= 0)
723 return (ENOSPC);
724 buflist = ap->a_buflist;
725 len = buflist->bs_nchildren;
726 start_lbn = buflist->bs_children[0]->b_lblkno;
727 end_lbn = start_lbn + len - 1;
728 #ifdef INVARIANTS
729 for (i = 0; i < len; i++)
730 if (!ffs_checkblk(ip,
731 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
732 panic("ffs_reallocblks: unallocated block 1");
733 for (i = 1; i < len; i++)
734 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
735 panic("ffs_reallocblks: non-logical cluster");
736 blkno = buflist->bs_children[0]->b_blkno;
737 ssize = fsbtodb(fs, fs->fs_frag);
738 for (i = 1; i < len - 1; i++)
739 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
740 panic("ffs_reallocblks: non-physical cluster %d", i);
741 #endif
742 /*
743 * If the latest allocation is in a new cylinder group, assume that
744 * the filesystem has decided to move and do not force it back to
745 * the previous cylinder group.
746 */
747 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
748 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
749 return (ENOSPC);
750 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
751 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
752 return (ENOSPC);
753 /*
754 * Get the starting offset and block map for the first block.
755 */
756 if (start_lvl == 0) {
757 sbap = &ip->i_din2->di_db[0];
758 soff = start_lbn;
759 } else {
760 idp = &start_ap[start_lvl - 1];
761 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
762 brelse(sbp);
763 return (ENOSPC);
764 }
765 sbap = (ufs2_daddr_t *)sbp->b_data;
766 soff = idp->in_off;
767 }
768 /*
769 * If the block range spans two block maps, get the second map.
770 */
771 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
772 ssize = len;
773 } else {
774 #ifdef INVARIANTS
775 if (start_lvl > 0 &&
776 start_ap[start_lvl - 1].in_lbn == idp->in_lbn)
777 panic("ffs_reallocblk: start == end");
778 #endif
779 ssize = len - (idp->in_off + 1);
780 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
781 goto fail;
782 ebap = (ufs2_daddr_t *)ebp->b_data;
783 }
784 /*
785 * Find the preferred location for the cluster.
786 */
787 UFS_LOCK(ump);
788 pref = ffs_blkpref_ufs2(ip, start_lbn, soff, sbap);
789 /*
790 * Search the block map looking for an allocation of the desired size.
791 */
792 if ((newblk = ffs_hashalloc(ip, dtog(fs, pref), pref,
793 len, ffs_clusteralloc)) == 0) {
794 UFS_UNLOCK(ump);
795 goto fail;
796 }
797 /*
798 * We have found a new contiguous block.
799 *
800 * First we have to replace the old block pointers with the new
801 * block pointers in the inode and indirect blocks associated
802 * with the file.
803 */
804 #ifdef DEBUG
805 if (prtrealloc)
806 printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number,
807 (intmax_t)start_lbn, (intmax_t)end_lbn);
808 #endif
809 blkno = newblk;
810 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
811 if (i == ssize) {
812 bap = ebap;
813 soff = -i;
814 }
815 #ifdef INVARIANTS
816 if (!ffs_checkblk(ip,
817 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
818 panic("ffs_reallocblks: unallocated block 2");
819 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
820 panic("ffs_reallocblks: alloc mismatch");
821 #endif
822 #ifdef DEBUG
823 if (prtrealloc)
824 printf(" %jd,", (intmax_t)*bap);
825 #endif
826 if (DOINGSOFTDEP(vp)) {
827 if (sbap == &ip->i_din2->di_db[0] && i < ssize)
828 softdep_setup_allocdirect(ip, start_lbn + i,
829 blkno, *bap, fs->fs_bsize, fs->fs_bsize,
830 buflist->bs_children[i]);
831 else
832 softdep_setup_allocindir_page(ip, start_lbn + i,
833 i < ssize ? sbp : ebp, soff + i, blkno,
834 *bap, buflist->bs_children[i]);
835 }
836 *bap++ = blkno;
837 }
838 /*
839 * Next we must write out the modified inode and indirect blocks.
840 * For strict correctness, the writes should be synchronous since
841 * the old block values may have been written to disk. In practise
842 * they are almost never written, but if we are concerned about
843 * strict correctness, the `doasyncfree' flag should be set to zero.
844 *
845 * The test on `doasyncfree' should be changed to test a flag
846 * that shows whether the associated buffers and inodes have
847 * been written. The flag should be set when the cluster is
848 * started and cleared whenever the buffer or inode is flushed.
849 * We can then check below to see if it is set, and do the
850 * synchronous write only when it has been cleared.
851 */
852 if (sbap != &ip->i_din2->di_db[0]) {
853 if (doasyncfree)
854 bdwrite(sbp);
855 else
856 bwrite(sbp);
857 } else {
858 ip->i_flag |= IN_CHANGE | IN_UPDATE;
859 if (!doasyncfree)
860 ffs_update(vp, 1);
861 }
862 if (ssize < len) {
863 if (doasyncfree)
864 bdwrite(ebp);
865 else
866 bwrite(ebp);
867 }
868 /*
869 * Last, free the old blocks and assign the new blocks to the buffers.
870 */
871 #ifdef DEBUG
872 if (prtrealloc)
873 printf("\n\tnew:");
874 #endif
875 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
876 if (!DOINGSOFTDEP(vp))
877 ffs_blkfree(ump, fs, ip->i_devvp,
878 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
879 fs->fs_bsize, ip->i_number);
880 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
881 #ifdef INVARIANTS
882 if (!ffs_checkblk(ip,
883 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
884 panic("ffs_reallocblks: unallocated block 3");
885 #endif
886 #ifdef DEBUG
887 if (prtrealloc)
888 printf(" %jd,", (intmax_t)blkno);
889 #endif
890 }
891 #ifdef DEBUG
892 if (prtrealloc) {
893 prtrealloc--;
894 printf("\n");
895 }
896 #endif
897 return (0);
898
899 fail:
900 if (ssize < len)
901 brelse(ebp);
902 if (sbap != &ip->i_din2->di_db[0])
903 brelse(sbp);
904 return (ENOSPC);
905 }
906
907 /*
908 * Allocate an inode in the filesystem.
909 *
910 * If allocating a directory, use ffs_dirpref to select the inode.
911 * If allocating in a directory, the following hierarchy is followed:
912 * 1) allocate the preferred inode.
913 * 2) allocate an inode in the same cylinder group.
914 * 3) quadradically rehash into other cylinder groups, until an
915 * available inode is located.
916 * If no inode preference is given the following hierarchy is used
917 * to allocate an inode:
918 * 1) allocate an inode in cylinder group 0.
919 * 2) quadradically rehash into other cylinder groups, until an
920 * available inode is located.
921 */
922 int
923 ffs_valloc(pvp, mode, cred, vpp)
924 struct vnode *pvp;
925 int mode;
926 struct ucred *cred;
927 struct vnode **vpp;
928 {
929 struct inode *pip;
930 struct fs *fs;
931 struct inode *ip;
932 struct timespec ts;
933 struct ufsmount *ump;
934 ino_t ino, ipref;
935 u_int cg;
936 int error, error1;
937 static struct timeval lastfail;
938 static int curfail;
939
940 *vpp = NULL;
941 pip = VTOI(pvp);
942 fs = pip->i_fs;
943 ump = pip->i_ump;
944
945 UFS_LOCK(ump);
946 if (fs->fs_cstotal.cs_nifree == 0)
947 goto noinodes;
948
949 if ((mode & IFMT) == IFDIR)
950 ipref = ffs_dirpref(pip);
951 else
952 ipref = pip->i_number;
953 if (ipref >= fs->fs_ncg * fs->fs_ipg)
954 ipref = 0;
955 cg = ino_to_cg(fs, ipref);
956 /*
957 * Track number of dirs created one after another
958 * in a same cg without intervening by files.
959 */
960 if ((mode & IFMT) == IFDIR) {
961 if (fs->fs_contigdirs[cg] < 255)
962 fs->fs_contigdirs[cg]++;
963 } else {
964 if (fs->fs_contigdirs[cg] > 0)
965 fs->fs_contigdirs[cg]--;
966 }
967 ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode,
968 (allocfcn_t *)ffs_nodealloccg);
969 if (ino == 0)
970 goto noinodes;
971 error = ffs_vget(pvp->v_mount, ino, LK_EXCLUSIVE, vpp);
972 if (error) {
973 error1 = ffs_vgetf(pvp->v_mount, ino, LK_EXCLUSIVE, vpp,
974 FFSV_FORCEINSMQ);
975 ffs_vfree(pvp, ino, mode);
976 if (error1 == 0) {
977 ip = VTOI(*vpp);
978 if (ip->i_mode)
979 goto dup_alloc;
980 ip->i_flag |= IN_MODIFIED;
981 vput(*vpp);
982 }
983 return (error);
984 }
985 ip = VTOI(*vpp);
986 if (ip->i_mode) {
987 dup_alloc:
988 printf("mode = 0%o, inum = %lu, fs = %s\n",
989 ip->i_mode, (u_long)ip->i_number, fs->fs_fsmnt);
990 panic("ffs_valloc: dup alloc");
991 }
992 if (DIP(ip, i_blocks) && (fs->fs_flags & FS_UNCLEAN) == 0) { /* XXX */
993 printf("free inode %s/%lu had %ld blocks\n",
994 fs->fs_fsmnt, (u_long)ino, (long)DIP(ip, i_blocks));
995 DIP_SET(ip, i_blocks, 0);
996 }
997 ip->i_flags = 0;
998 DIP_SET(ip, i_flags, 0);
999 /*
1000 * Set up a new generation number for this inode.
1001 */
1002 if (ip->i_gen == 0 || ++ip->i_gen == 0)
1003 ip->i_gen = arc4random() / 2 + 1;
1004 DIP_SET(ip, i_gen, ip->i_gen);
1005 if (fs->fs_magic == FS_UFS2_MAGIC) {
1006 vfs_timestamp(&ts);
1007 ip->i_din2->di_birthtime = ts.tv_sec;
1008 ip->i_din2->di_birthnsec = ts.tv_nsec;
1009 }
1010 ip->i_flag = 0;
1011 vnode_destroy_vobject(*vpp);
1012 (*vpp)->v_type = VNON;
1013 if (fs->fs_magic == FS_UFS2_MAGIC)
1014 (*vpp)->v_op = &ffs_vnodeops2;
1015 else
1016 (*vpp)->v_op = &ffs_vnodeops1;
1017 return (0);
1018 noinodes:
1019 UFS_UNLOCK(ump);
1020 if (ppsratecheck(&lastfail, &curfail, 1)) {
1021 ffs_fserr(fs, pip->i_number, "out of inodes");
1022 uprintf("\n%s: create/symlink failed, no inodes free\n",
1023 fs->fs_fsmnt);
1024 }
1025 return (ENOSPC);
1026 }
1027
1028 /*
1029 * Find a cylinder group to place a directory.
1030 *
1031 * The policy implemented by this algorithm is to allocate a
1032 * directory inode in the same cylinder group as its parent
1033 * directory, but also to reserve space for its files inodes
1034 * and data. Restrict the number of directories which may be
1035 * allocated one after another in the same cylinder group
1036 * without intervening allocation of files.
1037 *
1038 * If we allocate a first level directory then force allocation
1039 * in another cylinder group.
1040 */
1041 static ino_t
1042 ffs_dirpref(pip)
1043 struct inode *pip;
1044 {
1045 struct fs *fs;
1046 u_int cg, prefcg, dirsize, cgsize;
1047 u_int avgifree, avgbfree, avgndir, curdirsize;
1048 u_int minifree, minbfree, maxndir;
1049 u_int mincg, minndir;
1050 u_int maxcontigdirs;
1051
1052 mtx_assert(UFS_MTX(pip->i_ump), MA_OWNED);
1053 fs = pip->i_fs;
1054
1055 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
1056 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1057 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
1058
1059 /*
1060 * Force allocation in another cg if creating a first level dir.
1061 */
1062 ASSERT_VOP_LOCKED(ITOV(pip), "ffs_dirpref");
1063 if (ITOV(pip)->v_vflag & VV_ROOT) {
1064 prefcg = arc4random() % fs->fs_ncg;
1065 mincg = prefcg;
1066 minndir = fs->fs_ipg;
1067 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1068 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
1069 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
1070 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1071 mincg = cg;
1072 minndir = fs->fs_cs(fs, cg).cs_ndir;
1073 }
1074 for (cg = 0; cg < prefcg; cg++)
1075 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
1076 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
1077 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1078 mincg = cg;
1079 minndir = fs->fs_cs(fs, cg).cs_ndir;
1080 }
1081 return ((ino_t)(fs->fs_ipg * mincg));
1082 }
1083
1084 /*
1085 * Count various limits which used for
1086 * optimal allocation of a directory inode.
1087 */
1088 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
1089 minifree = avgifree - avgifree / 4;
1090 if (minifree < 1)
1091 minifree = 1;
1092 minbfree = avgbfree - avgbfree / 4;
1093 if (minbfree < 1)
1094 minbfree = 1;
1095 cgsize = fs->fs_fsize * fs->fs_fpg;
1096 dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir;
1097 curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
1098 if (dirsize < curdirsize)
1099 dirsize = curdirsize;
1100 if (dirsize <= 0)
1101 maxcontigdirs = 0; /* dirsize overflowed */
1102 else
1103 maxcontigdirs = min((avgbfree * fs->fs_bsize) / dirsize, 255);
1104 if (fs->fs_avgfpdir > 0)
1105 maxcontigdirs = min(maxcontigdirs,
1106 fs->fs_ipg / fs->fs_avgfpdir);
1107 if (maxcontigdirs == 0)
1108 maxcontigdirs = 1;
1109
1110 /*
1111 * Limit number of dirs in one cg and reserve space for
1112 * regular files, but only if we have no deficit in
1113 * inodes or space.
1114 */
1115 prefcg = ino_to_cg(fs, pip->i_number);
1116 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1117 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
1118 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
1119 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
1120 if (fs->fs_contigdirs[cg] < maxcontigdirs)
1121 return ((ino_t)(fs->fs_ipg * cg));
1122 }
1123 for (cg = 0; cg < prefcg; cg++)
1124 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
1125 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
1126 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
1127 if (fs->fs_contigdirs[cg] < maxcontigdirs)
1128 return ((ino_t)(fs->fs_ipg * cg));
1129 }
1130 /*
1131 * This is a backstop when we have deficit in space.
1132 */
1133 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1134 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1135 return ((ino_t)(fs->fs_ipg * cg));
1136 for (cg = 0; cg < prefcg; cg++)
1137 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1138 break;
1139 return ((ino_t)(fs->fs_ipg * cg));
1140 }
1141
1142 /*
1143 * Select the desired position for the next block in a file. The file is
1144 * logically divided into sections. The first section is composed of the
1145 * direct blocks. Each additional section contains fs_maxbpg blocks.
1146 *
1147 * If no blocks have been allocated in the first section, the policy is to
1148 * request a block in the same cylinder group as the inode that describes
1149 * the file. If no blocks have been allocated in any other section, the
1150 * policy is to place the section in a cylinder group with a greater than
1151 * average number of free blocks. An appropriate cylinder group is found
1152 * by using a rotor that sweeps the cylinder groups. When a new group of
1153 * blocks is needed, the sweep begins in the cylinder group following the
1154 * cylinder group from which the previous allocation was made. The sweep
1155 * continues until a cylinder group with greater than the average number
1156 * of free blocks is found. If the allocation is for the first block in an
1157 * indirect block, the information on the previous allocation is unavailable;
1158 * here a best guess is made based upon the logical block number being
1159 * allocated.
1160 *
1161 * If a section is already partially allocated, the policy is to
1162 * contiguously allocate fs_maxcontig blocks. The end of one of these
1163 * contiguous blocks and the beginning of the next is laid out
1164 * contiguously if possible.
1165 */
1166 ufs2_daddr_t
1167 ffs_blkpref_ufs1(ip, lbn, indx, bap)
1168 struct inode *ip;
1169 ufs_lbn_t lbn;
1170 int indx;
1171 ufs1_daddr_t *bap;
1172 {
1173 struct fs *fs;
1174 u_int cg;
1175 u_int avgbfree, startcg;
1176
1177 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1178 fs = ip->i_fs;
1179 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1180 if (lbn < NDADDR + NINDIR(fs)) {
1181 cg = ino_to_cg(fs, ip->i_number);
1182 return (cgbase(fs, cg) + fs->fs_frag);
1183 }
1184 /*
1185 * Find a cylinder with greater than average number of
1186 * unused data blocks.
1187 */
1188 if (indx == 0 || bap[indx - 1] == 0)
1189 startcg =
1190 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1191 else
1192 startcg = dtog(fs, bap[indx - 1]) + 1;
1193 startcg %= fs->fs_ncg;
1194 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1195 for (cg = startcg; cg < fs->fs_ncg; cg++)
1196 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1197 fs->fs_cgrotor = cg;
1198 return (cgbase(fs, cg) + fs->fs_frag);
1199 }
1200 for (cg = 0; cg <= startcg; cg++)
1201 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1202 fs->fs_cgrotor = cg;
1203 return (cgbase(fs, cg) + fs->fs_frag);
1204 }
1205 return (0);
1206 }
1207 /*
1208 * We just always try to lay things out contiguously.
1209 */
1210 return (bap[indx - 1] + fs->fs_frag);
1211 }
1212
1213 /*
1214 * Same as above, but for UFS2
1215 */
1216 ufs2_daddr_t
1217 ffs_blkpref_ufs2(ip, lbn, indx, bap)
1218 struct inode *ip;
1219 ufs_lbn_t lbn;
1220 int indx;
1221 ufs2_daddr_t *bap;
1222 {
1223 struct fs *fs;
1224 u_int cg;
1225 u_int avgbfree, startcg;
1226
1227 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1228 fs = ip->i_fs;
1229 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1230 if (lbn < NDADDR + NINDIR(fs)) {
1231 cg = ino_to_cg(fs, ip->i_number);
1232 return (cgbase(fs, cg) + fs->fs_frag);
1233 }
1234 /*
1235 * Find a cylinder with greater than average number of
1236 * unused data blocks.
1237 */
1238 if (indx == 0 || bap[indx - 1] == 0)
1239 startcg =
1240 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1241 else
1242 startcg = dtog(fs, bap[indx - 1]) + 1;
1243 startcg %= fs->fs_ncg;
1244 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1245 for (cg = startcg; cg < fs->fs_ncg; cg++)
1246 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1247 fs->fs_cgrotor = cg;
1248 return (cgbase(fs, cg) + fs->fs_frag);
1249 }
1250 for (cg = 0; cg <= startcg; cg++)
1251 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1252 fs->fs_cgrotor = cg;
1253 return (cgbase(fs, cg) + fs->fs_frag);
1254 }
1255 return (0);
1256 }
1257 /*
1258 * We just always try to lay things out contiguously.
1259 */
1260 return (bap[indx - 1] + fs->fs_frag);
1261 }
1262
1263 /*
1264 * Implement the cylinder overflow algorithm.
1265 *
1266 * The policy implemented by this algorithm is:
1267 * 1) allocate the block in its requested cylinder group.
1268 * 2) quadradically rehash on the cylinder group number.
1269 * 3) brute force search for a free block.
1270 *
1271 * Must be called with the UFS lock held. Will release the lock on success
1272 * and return with it held on failure.
1273 */
1274 /*VARARGS5*/
1275 static ufs2_daddr_t
1276 ffs_hashalloc(ip, cg, pref, size, allocator)
1277 struct inode *ip;
1278 u_int cg;
1279 ufs2_daddr_t pref;
1280 int size; /* size for data blocks, mode for inodes */
1281 allocfcn_t *allocator;
1282 {
1283 struct fs *fs;
1284 ufs2_daddr_t result;
1285 u_int i, icg = cg;
1286
1287 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1288 #ifdef INVARIANTS
1289 if (ITOV(ip)->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
1290 panic("ffs_hashalloc: allocation on suspended filesystem");
1291 #endif
1292 fs = ip->i_fs;
1293 /*
1294 * 1: preferred cylinder group
1295 */
1296 result = (*allocator)(ip, cg, pref, size);
1297 if (result)
1298 return (result);
1299 /*
1300 * 2: quadratic rehash
1301 */
1302 for (i = 1; i < fs->fs_ncg; i *= 2) {
1303 cg += i;
1304 if (cg >= fs->fs_ncg)
1305 cg -= fs->fs_ncg;
1306 result = (*allocator)(ip, cg, 0, size);
1307 if (result)
1308 return (result);
1309 }
1310 /*
1311 * 3: brute force search
1312 * Note that we start at i == 2, since 0 was checked initially,
1313 * and 1 is always checked in the quadratic rehash.
1314 */
1315 cg = (icg + 2) % fs->fs_ncg;
1316 for (i = 2; i < fs->fs_ncg; i++) {
1317 result = (*allocator)(ip, cg, 0, size);
1318 if (result)
1319 return (result);
1320 cg++;
1321 if (cg == fs->fs_ncg)
1322 cg = 0;
1323 }
1324 return (0);
1325 }
1326
1327 /*
1328 * Determine whether a fragment can be extended.
1329 *
1330 * Check to see if the necessary fragments are available, and
1331 * if they are, allocate them.
1332 */
1333 static ufs2_daddr_t
1334 ffs_fragextend(ip, cg, bprev, osize, nsize)
1335 struct inode *ip;
1336 u_int cg;
1337 ufs2_daddr_t bprev;
1338 int osize, nsize;
1339 {
1340 struct fs *fs;
1341 struct cg *cgp;
1342 struct buf *bp;
1343 struct ufsmount *ump;
1344 int nffree;
1345 long bno;
1346 int frags, bbase;
1347 int i, error;
1348 u_int8_t *blksfree;
1349
1350 ump = ip->i_ump;
1351 fs = ip->i_fs;
1352 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
1353 return (0);
1354 frags = numfrags(fs, nsize);
1355 bbase = fragnum(fs, bprev);
1356 if (bbase > fragnum(fs, (bprev + frags - 1))) {
1357 /* cannot extend across a block boundary */
1358 return (0);
1359 }
1360 UFS_UNLOCK(ump);
1361 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1362 (int)fs->fs_cgsize, NOCRED, &bp);
1363 if (error)
1364 goto fail;
1365 cgp = (struct cg *)bp->b_data;
1366 if (!cg_chkmagic(cgp))
1367 goto fail;
1368 bp->b_xflags |= BX_BKGRDWRITE;
1369 cgp->cg_old_time = cgp->cg_time = time_second;
1370 bno = dtogd(fs, bprev);
1371 blksfree = cg_blksfree(cgp);
1372 for (i = numfrags(fs, osize); i < frags; i++)
1373 if (isclr(blksfree, bno + i))
1374 goto fail;
1375 /*
1376 * the current fragment can be extended
1377 * deduct the count on fragment being extended into
1378 * increase the count on the remaining fragment (if any)
1379 * allocate the extended piece
1380 */
1381 for (i = frags; i < fs->fs_frag - bbase; i++)
1382 if (isclr(blksfree, bno + i))
1383 break;
1384 cgp->cg_frsum[i - numfrags(fs, osize)]--;
1385 if (i != frags)
1386 cgp->cg_frsum[i - frags]++;
1387 for (i = numfrags(fs, osize), nffree = 0; i < frags; i++) {
1388 clrbit(blksfree, bno + i);
1389 cgp->cg_cs.cs_nffree--;
1390 nffree++;
1391 }
1392 UFS_LOCK(ump);
1393 fs->fs_cstotal.cs_nffree -= nffree;
1394 fs->fs_cs(fs, cg).cs_nffree -= nffree;
1395 fs->fs_fmod = 1;
1396 ACTIVECLEAR(fs, cg);
1397 UFS_UNLOCK(ump);
1398 if (DOINGSOFTDEP(ITOV(ip)))
1399 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), bprev);
1400 bdwrite(bp);
1401 return (bprev);
1402
1403 fail:
1404 brelse(bp);
1405 UFS_LOCK(ump);
1406 return (0);
1407
1408 }
1409
1410 /*
1411 * Determine whether a block can be allocated.
1412 *
1413 * Check to see if a block of the appropriate size is available,
1414 * and if it is, allocate it.
1415 */
1416 static ufs2_daddr_t
1417 ffs_alloccg(ip, cg, bpref, size)
1418 struct inode *ip;
1419 u_int cg;
1420 ufs2_daddr_t bpref;
1421 int size;
1422 {
1423 struct fs *fs;
1424 struct cg *cgp;
1425 struct buf *bp;
1426 struct ufsmount *ump;
1427 ufs1_daddr_t bno;
1428 ufs2_daddr_t blkno;
1429 int i, allocsiz, error, frags;
1430 u_int8_t *blksfree;
1431
1432 ump = ip->i_ump;
1433 fs = ip->i_fs;
1434 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
1435 return (0);
1436 UFS_UNLOCK(ump);
1437 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1438 (int)fs->fs_cgsize, NOCRED, &bp);
1439 if (error)
1440 goto fail;
1441 cgp = (struct cg *)bp->b_data;
1442 if (!cg_chkmagic(cgp) ||
1443 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize))
1444 goto fail;
1445 bp->b_xflags |= BX_BKGRDWRITE;
1446 cgp->cg_old_time = cgp->cg_time = time_second;
1447 if (size == fs->fs_bsize) {
1448 UFS_LOCK(ump);
1449 blkno = ffs_alloccgblk(ip, bp, bpref);
1450 ACTIVECLEAR(fs, cg);
1451 UFS_UNLOCK(ump);
1452 bdwrite(bp);
1453 return (blkno);
1454 }
1455 /*
1456 * check to see if any fragments are already available
1457 * allocsiz is the size which will be allocated, hacking
1458 * it down to a smaller size if necessary
1459 */
1460 blksfree = cg_blksfree(cgp);
1461 frags = numfrags(fs, size);
1462 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1463 if (cgp->cg_frsum[allocsiz] != 0)
1464 break;
1465 if (allocsiz == fs->fs_frag) {
1466 /*
1467 * no fragments were available, so a block will be
1468 * allocated, and hacked up
1469 */
1470 if (cgp->cg_cs.cs_nbfree == 0)
1471 goto fail;
1472 UFS_LOCK(ump);
1473 blkno = ffs_alloccgblk(ip, bp, bpref);
1474 bno = dtogd(fs, blkno);
1475 for (i = frags; i < fs->fs_frag; i++)
1476 setbit(blksfree, bno + i);
1477 i = fs->fs_frag - frags;
1478 cgp->cg_cs.cs_nffree += i;
1479 fs->fs_cstotal.cs_nffree += i;
1480 fs->fs_cs(fs, cg).cs_nffree += i;
1481 fs->fs_fmod = 1;
1482 cgp->cg_frsum[i]++;
1483 ACTIVECLEAR(fs, cg);
1484 UFS_UNLOCK(ump);
1485 bdwrite(bp);
1486 return (blkno);
1487 }
1488 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1489 if (bno < 0)
1490 goto fail;
1491 for (i = 0; i < frags; i++)
1492 clrbit(blksfree, bno + i);
1493 cgp->cg_cs.cs_nffree -= frags;
1494 cgp->cg_frsum[allocsiz]--;
1495 if (frags != allocsiz)
1496 cgp->cg_frsum[allocsiz - frags]++;
1497 UFS_LOCK(ump);
1498 fs->fs_cstotal.cs_nffree -= frags;
1499 fs->fs_cs(fs, cg).cs_nffree -= frags;
1500 fs->fs_fmod = 1;
1501 blkno = cgbase(fs, cg) + bno;
1502 ACTIVECLEAR(fs, cg);
1503 UFS_UNLOCK(ump);
1504 if (DOINGSOFTDEP(ITOV(ip)))
1505 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno);
1506 bdwrite(bp);
1507 return (blkno);
1508
1509 fail:
1510 brelse(bp);
1511 UFS_LOCK(ump);
1512 return (0);
1513 }
1514
1515 /*
1516 * Allocate a block in a cylinder group.
1517 *
1518 * This algorithm implements the following policy:
1519 * 1) allocate the requested block.
1520 * 2) allocate a rotationally optimal block in the same cylinder.
1521 * 3) allocate the next available block on the block rotor for the
1522 * specified cylinder group.
1523 * Note that this routine only allocates fs_bsize blocks; these
1524 * blocks may be fragmented by the routine that allocates them.
1525 */
1526 static ufs2_daddr_t
1527 ffs_alloccgblk(ip, bp, bpref)
1528 struct inode *ip;
1529 struct buf *bp;
1530 ufs2_daddr_t bpref;
1531 {
1532 struct fs *fs;
1533 struct cg *cgp;
1534 struct ufsmount *ump;
1535 ufs1_daddr_t bno;
1536 ufs2_daddr_t blkno;
1537 u_int8_t *blksfree;
1538
1539 fs = ip->i_fs;
1540 ump = ip->i_ump;
1541 mtx_assert(UFS_MTX(ump), MA_OWNED);
1542 cgp = (struct cg *)bp->b_data;
1543 blksfree = cg_blksfree(cgp);
1544 if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
1545 bpref = cgp->cg_rotor;
1546 } else {
1547 bpref = blknum(fs, bpref);
1548 bno = dtogd(fs, bpref);
1549 /*
1550 * if the requested block is available, use it
1551 */
1552 if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno)))
1553 goto gotit;
1554 }
1555 /*
1556 * Take the next available block in this cylinder group.
1557 */
1558 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1559 if (bno < 0)
1560 return (0);
1561 cgp->cg_rotor = bno;
1562 gotit:
1563 blkno = fragstoblks(fs, bno);
1564 ffs_clrblock(fs, blksfree, (long)blkno);
1565 ffs_clusteracct(ump, fs, cgp, blkno, -1);
1566 cgp->cg_cs.cs_nbfree--;
1567 fs->fs_cstotal.cs_nbfree--;
1568 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
1569 fs->fs_fmod = 1;
1570 blkno = cgbase(fs, cgp->cg_cgx) + bno;
1571 /* XXX Fixme. */
1572 UFS_UNLOCK(ump);
1573 if (DOINGSOFTDEP(ITOV(ip)))
1574 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno);
1575 UFS_LOCK(ump);
1576 return (blkno);
1577 }
1578
1579 /*
1580 * Determine whether a cluster can be allocated.
1581 *
1582 * We do not currently check for optimal rotational layout if there
1583 * are multiple choices in the same cylinder group. Instead we just
1584 * take the first one that we find following bpref.
1585 */
1586 static ufs2_daddr_t
1587 ffs_clusteralloc(ip, cg, bpref, len)
1588 struct inode *ip;
1589 u_int cg;
1590 ufs2_daddr_t bpref;
1591 int len;
1592 {
1593 struct fs *fs;
1594 struct cg *cgp;
1595 struct buf *bp;
1596 struct ufsmount *ump;
1597 int i, run, bit, map, got;
1598 ufs2_daddr_t bno;
1599 u_char *mapp;
1600 int32_t *lp;
1601 u_int8_t *blksfree;
1602
1603 fs = ip->i_fs;
1604 ump = ip->i_ump;
1605 if (fs->fs_maxcluster[cg] < len)
1606 return (0);
1607 UFS_UNLOCK(ump);
1608 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1609 NOCRED, &bp))
1610 goto fail_lock;
1611 cgp = (struct cg *)bp->b_data;
1612 if (!cg_chkmagic(cgp))
1613 goto fail_lock;
1614 bp->b_xflags |= BX_BKGRDWRITE;
1615 /*
1616 * Check to see if a cluster of the needed size (or bigger) is
1617 * available in this cylinder group.
1618 */
1619 lp = &cg_clustersum(cgp)[len];
1620 for (i = len; i <= fs->fs_contigsumsize; i++)
1621 if (*lp++ > 0)
1622 break;
1623 if (i > fs->fs_contigsumsize) {
1624 /*
1625 * This is the first time looking for a cluster in this
1626 * cylinder group. Update the cluster summary information
1627 * to reflect the true maximum sized cluster so that
1628 * future cluster allocation requests can avoid reading
1629 * the cylinder group map only to find no clusters.
1630 */
1631 lp = &cg_clustersum(cgp)[len - 1];
1632 for (i = len - 1; i > 0; i--)
1633 if (*lp-- > 0)
1634 break;
1635 UFS_LOCK(ump);
1636 fs->fs_maxcluster[cg] = i;
1637 goto fail;
1638 }
1639 /*
1640 * Search the cluster map to find a big enough cluster.
1641 * We take the first one that we find, even if it is larger
1642 * than we need as we prefer to get one close to the previous
1643 * block allocation. We do not search before the current
1644 * preference point as we do not want to allocate a block
1645 * that is allocated before the previous one (as we will
1646 * then have to wait for another pass of the elevator
1647 * algorithm before it will be read). We prefer to fail and
1648 * be recalled to try an allocation in the next cylinder group.
1649 */
1650 if (dtog(fs, bpref) != cg)
1651 bpref = 0;
1652 else
1653 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1654 mapp = &cg_clustersfree(cgp)[bpref / NBBY];
1655 map = *mapp++;
1656 bit = 1 << (bpref % NBBY);
1657 for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
1658 if ((map & bit) == 0) {
1659 run = 0;
1660 } else {
1661 run++;
1662 if (run == len)
1663 break;
1664 }
1665 if ((got & (NBBY - 1)) != (NBBY - 1)) {
1666 bit <<= 1;
1667 } else {
1668 map = *mapp++;
1669 bit = 1;
1670 }
1671 }
1672 if (got >= cgp->cg_nclusterblks)
1673 goto fail_lock;
1674 /*
1675 * Allocate the cluster that we have found.
1676 */
1677 blksfree = cg_blksfree(cgp);
1678 for (i = 1; i <= len; i++)
1679 if (!ffs_isblock(fs, blksfree, got - run + i))
1680 panic("ffs_clusteralloc: map mismatch");
1681 bno = cgbase(fs, cg) + blkstofrags(fs, got - run + 1);
1682 if (dtog(fs, bno) != cg)
1683 panic("ffs_clusteralloc: allocated out of group");
1684 len = blkstofrags(fs, len);
1685 UFS_LOCK(ump);
1686 for (i = 0; i < len; i += fs->fs_frag)
1687 if (ffs_alloccgblk(ip, bp, bno + i) != bno + i)
1688 panic("ffs_clusteralloc: lost block");
1689 ACTIVECLEAR(fs, cg);
1690 UFS_UNLOCK(ump);
1691 bdwrite(bp);
1692 return (bno);
1693
1694 fail_lock:
1695 UFS_LOCK(ump);
1696 fail:
1697 brelse(bp);
1698 return (0);
1699 }
1700
1701 /*
1702 * Determine whether an inode can be allocated.
1703 *
1704 * Check to see if an inode is available, and if it is,
1705 * allocate it using the following policy:
1706 * 1) allocate the requested inode.
1707 * 2) allocate the next available inode after the requested
1708 * inode in the specified cylinder group.
1709 */
1710 static ufs2_daddr_t
1711 ffs_nodealloccg(ip, cg, ipref, mode)
1712 struct inode *ip;
1713 u_int cg;
1714 ufs2_daddr_t ipref;
1715 int mode;
1716 {
1717 struct fs *fs;
1718 struct cg *cgp;
1719 struct buf *bp, *ibp;
1720 struct ufsmount *ump;
1721 u_int8_t *inosused;
1722 struct ufs2_dinode *dp2;
1723 int error, start, len, loc, map, i;
1724
1725 fs = ip->i_fs;
1726 ump = ip->i_ump;
1727 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1728 return (0);
1729 UFS_UNLOCK(ump);
1730 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1731 (int)fs->fs_cgsize, NOCRED, &bp);
1732 if (error) {
1733 brelse(bp);
1734 UFS_LOCK(ump);
1735 return (0);
1736 }
1737 cgp = (struct cg *)bp->b_data;
1738 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
1739 brelse(bp);
1740 UFS_LOCK(ump);
1741 return (0);
1742 }
1743 bp->b_xflags |= BX_BKGRDWRITE;
1744 cgp->cg_old_time = cgp->cg_time = time_second;
1745 inosused = cg_inosused(cgp);
1746 if (ipref) {
1747 ipref %= fs->fs_ipg;
1748 if (isclr(inosused, ipref))
1749 goto gotit;
1750 }
1751 start = cgp->cg_irotor / NBBY;
1752 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
1753 loc = skpc(0xff, len, &inosused[start]);
1754 if (loc == 0) {
1755 len = start + 1;
1756 start = 0;
1757 loc = skpc(0xff, len, &inosused[0]);
1758 if (loc == 0) {
1759 printf("cg = %d, irotor = %ld, fs = %s\n",
1760 cg, (long)cgp->cg_irotor, fs->fs_fsmnt);
1761 panic("ffs_nodealloccg: map corrupted");
1762 /* NOTREACHED */
1763 }
1764 }
1765 i = start + len - loc;
1766 map = inosused[i];
1767 ipref = i * NBBY;
1768 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1769 if ((map & i) == 0) {
1770 cgp->cg_irotor = ipref;
1771 goto gotit;
1772 }
1773 }
1774 printf("fs = %s\n", fs->fs_fsmnt);
1775 panic("ffs_nodealloccg: block not in map");
1776 /* NOTREACHED */
1777 gotit:
1778 /*
1779 * Check to see if we need to initialize more inodes.
1780 */
1781 ibp = NULL;
1782 if (fs->fs_magic == FS_UFS2_MAGIC &&
1783 ipref + INOPB(fs) > cgp->cg_initediblk &&
1784 cgp->cg_initediblk < cgp->cg_niblk) {
1785 ibp = getblk(ip->i_devvp, fsbtodb(fs,
1786 ino_to_fsba(fs, cg * fs->fs_ipg + cgp->cg_initediblk)),
1787 (int)fs->fs_bsize, 0, 0, 0);
1788 bzero(ibp->b_data, (int)fs->fs_bsize);
1789 dp2 = (struct ufs2_dinode *)(ibp->b_data);
1790 for (i = 0; i < INOPB(fs); i++) {
1791 dp2->di_gen = arc4random() / 2 + 1;
1792 dp2++;
1793 }
1794 cgp->cg_initediblk += INOPB(fs);
1795 }
1796 UFS_LOCK(ump);
1797 ACTIVECLEAR(fs, cg);
1798 setbit(inosused, ipref);
1799 cgp->cg_cs.cs_nifree--;
1800 fs->fs_cstotal.cs_nifree--;
1801 fs->fs_cs(fs, cg).cs_nifree--;
1802 fs->fs_fmod = 1;
1803 if ((mode & IFMT) == IFDIR) {
1804 cgp->cg_cs.cs_ndir++;
1805 fs->fs_cstotal.cs_ndir++;
1806 fs->fs_cs(fs, cg).cs_ndir++;
1807 }
1808 UFS_UNLOCK(ump);
1809 if (DOINGSOFTDEP(ITOV(ip)))
1810 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1811 bdwrite(bp);
1812 if (ibp != NULL)
1813 bawrite(ibp);
1814 return ((ino_t)(cg * fs->fs_ipg + ipref));
1815 }
1816
1817 /*
1818 * check if a block is free
1819 */
1820 static int
1821 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
1822 {
1823
1824 switch ((int)fs->fs_frag) {
1825 case 8:
1826 return (cp[h] == 0);
1827 case 4:
1828 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
1829 case 2:
1830 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
1831 case 1:
1832 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
1833 default:
1834 panic("ffs_isfreeblock");
1835 }
1836 return (0);
1837 }
1838
1839 /*
1840 * Free a block or fragment.
1841 *
1842 * The specified block or fragment is placed back in the
1843 * free map. If a fragment is deallocated, a possible
1844 * block reassembly is checked.
1845 */
1846 void
1847 ffs_blkfree(ump, fs, devvp, bno, size, inum)
1848 struct ufsmount *ump;
1849 struct fs *fs;
1850 struct vnode *devvp;
1851 ufs2_daddr_t bno;
1852 long size;
1853 ino_t inum;
1854 {
1855 struct cg *cgp;
1856 struct buf *bp;
1857 ufs1_daddr_t fragno, cgbno;
1858 ufs2_daddr_t cgblkno;
1859 int i, blk, frags, bbase;
1860 u_int cg;
1861 u_int8_t *blksfree;
1862 struct cdev *dev;
1863
1864 cg = dtog(fs, bno);
1865 if (devvp->v_type == VREG) {
1866 /* devvp is a snapshot */
1867 dev = VTOI(devvp)->i_devvp->v_rdev;
1868 cgblkno = fragstoblks(fs, cgtod(fs, cg));
1869 } else {
1870 /* devvp is a normal disk device */
1871 dev = devvp->v_rdev;
1872 cgblkno = fsbtodb(fs, cgtod(fs, cg));
1873 ASSERT_VOP_LOCKED(devvp, "ffs_blkfree");
1874 if ((devvp->v_vflag & VV_COPYONWRITE) &&
1875 ffs_snapblkfree(fs, devvp, bno, size, inum))
1876 return;
1877 }
1878 #ifdef INVARIANTS
1879 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1880 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1881 printf("dev=%s, bno = %jd, bsize = %ld, size = %ld, fs = %s\n",
1882 devtoname(dev), (intmax_t)bno, (long)fs->fs_bsize,
1883 size, fs->fs_fsmnt);
1884 panic("ffs_blkfree: bad size");
1885 }
1886 #endif
1887 if ((u_int)bno >= fs->fs_size) {
1888 printf("bad block %jd, ino %lu\n", (intmax_t)bno,
1889 (u_long)inum);
1890 ffs_fserr(fs, inum, "bad block");
1891 return;
1892 }
1893 if (bread(devvp, cgblkno, (int)fs->fs_cgsize, NOCRED, &bp)) {
1894 brelse(bp);
1895 return;
1896 }
1897 cgp = (struct cg *)bp->b_data;
1898 if (!cg_chkmagic(cgp)) {
1899 brelse(bp);
1900 return;
1901 }
1902 bp->b_xflags |= BX_BKGRDWRITE;
1903 cgp->cg_old_time = cgp->cg_time = time_second;
1904 cgbno = dtogd(fs, bno);
1905 blksfree = cg_blksfree(cgp);
1906 UFS_LOCK(ump);
1907 if (size == fs->fs_bsize) {
1908 fragno = fragstoblks(fs, cgbno);
1909 if (!ffs_isfreeblock(fs, blksfree, fragno)) {
1910 if (devvp->v_type == VREG) {
1911 UFS_UNLOCK(ump);
1912 /* devvp is a snapshot */
1913 brelse(bp);
1914 return;
1915 }
1916 printf("dev = %s, block = %jd, fs = %s\n",
1917 devtoname(dev), (intmax_t)bno, fs->fs_fsmnt);
1918 panic("ffs_blkfree: freeing free block");
1919 }
1920 ffs_setblock(fs, blksfree, fragno);
1921 ffs_clusteracct(ump, fs, cgp, fragno, 1);
1922 cgp->cg_cs.cs_nbfree++;
1923 fs->fs_cstotal.cs_nbfree++;
1924 fs->fs_cs(fs, cg).cs_nbfree++;
1925 } else {
1926 bbase = cgbno - fragnum(fs, cgbno);
1927 /*
1928 * decrement the counts associated with the old frags
1929 */
1930 blk = blkmap(fs, blksfree, bbase);
1931 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
1932 /*
1933 * deallocate the fragment
1934 */
1935 frags = numfrags(fs, size);
1936 for (i = 0; i < frags; i++) {
1937 if (isset(blksfree, cgbno + i)) {
1938 printf("dev = %s, block = %jd, fs = %s\n",
1939 devtoname(dev), (intmax_t)(bno + i),
1940 fs->fs_fsmnt);
1941 panic("ffs_blkfree: freeing free frag");
1942 }
1943 setbit(blksfree, cgbno + i);
1944 }
1945 cgp->cg_cs.cs_nffree += i;
1946 fs->fs_cstotal.cs_nffree += i;
1947 fs->fs_cs(fs, cg).cs_nffree += i;
1948 /*
1949 * add back in counts associated with the new frags
1950 */
1951 blk = blkmap(fs, blksfree, bbase);
1952 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
1953 /*
1954 * if a complete block has been reassembled, account for it
1955 */
1956 fragno = fragstoblks(fs, bbase);
1957 if (ffs_isblock(fs, blksfree, fragno)) {
1958 cgp->cg_cs.cs_nffree -= fs->fs_frag;
1959 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1960 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1961 ffs_clusteracct(ump, fs, cgp, fragno, 1);
1962 cgp->cg_cs.cs_nbfree++;
1963 fs->fs_cstotal.cs_nbfree++;
1964 fs->fs_cs(fs, cg).cs_nbfree++;
1965 }
1966 }
1967 fs->fs_fmod = 1;
1968 ACTIVECLEAR(fs, cg);
1969 UFS_UNLOCK(ump);
1970 bdwrite(bp);
1971 }
1972
1973 #ifdef INVARIANTS
1974 /*
1975 * Verify allocation of a block or fragment. Returns true if block or
1976 * fragment is allocated, false if it is free.
1977 */
1978 static int
1979 ffs_checkblk(ip, bno, size)
1980 struct inode *ip;
1981 ufs2_daddr_t bno;
1982 long size;
1983 {
1984 struct fs *fs;
1985 struct cg *cgp;
1986 struct buf *bp;
1987 ufs1_daddr_t cgbno;
1988 int i, error, frags, free;
1989 u_int8_t *blksfree;
1990
1991 fs = ip->i_fs;
1992 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1993 printf("bsize = %ld, size = %ld, fs = %s\n",
1994 (long)fs->fs_bsize, size, fs->fs_fsmnt);
1995 panic("ffs_checkblk: bad size");
1996 }
1997 if ((u_int)bno >= fs->fs_size)
1998 panic("ffs_checkblk: bad block %jd", (intmax_t)bno);
1999 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
2000 (int)fs->fs_cgsize, NOCRED, &bp);
2001 if (error)
2002 panic("ffs_checkblk: cg bread failed");
2003 cgp = (struct cg *)bp->b_data;
2004 if (!cg_chkmagic(cgp))
2005 panic("ffs_checkblk: cg magic mismatch");
2006 bp->b_xflags |= BX_BKGRDWRITE;
2007 blksfree = cg_blksfree(cgp);
2008 cgbno = dtogd(fs, bno);
2009 if (size == fs->fs_bsize) {
2010 free = ffs_isblock(fs, blksfree, fragstoblks(fs, cgbno));
2011 } else {
2012 frags = numfrags(fs, size);
2013 for (free = 0, i = 0; i < frags; i++)
2014 if (isset(blksfree, cgbno + i))
2015 free++;
2016 if (free != 0 && free != frags)
2017 panic("ffs_checkblk: partially free fragment");
2018 }
2019 brelse(bp);
2020 return (!free);
2021 }
2022 #endif /* INVARIANTS */
2023
2024 /*
2025 * Free an inode.
2026 */
2027 int
2028 ffs_vfree(pvp, ino, mode)
2029 struct vnode *pvp;
2030 ino_t ino;
2031 int mode;
2032 {
2033 struct inode *ip;
2034
2035 if (DOINGSOFTDEP(pvp)) {
2036 softdep_freefile(pvp, ino, mode);
2037 return (0);
2038 }
2039 ip = VTOI(pvp);
2040 return (ffs_freefile(ip->i_ump, ip->i_fs, ip->i_devvp, ino, mode));
2041 }
2042
2043 /*
2044 * Do the actual free operation.
2045 * The specified inode is placed back in the free map.
2046 */
2047 int
2048 ffs_freefile(ump, fs, devvp, ino, mode)
2049 struct ufsmount *ump;
2050 struct fs *fs;
2051 struct vnode *devvp;
2052 ino_t ino;
2053 int mode;
2054 {
2055 struct cg *cgp;
2056 struct buf *bp;
2057 ufs2_daddr_t cgbno;
2058 int error;
2059 u_int cg;
2060 u_int8_t *inosused;
2061 struct cdev *dev;
2062
2063 cg = ino_to_cg(fs, ino);
2064 if (devvp->v_type == VREG) {
2065 /* devvp is a snapshot */
2066 dev = VTOI(devvp)->i_devvp->v_rdev;
2067 cgbno = fragstoblks(fs, cgtod(fs, cg));
2068 } else {
2069 /* devvp is a normal disk device */
2070 dev = devvp->v_rdev;
2071 cgbno = fsbtodb(fs, cgtod(fs, cg));
2072 }
2073 if (ino >= fs->fs_ipg * fs->fs_ncg)
2074 panic("ffs_freefile: range: dev = %s, ino = %lu, fs = %s",
2075 devtoname(dev), (u_long)ino, fs->fs_fsmnt);
2076 if ((error = bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp))) {
2077 brelse(bp);
2078 return (error);
2079 }
2080 cgp = (struct cg *)bp->b_data;
2081 if (!cg_chkmagic(cgp)) {
2082 brelse(bp);
2083 return (0);
2084 }
2085 bp->b_xflags |= BX_BKGRDWRITE;
2086 cgp->cg_old_time = cgp->cg_time = time_second;
2087 inosused = cg_inosused(cgp);
2088 ino %= fs->fs_ipg;
2089 if (isclr(inosused, ino)) {
2090 printf("dev = %s, ino = %u, fs = %s\n", devtoname(dev),
2091 ino + cg * fs->fs_ipg, fs->fs_fsmnt);
2092 if (fs->fs_ronly == 0)
2093 panic("ffs_freefile: freeing free inode");
2094 }
2095 clrbit(inosused, ino);
2096 if (ino < cgp->cg_irotor)
2097 cgp->cg_irotor = ino;
2098 cgp->cg_cs.cs_nifree++;
2099 UFS_LOCK(ump);
2100 fs->fs_cstotal.cs_nifree++;
2101 fs->fs_cs(fs, cg).cs_nifree++;
2102 if ((mode & IFMT) == IFDIR) {
2103 cgp->cg_cs.cs_ndir--;
2104 fs->fs_cstotal.cs_ndir--;
2105 fs->fs_cs(fs, cg).cs_ndir--;
2106 }
2107 fs->fs_fmod = 1;
2108 ACTIVECLEAR(fs, cg);
2109 UFS_UNLOCK(ump);
2110 bdwrite(bp);
2111 return (0);
2112 }
2113
2114 /*
2115 * Check to see if a file is free.
2116 */
2117 int
2118 ffs_checkfreefile(fs, devvp, ino)
2119 struct fs *fs;
2120 struct vnode *devvp;
2121 ino_t ino;
2122 {
2123 struct cg *cgp;
2124 struct buf *bp;
2125 ufs2_daddr_t cgbno;
2126 int ret;
2127 u_int cg;
2128 u_int8_t *inosused;
2129
2130 cg = ino_to_cg(fs, ino);
2131 if (devvp->v_type == VREG) {
2132 /* devvp is a snapshot */
2133 cgbno = fragstoblks(fs, cgtod(fs, cg));
2134 } else {
2135 /* devvp is a normal disk device */
2136 cgbno = fsbtodb(fs, cgtod(fs, cg));
2137 }
2138 if (ino >= fs->fs_ipg * fs->fs_ncg)
2139 return (1);
2140 if (bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp)) {
2141 brelse(bp);
2142 return (1);
2143 }
2144 cgp = (struct cg *)bp->b_data;
2145 if (!cg_chkmagic(cgp)) {
2146 brelse(bp);
2147 return (1);
2148 }
2149 inosused = cg_inosused(cgp);
2150 ino %= fs->fs_ipg;
2151 ret = isclr(inosused, ino);
2152 brelse(bp);
2153 return (ret);
2154 }
2155
2156 /*
2157 * Find a block of the specified size in the specified cylinder group.
2158 *
2159 * It is a panic if a request is made to find a block if none are
2160 * available.
2161 */
2162 static ufs1_daddr_t
2163 ffs_mapsearch(fs, cgp, bpref, allocsiz)
2164 struct fs *fs;
2165 struct cg *cgp;
2166 ufs2_daddr_t bpref;
2167 int allocsiz;
2168 {
2169 ufs1_daddr_t bno;
2170 int start, len, loc, i;
2171 int blk, field, subfield, pos;
2172 u_int8_t *blksfree;
2173
2174 /*
2175 * find the fragment by searching through the free block
2176 * map for an appropriate bit pattern
2177 */
2178 if (bpref)
2179 start = dtogd(fs, bpref) / NBBY;
2180 else
2181 start = cgp->cg_frotor / NBBY;
2182 blksfree = cg_blksfree(cgp);
2183 len = howmany(fs->fs_fpg, NBBY) - start;
2184 loc = scanc((u_int)len, (u_char *)&blksfree[start],
2185 fragtbl[fs->fs_frag],
2186 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
2187 if (loc == 0) {
2188 len = start + 1;
2189 start = 0;
2190 loc = scanc((u_int)len, (u_char *)&blksfree[0],
2191 fragtbl[fs->fs_frag],
2192 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
2193 if (loc == 0) {
2194 printf("start = %d, len = %d, fs = %s\n",
2195 start, len, fs->fs_fsmnt);
2196 panic("ffs_alloccg: map corrupted");
2197 /* NOTREACHED */
2198 }
2199 }
2200 bno = (start + len - loc) * NBBY;
2201 cgp->cg_frotor = bno;
2202 /*
2203 * found the byte in the map
2204 * sift through the bits to find the selected frag
2205 */
2206 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
2207 blk = blkmap(fs, blksfree, bno);
2208 blk <<= 1;
2209 field = around[allocsiz];
2210 subfield = inside[allocsiz];
2211 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
2212 if ((blk & field) == subfield)
2213 return (bno + pos);
2214 field <<= 1;
2215 subfield <<= 1;
2216 }
2217 }
2218 printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt);
2219 panic("ffs_alloccg: block not in map");
2220 return (-1);
2221 }
2222
2223 /*
2224 * Update the cluster map because of an allocation or free.
2225 *
2226 * Cnt == 1 means free; cnt == -1 means allocating.
2227 */
2228 void
2229 ffs_clusteracct(ump, fs, cgp, blkno, cnt)
2230 struct ufsmount *ump;
2231 struct fs *fs;
2232 struct cg *cgp;
2233 ufs1_daddr_t blkno;
2234 int cnt;
2235 {
2236 int32_t *sump;
2237 int32_t *lp;
2238 u_char *freemapp, *mapp;
2239 int i, start, end, forw, back, map, bit;
2240
2241 mtx_assert(UFS_MTX(ump), MA_OWNED);
2242
2243 if (fs->fs_contigsumsize <= 0)
2244 return;
2245 freemapp = cg_clustersfree(cgp);
2246 sump = cg_clustersum(cgp);
2247 /*
2248 * Allocate or clear the actual block.
2249 */
2250 if (cnt > 0)
2251 setbit(freemapp, blkno);
2252 else
2253 clrbit(freemapp, blkno);
2254 /*
2255 * Find the size of the cluster going forward.
2256 */
2257 start = blkno + 1;
2258 end = start + fs->fs_contigsumsize;
2259 if (end >= cgp->cg_nclusterblks)
2260 end = cgp->cg_nclusterblks;
2261 mapp = &freemapp[start / NBBY];
2262 map = *mapp++;
2263 bit = 1 << (start % NBBY);
2264 for (i = start; i < end; i++) {
2265 if ((map & bit) == 0)
2266 break;
2267 if ((i & (NBBY - 1)) != (NBBY - 1)) {
2268 bit <<= 1;
2269 } else {
2270 map = *mapp++;
2271 bit = 1;
2272 }
2273 }
2274 forw = i - start;
2275 /*
2276 * Find the size of the cluster going backward.
2277 */
2278 start = blkno - 1;
2279 end = start - fs->fs_contigsumsize;
2280 if (end < 0)
2281 end = -1;
2282 mapp = &freemapp[start / NBBY];
2283 map = *mapp--;
2284 bit = 1 << (start % NBBY);
2285 for (i = start; i > end; i--) {
2286 if ((map & bit) == 0)
2287 break;
2288 if ((i & (NBBY - 1)) != 0) {
2289 bit >>= 1;
2290 } else {
2291 map = *mapp--;
2292 bit = 1 << (NBBY - 1);
2293 }
2294 }
2295 back = start - i;
2296 /*
2297 * Account for old cluster and the possibly new forward and
2298 * back clusters.
2299 */
2300 i = back + forw + 1;
2301 if (i > fs->fs_contigsumsize)
2302 i = fs->fs_contigsumsize;
2303 sump[i] += cnt;
2304 if (back > 0)
2305 sump[back] -= cnt;
2306 if (forw > 0)
2307 sump[forw] -= cnt;
2308 /*
2309 * Update cluster summary information.
2310 */
2311 lp = &sump[fs->fs_contigsumsize];
2312 for (i = fs->fs_contigsumsize; i > 0; i--)
2313 if (*lp-- > 0)
2314 break;
2315 fs->fs_maxcluster[cgp->cg_cgx] = i;
2316 }
2317
2318 /*
2319 * Fserr prints the name of a filesystem with an error diagnostic.
2320 *
2321 * The form of the error message is:
2322 * fs: error message
2323 */
2324 static void
2325 ffs_fserr(fs, inum, cp)
2326 struct fs *fs;
2327 ino_t inum;
2328 char *cp;
2329 {
2330 struct thread *td = curthread; /* XXX */
2331 struct proc *p = td->td_proc;
2332
2333 log(LOG_ERR, "pid %d (%s), uid %d inumber %d on %s: %s\n",
2334 p->p_pid, p->p_comm, td->td_ucred->cr_uid, inum, fs->fs_fsmnt, cp);
2335 }
2336
2337 /*
2338 * This function provides the capability for the fsck program to
2339 * update an active filesystem. Eleven operations are provided:
2340 *
2341 * adjrefcnt(inode, amt) - adjusts the reference count on the
2342 * specified inode by the specified amount. Under normal
2343 * operation the count should always go down. Decrementing
2344 * the count to zero will cause the inode to be freed.
2345 * adjblkcnt(inode, amt) - adjust the number of blocks used to
2346 * by the specifed amount.
2347 * adjndir, adjbfree, adjifree, adjffree, adjnumclusters(amt) -
2348 * adjust the superblock summary.
2349 * freedirs(inode, count) - directory inodes [inode..inode + count - 1]
2350 * are marked as free. Inodes should never have to be marked
2351 * as in use.
2352 * freefiles(inode, count) - file inodes [inode..inode + count - 1]
2353 * are marked as free. Inodes should never have to be marked
2354 * as in use.
2355 * freeblks(blockno, size) - blocks [blockno..blockno + size - 1]
2356 * are marked as free. Blocks should never have to be marked
2357 * as in use.
2358 * setflags(flags, set/clear) - the fs_flags field has the specified
2359 * flags set (second parameter +1) or cleared (second parameter -1).
2360 */
2361
2362 static int sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS);
2363
2364 SYSCTL_PROC(_vfs_ffs, FFS_ADJ_REFCNT, adjrefcnt, CTLFLAG_WR|CTLTYPE_STRUCT,
2365 0, 0, sysctl_ffs_fsck, "S,fsck", "Adjust Inode Reference Count");
2366
2367 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_BLKCNT, adjblkcnt, CTLFLAG_WR,
2368 sysctl_ffs_fsck, "Adjust Inode Used Blocks Count");
2369
2370 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NDIR, adjndir, CTLFLAG_WR,
2371 sysctl_ffs_fsck, "Adjust number of directories");
2372
2373 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NBFREE, adjnbfree, CTLFLAG_WR,
2374 sysctl_ffs_fsck, "Adjust number of free blocks");
2375
2376 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NIFREE, adjnifree, CTLFLAG_WR,
2377 sysctl_ffs_fsck, "Adjust number of free inodes");
2378
2379 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NFFREE, adjnffree, CTLFLAG_WR,
2380 sysctl_ffs_fsck, "Adjust number of free frags");
2381
2382 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NUMCLUSTERS, adjnumclusters, CTLFLAG_WR,
2383 sysctl_ffs_fsck, "Adjust number of free clusters");
2384
2385 static SYSCTL_NODE(_vfs_ffs, FFS_DIR_FREE, freedirs, CTLFLAG_WR,
2386 sysctl_ffs_fsck, "Free Range of Directory Inodes");
2387
2388 static SYSCTL_NODE(_vfs_ffs, FFS_FILE_FREE, freefiles, CTLFLAG_WR,
2389 sysctl_ffs_fsck, "Free Range of File Inodes");
2390
2391 static SYSCTL_NODE(_vfs_ffs, FFS_BLK_FREE, freeblks, CTLFLAG_WR,
2392 sysctl_ffs_fsck, "Free Range of Blocks");
2393
2394 static SYSCTL_NODE(_vfs_ffs, FFS_SET_FLAGS, setflags, CTLFLAG_WR,
2395 sysctl_ffs_fsck, "Change Filesystem Flags");
2396
2397 #ifdef DEBUG
2398 static int fsckcmds = 0;
2399 SYSCTL_INT(_debug, OID_AUTO, fsckcmds, CTLFLAG_RW, &fsckcmds, 0, "");
2400 #endif /* DEBUG */
2401
2402 static int
2403 sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS)
2404 {
2405 struct fsck_cmd cmd;
2406 struct ufsmount *ump;
2407 struct vnode *vp;
2408 struct inode *ip;
2409 struct mount *mp;
2410 struct fs *fs;
2411 ufs2_daddr_t blkno;
2412 long blkcnt, blksize;
2413 struct file *fp;
2414 int filetype, error;
2415
2416 if (req->newlen > sizeof cmd)
2417 return (EBADRPC);
2418 if ((error = SYSCTL_IN(req, &cmd, sizeof cmd)) != 0)
2419 return (error);
2420 if (cmd.version != FFS_CMD_VERSION)
2421 return (ERPCMISMATCH);
2422 if ((error = getvnode(curproc->p_fd, cmd.handle, &fp)) != 0)
2423 return (error);
2424 vn_start_write(fp->f_data, &mp, V_WAIT);
2425 if (mp == 0 || strncmp(mp->mnt_stat.f_fstypename, "ufs", MFSNAMELEN)) {
2426 vn_finished_write(mp);
2427 fdrop(fp, curthread);
2428 return (EINVAL);
2429 }
2430 if (mp->mnt_flag & MNT_RDONLY) {
2431 vn_finished_write(mp);
2432 fdrop(fp, curthread);
2433 return (EROFS);
2434 }
2435 ump = VFSTOUFS(mp);
2436 fs = ump->um_fs;
2437 filetype = IFREG;
2438
2439 switch (oidp->oid_number) {
2440
2441 case FFS_SET_FLAGS:
2442 #ifdef DEBUG
2443 if (fsckcmds)
2444 printf("%s: %s flags\n", mp->mnt_stat.f_mntonname,
2445 cmd.size > 0 ? "set" : "clear");
2446 #endif /* DEBUG */
2447 if (cmd.size > 0)
2448 fs->fs_flags |= (long)cmd.value;
2449 else
2450 fs->fs_flags &= ~(long)cmd.value;
2451 break;
2452
2453 case FFS_ADJ_REFCNT:
2454 #ifdef DEBUG
2455 if (fsckcmds) {
2456 printf("%s: adjust inode %jd count by %jd\n",
2457 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2458 (intmax_t)cmd.size);
2459 }
2460 #endif /* DEBUG */
2461 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2462 break;
2463 ip = VTOI(vp);
2464 ip->i_nlink += cmd.size;
2465 DIP_SET(ip, i_nlink, ip->i_nlink);
2466 ip->i_effnlink += cmd.size;
2467 ip->i_flag |= IN_CHANGE;
2468 if (DOINGSOFTDEP(vp))
2469 softdep_change_linkcnt(ip);
2470 vput(vp);
2471 break;
2472
2473 case FFS_ADJ_BLKCNT:
2474 #ifdef DEBUG
2475 if (fsckcmds) {
2476 printf("%s: adjust inode %jd block count by %jd\n",
2477 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2478 (intmax_t)cmd.size);
2479 }
2480 #endif /* DEBUG */
2481 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2482 break;
2483 ip = VTOI(vp);
2484 if (ip->i_flag & IN_SPACECOUNTED) {
2485 UFS_LOCK(ump);
2486 fs->fs_pendingblocks += cmd.size;
2487 UFS_UNLOCK(ump);
2488 }
2489 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + cmd.size);
2490 ip->i_flag |= IN_CHANGE;
2491 vput(vp);
2492 break;
2493
2494 case FFS_DIR_FREE:
2495 filetype = IFDIR;
2496 /* fall through */
2497
2498 case FFS_FILE_FREE:
2499 #ifdef DEBUG
2500 if (fsckcmds) {
2501 if (cmd.size == 1)
2502 printf("%s: free %s inode %d\n",
2503 mp->mnt_stat.f_mntonname,
2504 filetype == IFDIR ? "directory" : "file",
2505 (ino_t)cmd.value);
2506 else
2507 printf("%s: free %s inodes %d-%d\n",
2508 mp->mnt_stat.f_mntonname,
2509 filetype == IFDIR ? "directory" : "file",
2510 (ino_t)cmd.value,
2511 (ino_t)(cmd.value + cmd.size - 1));
2512 }
2513 #endif /* DEBUG */
2514 while (cmd.size > 0) {
2515 if ((error = ffs_freefile(ump, fs, ump->um_devvp,
2516 cmd.value, filetype)))
2517 break;
2518 cmd.size -= 1;
2519 cmd.value += 1;
2520 }
2521 break;
2522
2523 case FFS_BLK_FREE:
2524 #ifdef DEBUG
2525 if (fsckcmds) {
2526 if (cmd.size == 1)
2527 printf("%s: free block %jd\n",
2528 mp->mnt_stat.f_mntonname,
2529 (intmax_t)cmd.value);
2530 else
2531 printf("%s: free blocks %jd-%jd\n",
2532 mp->mnt_stat.f_mntonname,
2533 (intmax_t)cmd.value,
2534 (intmax_t)cmd.value + cmd.size - 1);
2535 }
2536 #endif /* DEBUG */
2537 blkno = cmd.value;
2538 blkcnt = cmd.size;
2539 blksize = fs->fs_frag - (blkno % fs->fs_frag);
2540 while (blkcnt > 0) {
2541 if (blksize > blkcnt)
2542 blksize = blkcnt;
2543 ffs_blkfree(ump, fs, ump->um_devvp, blkno,
2544 blksize * fs->fs_fsize, ROOTINO);
2545 blkno += blksize;
2546 blkcnt -= blksize;
2547 blksize = fs->fs_frag;
2548 }
2549 break;
2550
2551 /*
2552 * Adjust superblock summaries. fsck(8) is expected to
2553 * submit deltas when necessary.
2554 */
2555 case FFS_ADJ_NDIR:
2556 #ifdef DEBUG
2557 if (fsckcmds) {
2558 printf("%s: adjust number of directories by %jd\n",
2559 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2560 }
2561 #endif /* DEBUG */
2562 fs->fs_cstotal.cs_ndir += cmd.value;
2563 break;
2564 case FFS_ADJ_NBFREE:
2565 #ifdef DEBUG
2566 if (fsckcmds) {
2567 printf("%s: adjust number of free blocks by %+jd\n",
2568 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2569 }
2570 #endif /* DEBUG */
2571 fs->fs_cstotal.cs_nbfree += cmd.value;
2572 break;
2573 case FFS_ADJ_NIFREE:
2574 #ifdef DEBUG
2575 if (fsckcmds) {
2576 printf("%s: adjust number of free inodes by %+jd\n",
2577 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2578 }
2579 #endif /* DEBUG */
2580 fs->fs_cstotal.cs_nifree += cmd.value;
2581 break;
2582 case FFS_ADJ_NFFREE:
2583 #ifdef DEBUG
2584 if (fsckcmds) {
2585 printf("%s: adjust number of free frags by %+jd\n",
2586 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2587 }
2588 #endif /* DEBUG */
2589 fs->fs_cstotal.cs_nffree += cmd.value;
2590 break;
2591 case FFS_ADJ_NUMCLUSTERS:
2592 #ifdef DEBUG
2593 if (fsckcmds) {
2594 printf("%s: adjust number of free clusters by %+jd\n",
2595 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2596 }
2597 #endif /* DEBUG */
2598 fs->fs_cstotal.cs_numclusters += cmd.value;
2599 break;
2600
2601 default:
2602 #ifdef DEBUG
2603 if (fsckcmds) {
2604 printf("Invalid request %d from fsck\n",
2605 oidp->oid_number);
2606 }
2607 #endif /* DEBUG */
2608 error = EINVAL;
2609 break;
2610
2611 }
2612 fdrop(fp, curthread);
2613 vn_finished_write(mp);
2614 return (error);
2615 }
Cache object: 16a1d4150e102a4665f23d5a344ba039
|