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