- * 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, request;
-
- 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;
- if (fs->fs_optim == FS_OPTSPACE)
- request = nsize;
- else /* if (fs->fs_optim == FS_OPTTIME) */
- request = fs->fs_bsize;
- bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request,
- (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), nsize);
- bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize);
- bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
- if (obp->b_flags & B_DELWRI) {
- obp->b_flags &= ~B_DELWRI;
- u.u_ru.ru_oublock--; /* delete charge */
- }
- brelse(obp);
- free(ip, bprev, (off_t)osize);
- if (nsize < request)
- free(ip, bno + numfrags(fs, nsize),
- (off_t)(request - nsize));
- 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.