ffs_alloc.c revision 1.30 1 /* $NetBSD: ffs_alloc.c,v 1.30 1999/11/15 18:49:13 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95
36 */
37
38 #if defined(_KERNEL) && !defined(_LKM)
39 #include "opt_ffs.h"
40 #include "opt_quota.h"
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/buf.h>
46 #include <sys/proc.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <sys/kernel.h>
50 #include <sys/syslog.h>
51
52 #include <vm/vm.h>
53
54 #include <uvm/uvm_extern.h>
55
56 #include <ufs/ufs/quota.h>
57 #include <ufs/ufs/ufsmount.h>
58 #include <ufs/ufs/inode.h>
59 #include <ufs/ufs/ufs_extern.h>
60 #include <ufs/ufs/ufs_bswap.h>
61
62 #include <ufs/ffs/fs.h>
63 #include <ufs/ffs/ffs_extern.h>
64
65 static ufs_daddr_t ffs_alloccg __P((struct inode *, int, ufs_daddr_t, int));
66 static ufs_daddr_t ffs_alloccgblk __P((struct inode *, struct buf *,
67 ufs_daddr_t));
68 static ufs_daddr_t ffs_clusteralloc __P((struct inode *, int, ufs_daddr_t, int));
69 static ino_t ffs_dirpref __P((struct fs *));
70 static ufs_daddr_t ffs_fragextend __P((struct inode *, int, long, int, int));
71 static void ffs_fserr __P((struct fs *, u_int, char *));
72 static u_long ffs_hashalloc
73 __P((struct inode *, int, long, int,
74 ufs_daddr_t (*)(struct inode *, int, ufs_daddr_t, int)));
75 static ufs_daddr_t ffs_nodealloccg __P((struct inode *, int, ufs_daddr_t, int));
76 static ufs_daddr_t ffs_mapsearch __P((struct fs *, struct cg *,
77 ufs_daddr_t, int));
78 #if defined(DIAGNOSTIC) || defined(DEBUG)
79 static int ffs_checkblk __P((struct inode *, ufs_daddr_t, long size));
80 #endif
81
82 /* in ffs_tables.c */
83 extern int inside[], around[];
84 extern u_char *fragtbl[];
85
86 /*
87 * Allocate a block in the file system.
88 *
89 * The size of the requested block is given, which must be some
90 * multiple of fs_fsize and <= fs_bsize.
91 * A preference may be optionally specified. If a preference is given
92 * the following hierarchy is used to allocate a block:
93 * 1) allocate the requested block.
94 * 2) allocate a rotationally optimal block in the same cylinder.
95 * 3) allocate a block in the same cylinder group.
96 * 4) quadradically rehash into other cylinder groups, until an
97 * available block is located.
98 * If no block preference is given the following heirarchy is used
99 * to allocate a block:
100 * 1) allocate a block in the cylinder group that contains the
101 * inode for the file.
102 * 2) quadradically rehash into other cylinder groups, until an
103 * available block is located.
104 */
105 int
106 ffs_alloc(ip, lbn, bpref, size, cred, bnp)
107 register struct inode *ip;
108 ufs_daddr_t lbn, bpref;
109 int size;
110 struct ucred *cred;
111 ufs_daddr_t *bnp;
112 {
113 register struct fs *fs;
114 ufs_daddr_t bno;
115 int cg;
116 #ifdef QUOTA
117 int error;
118 #endif
119
120 *bnp = 0;
121 fs = ip->i_fs;
122 #ifdef DIAGNOSTIC
123 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
124 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
125 ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
126 panic("ffs_alloc: bad size");
127 }
128 if (cred == NOCRED)
129 panic("ffs_alloc: missing credential\n");
130 #endif /* DIAGNOSTIC */
131 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
132 goto nospace;
133 if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
134 goto nospace;
135 #ifdef QUOTA
136 if ((error = chkdq(ip, (long)btodb(size), cred, 0)) != 0)
137 return (error);
138 #endif
139 if (bpref >= fs->fs_size)
140 bpref = 0;
141 if (bpref == 0)
142 cg = ino_to_cg(fs, ip->i_number);
143 else
144 cg = dtog(fs, bpref);
145 bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size,
146 ffs_alloccg);
147 if (bno > 0) {
148 ip->i_ffs_blocks += btodb(size);
149 ip->i_flag |= IN_CHANGE | IN_UPDATE;
150 *bnp = bno;
151 return (0);
152 }
153 #ifdef QUOTA
154 /*
155 * Restore user's disk quota because allocation failed.
156 */
157 (void) chkdq(ip, (long)-btodb(size), cred, FORCE);
158 #endif
159 nospace:
160 ffs_fserr(fs, cred->cr_uid, "file system full");
161 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
162 return (ENOSPC);
163 }
164
165 /*
166 * Reallocate a fragment to a bigger size
167 *
168 * The number and size of the old block is given, and a preference
169 * and new size is also specified. The allocator attempts to extend
170 * the original block. Failing that, the regular block allocator is
171 * invoked to get an appropriate block.
172 */
173 int
174 ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp)
175 register struct inode *ip;
176 ufs_daddr_t lbprev;
177 ufs_daddr_t bpref;
178 int osize, nsize;
179 struct ucred *cred;
180 struct buf **bpp;
181 {
182 register struct fs *fs;
183 struct buf *bp;
184 int cg, request, error;
185 ufs_daddr_t bprev, bno;
186
187 *bpp = 0;
188 fs = ip->i_fs;
189 #ifdef DIAGNOSTIC
190 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
191 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
192 printf(
193 "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
194 ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
195 panic("ffs_realloccg: bad size");
196 }
197 if (cred == NOCRED)
198 panic("ffs_realloccg: missing credential\n");
199 #endif /* DIAGNOSTIC */
200 if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
201 goto nospace;
202 if ((bprev = ufs_rw32(ip->i_ffs_db[lbprev], UFS_FSNEEDSWAP(fs))) == 0) {
203 printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
204 ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
205 panic("ffs_realloccg: bad bprev");
206 }
207 /*
208 * Allocate the extra space in the buffer.
209 */
210 if ((error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) != 0) {
211 brelse(bp);
212 return (error);
213 }
214 #ifdef QUOTA
215 if ((error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) != 0) {
216 brelse(bp);
217 return (error);
218 }
219 #endif
220 /*
221 * Check for extension in the existing location.
222 */
223 cg = dtog(fs, bprev);
224 if ((bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) != 0) {
225 if (bp->b_blkno != fsbtodb(fs, bno))
226 panic("bad blockno");
227 ip->i_ffs_blocks += btodb(nsize - osize);
228 ip->i_flag |= IN_CHANGE | IN_UPDATE;
229 allocbuf(bp, nsize);
230 bp->b_flags |= B_DONE;
231 memset((char *)bp->b_data + osize, 0, (u_int)nsize - osize);
232 *bpp = bp;
233 return (0);
234 }
235 /*
236 * Allocate a new disk location.
237 */
238 if (bpref >= fs->fs_size)
239 bpref = 0;
240 switch ((int)fs->fs_optim) {
241 case FS_OPTSPACE:
242 /*
243 * Allocate an exact sized fragment. Although this makes
244 * best use of space, we will waste time relocating it if
245 * the file continues to grow. If the fragmentation is
246 * less than half of the minimum free reserve, we choose
247 * to begin optimizing for time.
248 */
249 request = nsize;
250 if (fs->fs_minfree < 5 ||
251 fs->fs_cstotal.cs_nffree >
252 fs->fs_dsize * fs->fs_minfree / (2 * 100))
253 break;
254 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
255 fs->fs_fsmnt);
256 fs->fs_optim = FS_OPTTIME;
257 break;
258 case FS_OPTTIME:
259 /*
260 * At this point we have discovered a file that is trying to
261 * grow a small fragment to a larger fragment. To save time,
262 * we allocate a full sized block, then free the unused portion.
263 * If the file continues to grow, the `ffs_fragextend' call
264 * above will be able to grow it in place without further
265 * copying. If aberrant programs cause disk fragmentation to
266 * grow within 2% of the free reserve, we choose to begin
267 * optimizing for space.
268 */
269 request = fs->fs_bsize;
270 if (fs->fs_cstotal.cs_nffree <
271 fs->fs_dsize * (fs->fs_minfree - 2) / 100)
272 break;
273 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
274 fs->fs_fsmnt);
275 fs->fs_optim = FS_OPTSPACE;
276 break;
277 default:
278 printf("dev = 0x%x, optim = %d, fs = %s\n",
279 ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
280 panic("ffs_realloccg: bad optim");
281 /* NOTREACHED */
282 }
283 bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
284 ffs_alloccg);
285 if (bno > 0) {
286 bp->b_blkno = fsbtodb(fs, bno);
287 (void) uvm_vnp_uncache(ITOV(ip));
288 if (!DOINGSOFTDEP(ITOV(ip)))
289 ffs_blkfree(ip, bprev, (long)osize);
290 if (nsize < request)
291 ffs_blkfree(ip, bno + numfrags(fs, nsize),
292 (long)(request - nsize));
293 ip->i_ffs_blocks += btodb(nsize - osize);
294 ip->i_flag |= IN_CHANGE | IN_UPDATE;
295 allocbuf(bp, nsize);
296 bp->b_flags |= B_DONE;
297 memset((char *)bp->b_data + osize, 0, (u_int)nsize - osize);
298 *bpp = bp;
299 return (0);
300 }
301 #ifdef QUOTA
302 /*
303 * Restore user's disk quota because allocation failed.
304 */
305 (void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE);
306 #endif
307 brelse(bp);
308 nospace:
309 /*
310 * no space available
311 */
312 ffs_fserr(fs, cred->cr_uid, "file system full");
313 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
314 return (ENOSPC);
315 }
316
317 /*
318 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
319 *
320 * The vnode and an array of buffer pointers for a range of sequential
321 * logical blocks to be made contiguous is given. The allocator attempts
322 * to find a range of sequential blocks starting as close as possible to
323 * an fs_rotdelay offset from the end of the allocation for the logical
324 * block immediately preceeding the current range. If successful, the
325 * physical block numbers in the buffer pointers and in the inode are
326 * changed to reflect the new allocation. If unsuccessful, the allocation
327 * is left unchanged. The success in doing the reallocation is returned.
328 * Note that the error return is not reflected back to the user. Rather
329 * the previous block allocation will be used.
330 */
331 #ifdef DEBUG
332 #include <sys/sysctl.h>
333 int prtrealloc = 0;
334 struct ctldebug debug15 = { "prtrealloc", &prtrealloc };
335 #endif
336
337 int doasyncfree = 1;
338 extern int doreallocblks;
339
340 int
341 ffs_reallocblks(v)
342 void *v;
343 {
344 struct vop_reallocblks_args /* {
345 struct vnode *a_vp;
346 struct cluster_save *a_buflist;
347 } */ *ap = v;
348 struct fs *fs;
349 struct inode *ip;
350 struct vnode *vp;
351 struct buf *sbp, *ebp;
352 ufs_daddr_t *bap, *sbap, *ebap = NULL;
353 struct cluster_save *buflist;
354 ufs_daddr_t start_lbn, end_lbn, soff, newblk, blkno;
355 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
356 int i, len, start_lvl, end_lvl, pref, ssize;
357
358 vp = ap->a_vp;
359 ip = VTOI(vp);
360 fs = ip->i_fs;
361 if (fs->fs_contigsumsize <= 0)
362 return (ENOSPC);
363 buflist = ap->a_buflist;
364 len = buflist->bs_nchildren;
365 start_lbn = buflist->bs_children[0]->b_lblkno;
366 end_lbn = start_lbn + len - 1;
367 #ifdef DIAGNOSTIC
368 for (i = 0; i < len; i++)
369 if (!ffs_checkblk(ip,
370 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
371 panic("ffs_reallocblks: unallocated block 1");
372 for (i = 1; i < len; i++)
373 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
374 panic("ffs_reallocblks: non-logical cluster");
375 blkno = buflist->bs_children[0]->b_blkno;
376 ssize = fsbtodb(fs, fs->fs_frag);
377 for (i = 1; i < len - 1; i++)
378 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
379 panic("ffs_reallocblks: non-physical cluster %d", i);
380 #endif
381 /*
382 * If the latest allocation is in a new cylinder group, assume that
383 * the filesystem has decided to move and do not force it back to
384 * the previous cylinder group.
385 */
386 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
387 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
388 return (ENOSPC);
389 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
390 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
391 return (ENOSPC);
392 /*
393 * Get the starting offset and block map for the first block.
394 */
395 if (start_lvl == 0) {
396 sbap = &ip->i_ffs_db[0];
397 soff = start_lbn;
398 } else {
399 idp = &start_ap[start_lvl - 1];
400 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
401 brelse(sbp);
402 return (ENOSPC);
403 }
404 sbap = (ufs_daddr_t *)sbp->b_data;
405 soff = idp->in_off;
406 }
407 /*
408 * Find the preferred location for the cluster.
409 */
410 pref = ffs_blkpref(ip, start_lbn, soff, sbap);
411 /*
412 * If the block range spans two block maps, get the second map.
413 */
414 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
415 ssize = len;
416 } else {
417 #ifdef DIAGNOSTIC
418 if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
419 panic("ffs_reallocblk: start == end");
420 #endif
421 ssize = len - (idp->in_off + 1);
422 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
423 goto fail;
424 ebap = (ufs_daddr_t *)ebp->b_data;
425 }
426 /*
427 * Search the block map looking for an allocation of the desired size.
428 */
429 if ((newblk = (ufs_daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref,
430 len, ffs_clusteralloc)) == 0)
431 goto fail;
432 /*
433 * We have found a new contiguous block.
434 *
435 * First we have to replace the old block pointers with the new
436 * block pointers in the inode and indirect blocks associated
437 * with the file.
438 */
439 #ifdef DEBUG
440 if (prtrealloc)
441 printf("realloc: ino %d, lbns %d-%d\n\told:", ip->i_number,
442 start_lbn, end_lbn);
443 #endif
444 blkno = newblk;
445 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
446 ufs_daddr_t ba;
447
448 if (i == ssize) {
449 bap = ebap;
450 soff = -i;
451 }
452 ba = ufs_rw32(*bap, UFS_FSNEEDSWAP(fs));
453 #ifdef DIAGNOSTIC
454 if (!ffs_checkblk(ip,
455 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
456 panic("ffs_reallocblks: unallocated block 2");
457 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != ba)
458 panic("ffs_reallocblks: alloc mismatch");
459 #endif
460 #ifdef DEBUG
461 if (prtrealloc)
462 printf(" %d,", ba);
463 #endif
464 if (DOINGSOFTDEP(vp)) {
465 if (sbap == &ip->i_ffs_db[0] && i < ssize)
466 softdep_setup_allocdirect(ip, start_lbn + i,
467 blkno, ba, fs->fs_bsize, fs->fs_bsize,
468 buflist->bs_children[i]);
469 else
470 softdep_setup_allocindir_page(ip, start_lbn + i,
471 i < ssize ? sbp : ebp, soff + i, blkno,
472 ba, buflist->bs_children[i]);
473 }
474 *bap++ = ufs_rw32(blkno, UFS_FSNEEDSWAP(fs));
475 }
476 /*
477 * Next we must write out the modified inode and indirect blocks.
478 * For strict correctness, the writes should be synchronous since
479 * the old block values may have been written to disk. In practise
480 * they are almost never written, but if we are concerned about
481 * strict correctness, the `doasyncfree' flag should be set to zero.
482 *
483 * The test on `doasyncfree' should be changed to test a flag
484 * that shows whether the associated buffers and inodes have
485 * been written. The flag should be set when the cluster is
486 * started and cleared whenever the buffer or inode is flushed.
487 * We can then check below to see if it is set, and do the
488 * synchronous write only when it has been cleared.
489 */
490 if (sbap != &ip->i_ffs_db[0]) {
491 if (doasyncfree)
492 bdwrite(sbp);
493 else
494 bwrite(sbp);
495 } else {
496 ip->i_flag |= IN_CHANGE | IN_UPDATE;
497 if (!doasyncfree)
498 VOP_UPDATE(vp, NULL, NULL, 1);
499 }
500 if (ssize < len) {
501 if (doasyncfree)
502 bdwrite(ebp);
503 else
504 bwrite(ebp);
505 }
506 /*
507 * Last, free the old blocks and assign the new blocks to the buffers.
508 */
509 #ifdef DEBUG
510 if (prtrealloc)
511 printf("\n\tnew:");
512 #endif
513 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
514 if (!DOINGSOFTDEP(vp))
515 ffs_blkfree(ip,
516 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
517 fs->fs_bsize);
518 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
519 #ifdef DEBUG
520 if (!ffs_checkblk(ip,
521 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
522 panic("ffs_reallocblks: unallocated block 3");
523 if (prtrealloc)
524 printf(" %d,", blkno);
525 #endif
526 }
527 #ifdef DEBUG
528 if (prtrealloc) {
529 prtrealloc--;
530 printf("\n");
531 }
532 #endif
533 return (0);
534
535 fail:
536 if (ssize < len)
537 brelse(ebp);
538 if (sbap != &ip->i_ffs_db[0])
539 brelse(sbp);
540 return (ENOSPC);
541 }
542
543 /*
544 * Allocate an inode in the file system.
545 *
546 * If allocating a directory, use ffs_dirpref to select the inode.
547 * If allocating in a directory, the following hierarchy is followed:
548 * 1) allocate the preferred inode.
549 * 2) allocate an inode in the same cylinder group.
550 * 3) quadradically rehash into other cylinder groups, until an
551 * available inode is located.
552 * If no inode preference is given the following heirarchy is used
553 * to allocate an inode:
554 * 1) allocate an inode in cylinder group 0.
555 * 2) quadradically rehash into other cylinder groups, until an
556 * available inode is located.
557 */
558 int
559 ffs_valloc(v)
560 void *v;
561 {
562 struct vop_valloc_args /* {
563 struct vnode *a_pvp;
564 int a_mode;
565 struct ucred *a_cred;
566 struct vnode **a_vpp;
567 } */ *ap = v;
568 register struct vnode *pvp = ap->a_pvp;
569 register struct inode *pip;
570 register struct fs *fs;
571 register struct inode *ip;
572 mode_t mode = ap->a_mode;
573 ino_t ino, ipref;
574 int cg, error;
575
576 *ap->a_vpp = NULL;
577 pip = VTOI(pvp);
578 fs = pip->i_fs;
579 if (fs->fs_cstotal.cs_nifree == 0)
580 goto noinodes;
581
582 if ((mode & IFMT) == IFDIR)
583 ipref = ffs_dirpref(fs);
584 else
585 ipref = pip->i_number;
586 if (ipref >= fs->fs_ncg * fs->fs_ipg)
587 ipref = 0;
588 cg = ino_to_cg(fs, ipref);
589 ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_nodealloccg);
590 if (ino == 0)
591 goto noinodes;
592 error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp);
593 if (error) {
594 VOP_VFREE(pvp, ino, mode);
595 return (error);
596 }
597 ip = VTOI(*ap->a_vpp);
598 if (ip->i_ffs_mode) {
599 printf("mode = 0%o, inum = %d, fs = %s\n",
600 ip->i_ffs_mode, ip->i_number, fs->fs_fsmnt);
601 panic("ffs_valloc: dup alloc");
602 }
603 if (ip->i_ffs_blocks) { /* XXX */
604 printf("free inode %s/%d had %d blocks\n",
605 fs->fs_fsmnt, ino, ip->i_ffs_blocks);
606 ip->i_ffs_blocks = 0;
607 }
608 ip->i_ffs_flags = 0;
609 /*
610 * Set up a new generation number for this inode.
611 */
612 ip->i_ffs_gen++;
613 return (0);
614 noinodes:
615 ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes");
616 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
617 return (ENOSPC);
618 }
619
620 /*
621 * Find a cylinder to place a directory.
622 *
623 * The policy implemented by this algorithm is to select from
624 * among those cylinder groups with above the average number of
625 * free inodes, the one with the smallest number of directories.
626 */
627 static ino_t
628 ffs_dirpref(fs)
629 register struct fs *fs;
630 {
631 int cg, minndir, mincg, avgifree;
632
633 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
634 minndir = fs->fs_ipg;
635 mincg = 0;
636 for (cg = 0; cg < fs->fs_ncg; cg++)
637 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
638 fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
639 mincg = cg;
640 minndir = fs->fs_cs(fs, cg).cs_ndir;
641 }
642 return ((ino_t)(fs->fs_ipg * mincg));
643 }
644
645 /*
646 * Select the desired position for the next block in a file. The file is
647 * logically divided into sections. The first section is composed of the
648 * direct blocks. Each additional section contains fs_maxbpg blocks.
649 *
650 * If no blocks have been allocated in the first section, the policy is to
651 * request a block in the same cylinder group as the inode that describes
652 * the file. If no blocks have been allocated in any other section, the
653 * policy is to place the section in a cylinder group with a greater than
654 * average number of free blocks. An appropriate cylinder group is found
655 * by using a rotor that sweeps the cylinder groups. When a new group of
656 * blocks is needed, the sweep begins in the cylinder group following the
657 * cylinder group from which the previous allocation was made. The sweep
658 * continues until a cylinder group with greater than the average number
659 * of free blocks is found. If the allocation is for the first block in an
660 * indirect block, the information on the previous allocation is unavailable;
661 * here a best guess is made based upon the logical block number being
662 * allocated.
663 *
664 * If a section is already partially allocated, the policy is to
665 * contiguously allocate fs_maxcontig blocks. The end of one of these
666 * contiguous blocks and the beginning of the next is physically separated
667 * so that the disk head will be in transit between them for at least
668 * fs_rotdelay milliseconds. This is to allow time for the processor to
669 * schedule another I/O transfer.
670 */
671 ufs_daddr_t
672 ffs_blkpref(ip, lbn, indx, bap)
673 struct inode *ip;
674 ufs_daddr_t lbn;
675 int indx;
676 ufs_daddr_t *bap;
677 {
678 register struct fs *fs;
679 register int cg;
680 int avgbfree, startcg;
681 ufs_daddr_t nextblk;
682
683 fs = ip->i_fs;
684 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
685 if (lbn < NDADDR) {
686 cg = ino_to_cg(fs, ip->i_number);
687 return (fs->fs_fpg * cg + fs->fs_frag);
688 }
689 /*
690 * Find a cylinder with greater than average number of
691 * unused data blocks.
692 */
693 if (indx == 0 || bap[indx - 1] == 0)
694 startcg =
695 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
696 else
697 startcg = dtog(fs,
698 ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
699 startcg %= fs->fs_ncg;
700 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
701 for (cg = startcg; cg < fs->fs_ncg; cg++)
702 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
703 fs->fs_cgrotor = cg;
704 return (fs->fs_fpg * cg + fs->fs_frag);
705 }
706 for (cg = 0; cg <= startcg; cg++)
707 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
708 fs->fs_cgrotor = cg;
709 return (fs->fs_fpg * cg + fs->fs_frag);
710 }
711 return (NULL);
712 }
713 /*
714 * One or more previous blocks have been laid out. If less
715 * than fs_maxcontig previous blocks are contiguous, the
716 * next block is requested contiguously, otherwise it is
717 * requested rotationally delayed by fs_rotdelay milliseconds.
718 */
719 nextblk = ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
720 if (indx < fs->fs_maxcontig ||
721 ufs_rw32(bap[indx - fs->fs_maxcontig], UFS_FSNEEDSWAP(fs)) +
722 blkstofrags(fs, fs->fs_maxcontig) != nextblk)
723 return (nextblk);
724 if (fs->fs_rotdelay != 0)
725 /*
726 * Here we convert ms of delay to frags as:
727 * (frags) = (ms) * (rev/sec) * (sect/rev) /
728 * ((sect/frag) * (ms/sec))
729 * then round up to the next block.
730 */
731 nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
732 (NSPF(fs) * 1000), fs->fs_frag);
733 return (nextblk);
734 }
735
736 /*
737 * Implement the cylinder overflow algorithm.
738 *
739 * The policy implemented by this algorithm is:
740 * 1) allocate the block in its requested cylinder group.
741 * 2) quadradically rehash on the cylinder group number.
742 * 3) brute force search for a free block.
743 */
744 /*VARARGS5*/
745 static u_long
746 ffs_hashalloc(ip, cg, pref, size, allocator)
747 struct inode *ip;
748 int cg;
749 long pref;
750 int size; /* size for data blocks, mode for inodes */
751 ufs_daddr_t (*allocator) __P((struct inode *, int, ufs_daddr_t, int));
752 {
753 register struct fs *fs;
754 long result;
755 int i, icg = cg;
756
757 fs = ip->i_fs;
758 /*
759 * 1: preferred cylinder group
760 */
761 result = (*allocator)(ip, cg, pref, size);
762 if (result)
763 return (result);
764 /*
765 * 2: quadratic rehash
766 */
767 for (i = 1; i < fs->fs_ncg; i *= 2) {
768 cg += i;
769 if (cg >= fs->fs_ncg)
770 cg -= fs->fs_ncg;
771 result = (*allocator)(ip, cg, 0, size);
772 if (result)
773 return (result);
774 }
775 /*
776 * 3: brute force search
777 * Note that we start at i == 2, since 0 was checked initially,
778 * and 1 is always checked in the quadratic rehash.
779 */
780 cg = (icg + 2) % fs->fs_ncg;
781 for (i = 2; i < fs->fs_ncg; i++) {
782 result = (*allocator)(ip, cg, 0, size);
783 if (result)
784 return (result);
785 cg++;
786 if (cg == fs->fs_ncg)
787 cg = 0;
788 }
789 return (NULL);
790 }
791
792 /*
793 * Determine whether a fragment can be extended.
794 *
795 * Check to see if the necessary fragments are available, and
796 * if they are, allocate them.
797 */
798 static ufs_daddr_t
799 ffs_fragextend(ip, cg, bprev, osize, nsize)
800 struct inode *ip;
801 int cg;
802 long bprev;
803 int osize, nsize;
804 {
805 register struct fs *fs;
806 register struct cg *cgp;
807 struct buf *bp;
808 long bno;
809 int frags, bbase;
810 int i, error;
811
812 fs = ip->i_fs;
813 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
814 return (NULL);
815 frags = numfrags(fs, nsize);
816 bbase = fragnum(fs, bprev);
817 if (bbase > fragnum(fs, (bprev + frags - 1))) {
818 /* cannot extend across a block boundary */
819 return (NULL);
820 }
821 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
822 (int)fs->fs_cgsize, NOCRED, &bp);
823 if (error) {
824 brelse(bp);
825 return (NULL);
826 }
827 cgp = (struct cg *)bp->b_data;
828 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
829 brelse(bp);
830 return (NULL);
831 }
832 cgp->cg_time = ufs_rw32(time.tv_sec, UFS_FSNEEDSWAP(fs));
833 bno = dtogd(fs, bprev);
834 for (i = numfrags(fs, osize); i < frags; i++)
835 if (isclr(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i)) {
836 brelse(bp);
837 return (NULL);
838 }
839 /*
840 * the current fragment can be extended
841 * deduct the count on fragment being extended into
842 * increase the count on the remaining fragment (if any)
843 * allocate the extended piece
844 */
845 for (i = frags; i < fs->fs_frag - bbase; i++)
846 if (isclr(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i))
847 break;
848 ufs_add32(cgp->cg_frsum[i - numfrags(fs, osize)], -1, UFS_FSNEEDSWAP(fs));
849 if (i != frags)
850 ufs_add32(cgp->cg_frsum[i - frags], 1, UFS_FSNEEDSWAP(fs));
851 for (i = numfrags(fs, osize); i < frags; i++) {
852 clrbit(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i);
853 ufs_add32(cgp->cg_cs.cs_nffree, -1, UFS_FSNEEDSWAP(fs));
854 fs->fs_cstotal.cs_nffree--;
855 fs->fs_cs(fs, cg).cs_nffree--;
856 }
857 fs->fs_fmod = 1;
858 if (DOINGSOFTDEP(ITOV(ip)))
859 softdep_setup_blkmapdep(bp, fs, bprev);
860 bdwrite(bp);
861 return (bprev);
862 }
863
864 /*
865 * Determine whether a block can be allocated.
866 *
867 * Check to see if a block of the appropriate size is available,
868 * and if it is, allocate it.
869 */
870 static ufs_daddr_t
871 ffs_alloccg(ip, cg, bpref, size)
872 struct inode *ip;
873 int cg;
874 ufs_daddr_t bpref;
875 int size;
876 {
877 struct cg *cgp;
878 struct buf *bp;
879 ufs_daddr_t bno, blkno;
880 int error, frags, allocsiz, i;
881 struct fs *fs = ip->i_fs;
882 #ifdef FFS_EI
883 const int needswap = UFS_FSNEEDSWAP(fs);
884 #endif
885
886 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
887 return (NULL);
888 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
889 (int)fs->fs_cgsize, NOCRED, &bp);
890 if (error) {
891 brelse(bp);
892 return (NULL);
893 }
894 cgp = (struct cg *)bp->b_data;
895 if (!cg_chkmagic(cgp, needswap) ||
896 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
897 brelse(bp);
898 return (NULL);
899 }
900 cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
901 if (size == fs->fs_bsize) {
902 bno = ffs_alloccgblk(ip, bp, bpref);
903 bdwrite(bp);
904 return (bno);
905 }
906 /*
907 * check to see if any fragments are already available
908 * allocsiz is the size which will be allocated, hacking
909 * it down to a smaller size if necessary
910 */
911 frags = numfrags(fs, size);
912 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
913 if (cgp->cg_frsum[allocsiz] != 0)
914 break;
915 if (allocsiz == fs->fs_frag) {
916 /*
917 * no fragments were available, so a block will be
918 * allocated, and hacked up
919 */
920 if (cgp->cg_cs.cs_nbfree == 0) {
921 brelse(bp);
922 return (NULL);
923 }
924 bno = ffs_alloccgblk(ip, bp, bpref);
925 bpref = dtogd(fs, bno);
926 for (i = frags; i < fs->fs_frag; i++)
927 setbit(cg_blksfree(cgp, needswap), bpref + i);
928 i = fs->fs_frag - frags;
929 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
930 fs->fs_cstotal.cs_nffree += i;
931 fs->fs_cs(fs, cg).cs_nffree += i;
932 fs->fs_fmod = 1;
933 ufs_add32(cgp->cg_frsum[i], 1, needswap);
934 bdwrite(bp);
935 return (bno);
936 }
937 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
938 #if 0
939 /*
940 * XXX fvdl mapsearch will panic, and never return -1
941 * also: returning NULL as ufs_daddr_t ?
942 */
943 if (bno < 0) {
944 brelse(bp);
945 return (NULL);
946 }
947 #endif
948 for (i = 0; i < frags; i++)
949 clrbit(cg_blksfree(cgp, needswap), bno + i);
950 ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
951 fs->fs_cstotal.cs_nffree -= frags;
952 fs->fs_cs(fs, cg).cs_nffree -= frags;
953 fs->fs_fmod = 1;
954 ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
955 if (frags != allocsiz)
956 ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
957 blkno = cg * fs->fs_fpg + bno;
958 if (DOINGSOFTDEP(ITOV(ip)))
959 softdep_setup_blkmapdep(bp, fs, blkno);
960 bdwrite(bp);
961 return blkno;
962 }
963
964 /*
965 * Allocate a block in a cylinder group.
966 *
967 * This algorithm implements the following policy:
968 * 1) allocate the requested block.
969 * 2) allocate a rotationally optimal block in the same cylinder.
970 * 3) allocate the next available block on the block rotor for the
971 * specified cylinder group.
972 * Note that this routine only allocates fs_bsize blocks; these
973 * blocks may be fragmented by the routine that allocates them.
974 */
975 static ufs_daddr_t
976 ffs_alloccgblk(ip, bp, bpref)
977 struct inode *ip;
978 struct buf *bp;
979 ufs_daddr_t bpref;
980 {
981 struct cg *cgp;
982 ufs_daddr_t bno, blkno;
983 int cylno, pos, delta;
984 short *cylbp;
985 register int i;
986 struct fs *fs = ip->i_fs;
987 #ifdef FFS_EI
988 const int needswap = UFS_FSNEEDSWAP(fs);
989 #endif
990
991 cgp = (struct cg *)bp->b_data;
992 if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
993 bpref = ufs_rw32(cgp->cg_rotor, needswap);
994 goto norot;
995 }
996 bpref = blknum(fs, bpref);
997 bpref = dtogd(fs, bpref);
998 /*
999 * if the requested block is available, use it
1000 */
1001 if (ffs_isblock(fs, cg_blksfree(cgp, needswap),
1002 fragstoblks(fs, bpref))) {
1003 bno = bpref;
1004 goto gotit;
1005 }
1006 if (fs->fs_nrpos <= 1 || fs->fs_cpc == 0) {
1007 /*
1008 * Block layout information is not available.
1009 * Leaving bpref unchanged means we take the
1010 * next available free block following the one
1011 * we just allocated. Hopefully this will at
1012 * least hit a track cache on drives of unknown
1013 * geometry (e.g. SCSI).
1014 */
1015 goto norot;
1016 }
1017 /*
1018 * check for a block available on the same cylinder
1019 */
1020 cylno = cbtocylno(fs, bpref);
1021 if (cg_blktot(cgp, needswap)[cylno] == 0)
1022 goto norot;
1023 /*
1024 * check the summary information to see if a block is
1025 * available in the requested cylinder starting at the
1026 * requested rotational position and proceeding around.
1027 */
1028 cylbp = cg_blks(fs, cgp, cylno, needswap);
1029 pos = cbtorpos(fs, bpref);
1030 for (i = pos; i < fs->fs_nrpos; i++)
1031 if (ufs_rw16(cylbp[i], needswap) > 0)
1032 break;
1033 if (i == fs->fs_nrpos)
1034 for (i = 0; i < pos; i++)
1035 if (ufs_rw16(cylbp[i], needswap) > 0)
1036 break;
1037 if (ufs_rw16(cylbp[i], needswap) > 0) {
1038 /*
1039 * found a rotational position, now find the actual
1040 * block. A panic if none is actually there.
1041 */
1042 pos = cylno % fs->fs_cpc;
1043 bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
1044 if (fs_postbl(fs, pos)[i] == -1) {
1045 printf("pos = %d, i = %d, fs = %s\n",
1046 pos, i, fs->fs_fsmnt);
1047 panic("ffs_alloccgblk: cyl groups corrupted");
1048 }
1049 for (i = fs_postbl(fs, pos)[i];; ) {
1050 if (ffs_isblock(fs, cg_blksfree(cgp, needswap), bno + i)) {
1051 bno = blkstofrags(fs, (bno + i));
1052 goto gotit;
1053 }
1054 delta = fs_rotbl(fs)[i];
1055 if (delta <= 0 ||
1056 delta + i > fragstoblks(fs, fs->fs_fpg))
1057 break;
1058 i += delta;
1059 }
1060 printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
1061 panic("ffs_alloccgblk: can't find blk in cyl");
1062 }
1063 norot:
1064 /*
1065 * no blocks in the requested cylinder, so take next
1066 * available one in this cylinder group.
1067 */
1068 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1069 if (bno < 0)
1070 return (NULL);
1071 cgp->cg_rotor = ufs_rw32(bno, needswap);
1072 gotit:
1073 blkno = fragstoblks(fs, bno);
1074 ffs_clrblock(fs, cg_blksfree(cgp, needswap), (long)blkno);
1075 ffs_clusteracct(fs, cgp, blkno, -1);
1076 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
1077 fs->fs_cstotal.cs_nbfree--;
1078 fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
1079 cylno = cbtocylno(fs, bno);
1080 ufs_add16(cg_blks(fs, cgp, cylno, needswap)[cbtorpos(fs, bno)], -1,
1081 needswap);
1082 ufs_add32(cg_blktot(cgp, needswap)[cylno], -1, needswap);
1083 fs->fs_fmod = 1;
1084 blkno = ufs_rw32(cgp->cg_cgx, needswap) * fs->fs_fpg + bno;
1085 if (DOINGSOFTDEP(ITOV(ip)))
1086 softdep_setup_blkmapdep(bp, fs, blkno);
1087 return (blkno);
1088 }
1089
1090 /*
1091 * Determine whether a cluster can be allocated.
1092 *
1093 * We do not currently check for optimal rotational layout if there
1094 * are multiple choices in the same cylinder group. Instead we just
1095 * take the first one that we find following bpref.
1096 */
1097 static ufs_daddr_t
1098 ffs_clusteralloc(ip, cg, bpref, len)
1099 struct inode *ip;
1100 int cg;
1101 ufs_daddr_t bpref;
1102 int len;
1103 {
1104 register struct fs *fs;
1105 register struct cg *cgp;
1106 struct buf *bp;
1107 int i, got, run, bno, bit, map;
1108 u_char *mapp;
1109 int32_t *lp;
1110
1111 fs = ip->i_fs;
1112 if (fs->fs_maxcluster[cg] < len)
1113 return (NULL);
1114 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1115 NOCRED, &bp))
1116 goto fail;
1117 cgp = (struct cg *)bp->b_data;
1118 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs)))
1119 goto fail;
1120 /*
1121 * Check to see if a cluster of the needed size (or bigger) is
1122 * available in this cylinder group.
1123 */
1124 lp = &cg_clustersum(cgp, UFS_FSNEEDSWAP(fs))[len];
1125 for (i = len; i <= fs->fs_contigsumsize; i++)
1126 if (ufs_rw32(*lp++, UFS_FSNEEDSWAP(fs)) > 0)
1127 break;
1128 if (i > fs->fs_contigsumsize) {
1129 /*
1130 * This is the first time looking for a cluster in this
1131 * cylinder group. Update the cluster summary information
1132 * to reflect the true maximum sized cluster so that
1133 * future cluster allocation requests can avoid reading
1134 * the cylinder group map only to find no clusters.
1135 */
1136 lp = &cg_clustersum(cgp, UFS_FSNEEDSWAP(fs))[len - 1];
1137 for (i = len - 1; i > 0; i--)
1138 if (ufs_rw32(*lp--, UFS_FSNEEDSWAP(fs)) > 0)
1139 break;
1140 fs->fs_maxcluster[cg] = i;
1141 goto fail;
1142 }
1143 /*
1144 * Search the cluster map to find a big enough cluster.
1145 * We take the first one that we find, even if it is larger
1146 * than we need as we prefer to get one close to the previous
1147 * block allocation. We do not search before the current
1148 * preference point as we do not want to allocate a block
1149 * that is allocated before the previous one (as we will
1150 * then have to wait for another pass of the elevator
1151 * algorithm before it will be read). We prefer to fail and
1152 * be recalled to try an allocation in the next cylinder group.
1153 */
1154 if (dtog(fs, bpref) != cg)
1155 bpref = 0;
1156 else
1157 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1158 mapp = &cg_clustersfree(cgp, UFS_FSNEEDSWAP(fs))[bpref / NBBY];
1159 map = *mapp++;
1160 bit = 1 << (bpref % NBBY);
1161 for (run = 0, got = bpref;
1162 got < ufs_rw32(cgp->cg_nclusterblks, UFS_FSNEEDSWAP(fs)); got++) {
1163 if ((map & bit) == 0) {
1164 run = 0;
1165 } else {
1166 run++;
1167 if (run == len)
1168 break;
1169 }
1170 if ((got & (NBBY - 1)) != (NBBY - 1)) {
1171 bit <<= 1;
1172 } else {
1173 map = *mapp++;
1174 bit = 1;
1175 }
1176 }
1177 if (got == ufs_rw32(cgp->cg_nclusterblks, UFS_FSNEEDSWAP(fs)))
1178 goto fail;
1179 /*
1180 * Allocate the cluster that we have found.
1181 */
1182 #ifdef DIAGNOSTIC
1183 for (i = 1; i <= len; i++)
1184 if (!ffs_isblock(fs, cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)),
1185 got - run + i))
1186 panic("ffs_clusteralloc: map mismatch");
1187 #endif
1188 bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1);
1189 if (dtog(fs, bno) != cg)
1190 panic("ffs_clusteralloc: allocated out of group");
1191 len = blkstofrags(fs, len);
1192 for (i = 0; i < len; i += fs->fs_frag)
1193 if ((got = ffs_alloccgblk(ip, bp, bno + i)) != bno + i)
1194 panic("ffs_clusteralloc: lost block");
1195 bdwrite(bp);
1196 return (bno);
1197
1198 fail:
1199 brelse(bp);
1200 return (0);
1201 }
1202
1203 /*
1204 * Determine whether an inode can be allocated.
1205 *
1206 * Check to see if an inode is available, and if it is,
1207 * allocate it using the following policy:
1208 * 1) allocate the requested inode.
1209 * 2) allocate the next available inode after the requested
1210 * inode in the specified cylinder group.
1211 */
1212 static ufs_daddr_t
1213 ffs_nodealloccg(ip, cg, ipref, mode)
1214 struct inode *ip;
1215 int cg;
1216 ufs_daddr_t ipref;
1217 int mode;
1218 {
1219 register struct cg *cgp;
1220 struct buf *bp;
1221 int error, start, len, loc, map, i;
1222 register struct fs *fs = ip->i_fs;
1223 #ifdef FFS_EI
1224 const int needswap = UFS_FSNEEDSWAP(fs);
1225 #endif
1226
1227 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1228 return (NULL);
1229 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1230 (int)fs->fs_cgsize, NOCRED, &bp);
1231 if (error) {
1232 brelse(bp);
1233 return (NULL);
1234 }
1235 cgp = (struct cg *)bp->b_data;
1236 if (!cg_chkmagic(cgp, needswap) || cgp->cg_cs.cs_nifree == 0) {
1237 brelse(bp);
1238 return (NULL);
1239 }
1240 cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
1241 if (ipref) {
1242 ipref %= fs->fs_ipg;
1243 if (isclr(cg_inosused(cgp, needswap), ipref))
1244 goto gotit;
1245 }
1246 start = ufs_rw32(cgp->cg_irotor, needswap) / NBBY;
1247 len = howmany(fs->fs_ipg - ufs_rw32(cgp->cg_irotor, needswap),
1248 NBBY);
1249 loc = skpc(0xff, len, &cg_inosused(cgp, needswap)[start]);
1250 if (loc == 0) {
1251 len = start + 1;
1252 start = 0;
1253 loc = skpc(0xff, len, &cg_inosused(cgp, needswap)[0]);
1254 if (loc == 0) {
1255 printf("cg = %d, irotor = %d, fs = %s\n",
1256 cg, ufs_rw32(cgp->cg_irotor, needswap),
1257 fs->fs_fsmnt);
1258 panic("ffs_nodealloccg: map corrupted");
1259 /* NOTREACHED */
1260 }
1261 }
1262 i = start + len - loc;
1263 map = cg_inosused(cgp, needswap)[i];
1264 ipref = i * NBBY;
1265 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1266 if ((map & i) == 0) {
1267 cgp->cg_irotor = ufs_rw32(ipref, needswap);
1268 goto gotit;
1269 }
1270 }
1271 printf("fs = %s\n", fs->fs_fsmnt);
1272 panic("ffs_nodealloccg: block not in map");
1273 /* NOTREACHED */
1274 gotit:
1275 if (DOINGSOFTDEP(ITOV(ip)))
1276 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1277 setbit(cg_inosused(cgp, needswap), ipref);
1278 ufs_add32(cgp->cg_cs.cs_nifree, -1, needswap);
1279 fs->fs_cstotal.cs_nifree--;
1280 fs->fs_cs(fs, cg).cs_nifree--;
1281 fs->fs_fmod = 1;
1282 if ((mode & IFMT) == IFDIR) {
1283 ufs_add32(cgp->cg_cs.cs_ndir, 1, needswap);
1284 fs->fs_cstotal.cs_ndir++;
1285 fs->fs_cs(fs, cg).cs_ndir++;
1286 }
1287 bdwrite(bp);
1288 return (cg * fs->fs_ipg + ipref);
1289 }
1290
1291 /*
1292 * Free a block or fragment.
1293 *
1294 * The specified block or fragment is placed back in the
1295 * free map. If a fragment is deallocated, a possible
1296 * block reassembly is checked.
1297 */
1298 void
1299 ffs_blkfree(ip, bno, size)
1300 register struct inode *ip;
1301 ufs_daddr_t bno;
1302 long size;
1303 {
1304 register struct cg *cgp;
1305 struct buf *bp;
1306 ufs_daddr_t blkno;
1307 int i, error, cg, blk, frags, bbase;
1308 register struct fs *fs = ip->i_fs;
1309 const int needswap = UFS_FSNEEDSWAP(fs);
1310
1311 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1312 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1313 printf("dev = 0x%x, bno = %u bsize = %d, size = %ld, fs = %s\n",
1314 ip->i_dev, bno, fs->fs_bsize, size, fs->fs_fsmnt);
1315 panic("blkfree: bad size");
1316 }
1317 cg = dtog(fs, bno);
1318 if ((u_int)bno >= fs->fs_size) {
1319 printf("bad block %d, ino %d\n", bno, ip->i_number);
1320 ffs_fserr(fs, ip->i_ffs_uid, "bad block");
1321 return;
1322 }
1323 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1324 (int)fs->fs_cgsize, NOCRED, &bp);
1325 if (error) {
1326 brelse(bp);
1327 return;
1328 }
1329 cgp = (struct cg *)bp->b_data;
1330 if (!cg_chkmagic(cgp, needswap)) {
1331 brelse(bp);
1332 return;
1333 }
1334 cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
1335 bno = dtogd(fs, bno);
1336 if (size == fs->fs_bsize) {
1337 blkno = fragstoblks(fs, bno);
1338 if (!ffs_isfreeblock(fs, cg_blksfree(cgp, needswap), blkno)) {
1339 printf("dev = 0x%x, block = %d, fs = %s\n",
1340 ip->i_dev, bno, fs->fs_fsmnt);
1341 panic("blkfree: freeing free block");
1342 }
1343 ffs_setblock(fs, cg_blksfree(cgp, needswap), blkno);
1344 ffs_clusteracct(fs, cgp, blkno, 1);
1345 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1346 fs->fs_cstotal.cs_nbfree++;
1347 fs->fs_cs(fs, cg).cs_nbfree++;
1348 i = cbtocylno(fs, bno);
1349 ufs_add16(cg_blks(fs, cgp, i, needswap)[cbtorpos(fs, bno)], 1,
1350 needswap);
1351 ufs_add32(cg_blktot(cgp, needswap)[i], 1, needswap);
1352 } else {
1353 bbase = bno - fragnum(fs, bno);
1354 /*
1355 * decrement the counts associated with the old frags
1356 */
1357 blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
1358 ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
1359 /*
1360 * deallocate the fragment
1361 */
1362 frags = numfrags(fs, size);
1363 for (i = 0; i < frags; i++) {
1364 if (isset(cg_blksfree(cgp, needswap), bno + i)) {
1365 printf("dev = 0x%x, block = %d, fs = %s\n",
1366 ip->i_dev, bno + i, fs->fs_fsmnt);
1367 panic("blkfree: freeing free frag");
1368 }
1369 setbit(cg_blksfree(cgp, needswap), bno + i);
1370 }
1371 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1372 fs->fs_cstotal.cs_nffree += i;
1373 fs->fs_cs(fs, cg).cs_nffree += i;
1374 /*
1375 * add back in counts associated with the new frags
1376 */
1377 blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
1378 ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
1379 /*
1380 * if a complete block has been reassembled, account for it
1381 */
1382 blkno = fragstoblks(fs, bbase);
1383 if (ffs_isblock(fs, cg_blksfree(cgp, needswap), blkno)) {
1384 ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
1385 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1386 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1387 ffs_clusteracct(fs, cgp, blkno, 1);
1388 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1389 fs->fs_cstotal.cs_nbfree++;
1390 fs->fs_cs(fs, cg).cs_nbfree++;
1391 i = cbtocylno(fs, bbase);
1392 ufs_add16(cg_blks(fs, cgp, i, needswap)[cbtorpos(fs,
1393 bbase)], 1,
1394 needswap);
1395 ufs_add32(cg_blktot(cgp, needswap)[i], 1, needswap);
1396 }
1397 }
1398 fs->fs_fmod = 1;
1399 bdwrite(bp);
1400 }
1401
1402 #if defined(DIAGNOSTIC) || defined(DEBUG)
1403 /*
1404 * Verify allocation of a block or fragment. Returns true if block or
1405 * fragment is allocated, false if it is free.
1406 */
1407 static int
1408 ffs_checkblk(ip, bno, size)
1409 struct inode *ip;
1410 ufs_daddr_t bno;
1411 long size;
1412 {
1413 struct fs *fs;
1414 struct cg *cgp;
1415 struct buf *bp;
1416 int i, error, frags, free;
1417
1418 fs = ip->i_fs;
1419 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1420 printf("bsize = %d, size = %ld, fs = %s\n",
1421 fs->fs_bsize, size, fs->fs_fsmnt);
1422 panic("checkblk: bad size");
1423 }
1424 if ((u_int)bno >= fs->fs_size)
1425 panic("checkblk: bad block %d", bno);
1426 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
1427 (int)fs->fs_cgsize, NOCRED, &bp);
1428 if (error) {
1429 brelse(bp);
1430 return 0;
1431 }
1432 cgp = (struct cg *)bp->b_data;
1433 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
1434 brelse(bp);
1435 return 0;
1436 }
1437 bno = dtogd(fs, bno);
1438 if (size == fs->fs_bsize) {
1439 free = ffs_isblock(fs, cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)),
1440 fragstoblks(fs, bno));
1441 } else {
1442 frags = numfrags(fs, size);
1443 for (free = 0, i = 0; i < frags; i++)
1444 if (isset(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i))
1445 free++;
1446 if (free != 0 && free != frags)
1447 panic("checkblk: partially free fragment");
1448 }
1449 brelse(bp);
1450 return (!free);
1451 }
1452 #endif /* DIAGNOSTIC */
1453
1454 /*
1455 * Free an inode.
1456 */
1457 int
1458 ffs_vfree(v)
1459 void *v;
1460 {
1461 struct vop_vfree_args /* {
1462 struct vnode *a_pvp;
1463 ino_t a_ino;
1464 int a_mode;
1465 } */ *ap = v;
1466
1467 if (DOINGSOFTDEP(ap->a_pvp)) {
1468 softdep_freefile(ap);
1469 return (0);
1470 }
1471 return (ffs_freefile(ap));
1472 }
1473
1474 /*
1475 * Do the actual free operation.
1476 * The specified inode is placed back in the free map.
1477 */
1478 int
1479 ffs_freefile(v)
1480 void *v;
1481 {
1482 struct vop_vfree_args /* {
1483 struct vnode *a_pvp;
1484 ino_t a_ino;
1485 int a_mode;
1486 } */ *ap = v;
1487 register struct cg *cgp;
1488 register struct inode *pip = VTOI(ap->a_pvp);
1489 register struct fs *fs = pip->i_fs;
1490 ino_t ino = ap->a_ino;
1491 struct buf *bp;
1492 int error, cg;
1493 #ifdef FFS_EI
1494 const int needswap = UFS_FSNEEDSWAP(fs);
1495 #endif
1496
1497 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1498 panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n",
1499 pip->i_dev, ino, fs->fs_fsmnt);
1500 cg = ino_to_cg(fs, ino);
1501 error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1502 (int)fs->fs_cgsize, NOCRED, &bp);
1503 if (error) {
1504 brelse(bp);
1505 return (error);
1506 }
1507 cgp = (struct cg *)bp->b_data;
1508 if (!cg_chkmagic(cgp, needswap)) {
1509 brelse(bp);
1510 return (0);
1511 }
1512 cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
1513 ino %= fs->fs_ipg;
1514 if (isclr(cg_inosused(cgp, needswap), ino)) {
1515 printf("dev = 0x%x, ino = %d, fs = %s\n",
1516 pip->i_dev, ino, fs->fs_fsmnt);
1517 if (fs->fs_ronly == 0)
1518 panic("ifree: freeing free inode");
1519 }
1520 clrbit(cg_inosused(cgp, needswap), ino);
1521 if (ino < ufs_rw32(cgp->cg_irotor, needswap))
1522 cgp->cg_irotor = ufs_rw32(ino, needswap);
1523 ufs_add32(cgp->cg_cs.cs_nifree, 1, needswap);
1524 fs->fs_cstotal.cs_nifree++;
1525 fs->fs_cs(fs, cg).cs_nifree++;
1526 if ((ap->a_mode & IFMT) == IFDIR) {
1527 ufs_add32(cgp->cg_cs.cs_ndir, -1, needswap);
1528 fs->fs_cstotal.cs_ndir--;
1529 fs->fs_cs(fs, cg).cs_ndir--;
1530 }
1531 fs->fs_fmod = 1;
1532 bdwrite(bp);
1533 return (0);
1534 }
1535
1536 /*
1537 * Find a block of the specified size in the specified cylinder group.
1538 *
1539 * It is a panic if a request is made to find a block if none are
1540 * available.
1541 */
1542 static ufs_daddr_t
1543 ffs_mapsearch(fs, cgp, bpref, allocsiz)
1544 register struct fs *fs;
1545 register struct cg *cgp;
1546 ufs_daddr_t bpref;
1547 int allocsiz;
1548 {
1549 ufs_daddr_t bno;
1550 int start, len, loc, i;
1551 int blk, field, subfield, pos;
1552 int ostart, olen;
1553 #ifdef FFS_EI
1554 const int needswap = UFS_FSNEEDSWAP(fs);
1555 #endif
1556
1557 /*
1558 * find the fragment by searching through the free block
1559 * map for an appropriate bit pattern
1560 */
1561 if (bpref)
1562 start = dtogd(fs, bpref) / NBBY;
1563 else
1564 start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
1565 len = howmany(fs->fs_fpg, NBBY) - start;
1566 ostart = start;
1567 olen = len;
1568 loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp, needswap)[start],
1569 (u_char *)fragtbl[fs->fs_frag],
1570 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1571 if (loc == 0) {
1572 len = start + 1;
1573 start = 0;
1574 loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp, needswap)[0],
1575 (u_char *)fragtbl[fs->fs_frag],
1576 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1577 if (loc == 0) {
1578 printf("start = %d, len = %d, fs = %s\n",
1579 ostart, olen, fs->fs_fsmnt);
1580 printf("offset=%d %ld\n",
1581 ufs_rw32(cgp->cg_freeoff, needswap),
1582 (long)cg_blksfree(cgp, needswap) - (long)cgp);
1583 panic("ffs_alloccg: map corrupted");
1584 /* NOTREACHED */
1585 }
1586 }
1587 bno = (start + len - loc) * NBBY;
1588 cgp->cg_frotor = ufs_rw32(bno, needswap);
1589 /*
1590 * found the byte in the map
1591 * sift through the bits to find the selected frag
1592 */
1593 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1594 blk = blkmap(fs, cg_blksfree(cgp, needswap), bno);
1595 blk <<= 1;
1596 field = around[allocsiz];
1597 subfield = inside[allocsiz];
1598 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
1599 if ((blk & field) == subfield)
1600 return (bno + pos);
1601 field <<= 1;
1602 subfield <<= 1;
1603 }
1604 }
1605 printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
1606 panic("ffs_alloccg: block not in map");
1607 return (-1);
1608 }
1609
1610 /*
1611 * Update the cluster map because of an allocation or free.
1612 *
1613 * Cnt == 1 means free; cnt == -1 means allocating.
1614 */
1615 void
1616 ffs_clusteracct(fs, cgp, blkno, cnt)
1617 struct fs *fs;
1618 struct cg *cgp;
1619 ufs_daddr_t blkno;
1620 int cnt;
1621 {
1622 int32_t *sump;
1623 int32_t *lp;
1624 u_char *freemapp, *mapp;
1625 int i, start, end, forw, back, map, bit;
1626 #ifdef FFS_EI
1627 const int needswap = UFS_FSNEEDSWAP(fs);
1628 #endif
1629
1630 if (fs->fs_contigsumsize <= 0)
1631 return;
1632 freemapp = cg_clustersfree(cgp, needswap);
1633 sump = cg_clustersum(cgp, needswap);
1634 /*
1635 * Allocate or clear the actual block.
1636 */
1637 if (cnt > 0)
1638 setbit(freemapp, blkno);
1639 else
1640 clrbit(freemapp, blkno);
1641 /*
1642 * Find the size of the cluster going forward.
1643 */
1644 start = blkno + 1;
1645 end = start + fs->fs_contigsumsize;
1646 if (end >= ufs_rw32(cgp->cg_nclusterblks, needswap))
1647 end = ufs_rw32(cgp->cg_nclusterblks, needswap);
1648 mapp = &freemapp[start / NBBY];
1649 map = *mapp++;
1650 bit = 1 << (start % NBBY);
1651 for (i = start; i < end; i++) {
1652 if ((map & bit) == 0)
1653 break;
1654 if ((i & (NBBY - 1)) != (NBBY - 1)) {
1655 bit <<= 1;
1656 } else {
1657 map = *mapp++;
1658 bit = 1;
1659 }
1660 }
1661 forw = i - start;
1662 /*
1663 * Find the size of the cluster going backward.
1664 */
1665 start = blkno - 1;
1666 end = start - fs->fs_contigsumsize;
1667 if (end < 0)
1668 end = -1;
1669 mapp = &freemapp[start / NBBY];
1670 map = *mapp--;
1671 bit = 1 << (start % NBBY);
1672 for (i = start; i > end; i--) {
1673 if ((map & bit) == 0)
1674 break;
1675 if ((i & (NBBY - 1)) != 0) {
1676 bit >>= 1;
1677 } else {
1678 map = *mapp--;
1679 bit = 1 << (NBBY - 1);
1680 }
1681 }
1682 back = start - i;
1683 /*
1684 * Account for old cluster and the possibly new forward and
1685 * back clusters.
1686 */
1687 i = back + forw + 1;
1688 if (i > fs->fs_contigsumsize)
1689 i = fs->fs_contigsumsize;
1690 ufs_add32(sump[i], cnt, needswap);
1691 if (back > 0)
1692 ufs_add32(sump[back], -cnt, needswap);
1693 if (forw > 0)
1694 ufs_add32(sump[forw], -cnt, needswap);
1695
1696 /*
1697 * Update cluster summary information.
1698 */
1699 lp = &sump[fs->fs_contigsumsize];
1700 for (i = fs->fs_contigsumsize; i > 0; i--)
1701 if (ufs_rw32(*lp--, needswap) > 0)
1702 break;
1703 fs->fs_maxcluster[ufs_rw32(cgp->cg_cgx, needswap)] = i;
1704 }
1705
1706 /*
1707 * Fserr prints the name of a file system with an error diagnostic.
1708 *
1709 * The form of the error message is:
1710 * fs: error message
1711 */
1712 static void
1713 ffs_fserr(fs, uid, cp)
1714 struct fs *fs;
1715 u_int uid;
1716 char *cp;
1717 {
1718
1719 log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
1720 }
1721