-/* lfs_alloc.c 2.18 82/10/21 */
-
-#include "../h/param.h"
-#include "../h/systm.h"
-#include "../h/mount.h"
-#include "../h/fs.h"
-#include "../h/conf.h"
-#include "../h/buf.h"
-#include "../h/inode.h"
-#include "../h/dir.h"
-#include "../h/user.h"
-#include "../h/quota.h"
-#include "../h/kernel.h"
-
-extern u_long hashalloc();
-extern u_long ialloccg();
-extern u_long alloccg();
-extern daddr_t alloccgblk();
-extern daddr_t fragextend();
-extern daddr_t blkpref();
-extern daddr_t mapsearch();
-extern int inside[], around[];
-extern unsigned char *fragtbl[];
-
-/*
- * Allocate a block in the file system.
- *
- * The size of the requested block is given, which must be some
- * multiple of fs_fsize and <= fs_bsize.
- * A preference may be optionally specified. If a preference is given
- * the following hierarchy is used to allocate a block:
- * 1) allocate the requested block.
- * 2) allocate a rotationally optimal block in the same cylinder.
- * 3) allocate a block in the same cylinder group.
- * 4) quadradically rehash into other cylinder groups, until an
- * available block is located.
- * If no block preference is given the following heirarchy is used
- * to allocate a block:
- * 1) allocate a block in the cylinder group that contains the
- * inode for the file.
- * 2) quadradically rehash into other cylinder groups, until an
- * available block is located.
- */
-struct buf *
-alloc(ip, bpref, size)
- register struct inode *ip;
- daddr_t bpref;
- int size;
-{
- daddr_t bno;
- register struct fs *fs;
- register struct buf *bp;
- int cg;
-
- fs = ip->i_fs;
- if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
- printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
- ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
- panic("alloc: bad size");
- }
- if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
- goto nospace;
- if (u.u_uid != 0 &&
- fs->fs_cstotal.cs_nbfree * fs->fs_frag + fs->fs_cstotal.cs_nffree <
- fs->fs_dsize * fs->fs_minfree / 100)
- goto nospace;
-#ifdef QUOTA
- if (chkdq(ip, (long)((unsigned)size/DEV_BSIZE), 0))
- return(NULL);
-#endif
- if (bpref >= fs->fs_size)
- bpref = 0;
- if (bpref == 0)
- cg = itog(fs, ip->i_number);
- else
- cg = dtog(fs, bpref);
- bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size, alloccg);
- if (bno <= 0)
- goto nospace;
- bp = getblk(ip->i_dev, fsbtodb(fs, bno), size);
- clrbuf(bp);
- return (bp);
-nospace:
- fserr(fs, "file system full");
- uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
- u.u_error = ENOSPC;
- return (NULL);
-}
-
-/*
- * Reallocate a fragment to a bigger size
- *
- * 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 &&
- fs->fs_cstotal.cs_nbfree * fs->fs_frag + fs->fs_cstotal.cs_nffree <
- fs->fs_dsize * fs->fs_minfree / 100)
- 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
- if (chkdq(ip, (long)((unsigned)(nsize-osize)/DEV_BSIZE), 0))
- 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, nsize - osize);
- return (bp);
- }
- if (bpref >= fs->fs_size)
- bpref = 0;
- bno = (daddr_t)hashalloc(ip, cg, (long)bpref, nsize, 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, nsize - osize);
- brelse(obp);
- fre(ip, bprev, (off_t)osize);
- 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);
-}
-