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