//读文件 int general_file_read(struct m_inode *inode,struct file *filp,char *buf,int count) { int i,c,m = 0; char *p; // char *ptr = buf; struct buffer_head *bh; struct dir_entry *de; int block_nr = 0; if(!inode || !filp) return -EINVAL; if(!buf || count <= 0) return 0; int nr_start_sect = get_first_block(inode); printk("read num = %d.start_sect = %d.",inode->i_num, nr_start_sect); // printk("inode->i_dev = %d inode->i_num = %d inode->i_start_sect = %d\n",inode->i_dev,inode->i_num,inode->i_start_sect); //文件所占的磁盘块总数 off_t pos = filp->f_pos; //开始读取的磁盘块 nr_start_sect += pos/SECTOR_SIZE; // nr_sects = (inode->i_size + SECTOR_SIZE -1)/SECTOR_SIZE; while(i < count) { block_nr = get_pos_block(inode, pos); c = pos % SECTOR_SIZE; c = SECTOR_SIZE -c ; if(c > count) { c = count - i; } // printk("file_read : inode->i_dev = %d nr_start_sect = %d count = %d\n",inode->i_dev,nr_start_sect + pos/SECTOR_SIZE,count); bh = getblk(inode->i_dev,block_nr); hd_rw(inode->i_dev,block_nr,1,ATA_READ,bh); p = bh->b_data + pos; de = (struct dir_entry*)bh->b_data; // printk("de->file_name = %s\n",(++de)->file_name); filp->f_pos += c; pos += c; i += c; m++; // printk("c = %d\n",c); while(c-- > 0) { if(*p == 0) { p++; continue; } else { *buf = *p; // printk("%d %c %c\t",512 - c,*p,*buf); buf++; p++; } } brelse(bh); } return 0; }
int general_file_write(struct m_inode *inode,struct file *filp,char *buf,int count) { int i = 0,block_nr,c; off_t pos = 0; char *p = NULL; struct buffer_head *bh = NULL; int nr_start_sect = 0; printk("write 5"); if(!inode || !filp || !buf || count < 0) { printk("write parameters error."); return -EINVAL; } /*int nr_start_sect = inode->i_start_sect;*/ if((nr_start_sect = get_first_block(inode)) == 0) { set_block_nums(inode->i_dev ,(struct d_inode *)inode, NR_DEFAULT_SECTS); if((nr_start_sect = get_first_block(inode)) == 0) return -3; } printk("write num = %d.start_sect = %d.",inode->i_num, nr_start_sect); if(filp->f_flag & O_APPEND) pos = inode->i_size; else pos = filp->f_pos; while(i < count) { /*block = nr_start_sect + pos/SECTOR_SIZE;*/ block_nr = get_pos_block(inode, pos); bh = getblk(inode->i_dev,block_nr); c = pos % SECTOR_SIZE; p = bh->b_data + c; c = SECTOR_SIZE -c; if(c > count) c = count - i; pos += c; i += c; if(pos > inode->i_size) { inode->i_size = pos; } while(c-- > 0) *(p++) = *(buf++); hd_rw(inode->i_dev,block_nr,1,ATA_WRITE,bh); brelse(bh); } inode->i_dirty = 1; return 0; }
static void buf_ll_rw(struct buf *bp) { if(MAJOR(bp->dev) == MEM_MAJOR){ memhd_rw(bp); } else if(MAJOR(bp->dev) == HD_MAJOR){ hd_rw(bp); } else { printk("buf_ll_rw: unknown dev %d\n", MAJOR(bp->dev)); panic("buf_ll_rw"); } }
struct buffer_head *bread(int dev,int block_nr) { struct buffer_head *bh; if(!(bh = getblk(dev,block_nr))) panic("bread:getblk returned NULL\n"); if(bh->b_uptodate) return bh; hd_rw(dev,block_nr,1,ATA_READ,bh); wait_on_buffer(bh); if(bh->b_uptodate) return bh; brelse(bh); return NULL; }
void verify_fs(void) { unsigned int *q = (unsigned int *)(HD0_ADDR); unsigned char sect[512] = {0}; struct SUPER_BLOCK sb; unsigned int i = 0, j = 0, m = 0, n = 0; /* get the fs sb */ sb.sb_start = q[0]; hd_rw(ABS_SUPER_BLK(sb), HD_READ, 1, &sb); /* the first partition must be FST_FS type */ if (sb.sb_magic != FST_FS) { kprintf(KPL_DUMP, "Partition 1 does not have a valid file system\n"); kprintf(KPL_DUMP, "Creating file system\t\t\t\t\t\t\t "); sb.sb_magic = FST_FS; sb.sb_start = q[0]; sb.sb_blocks = q[1]; sb.sb_dmap_blks = (sb.sb_blocks+0xfff)>>12; sb.sb_imap_blks = INODE_BIT_BLKS; sb.sb_inode_blks = INODE_BLKS; hd_rw(ABS_SUPER_BLK(sb), HD_WRITE, 1, &sb); /* initial data bitmap */ n = ABS_DMAP_BLK(sb); j = sb.sb_dmap_blks+sb.sb_imap_blks+sb.sb_inode_blks+2; memset(sect, 0xff, sizeof sect/sizeof sect[0]); for (i=j/(512*8); i>0; --i) { hd_rw(n++, HD_WRITE, 1, sect); m += 4096; } m += 4096; for (i=j%(512*8); i<512*8; ++i) { clrb(sect, i); --m; } hd_rw(n++, HD_WRITE, 1, sect); memset(sect, 0, sizeof sect/sizeof sect[0]); for (i=sb.sb_imap_blks-(n-ABS_DMAP_BLK(sb)); i>0; --i) hd_rw(n++, HD_WRITE, 1, sect); /* initail inode bitmap */ for (i=sb.sb_imap_blks; i>0; --i) hd_rw(ABS_IMAP_BLK(sb)+i-1, HD_WRITE, 1, sect); /* initial inode blocks */ for (i=sb.sb_inode_blks; i>0; --i) hd_rw(ABS_INODE_BLK(sb)+i-1, HD_WRITE, 1, sect); kprintf(KPL_DUMP, "[DONE]"); }