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