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