- * The number and size of the old block is given, and a preference
- * and new size is also specified. The allocator attempts to extend
- * the original block. Failing that, the regular block allocator is
- * invoked to get an appropriate block.
- */
-struct buf *
-realloccg(ip, bprev, bpref, osize, nsize)
- register struct inode *ip;
- daddr_t bprev, bpref;
- int osize, nsize;
-{
- daddr_t bno;
- register struct fs *fs;
- register struct buf *bp, *obp;
- int cg;
-
- fs = ip->i_fs;
- if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
- (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
- printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
- ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
- panic("realloccg: bad size");
- }
- if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
- goto nospace;
- if (bprev == 0) {
- printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
- ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
- panic("realloccg: bad bprev");
- }
-#ifdef QUOTA
- u.u_error = chkdq(ip, (long)btodb(nsize - osize), 0);
- if (u.u_error)
- return (NULL);
-#endif
- cg = dtog(fs, bprev);
- bno = fragextend(ip, cg, (long)bprev, osize, nsize);
- if (bno != 0) {
- do {
- bp = bread(ip->i_dev, fsbtodb(fs, bno), osize);
- if (bp->b_flags & B_ERROR) {
- brelse(bp);
- return (NULL);
- }
- } while (brealloc(bp, nsize) == 0);
- bp->b_flags |= B_DONE;
- bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
- ip->i_blocks += btodb(nsize - osize);
- ip->i_flag |= IUPD|ICHG;
- return (bp);
- }
- if (bpref >= fs->fs_size)
- bpref = 0;
- bno = (daddr_t)hashalloc(ip, cg, (long)bpref, nsize,
- (u_long (*)())alloccg);
- if (bno > 0) {
- obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize);
- if (obp->b_flags & B_ERROR) {
- brelse(obp);
- return (NULL);
- }
- bp = getblk(ip->i_dev, fsbtodb(fs, bno), osize);
- swapbuf(obp, bp);
- obp->b_flags &= ~B_DELWRI;
- obp->b_flags |= B_INVAL;
- brelse(obp);
- free(ip, bprev, (off_t)osize);
- while (brealloc(bp, nsize) == 0) {
- bp = bread(ip->i_dev, fsbtodb(fs, bno), osize);
- if (bp->b_flags & B_ERROR) {
- brelse(bp);
- return (NULL);
- }
- }
- bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
- ip->i_blocks += btodb(nsize - osize);
- ip->i_flag |= IUPD|ICHG;
- return (bp);
- }
-nospace:
- /*
- * no space available
- */
- fserr(fs, "file system full");
- uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
- u.u_error = ENOSPC;
- return (NULL);
-}
-
-/*
- * Allocate an inode in the file system.
- *
- * A preference may be optionally specified. If a preference is given
- * the following hierarchy is used to allocate an inode:
- * 1) allocate the requested inode.
- * 2) allocate an inode in the same cylinder group.
- * 3) quadradically rehash into other cylinder groups, until an
- * available inode is located.
- * If no inode preference is given the following heirarchy is used
- * to allocate an inode:
- * 1) allocate an inode in cylinder group 0.
- * 2) quadradically rehash into other cylinder groups, until an
- * available inode is located.
- */
-struct inode *
-ialloc(pip, ipref, mode)
- register struct inode *pip;
- ino_t ipref;
- int mode;
-{
- ino_t ino;
- register struct fs *fs;
- register struct inode *ip;
- int cg;
-
- fs = pip->i_fs;
- if (fs->fs_cstotal.cs_nifree == 0)
- goto noinodes;
-#ifdef QUOTA
- u.u_error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0);
- if (u.u_error)
- return (NULL);
-#endif
- if (ipref >= fs->fs_ncg * fs->fs_ipg)
- ipref = 0;
- cg = itog(fs, ipref);
- ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
- if (ino == 0)
- goto noinodes;
- ip = iget(pip->i_dev, pip->i_fs, ino);
- if (ip == NULL) {
- ifree(pip, ino, 0);
- return (NULL);
- }
- if (ip->i_mode) {
- printf("mode = 0%o, inum = %d, fs = %s\n",
- ip->i_mode, ip->i_number, fs->fs_fsmnt);
- panic("ialloc: dup alloc");
- }
- if (ip->i_blocks) { /* XXX */
- printf("free inode %s/%d had %d blocks\n",
- fs->fs_fsmnt, ino, ip->i_blocks);
- ip->i_blocks = 0;
- }
- return (ip);
-noinodes:
- fserr(fs, "out of inodes");
- uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
- u.u_error = ENOSPC;
- return (NULL);
-}
-
-/*
- * Find a cylinder to place a directory.
- *
- * The policy implemented by this algorithm is to select from
- * among those cylinder groups with above the average number of
- * free inodes, the one with the smallest number of directories.
- */
-ino_t
-dirpref(fs)
- register struct fs *fs;
-{
- int cg, minndir, mincg, avgifree;
-
- avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
- minndir = fs->fs_ipg;
- mincg = 0;
- for (cg = 0; cg < fs->fs_ncg; cg++)
- if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
- fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
- mincg = cg;
- minndir = fs->fs_cs(fs, cg).cs_ndir;
- }
- return ((ino_t)(fs->fs_ipg * mincg));
-}
-
-/*
- * Select the desired position for the next block in a file. The file is
- * logically divided into sections. The first section is composed of the
- * direct blocks. Each additional section contains fs_maxbpg blocks.
- *
- * If no blocks have been allocated in the first section, the policy is to
- * request a block in the same cylinder group as the inode that describes
- * the file. If no blocks have been allocated in any other section, the
- * policy is to place the section in a cylinder group with a greater than
- * average number of free blocks. An appropriate cylinder group is found
- * by maintaining a rotor that sweeps the cylinder groups. When a new
- * group of blocks is needed, the rotor is advanced until a cylinder group
- * with greater than the average number of free blocks is found.
- *
- * If a section is already partially allocated, the policy is to
- * contiguously allocate fs_maxcontig blocks. The end of one of these
- * contiguous blocks and the beginning of the next is physically separated
- * so that the disk head will be in transit between them for at least
- * fs_rotdelay milliseconds. This is to allow time for the processor to
- * schedule another I/O transfer.
- */
-daddr_t
-blkpref(ip, lbn, indx, bap)
- struct inode *ip;
- daddr_t lbn;
- int indx;
- daddr_t *bap;
-{
- register struct fs *fs;
- int cg, avgbfree;
- daddr_t nextblk;
-
- fs = ip->i_fs;
- if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
- if (lbn < NDADDR) {
- cg = itog(fs, ip->i_number);
- return (fs->fs_fpg * cg + fs->fs_frag);
- }
- /*
- * Find a cylinder with greater than average number of
- * unused data blocks.
- */
- avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
- for (cg = fs->fs_cgrotor + 1; cg < fs->fs_ncg; cg++)
- if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
- fs->fs_cgrotor = cg;
- return (fs->fs_fpg * cg + fs->fs_frag);
- }
- for (cg = 0; cg <= fs->fs_cgrotor; cg++)
- if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
- fs->fs_cgrotor = cg;
- return (fs->fs_fpg * cg + fs->fs_frag);
- }
- return (NULL);
- }
- /*
- * One or more previous blocks have been laid out. If less
- * than fs_maxcontig previous blocks are contiguous, the
- * next block is requested contiguously, otherwise it is
- * requested rotationally delayed by fs_rotdelay milliseconds.
- */
- nextblk = bap[indx - 1] + fs->fs_frag;
- if (indx > fs->fs_maxcontig &&
- bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
- != nextblk)
- return (nextblk);
- if (fs->fs_rotdelay != 0)
- /*
- * Here we convert ms of delay to frags as:
- * (frags) = (ms) * (rev/sec) * (sect/rev) /
- * ((sect/frag) * (ms/sec))
- * then round up to the next block.
- */
- nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
- (NSPF(fs) * 1000), fs->fs_frag);
- return (nextblk);
-}
-
-/*
- * Implement the cylinder overflow algorithm.