# HG changeset patch # User John Tsiombikas # Date 1323630935 -7200 # Node ID ec62cbe00b556fce964f11a36e9479c506950319 # Parent b3351d018ac6373c4b288978b889702defbb1936 whatever diff -r b3351d018ac6 -r ec62cbe00b55 src/fs.c --- a/src/fs.c Sun Dec 11 11:12:30 2011 +0200 +++ b/src/fs.c Sun Dec 11 21:15:35 2011 +0200 @@ -10,6 +10,13 @@ #include "fs.h" #include "bdev.h" +#define BM_IDX(x) ((x) / 32) +#define BM_BIT(x) ((x) & 0x1f) + +#define BM_ISFREE(bm, x) (((bm)[BM_IDX(x)] & (1 << BM_BIT(x))) == 0) +#define BM_SET(bm, x) ((bm)[BM_IDX(x)] |= (1 << BM_BIT(x))) +#define BM_CLR(bm, x) ((bm)[BM_IDX(x)] &= ~(1 << BM_BIT(x))) + int openfs(struct filesys *fs, dev_t dev); static int read_superblock(struct filesys *fs); @@ -156,3 +163,35 @@ free(buf); return 0; } + +static int find_free(uint32_t *bm, int sz) +{ + int i; + uint32_t ent; + + for(i=0; i<=sz/32; i++) { + if(bm[i] != 0xffffffff) { + ent = i * 32; + for(j=0; j<32; j++) { + if(BM_ISFREE(bm, ent)) { + return ent; + } + } + + panic("shouldn't happen (in find_free:fs.c)"); + } + } + + return -1; +} + +static int alloc_inode(struct filesys *fs) +{ + int ino; + + if((ino = find_free(fs->ibm, fs->ibm_count)) == -1) { + return -1; + } + BM_SET(fs->ibm, ino); + return 0; +} diff -r b3351d018ac6 -r ec62cbe00b55 src/fs.h --- a/src/fs.h Sun Dec 11 11:12:30 2011 +0200 +++ b/src/fs.h Sun Dec 11 21:15:35 2011 +0200 @@ -40,6 +40,9 @@ /* total number of blocks */ unsigned int num_blocks; + /* total number of inodes */ + unsigned int num_inodes; + /* inode allocation bitmap start and count */ blkid ibm_start; unsigned int ibm_count;