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