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