as ifdef the code, ifdef the variable
[unix-history] / usr / src / sys / ufs / lfs / lfs_alloc.c
CommitLineData
da7c5cc6
KM
1/*
2 * Copyright (c) 1982 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 *
5d934daa 6 * @(#)lfs_alloc.c 6.18 (Berkeley) %G%
da7c5cc6 7 */
e3fe2d69 8
94368568
JB
9#include "param.h"
10#include "systm.h"
11#include "mount.h"
12#include "fs.h"
94368568
JB
13#include "buf.h"
14#include "inode.h"
15#include "dir.h"
16#include "user.h"
17#include "quota.h"
18#include "kernel.h"
7580177b 19#include "syslog.h"
e3fe2d69 20
daaf7bee 21extern u_long hashalloc();
4f083fd7
SL
22extern ino_t ialloccg();
23extern daddr_t alloccg();
743f1ef7
KM
24extern daddr_t alloccgblk();
25extern daddr_t fragextend();
26extern daddr_t blkpref();
27extern daddr_t mapsearch();
1d7a08c5 28extern int inside[], around[];
b6407c9d 29extern unsigned char *fragtbl[];
e3fe2d69 30
502770a3
KM
31/*
32 * Allocate a block in the file system.
33 *
34 * The size of the requested block is given, which must be some
35 * multiple of fs_fsize and <= fs_bsize.
36 * A preference may be optionally specified. If a preference is given
37 * the following hierarchy is used to allocate a block:
38 * 1) allocate the requested block.
39 * 2) allocate a rotationally optimal block in the same cylinder.
40 * 3) allocate a block in the same cylinder group.
41 * 4) quadradically rehash into other cylinder groups, until an
42 * available block is located.
43 * If no block preference is given the following heirarchy is used
44 * to allocate a block:
45 * 1) allocate a block in the cylinder group that contains the
46 * inode for the file.
47 * 2) quadradically rehash into other cylinder groups, until an
48 * available block is located.
49 */
e3fe2d69 50struct buf *
f7287e4b 51alloc(ip, bpref, size)
f3c028b7 52 register struct inode *ip;
e3fe2d69
KM
53 daddr_t bpref;
54 int size;
55{
56 daddr_t bno;
57 register struct fs *fs;
f3c028b7 58 register struct buf *bp;
e3fe2d69
KM
59 int cg;
60
f7287e4b 61 fs = ip->i_fs;
ffd90e52
KM
62 if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
63 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
64 ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
b6407c9d 65 panic("alloc: bad size");
ffd90e52 66 }
b6407c9d 67 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
0947395d 68 goto nospace;
240a4664 69 if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
e3fe2d69 70 goto nospace;
b4567e9c 71#ifdef QUOTA
2523b389
SL
72 u.u_error = chkdq(ip, (long)btodb(size), 0);
73 if (u.u_error)
74 return (NULL);
ca90a6bf 75#endif
260e5e3c
KM
76 if (bpref >= fs->fs_size)
77 bpref = 0;
e3fe2d69 78 if (bpref == 0)
6994bf5d 79 cg = itog(fs, ip->i_number);
e3fe2d69 80 else
6994bf5d 81 cg = dtog(fs, bpref);
4f083fd7
SL
82 bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size,
83 (u_long (*)())alloccg);
6459ebe0 84 if (bno <= 0)
e3fe2d69 85 goto nospace;
2523b389
SL
86 ip->i_blocks += btodb(size);
87 ip->i_flag |= IUPD|ICHG;
f7287e4b 88 bp = getblk(ip->i_dev, fsbtodb(fs, bno), size);
e3fe2d69
KM
89 clrbuf(bp);
90 return (bp);
91nospace:
92 fserr(fs, "file system full");
93 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
94 u.u_error = ENOSPC;
95 return (NULL);
96}
97
502770a3
KM
98/*
99 * Reallocate a fragment to a bigger size
100 *
101 * The number and size of the old block is given, and a preference
102 * and new size is also specified. The allocator attempts to extend
103 * the original block. Failing that, the regular block allocator is
104 * invoked to get an appropriate block.
105 */
07670f7d 106struct buf *
f7287e4b
KM
107realloccg(ip, bprev, bpref, osize, nsize)
108 register struct inode *ip;
743f1ef7 109 daddr_t bprev, bpref;
07670f7d
KM
110 int osize, nsize;
111{
07670f7d 112 register struct fs *fs;
f3c028b7 113 register struct buf *bp, *obp;
f7fa0c54 114 int cg, request;
5d934daa
KM
115 daddr_t bno, bn;
116 int i, count, s;
117 extern struct cmap *mfind();
07670f7d 118
f7287e4b 119 fs = ip->i_fs;
d995d89d 120 if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
ffd90e52
KM
121 (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
122 printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
123 ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
b6407c9d 124 panic("realloccg: bad size");
ffd90e52 125 }
240a4664 126 if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
0947395d 127 goto nospace;
ffd90e52
KM
128 if (bprev == 0) {
129 printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
130 ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
502770a3 131 panic("realloccg: bad bprev");
ffd90e52 132 }
b4567e9c 133#ifdef QUOTA
2523b389
SL
134 u.u_error = chkdq(ip, (long)btodb(nsize - osize), 0);
135 if (u.u_error)
136 return (NULL);
ca90a6bf 137#endif
ae851115 138 cg = dtog(fs, bprev);
f7287e4b 139 bno = fragextend(ip, cg, (long)bprev, osize, nsize);
f3c028b7 140 if (bno != 0) {
9d6d37ce
BJ
141 do {
142 bp = bread(ip->i_dev, fsbtodb(fs, bno), osize);
143 if (bp->b_flags & B_ERROR) {
144 brelse(bp);
145 return (NULL);
146 }
147 } while (brealloc(bp, nsize) == 0);
148 bp->b_flags |= B_DONE;
4f083fd7 149 bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
2523b389
SL
150 ip->i_blocks += btodb(nsize - osize);
151 ip->i_flag |= IUPD|ICHG;
f3c028b7
KM
152 return (bp);
153 }
260e5e3c
KM
154 if (bpref >= fs->fs_size)
155 bpref = 0;
f8484b5f
KM
156 switch (fs->fs_optim) {
157 case FS_OPTSPACE:
158 /*
159 * Allocate an exact sized fragment. Although this makes
160 * best use of space, we will waste time relocating it if
161 * the file continues to grow. If the fragmentation is
162 * less than half of the minimum free reserve, we choose
163 * to begin optimizing for time.
164 */
f7fa0c54 165 request = nsize;
f8484b5f
KM
166 if (fs->fs_minfree < 5 ||
167 fs->fs_cstotal.cs_nffree >
168 fs->fs_dsize * fs->fs_minfree / (2 * 100))
169 break;
170 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
171 fs->fs_fsmnt);
172 fs->fs_optim = FS_OPTTIME;
173 break;
174 case FS_OPTTIME:
175 /*
176 * At this point we have discovered a file that is trying
177 * to grow a small fragment to a larger fragment. To save
178 * time, we allocate a full sized block, then free the
179 * unused portion. If the file continues to grow, the
180 * `fragextend' call above will be able to grow it in place
181 * without further copying. If aberrant programs cause
182 * disk fragmentation to grow within 2% of the free reserve,
183 * we choose to begin optimizing for space.
184 */
f7fa0c54 185 request = fs->fs_bsize;
f8484b5f
KM
186 if (fs->fs_cstotal.cs_nffree <
187 fs->fs_dsize * (fs->fs_minfree - 2) / 100)
188 break;
189 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
190 fs->fs_fsmnt);
191 fs->fs_optim = FS_OPTSPACE;
192 break;
193 default:
194 printf("dev = 0x%x, optim = %d, fs = %s\n",
195 ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
196 panic("realloccg: bad optim");
197 /* NOTREACHED */
198 }
f7fa0c54 199 bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request,
4f083fd7 200 (u_long (*)())alloccg);
6459ebe0 201 if (bno > 0) {
f7287e4b 202 obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize);
d995d89d
KM
203 if (obp->b_flags & B_ERROR) {
204 brelse(obp);
ae851115 205 return (NULL);
d995d89d 206 }
5d934daa
KM
207 bn = fsbtodb(fs, bno);
208 bp = getblk(ip->i_dev, bn, nsize);
260ede8e 209 bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize);
5d934daa
KM
210 count = howmany(osize, DEV_BSIZE);
211 s = splimp();
212 for (i = 0; i < count; i += CLBYTES / DEV_BSIZE)
213 if (mfind(ip->i_dev, bn + i))
214 munhash(ip->i_dev, bn + i);
215 splx(s);
260ede8e
KM
216 bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
217 if (obp->b_flags & B_DELWRI) {
218 obp->b_flags &= ~B_DELWRI;
219 u.u_ru.ru_oublock--; /* delete charge */
220 }
f3c028b7 221 brelse(obp);
4f083fd7 222 free(ip, bprev, (off_t)osize);
f7fa0c54 223 if (nsize < request)
e8727f9b 224 free(ip, bno + numfrags(fs, nsize),
f7fa0c54 225 (off_t)(request - nsize));
2523b389
SL
226 ip->i_blocks += btodb(nsize - osize);
227 ip->i_flag |= IUPD|ICHG;
ae851115 228 return (bp);
f3c028b7 229 }
0947395d 230nospace:
f3c028b7
KM
231 /*
232 * no space available
233 */
07670f7d
KM
234 fserr(fs, "file system full");
235 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
236 u.u_error = ENOSPC;
237 return (NULL);
238}
239
502770a3
KM
240/*
241 * Allocate an inode in the file system.
242 *
243 * A preference may be optionally specified. If a preference is given
244 * the following hierarchy is used to allocate an inode:
245 * 1) allocate the requested inode.
246 * 2) allocate an inode in the same cylinder group.
247 * 3) quadradically rehash into other cylinder groups, until an
248 * available inode is located.
249 * If no inode preference is given the following heirarchy is used
250 * to allocate an inode:
251 * 1) allocate an inode in cylinder group 0.
252 * 2) quadradically rehash into other cylinder groups, until an
253 * available inode is located.
254 */
e3fe2d69 255struct inode *
f7287e4b
KM
256ialloc(pip, ipref, mode)
257 register struct inode *pip;
e3fe2d69
KM
258 ino_t ipref;
259 int mode;
260{
daaf7bee 261 ino_t ino;
e3fe2d69
KM
262 register struct fs *fs;
263 register struct inode *ip;
264 int cg;
265
f7287e4b 266 fs = pip->i_fs;
0947395d 267 if (fs->fs_cstotal.cs_nifree == 0)
e3fe2d69 268 goto noinodes;
b4567e9c 269#ifdef QUOTA
2523b389
SL
270 u.u_error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0);
271 if (u.u_error)
272 return (NULL);
ca90a6bf 273#endif
260e5e3c
KM
274 if (ipref >= fs->fs_ncg * fs->fs_ipg)
275 ipref = 0;
6994bf5d 276 cg = itog(fs, ipref);
f7287e4b 277 ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
e3fe2d69
KM
278 if (ino == 0)
279 goto noinodes;
f7287e4b 280 ip = iget(pip->i_dev, pip->i_fs, ino);
e3fe2d69 281 if (ip == NULL) {
bcd8515e 282 ifree(pip, ino, 0);
e3fe2d69
KM
283 return (NULL);
284 }
ffd90e52
KM
285 if (ip->i_mode) {
286 printf("mode = 0%o, inum = %d, fs = %s\n",
287 ip->i_mode, ip->i_number, fs->fs_fsmnt);
e3fe2d69 288 panic("ialloc: dup alloc");
ffd90e52 289 }
2523b389
SL
290 if (ip->i_blocks) { /* XXX */
291 printf("free inode %s/%d had %d blocks\n",
292 fs->fs_fsmnt, ino, ip->i_blocks);
293 ip->i_blocks = 0;
294 }
e3fe2d69
KM
295 return (ip);
296noinodes:
297 fserr(fs, "out of inodes");
ae851115 298 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
e3fe2d69
KM
299 u.u_error = ENOSPC;
300 return (NULL);
301}
302
743f1ef7 303/*
502770a3
KM
304 * Find a cylinder to place a directory.
305 *
306 * The policy implemented by this algorithm is to select from
307 * among those cylinder groups with above the average number of
308 * free inodes, the one with the smallest number of directories.
743f1ef7 309 */
4f083fd7 310ino_t
f7287e4b 311dirpref(fs)
e3fe2d69 312 register struct fs *fs;
f7287e4b 313{
743f1ef7 314 int cg, minndir, mincg, avgifree;
e3fe2d69 315
0947395d 316 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
743f1ef7 317 minndir = fs->fs_ipg;
e3fe2d69 318 mincg = 0;
743f1ef7 319 for (cg = 0; cg < fs->fs_ncg; cg++)
b6407c9d
KM
320 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
321 fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
e3fe2d69 322 mincg = cg;
b6407c9d 323 minndir = fs->fs_cs(fs, cg).cs_ndir;
e3fe2d69 324 }
4f083fd7 325 return ((ino_t)(fs->fs_ipg * mincg));
e3fe2d69
KM
326}
327
743f1ef7 328/*
4f083fd7
SL
329 * Select the desired position for the next block in a file. The file is
330 * logically divided into sections. The first section is composed of the
331 * direct blocks. Each additional section contains fs_maxbpg blocks.
332 *
333 * If no blocks have been allocated in the first section, the policy is to
334 * request a block in the same cylinder group as the inode that describes
335 * the file. If no blocks have been allocated in any other section, the
336 * policy is to place the section in a cylinder group with a greater than
337 * average number of free blocks. An appropriate cylinder group is found
16e7863f
KM
338 * by using a rotor that sweeps the cylinder groups. When a new group of
339 * blocks is needed, the sweep begins in the cylinder group following the
340 * cylinder group from which the previous allocation was made. The sweep
341 * continues until a cylinder group with greater than the average number
342 * of free blocks is found. If the allocation is for the first block in an
343 * indirect block, the information on the previous allocation is unavailable;
344 * here a best guess is made based upon the logical block number being
345 * allocated.
4f083fd7
SL
346 *
347 * If a section is already partially allocated, the policy is to
348 * contiguously allocate fs_maxcontig blocks. The end of one of these
349 * contiguous blocks and the beginning of the next is physically separated
350 * so that the disk head will be in transit between them for at least
351 * fs_rotdelay milliseconds. This is to allow time for the processor to
352 * schedule another I/O transfer.
743f1ef7 353 */
daaf7bee 354daddr_t
4f083fd7
SL
355blkpref(ip, lbn, indx, bap)
356 struct inode *ip;
357 daddr_t lbn;
358 int indx;
359 daddr_t *bap;
f7287e4b 360{
4f083fd7 361 register struct fs *fs;
16e7863f
KM
362 register int cg;
363 int avgbfree, startcg;
4f083fd7 364 daddr_t nextblk;
743f1ef7 365
4f083fd7
SL
366 fs = ip->i_fs;
367 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
368 if (lbn < NDADDR) {
369 cg = itog(fs, ip->i_number);
b6407c9d 370 return (fs->fs_fpg * cg + fs->fs_frag);
743f1ef7 371 }
4f083fd7
SL
372 /*
373 * Find a cylinder with greater than average number of
374 * unused data blocks.
375 */
16e7863f
KM
376 if (indx == 0 || bap[indx - 1] == 0)
377 startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
378 else
379 startcg = dtog(fs, bap[indx - 1]) + 1;
380 startcg %= fs->fs_ncg;
4f083fd7 381 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
16e7863f 382 for (cg = startcg; cg < fs->fs_ncg; cg++)
4f083fd7
SL
383 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
384 fs->fs_cgrotor = cg;
385 return (fs->fs_fpg * cg + fs->fs_frag);
386 }
16e7863f 387 for (cg = 0; cg <= startcg; cg++)
4f083fd7
SL
388 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
389 fs->fs_cgrotor = cg;
390 return (fs->fs_fpg * cg + fs->fs_frag);
391 }
392 return (NULL);
393 }
394 /*
395 * One or more previous blocks have been laid out. If less
396 * than fs_maxcontig previous blocks are contiguous, the
397 * next block is requested contiguously, otherwise it is
398 * requested rotationally delayed by fs_rotdelay milliseconds.
399 */
400 nextblk = bap[indx - 1] + fs->fs_frag;
401 if (indx > fs->fs_maxcontig &&
240a4664 402 bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
4f083fd7
SL
403 != nextblk)
404 return (nextblk);
405 if (fs->fs_rotdelay != 0)
406 /*
407 * Here we convert ms of delay to frags as:
408 * (frags) = (ms) * (rev/sec) * (sect/rev) /
409 * ((sect/frag) * (ms/sec))
410 * then round up to the next block.
411 */
412 nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
413 (NSPF(fs) * 1000), fs->fs_frag);
414 return (nextblk);
743f1ef7
KM
415}
416
502770a3
KM
417/*
418 * Implement the cylinder overflow algorithm.
419 *
420 * The policy implemented by this algorithm is:
421 * 1) allocate the block in its requested cylinder group.
422 * 2) quadradically rehash on the cylinder group number.
423 * 3) brute force search for a free block.
424 */
daaf7bee
KM
425/*VARARGS5*/
426u_long
f7287e4b
KM
427hashalloc(ip, cg, pref, size, allocator)
428 struct inode *ip;
e3fe2d69
KM
429 int cg;
430 long pref;
431 int size; /* size for data blocks, mode for inodes */
daaf7bee 432 u_long (*allocator)();
e3fe2d69 433{
f7287e4b 434 register struct fs *fs;
e3fe2d69
KM
435 long result;
436 int i, icg = cg;
437
f7287e4b 438 fs = ip->i_fs;
e3fe2d69
KM
439 /*
440 * 1: preferred cylinder group
441 */
f7287e4b 442 result = (*allocator)(ip, cg, pref, size);
e3fe2d69
KM
443 if (result)
444 return (result);
445 /*
446 * 2: quadratic rehash
447 */
448 for (i = 1; i < fs->fs_ncg; i *= 2) {
449 cg += i;
450 if (cg >= fs->fs_ncg)
451 cg -= fs->fs_ncg;
f7287e4b 452 result = (*allocator)(ip, cg, 0, size);
e3fe2d69
KM
453 if (result)
454 return (result);
455 }
456 /*
457 * 3: brute force search
620b3290
SL
458 * Note that we start at i == 2, since 0 was checked initially,
459 * and 1 is always checked in the quadratic rehash.
e3fe2d69 460 */
2136305e 461 cg = (icg + 2) % fs->fs_ncg;
620b3290 462 for (i = 2; i < fs->fs_ncg; i++) {
f7287e4b 463 result = (*allocator)(ip, cg, 0, size);
e3fe2d69
KM
464 if (result)
465 return (result);
466 cg++;
467 if (cg == fs->fs_ncg)
468 cg = 0;
469 }
ae851115 470 return (NULL);
e3fe2d69
KM
471}
472
502770a3
KM
473/*
474 * Determine whether a fragment can be extended.
475 *
476 * Check to see if the necessary fragments are available, and
477 * if they are, allocate them.
478 */
07670f7d 479daddr_t
f7287e4b
KM
480fragextend(ip, cg, bprev, osize, nsize)
481 struct inode *ip;
07670f7d 482 int cg;
f3c028b7 483 long bprev;
07670f7d
KM
484 int osize, nsize;
485{
f7287e4b 486 register struct fs *fs;
f3c028b7
KM
487 register struct buf *bp;
488 register struct cg *cgp;
489 long bno;
490 int frags, bbase;
07670f7d
KM
491 int i;
492
f7287e4b 493 fs = ip->i_fs;
a8580723 494 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
e5476900 495 return (NULL);
d995d89d 496 frags = numfrags(fs, nsize);
a8580723
KM
497 bbase = fragnum(fs, bprev);
498 if (bbase > fragnum(fs, (bprev + frags - 1))) {
f3c028b7 499 /* cannot extend across a block boundry */
ae851115 500 return (NULL);
f3c028b7 501 }
d65bd829 502 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
e5476900
KM
503 cgp = bp->b_un.b_cg;
504 if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
d995d89d 505 brelse(bp);
ae851115 506 return (NULL);
d995d89d 507 }
ad9250ee 508 cgp->cg_time = time.tv_sec;
6994bf5d 509 bno = dtogd(fs, bprev);
d995d89d 510 for (i = numfrags(fs, osize); i < frags; i++)
aca50d72
KM
511 if (isclr(cgp->cg_free, bno + i)) {
512 brelse(bp);
ae851115 513 return (NULL);
aca50d72
KM
514 }
515 /*
516 * the current fragment can be extended
517 * deduct the count on fragment being extended into
518 * increase the count on the remaining fragment (if any)
519 * allocate the extended piece
520 */
521 for (i = frags; i < fs->fs_frag - bbase; i++)
f3c028b7
KM
522 if (isclr(cgp->cg_free, bno + i))
523 break;
d995d89d 524 cgp->cg_frsum[i - numfrags(fs, osize)]--;
aca50d72
KM
525 if (i != frags)
526 cgp->cg_frsum[i - frags]++;
d995d89d 527 for (i = numfrags(fs, osize); i < frags; i++) {
aca50d72
KM
528 clrbit(cgp->cg_free, bno + i);
529 cgp->cg_cs.cs_nffree--;
530 fs->fs_cstotal.cs_nffree--;
531 fs->fs_cs(fs, cg).cs_nffree--;
f3c028b7 532 }
aca50d72
KM
533 fs->fs_fmod++;
534 bdwrite(bp);
535 return (bprev);
07670f7d
KM
536}
537
502770a3
KM
538/*
539 * Determine whether a block can be allocated.
540 *
541 * Check to see if a block of the apprpriate size is available,
542 * and if it is, allocate it.
543 */
4f083fd7 544daddr_t
f7287e4b
KM
545alloccg(ip, cg, bpref, size)
546 struct inode *ip;
e3fe2d69
KM
547 int cg;
548 daddr_t bpref;
549 int size;
550{
f7287e4b 551 register struct fs *fs;
f3c028b7
KM
552 register struct buf *bp;
553 register struct cg *cgp;
554 int bno, frags;
555 int allocsiz;
f3c028b7 556 register int i;
e3fe2d69 557
f7287e4b 558 fs = ip->i_fs;
b6407c9d 559 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
ae851115 560 return (NULL);
d65bd829 561 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
e5476900 562 cgp = bp->b_un.b_cg;
0f538882
KM
563 if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC ||
564 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
d995d89d 565 brelse(bp);
ae851115 566 return (NULL);
d995d89d 567 }
ad9250ee 568 cgp->cg_time = time.tv_sec;
b6407c9d 569 if (size == fs->fs_bsize) {
daaf7bee 570 bno = alloccgblk(fs, cgp, bpref);
f3c028b7
KM
571 bdwrite(bp);
572 return (bno);
573 }
574 /*
575 * check to see if any fragments are already available
576 * allocsiz is the size which will be allocated, hacking
577 * it down to a smaller size if necessary
578 */
d995d89d 579 frags = numfrags(fs, size);
b6407c9d 580 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
f3c028b7
KM
581 if (cgp->cg_frsum[allocsiz] != 0)
582 break;
b6407c9d 583 if (allocsiz == fs->fs_frag) {
f3c028b7
KM
584 /*
585 * no fragments were available, so a block will be
586 * allocated, and hacked up
587 */
0947395d 588 if (cgp->cg_cs.cs_nbfree == 0) {
f3c028b7 589 brelse(bp);
ae851115 590 return (NULL);
f3c028b7 591 }
daaf7bee 592 bno = alloccgblk(fs, cgp, bpref);
6994bf5d 593 bpref = dtogd(fs, bno);
b6407c9d 594 for (i = frags; i < fs->fs_frag; i++)
f3c028b7 595 setbit(cgp->cg_free, bpref + i);
b6407c9d 596 i = fs->fs_frag - frags;
0947395d
KM
597 cgp->cg_cs.cs_nffree += i;
598 fs->fs_cstotal.cs_nffree += i;
b6407c9d 599 fs->fs_cs(fs, cg).cs_nffree += i;
961945a8 600 fs->fs_fmod++;
f3c028b7
KM
601 cgp->cg_frsum[i]++;
602 bdwrite(bp);
603 return (bno);
604 }
743f1ef7 605 bno = mapsearch(fs, cgp, bpref, allocsiz);
0f538882
KM
606 if (bno < 0) {
607 brelse(bp);
ae851115 608 return (NULL);
0f538882 609 }
f3c028b7
KM
610 for (i = 0; i < frags; i++)
611 clrbit(cgp->cg_free, bno + i);
0947395d
KM
612 cgp->cg_cs.cs_nffree -= frags;
613 fs->fs_cstotal.cs_nffree -= frags;
b6407c9d 614 fs->fs_cs(fs, cg).cs_nffree -= frags;
961945a8 615 fs->fs_fmod++;
f3c028b7
KM
616 cgp->cg_frsum[allocsiz]--;
617 if (frags != allocsiz)
618 cgp->cg_frsum[allocsiz - frags]++;
619 bdwrite(bp);
620 return (cg * fs->fs_fpg + bno);
621}
622
502770a3
KM
623/*
624 * Allocate a block in a cylinder group.
625 *
626 * This algorithm implements the following policy:
627 * 1) allocate the requested block.
628 * 2) allocate a rotationally optimal block in the same cylinder.
629 * 3) allocate the next available block on the block rotor for the
630 * specified cylinder group.
631 * Note that this routine only allocates fs_bsize blocks; these
632 * blocks may be fragmented by the routine that allocates them.
633 */
f3c028b7 634daddr_t
daaf7bee 635alloccgblk(fs, cgp, bpref)
f7287e4b 636 register struct fs *fs;
f3c028b7
KM
637 register struct cg *cgp;
638 daddr_t bpref;
639{
743f1ef7 640 daddr_t bno;
ae851115 641 int cylno, pos, delta;
743f1ef7 642 short *cylbp;
aca50d72 643 register int i;
f3c028b7 644
743f1ef7
KM
645 if (bpref == 0) {
646 bpref = cgp->cg_rotor;
aca50d72
KM
647 goto norot;
648 }
a8580723 649 bpref = blknum(fs, bpref);
6994bf5d 650 bpref = dtogd(fs, bpref);
aca50d72
KM
651 /*
652 * if the requested block is available, use it
653 */
240a4664 654 if (isblock(fs, cgp->cg_free, fragstoblks(fs, bpref))) {
aca50d72
KM
655 bno = bpref;
656 goto gotit;
657 }
aca50d72
KM
658 /*
659 * check for a block available on the same cylinder
aca50d72
KM
660 */
661 cylno = cbtocylno(fs, bpref);
502770a3
KM
662 if (cgp->cg_btot[cylno] == 0)
663 goto norot;
664 if (fs->fs_cpc == 0) {
665 /*
666 * block layout info is not available, so just have
667 * to take any block in this cylinder.
668 */
669 bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
670 goto norot;
671 }
aca50d72
KM
672 /*
673 * check the summary information to see if a block is
674 * available in the requested cylinder starting at the
4f083fd7 675 * requested rotational position and proceeding around.
aca50d72 676 */
4f083fd7
SL
677 cylbp = cgp->cg_b[cylno];
678 pos = cbtorpos(fs, bpref);
aca50d72
KM
679 for (i = pos; i < NRPOS; i++)
680 if (cylbp[i] > 0)
681 break;
682 if (i == NRPOS)
683 for (i = 0; i < pos; i++)
743f1ef7
KM
684 if (cylbp[i] > 0)
685 break;
aca50d72
KM
686 if (cylbp[i] > 0) {
687 /*
688 * found a rotational position, now find the actual
689 * block. A panic if none is actually there.
690 */
691 pos = cylno % fs->fs_cpc;
692 bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
ffd90e52
KM
693 if (fs->fs_postbl[pos][i] == -1) {
694 printf("pos = %d, i = %d, fs = %s\n",
695 pos, i, fs->fs_fsmnt);
aca50d72 696 panic("alloccgblk: cyl groups corrupted");
ffd90e52 697 }
ae851115 698 for (i = fs->fs_postbl[pos][i];; ) {
aca50d72 699 if (isblock(fs, cgp->cg_free, bno + i)) {
240a4664 700 bno = blkstofrags(fs, (bno + i));
aca50d72 701 goto gotit;
743f1ef7 702 }
ae851115
KM
703 delta = fs->fs_rotbl[i];
704 if (delta <= 0 || delta > MAXBPC - i)
aca50d72 705 break;
ae851115 706 i += delta;
743f1ef7 707 }
ffd90e52 708 printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
aca50d72 709 panic("alloccgblk: can't find blk in cyl");
e3fe2d69 710 }
aca50d72
KM
711norot:
712 /*
713 * no blocks in the requested cylinder, so take next
714 * available one in this cylinder group.
715 */
b32450f4 716 bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
6459ebe0 717 if (bno < 0)
ae851115 718 return (NULL);
743f1ef7 719 cgp->cg_rotor = bno;
e3fe2d69 720gotit:
240a4664 721 clrblock(fs, cgp->cg_free, (long)fragstoblks(fs, bno));
0947395d
KM
722 cgp->cg_cs.cs_nbfree--;
723 fs->fs_cstotal.cs_nbfree--;
b6407c9d 724 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
502770a3
KM
725 cylno = cbtocylno(fs, bno);
726 cgp->cg_b[cylno][cbtorpos(fs, bno)]--;
727 cgp->cg_btot[cylno]--;
e3fe2d69 728 fs->fs_fmod++;
743f1ef7 729 return (cgp->cg_cgx * fs->fs_fpg + bno);
e3fe2d69
KM
730}
731
502770a3
KM
732/*
733 * Determine whether an inode can be allocated.
734 *
735 * Check to see if an inode is available, and if it is,
736 * allocate it using the following policy:
737 * 1) allocate the requested inode.
738 * 2) allocate the next available inode after the requested
739 * inode in the specified cylinder group.
740 */
4f083fd7 741ino_t
f7287e4b
KM
742ialloccg(ip, cg, ipref, mode)
743 struct inode *ip;
e3fe2d69
KM
744 int cg;
745 daddr_t ipref;
746 int mode;
747{
f7287e4b 748 register struct fs *fs;
f3c028b7 749 register struct cg *cgp;
4e0c7b8a
KM
750 struct buf *bp;
751 int start, len, loc, map, i;
e3fe2d69 752
f7287e4b 753 fs = ip->i_fs;
b6407c9d 754 if (fs->fs_cs(fs, cg).cs_nifree == 0)
ae851115 755 return (NULL);
d65bd829 756 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
e5476900 757 cgp = bp->b_un.b_cg;
0f538882
KM
758 if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC ||
759 cgp->cg_cs.cs_nifree == 0) {
d995d89d 760 brelse(bp);
ae851115 761 return (NULL);
d995d89d 762 }
ad9250ee 763 cgp->cg_time = time.tv_sec;
e3fe2d69
KM
764 if (ipref) {
765 ipref %= fs->fs_ipg;
766 if (isclr(cgp->cg_iused, ipref))
767 goto gotit;
4e0c7b8a
KM
768 }
769 start = cgp->cg_irotor / NBBY;
770 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
771 loc = skpc(0xff, len, &cgp->cg_iused[start]);
772 if (loc == 0) {
e5889092
KM
773 len = start + 1;
774 start = 0;
775 loc = skpc(0xff, len, &cgp->cg_iused[0]);
776 if (loc == 0) {
777 printf("cg = %s, irotor = %d, fs = %s\n",
778 cg, cgp->cg_irotor, fs->fs_fsmnt);
779 panic("ialloccg: map corrupted");
780 /* NOTREACHED */
781 }
4e0c7b8a
KM
782 }
783 i = start + len - loc;
784 map = cgp->cg_iused[i];
785 ipref = i * NBBY;
786 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
787 if ((map & i) == 0) {
e3fe2d69
KM
788 cgp->cg_irotor = ipref;
789 goto gotit;
790 }
791 }
4e0c7b8a
KM
792 printf("fs = %s\n", fs->fs_fsmnt);
793 panic("ialloccg: block not in map");
794 /* NOTREACHED */
e3fe2d69
KM
795gotit:
796 setbit(cgp->cg_iused, ipref);
0947395d
KM
797 cgp->cg_cs.cs_nifree--;
798 fs->fs_cstotal.cs_nifree--;
b6407c9d 799 fs->fs_cs(fs, cg).cs_nifree--;
e3fe2d69
KM
800 fs->fs_fmod++;
801 if ((mode & IFMT) == IFDIR) {
0947395d
KM
802 cgp->cg_cs.cs_ndir++;
803 fs->fs_cstotal.cs_ndir++;
b6407c9d 804 fs->fs_cs(fs, cg).cs_ndir++;
e3fe2d69
KM
805 }
806 bdwrite(bp);
807 return (cg * fs->fs_ipg + ipref);
808}
809
502770a3
KM
810/*
811 * Free a block or fragment.
812 *
813 * The specified block or fragment is placed back in the
814 * free map. If a fragment is deallocated, a possible
815 * block reassembly is checked.
816 */
4f083fd7 817free(ip, bno, size)
f7287e4b 818 register struct inode *ip;
e3fe2d69 819 daddr_t bno;
daaf7bee 820 off_t size;
e3fe2d69
KM
821{
822 register struct fs *fs;
823 register struct cg *cgp;
824 register struct buf *bp;
f3c028b7
KM
825 int cg, blk, frags, bbase;
826 register int i;
e3fe2d69 827
f7287e4b 828 fs = ip->i_fs;
ffd90e52
KM
829 if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
830 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
831 ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
b6407c9d 832 panic("free: bad size");
ffd90e52 833 }
6994bf5d 834 cg = dtog(fs, bno);
6459ebe0
KM
835 if (badblock(fs, bno)) {
836 printf("bad block %d, ino %d\n", bno, ip->i_number);
e3fe2d69 837 return;
6459ebe0 838 }
d65bd829 839 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
e5476900
KM
840 cgp = bp->b_un.b_cg;
841 if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
d995d89d 842 brelse(bp);
e3fe2d69 843 return;
d995d89d 844 }
ad9250ee 845 cgp->cg_time = time.tv_sec;
6994bf5d 846 bno = dtogd(fs, bno);
b6407c9d 847 if (size == fs->fs_bsize) {
240a4664 848 if (isblock(fs, cgp->cg_free, fragstoblks(fs, bno))) {
ffd90e52
KM
849 printf("dev = 0x%x, block = %d, fs = %s\n",
850 ip->i_dev, bno, fs->fs_fsmnt);
07670f7d 851 panic("free: freeing free block");
6459ebe0 852 }
240a4664 853 setblock(fs, cgp->cg_free, fragstoblks(fs, bno));
0947395d
KM
854 cgp->cg_cs.cs_nbfree++;
855 fs->fs_cstotal.cs_nbfree++;
b6407c9d 856 fs->fs_cs(fs, cg).cs_nbfree++;
502770a3
KM
857 i = cbtocylno(fs, bno);
858 cgp->cg_b[i][cbtorpos(fs, bno)]++;
859 cgp->cg_btot[i]++;
07670f7d 860 } else {
a8580723 861 bbase = bno - fragnum(fs, bno);
f3c028b7
KM
862 /*
863 * decrement the counts associated with the old frags
864 */
ae851115 865 blk = blkmap(fs, cgp->cg_free, bbase);
b6407c9d 866 fragacct(fs, blk, cgp->cg_frsum, -1);
f3c028b7
KM
867 /*
868 * deallocate the fragment
869 */
d995d89d 870 frags = numfrags(fs, size);
f3c028b7 871 for (i = 0; i < frags; i++) {
ffd90e52
KM
872 if (isset(cgp->cg_free, bno + i)) {
873 printf("dev = 0x%x, block = %d, fs = %s\n",
874 ip->i_dev, bno + i, fs->fs_fsmnt);
07670f7d 875 panic("free: freeing free frag");
ffd90e52 876 }
07670f7d 877 setbit(cgp->cg_free, bno + i);
07670f7d 878 }
ae851115
KM
879 cgp->cg_cs.cs_nffree += i;
880 fs->fs_cstotal.cs_nffree += i;
881 fs->fs_cs(fs, cg).cs_nffree += i;
f3c028b7
KM
882 /*
883 * add back in counts associated with the new frags
884 */
ae851115 885 blk = blkmap(fs, cgp->cg_free, bbase);
b6407c9d 886 fragacct(fs, blk, cgp->cg_frsum, 1);
f3c028b7
KM
887 /*
888 * if a complete block has been reassembled, account for it
889 */
240a4664 890 if (isblock(fs, cgp->cg_free, fragstoblks(fs, bbase))) {
b6407c9d
KM
891 cgp->cg_cs.cs_nffree -= fs->fs_frag;
892 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
893 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
0947395d
KM
894 cgp->cg_cs.cs_nbfree++;
895 fs->fs_cstotal.cs_nbfree++;
b6407c9d 896 fs->fs_cs(fs, cg).cs_nbfree++;
502770a3
KM
897 i = cbtocylno(fs, bbase);
898 cgp->cg_b[i][cbtorpos(fs, bbase)]++;
899 cgp->cg_btot[i]++;
07670f7d
KM
900 }
901 }
e3fe2d69 902 fs->fs_fmod++;
e3fe2d69
KM
903 bdwrite(bp);
904}
905
502770a3
KM
906/*
907 * Free an inode.
908 *
909 * The specified inode is placed back in the free map.
910 */
f7287e4b
KM
911ifree(ip, ino, mode)
912 struct inode *ip;
e3fe2d69
KM
913 ino_t ino;
914 int mode;
915{
916 register struct fs *fs;
917 register struct cg *cgp;
918 register struct buf *bp;
e3fe2d69
KM
919 int cg;
920
f7287e4b 921 fs = ip->i_fs;
ffd90e52
KM
922 if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) {
923 printf("dev = 0x%x, ino = %d, fs = %s\n",
924 ip->i_dev, ino, fs->fs_fsmnt);
e3fe2d69 925 panic("ifree: range");
ffd90e52 926 }
6994bf5d 927 cg = itog(fs, ino);
d65bd829 928 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
e5476900
KM
929 cgp = bp->b_un.b_cg;
930 if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
d995d89d 931 brelse(bp);
e3fe2d69 932 return;
d995d89d 933 }
ad9250ee 934 cgp->cg_time = time.tv_sec;
e3fe2d69 935 ino %= fs->fs_ipg;
ffd90e52
KM
936 if (isclr(cgp->cg_iused, ino)) {
937 printf("dev = 0x%x, ino = %d, fs = %s\n",
938 ip->i_dev, ino, fs->fs_fsmnt);
e3fe2d69 939 panic("ifree: freeing free inode");
ffd90e52 940 }
e3fe2d69 941 clrbit(cgp->cg_iused, ino);
4e0c7b8a
KM
942 if (ino < cgp->cg_irotor)
943 cgp->cg_irotor = ino;
0947395d
KM
944 cgp->cg_cs.cs_nifree++;
945 fs->fs_cstotal.cs_nifree++;
b6407c9d 946 fs->fs_cs(fs, cg).cs_nifree++;
e3fe2d69 947 if ((mode & IFMT) == IFDIR) {
0947395d
KM
948 cgp->cg_cs.cs_ndir--;
949 fs->fs_cstotal.cs_ndir--;
b6407c9d 950 fs->fs_cs(fs, cg).cs_ndir--;
e3fe2d69
KM
951 }
952 fs->fs_fmod++;
953 bdwrite(bp);
954}
955
743f1ef7 956/*
502770a3
KM
957 * Find a block of the specified size in the specified cylinder group.
958 *
743f1ef7
KM
959 * It is a panic if a request is made to find a block if none are
960 * available.
961 */
962daddr_t
963mapsearch(fs, cgp, bpref, allocsiz)
964 register struct fs *fs;
965 register struct cg *cgp;
966 daddr_t bpref;
967 int allocsiz;
968{
969 daddr_t bno;
970 int start, len, loc, i;
971 int blk, field, subfield, pos;
972
973 /*
974 * find the fragment by searching through the free block
975 * map for an appropriate bit pattern
976 */
977 if (bpref)
6994bf5d 978 start = dtogd(fs, bpref) / NBBY;
743f1ef7
KM
979 else
980 start = cgp->cg_frotor / NBBY;
942bd18b 981 len = howmany(fs->fs_fpg, NBBY) - start;
88a7a62a
SL
982 loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[start],
983 (caddr_t)fragtbl[fs->fs_frag],
984 (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
743f1ef7 985 if (loc == 0) {
e5476900
KM
986 len = start + 1;
987 start = 0;
e5889092 988 loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[0],
88a7a62a
SL
989 (caddr_t)fragtbl[fs->fs_frag],
990 (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
4e0c7b8a
KM
991 if (loc == 0) {
992 printf("start = %d, len = %d, fs = %s\n",
993 start, len, fs->fs_fsmnt);
994 panic("alloccg: map corrupted");
e5889092 995 /* NOTREACHED */
4e0c7b8a 996 }
743f1ef7
KM
997 }
998 bno = (start + len - loc) * NBBY;
999 cgp->cg_frotor = bno;
1000 /*
1001 * found the byte in the map
1002 * sift through the bits to find the selected frag
1003 */
ae851115
KM
1004 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1005 blk = blkmap(fs, cgp->cg_free, bno);
743f1ef7
KM
1006 blk <<= 1;
1007 field = around[allocsiz];
1008 subfield = inside[allocsiz];
b6407c9d 1009 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
ae851115
KM
1010 if ((blk & field) == subfield)
1011 return (bno + pos);
743f1ef7
KM
1012 field <<= 1;
1013 subfield <<= 1;
1014 }
1015 }
ffd90e52 1016 printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
743f1ef7 1017 panic("alloccg: block not in map");
e5476900 1018 return (-1);
743f1ef7
KM
1019}
1020
e3fe2d69 1021/*
502770a3
KM
1022 * Fserr prints the name of a file system with an error diagnostic.
1023 *
1024 * The form of the error message is:
e3fe2d69
KM
1025 * fs: error message
1026 */
1027fserr(fs, cp)
1028 struct fs *fs;
1029 char *cp;
1030{
1031
283ffc90 1032 log(LOG_ERR, "%s: %s\n", fs->fs_fsmnt, cp);
e3fe2d69 1033}