print user@hostname; filter header lines
[unix-history] / usr / src / sys / ufs / ffs / ffs_alloc.c
CommitLineData
2523b389 1/* ffs_alloc.c 2.25 83/05/21 */
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"
95d0807c 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) {
f7287e4b 217 ifree(ip, 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 }
95d0807c 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
KM
486 cgp = bp->b_un.b_cg;
487 if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
d995d89d 488 brelse(bp);
ae851115 489 return (NULL);
d995d89d 490 }
d65bd829
KM
491 if (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)
492 return (NULL);
95d0807c 493 cgp->cg_time = time.tv_sec;
b6407c9d 494 if (size == fs->fs_bsize) {
daaf7bee 495 bno = alloccgblk(fs, cgp, bpref);
f3c028b7
KM
496 bdwrite(bp);
497 return (bno);
498 }
499 /*
500 * check to see if any fragments are already available
501 * allocsiz is the size which will be allocated, hacking
502 * it down to a smaller size if necessary
503 */
d995d89d 504 frags = numfrags(fs, size);
b6407c9d 505 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
f3c028b7
KM
506 if (cgp->cg_frsum[allocsiz] != 0)
507 break;
b6407c9d 508 if (allocsiz == fs->fs_frag) {
f3c028b7
KM
509 /*
510 * no fragments were available, so a block will be
511 * allocated, and hacked up
512 */
0947395d 513 if (cgp->cg_cs.cs_nbfree == 0) {
f3c028b7 514 brelse(bp);
ae851115 515 return (NULL);
f3c028b7 516 }
daaf7bee 517 bno = alloccgblk(fs, cgp, bpref);
6994bf5d 518 bpref = dtogd(fs, bno);
b6407c9d 519 for (i = frags; i < fs->fs_frag; i++)
f3c028b7 520 setbit(cgp->cg_free, bpref + i);
b6407c9d 521 i = fs->fs_frag - frags;
0947395d
KM
522 cgp->cg_cs.cs_nffree += i;
523 fs->fs_cstotal.cs_nffree += i;
b6407c9d 524 fs->fs_cs(fs, cg).cs_nffree += i;
961945a8 525 fs->fs_fmod++;
f3c028b7
KM
526 cgp->cg_frsum[i]++;
527 bdwrite(bp);
528 return (bno);
529 }
743f1ef7 530 bno = mapsearch(fs, cgp, bpref, allocsiz);
6459ebe0 531 if (bno < 0)
ae851115 532 return (NULL);
f3c028b7
KM
533 for (i = 0; i < frags; i++)
534 clrbit(cgp->cg_free, bno + i);
0947395d
KM
535 cgp->cg_cs.cs_nffree -= frags;
536 fs->fs_cstotal.cs_nffree -= frags;
b6407c9d 537 fs->fs_cs(fs, cg).cs_nffree -= frags;
961945a8 538 fs->fs_fmod++;
f3c028b7
KM
539 cgp->cg_frsum[allocsiz]--;
540 if (frags != allocsiz)
541 cgp->cg_frsum[allocsiz - frags]++;
542 bdwrite(bp);
543 return (cg * fs->fs_fpg + bno);
544}
545
502770a3
KM
546/*
547 * Allocate a block in a cylinder group.
548 *
549 * This algorithm implements the following policy:
550 * 1) allocate the requested block.
551 * 2) allocate a rotationally optimal block in the same cylinder.
552 * 3) allocate the next available block on the block rotor for the
553 * specified cylinder group.
554 * Note that this routine only allocates fs_bsize blocks; these
555 * blocks may be fragmented by the routine that allocates them.
556 */
f3c028b7 557daddr_t
daaf7bee 558alloccgblk(fs, cgp, bpref)
f7287e4b 559 register struct fs *fs;
f3c028b7
KM
560 register struct cg *cgp;
561 daddr_t bpref;
562{
743f1ef7 563 daddr_t bno;
ae851115 564 int cylno, pos, delta;
743f1ef7 565 short *cylbp;
aca50d72 566 register int i;
f3c028b7 567
743f1ef7
KM
568 if (bpref == 0) {
569 bpref = cgp->cg_rotor;
aca50d72
KM
570 goto norot;
571 }
572 bpref &= ~(fs->fs_frag - 1);
6994bf5d 573 bpref = dtogd(fs, bpref);
aca50d72
KM
574 /*
575 * if the requested block is available, use it
576 */
240a4664 577 if (isblock(fs, cgp->cg_free, fragstoblks(fs, bpref))) {
aca50d72
KM
578 bno = bpref;
579 goto gotit;
580 }
aca50d72
KM
581 /*
582 * check for a block available on the same cylinder
aca50d72
KM
583 */
584 cylno = cbtocylno(fs, bpref);
502770a3
KM
585 if (cgp->cg_btot[cylno] == 0)
586 goto norot;
587 if (fs->fs_cpc == 0) {
588 /*
589 * block layout info is not available, so just have
590 * to take any block in this cylinder.
591 */
592 bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
593 goto norot;
594 }
aca50d72
KM
595 /*
596 * check the summary information to see if a block is
597 * available in the requested cylinder starting at the
4f083fd7 598 * requested rotational position and proceeding around.
aca50d72 599 */
4f083fd7
SL
600 cylbp = cgp->cg_b[cylno];
601 pos = cbtorpos(fs, bpref);
aca50d72
KM
602 for (i = pos; i < NRPOS; i++)
603 if (cylbp[i] > 0)
604 break;
605 if (i == NRPOS)
606 for (i = 0; i < pos; i++)
743f1ef7
KM
607 if (cylbp[i] > 0)
608 break;
aca50d72
KM
609 if (cylbp[i] > 0) {
610 /*
611 * found a rotational position, now find the actual
612 * block. A panic if none is actually there.
613 */
614 pos = cylno % fs->fs_cpc;
615 bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
ffd90e52
KM
616 if (fs->fs_postbl[pos][i] == -1) {
617 printf("pos = %d, i = %d, fs = %s\n",
618 pos, i, fs->fs_fsmnt);
aca50d72 619 panic("alloccgblk: cyl groups corrupted");
ffd90e52 620 }
ae851115 621 for (i = fs->fs_postbl[pos][i];; ) {
aca50d72 622 if (isblock(fs, cgp->cg_free, bno + i)) {
240a4664 623 bno = blkstofrags(fs, (bno + i));
aca50d72 624 goto gotit;
743f1ef7 625 }
ae851115
KM
626 delta = fs->fs_rotbl[i];
627 if (delta <= 0 || delta > MAXBPC - i)
aca50d72 628 break;
ae851115 629 i += delta;
743f1ef7 630 }
ffd90e52 631 printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
aca50d72 632 panic("alloccgblk: can't find blk in cyl");
e3fe2d69 633 }
aca50d72
KM
634norot:
635 /*
636 * no blocks in the requested cylinder, so take next
637 * available one in this cylinder group.
638 */
b32450f4 639 bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
6459ebe0 640 if (bno < 0)
ae851115 641 return (NULL);
743f1ef7 642 cgp->cg_rotor = bno;
e3fe2d69 643gotit:
240a4664 644 clrblock(fs, cgp->cg_free, (long)fragstoblks(fs, bno));
0947395d
KM
645 cgp->cg_cs.cs_nbfree--;
646 fs->fs_cstotal.cs_nbfree--;
b6407c9d 647 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
502770a3
KM
648 cylno = cbtocylno(fs, bno);
649 cgp->cg_b[cylno][cbtorpos(fs, bno)]--;
650 cgp->cg_btot[cylno]--;
e3fe2d69 651 fs->fs_fmod++;
743f1ef7 652 return (cgp->cg_cgx * fs->fs_fpg + bno);
e3fe2d69
KM
653}
654
502770a3
KM
655/*
656 * Determine whether an inode can be allocated.
657 *
658 * Check to see if an inode is available, and if it is,
659 * allocate it using the following policy:
660 * 1) allocate the requested inode.
661 * 2) allocate the next available inode after the requested
662 * inode in the specified cylinder group.
663 */
4f083fd7 664ino_t
f7287e4b
KM
665ialloccg(ip, cg, ipref, mode)
666 struct inode *ip;
e3fe2d69
KM
667 int cg;
668 daddr_t ipref;
669 int mode;
670{
f7287e4b 671 register struct fs *fs;
f3c028b7
KM
672 register struct buf *bp;
673 register struct cg *cgp;
e3fe2d69
KM
674 int i;
675
f7287e4b 676 fs = ip->i_fs;
b6407c9d 677 if (fs->fs_cs(fs, cg).cs_nifree == 0)
ae851115 678 return (NULL);
d65bd829 679 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
e5476900
KM
680 cgp = bp->b_un.b_cg;
681 if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
d995d89d 682 brelse(bp);
ae851115 683 return (NULL);
d995d89d 684 }
d65bd829
KM
685 if (cgp->cg_cs.cs_nifree == 0)
686 return (NULL);
95d0807c 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;
692 } else
693 ipref = cgp->cg_irotor;
694 for (i = 0; i < fs->fs_ipg; i++) {
695 ipref++;
696 if (ipref >= fs->fs_ipg)
697 ipref = 0;
698 if (isclr(cgp->cg_iused, ipref)) {
699 cgp->cg_irotor = ipref;
700 goto gotit;
701 }
702 }
703 brelse(bp);
ae851115 704 return (NULL);
e3fe2d69
KM
705gotit:
706 setbit(cgp->cg_iused, ipref);
0947395d
KM
707 cgp->cg_cs.cs_nifree--;
708 fs->fs_cstotal.cs_nifree--;
b6407c9d 709 fs->fs_cs(fs, cg).cs_nifree--;
e3fe2d69
KM
710 fs->fs_fmod++;
711 if ((mode & IFMT) == IFDIR) {
0947395d
KM
712 cgp->cg_cs.cs_ndir++;
713 fs->fs_cstotal.cs_ndir++;
b6407c9d 714 fs->fs_cs(fs, cg).cs_ndir++;
e3fe2d69
KM
715 }
716 bdwrite(bp);
717 return (cg * fs->fs_ipg + ipref);
718}
719
502770a3
KM
720/*
721 * Free a block or fragment.
722 *
723 * The specified block or fragment is placed back in the
724 * free map. If a fragment is deallocated, a possible
725 * block reassembly is checked.
726 */
4f083fd7 727free(ip, bno, size)
f7287e4b 728 register struct inode *ip;
e3fe2d69 729 daddr_t bno;
daaf7bee 730 off_t size;
e3fe2d69
KM
731{
732 register struct fs *fs;
733 register struct cg *cgp;
734 register struct buf *bp;
f3c028b7
KM
735 int cg, blk, frags, bbase;
736 register int i;
e3fe2d69 737
f7287e4b 738 fs = ip->i_fs;
ffd90e52
KM
739 if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
740 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
741 ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
b6407c9d 742 panic("free: bad size");
ffd90e52 743 }
6994bf5d 744 cg = dtog(fs, bno);
6459ebe0
KM
745 if (badblock(fs, bno)) {
746 printf("bad block %d, ino %d\n", bno, ip->i_number);
e3fe2d69 747 return;
6459ebe0 748 }
d65bd829 749 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
e5476900
KM
750 cgp = bp->b_un.b_cg;
751 if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
d995d89d 752 brelse(bp);
e3fe2d69 753 return;
d995d89d 754 }
95d0807c 755 cgp->cg_time = time.tv_sec;
6994bf5d 756 bno = dtogd(fs, bno);
b6407c9d 757 if (size == fs->fs_bsize) {
240a4664 758 if (isblock(fs, cgp->cg_free, fragstoblks(fs, bno))) {
ffd90e52
KM
759 printf("dev = 0x%x, block = %d, fs = %s\n",
760 ip->i_dev, bno, fs->fs_fsmnt);
07670f7d 761 panic("free: freeing free block");
6459ebe0 762 }
240a4664 763 setblock(fs, cgp->cg_free, fragstoblks(fs, bno));
0947395d
KM
764 cgp->cg_cs.cs_nbfree++;
765 fs->fs_cstotal.cs_nbfree++;
b6407c9d 766 fs->fs_cs(fs, cg).cs_nbfree++;
502770a3
KM
767 i = cbtocylno(fs, bno);
768 cgp->cg_b[i][cbtorpos(fs, bno)]++;
769 cgp->cg_btot[i]++;
07670f7d 770 } else {
b6407c9d 771 bbase = bno - (bno % fs->fs_frag);
f3c028b7
KM
772 /*
773 * decrement the counts associated with the old frags
774 */
ae851115 775 blk = blkmap(fs, cgp->cg_free, bbase);
b6407c9d 776 fragacct(fs, blk, cgp->cg_frsum, -1);
f3c028b7
KM
777 /*
778 * deallocate the fragment
779 */
d995d89d 780 frags = numfrags(fs, size);
f3c028b7 781 for (i = 0; i < frags; i++) {
ffd90e52
KM
782 if (isset(cgp->cg_free, bno + i)) {
783 printf("dev = 0x%x, block = %d, fs = %s\n",
784 ip->i_dev, bno + i, fs->fs_fsmnt);
07670f7d 785 panic("free: freeing free frag");
ffd90e52 786 }
07670f7d 787 setbit(cgp->cg_free, bno + i);
07670f7d 788 }
ae851115
KM
789 cgp->cg_cs.cs_nffree += i;
790 fs->fs_cstotal.cs_nffree += i;
791 fs->fs_cs(fs, cg).cs_nffree += i;
f3c028b7
KM
792 /*
793 * add back in counts associated with the new frags
794 */
ae851115 795 blk = blkmap(fs, cgp->cg_free, bbase);
b6407c9d 796 fragacct(fs, blk, cgp->cg_frsum, 1);
f3c028b7
KM
797 /*
798 * if a complete block has been reassembled, account for it
799 */
240a4664 800 if (isblock(fs, cgp->cg_free, fragstoblks(fs, bbase))) {
b6407c9d
KM
801 cgp->cg_cs.cs_nffree -= fs->fs_frag;
802 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
803 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
0947395d
KM
804 cgp->cg_cs.cs_nbfree++;
805 fs->fs_cstotal.cs_nbfree++;
b6407c9d 806 fs->fs_cs(fs, cg).cs_nbfree++;
502770a3
KM
807 i = cbtocylno(fs, bbase);
808 cgp->cg_b[i][cbtorpos(fs, bbase)]++;
809 cgp->cg_btot[i]++;
07670f7d
KM
810 }
811 }
e3fe2d69 812 fs->fs_fmod++;
e3fe2d69
KM
813 bdwrite(bp);
814}
815
502770a3
KM
816/*
817 * Free an inode.
818 *
819 * The specified inode is placed back in the free map.
820 */
f7287e4b
KM
821ifree(ip, ino, mode)
822 struct inode *ip;
e3fe2d69
KM
823 ino_t ino;
824 int mode;
825{
826 register struct fs *fs;
827 register struct cg *cgp;
828 register struct buf *bp;
e3fe2d69
KM
829 int cg;
830
f7287e4b 831 fs = ip->i_fs;
ffd90e52
KM
832 if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) {
833 printf("dev = 0x%x, ino = %d, fs = %s\n",
834 ip->i_dev, ino, fs->fs_fsmnt);
e3fe2d69 835 panic("ifree: range");
ffd90e52 836 }
6994bf5d 837 cg = itog(fs, ino);
d65bd829 838 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
e5476900
KM
839 cgp = bp->b_un.b_cg;
840 if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
d995d89d 841 brelse(bp);
e3fe2d69 842 return;
d995d89d 843 }
95d0807c 844 cgp->cg_time = time.tv_sec;
e3fe2d69 845 ino %= fs->fs_ipg;
ffd90e52
KM
846 if (isclr(cgp->cg_iused, ino)) {
847 printf("dev = 0x%x, ino = %d, fs = %s\n",
848 ip->i_dev, ino, fs->fs_fsmnt);
e3fe2d69 849 panic("ifree: freeing free inode");
ffd90e52 850 }
e3fe2d69 851 clrbit(cgp->cg_iused, ino);
0947395d
KM
852 cgp->cg_cs.cs_nifree++;
853 fs->fs_cstotal.cs_nifree++;
b6407c9d 854 fs->fs_cs(fs, cg).cs_nifree++;
e3fe2d69 855 if ((mode & IFMT) == IFDIR) {
0947395d
KM
856 cgp->cg_cs.cs_ndir--;
857 fs->fs_cstotal.cs_ndir--;
b6407c9d 858 fs->fs_cs(fs, cg).cs_ndir--;
e3fe2d69
KM
859 }
860 fs->fs_fmod++;
861 bdwrite(bp);
862}
863
743f1ef7 864/*
502770a3
KM
865 * Find a block of the specified size in the specified cylinder group.
866 *
743f1ef7
KM
867 * It is a panic if a request is made to find a block if none are
868 * available.
869 */
870daddr_t
871mapsearch(fs, cgp, bpref, allocsiz)
872 register struct fs *fs;
873 register struct cg *cgp;
874 daddr_t bpref;
875 int allocsiz;
876{
877 daddr_t bno;
878 int start, len, loc, i;
879 int blk, field, subfield, pos;
880
881 /*
882 * find the fragment by searching through the free block
883 * map for an appropriate bit pattern
884 */
885 if (bpref)
6994bf5d 886 start = dtogd(fs, bpref) / NBBY;
743f1ef7
KM
887 else
888 start = cgp->cg_frotor / NBBY;
942bd18b 889 len = howmany(fs->fs_fpg, NBBY) - start;
b6407c9d 890 loc = scanc(len, &cgp->cg_free[start], fragtbl[fs->fs_frag],
156b8f82 891 1 << (allocsiz - 1 + (fs->fs_frag % NBBY)));
743f1ef7 892 if (loc == 0) {
e5476900
KM
893 len = start + 1;
894 start = 0;
b6407c9d 895 loc = scanc(len, &cgp->cg_free[start], fragtbl[fs->fs_frag],
156b8f82 896 1 << (allocsiz - 1 + (fs->fs_frag % NBBY)));
961945a8 897 if (loc == 0)
e5476900 898 return (-1);
743f1ef7
KM
899 }
900 bno = (start + len - loc) * NBBY;
901 cgp->cg_frotor = bno;
902 /*
903 * found the byte in the map
904 * sift through the bits to find the selected frag
905 */
ae851115
KM
906 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
907 blk = blkmap(fs, cgp->cg_free, bno);
743f1ef7
KM
908 blk <<= 1;
909 field = around[allocsiz];
910 subfield = inside[allocsiz];
b6407c9d 911 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
ae851115
KM
912 if ((blk & field) == subfield)
913 return (bno + pos);
743f1ef7
KM
914 field <<= 1;
915 subfield <<= 1;
916 }
917 }
ffd90e52 918 printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
743f1ef7 919 panic("alloccg: block not in map");
e5476900 920 return (-1);
743f1ef7
KM
921}
922
e3fe2d69 923/*
502770a3
KM
924 * Fserr prints the name of a file system with an error diagnostic.
925 *
926 * The form of the error message is:
e3fe2d69
KM
927 * fs: error message
928 */
929fserr(fs, cp)
930 struct fs *fs;
931 char *cp;
932{
933
934 printf("%s: %s\n", fs->fs_fsmnt, cp);
935}