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