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