--- linux/fs/inode.c 2018/04/24 18:00:41 1.1 +++ linux/fs/inode.c 2018/04/24 18:07:05 1.1.1.8 @@ -1,16 +1,20 @@ -#include +/* + * linux/fs/inode.c + * + * (C) 1991 Linus Torvalds + */ + +#include +#include #include #include #include #include -struct m_inode inode_table[NR_INODE]={{0,},}; - -static void read_inode(struct m_inode * inode); -static void write_inode(struct m_inode * inode); +struct inode inode_table[NR_INODE]={{0,},}; -static inline void wait_on_inode(struct m_inode * inode) +static inline void wait_on_inode(struct inode * inode) { cli(); while (inode->i_lock) @@ -18,7 +22,7 @@ static inline void wait_on_inode(struct sti(); } -static inline void lock_inode(struct m_inode * inode) +static inline void lock_inode(struct inode * inode) { cli(); while (inode->i_lock) @@ -27,112 +31,94 @@ static inline void lock_inode(struct m_i sti(); } -static inline void unlock_inode(struct m_inode * inode) +static inline void unlock_inode(struct inode * inode) { inode->i_lock=0; wake_up(&inode->i_wait); } -void sync_inodes(void) +static void write_inode(struct inode * inode) +{ + if (!inode->i_dirt) + return; + inode->i_dirt = 0; + lock_inode(inode); + if (inode->i_dev && inode->i_sb && + inode->i_sb->s_op && inode->i_sb->s_op->write_inode) + inode->i_sb->s_op->write_inode(inode); + unlock_inode(inode); +} + +static void read_inode(struct inode * inode) +{ + lock_inode(inode); + if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->read_inode) + inode->i_sb->s_op->read_inode(inode); + unlock_inode(inode); +} + +/* + * bmap is needed for demand-loading and paging: if this function + * doesn't exist for a filesystem, then those things are impossible: + * executables cannot be run from the filesystem etc... + * + * This isn't as bad as it sounds: the read-routines might still work, + * so the filesystem would be otherwise ok (for example, you might have + * a DOS filesystem, which doesn't lend itself to bmap very well, but + * you could still transfer files to/from the filesystem) + */ +int bmap(struct inode * inode, int block) +{ + if (inode->i_op && inode->i_op->bmap) + return inode->i_op->bmap(inode,block); + return 0; +} + +void invalidate_inodes(int dev) { int i; - struct m_inode * inode; + struct inode * inode; inode = 0+inode_table; for(i=0 ; ii_dirt && !inode->i_pipe) - write_inode(inode); + if (inode->i_dev == dev) { + if (inode->i_count) { + printk("inode in use on removed disk\n\r"); + continue; + } + inode->i_dev = inode->i_dirt = 0; + } } } -static int _bmap(struct m_inode * inode,int block,int create) +void sync_inodes(void) { - struct buffer_head * bh; int i; + struct inode * inode; - if (block<0) - panic("_bmap: block<0"); - if (block >= 7+512+512*512) - panic("_bmap: block>big"); - if (block<7) { - if (create && !inode->i_zone[block]) - if (inode->i_zone[block]=new_block(inode->i_dev)) { - inode->i_ctime=CURRENT_TIME; - inode->i_dirt=1; - } - return inode->i_zone[block]; - } - block -= 7; - if (block<512) { - if (create && !inode->i_zone[7]) - if (inode->i_zone[7]=new_block(inode->i_dev)) { - inode->i_dirt=1; - inode->i_ctime=CURRENT_TIME; - } - if (!inode->i_zone[7]) - return 0; - if (!(bh = bread(inode->i_dev,inode->i_zone[7]))) - return 0; - i = ((unsigned short *) (bh->b_data))[block]; - if (create && !i) - if (i=new_block(inode->i_dev)) { - ((unsigned short *) (bh->b_data))[block]=i; - bh->b_dirt=1; - } - brelse(bh); - return i; + inode = 0+inode_table; + for(i=0 ; ii_dirt && !inode->i_pipe) + write_inode(inode); } - block -= 512; - if (create && !inode->i_zone[8]) - if (inode->i_zone[8]=new_block(inode->i_dev)) { - inode->i_dirt=1; - inode->i_ctime=CURRENT_TIME; - } - if (!inode->i_zone[8]) - return 0; - if (!(bh=bread(inode->i_dev,inode->i_zone[8]))) - return 0; - i = ((unsigned short *)bh->b_data)[block>>9]; - if (create && !i) - if (i=new_block(inode->i_dev)) { - ((unsigned short *) (bh->b_data))[block>>9]=i; - bh->b_dirt=1; - } - brelse(bh); - if (!i) - return 0; - if (!(bh=bread(inode->i_dev,i))) - return 0; - i = ((unsigned short *)bh->b_data)[block&511]; - if (create && !i) - if (i=new_block(inode->i_dev)) { - ((unsigned short *) (bh->b_data))[block&511]=i; - bh->b_dirt=1; - } - brelse(bh); - return i; } -int bmap(struct m_inode * inode,int block) -{ - return _bmap(inode,block,0); -} - -int create_block(struct m_inode * inode, int block) -{ - return _bmap(inode,block,1); -} - -void iput(struct m_inode * inode) +void iput(struct inode * inode) { if (!inode) return; wait_on_inode(inode); - if (!inode->i_count) - panic("iput: trying to free free inode"); + if (!inode->i_count) { + printk("iput: trying to free free inode\n"); + printk("device %04x, inode %d, mode=%07o\n",inode->i_rdev, + inode->i_ino,inode->i_mode); + return; + } if (inode->i_pipe) { wake_up(&inode->i_wait); + wake_up(&inode->i_wait2); if (--inode->i_count) return; free_page(inode->i_size); @@ -141,14 +127,18 @@ void iput(struct m_inode * inode) inode->i_pipe=0; return; } - if (!inode->i_dev || inode->i_count>1) { + if (!inode->i_dev) { inode->i_count--; return; } repeat: - if (!inode->i_nlinks) { - truncate(inode); - free_inode(inode); + if (inode->i_count>1) { + inode->i_count--; + return; + } + if (!inode->i_nlink) { + if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->put_inode) + inode->i_sb->s_op->put_inode(inode); return; } if (inode->i_dirt) { @@ -160,48 +150,43 @@ repeat: return; } -static volatile int last_allocated_inode = 0; - -struct m_inode * get_empty_inode(void) +struct inode * get_empty_inode(void) { - struct m_inode * inode; - int inr; + struct inode * inode; + static struct inode * last_inode = inode_table; + int i; - while (1) { + do { inode = NULL; - inr = last_allocated_inode; - do { - if (!inode_table[inr].i_count) { - inode = inr + inode_table; - break; + for (i = NR_INODE; i ; i--) { + if (++last_inode >= inode_table + NR_INODE) + last_inode = inode_table; + if (!last_inode->i_count) { + inode = last_inode; + if (!inode->i_dirt && !inode->i_lock) + break; } - inr++; - if (inr>=NR_INODE) - inr=0; - } while (inr != last_allocated_inode); + } if (!inode) { - for (inr=0 ; inri_dirt) { write_inode(inode); wait_on_inode(inode); } - if (!inode->i_count) - break; - } + } while (inode->i_count); memset(inode,0,sizeof(*inode)); inode->i_count = 1; return inode; } -struct m_inode * get_pipe_inode(void) +struct inode * get_pipe_inode(void) { - struct m_inode * inode; + struct inode * inode; if (!(inode = get_empty_inode())) return NULL; @@ -215,74 +200,59 @@ struct m_inode * get_pipe_inode(void) return inode; } -struct m_inode * iget(int dev,int nr) +struct inode * iget(int dev,int nr) { - struct m_inode * inode, * empty; + struct inode * inode, * empty; if (!dev) panic("iget with dev==0"); empty = get_empty_inode(); inode = inode_table; while (inode < NR_INODE+inode_table) { - if (inode->i_dev != dev || inode->i_num != nr) { + if (inode->i_dev != dev || inode->i_ino != nr) { inode++; continue; } wait_on_inode(inode); - if (inode->i_dev != dev || inode->i_num != nr) { + if (inode->i_dev != dev || inode->i_ino != nr) { inode = inode_table; continue; } inode->i_count++; + if (inode->i_mount) { + int i; + + for (i = 0 ; i= NR_SUPER) { + printk("Mounted inode hasn't got sb\n"); + if (empty) + iput(empty); + return inode; + } + iput(inode); + if (!(inode = super_block[i].s_mounted)) + printk("iget: mounted dev has no rootinode\n"); + else { + inode->i_count++; + wait_on_inode(inode); + } + } if (empty) iput(empty); return inode; } if (!empty) return (NULL); - inode=empty; + inode = empty; + if (!(inode->i_sb = get_super(dev))) { + printk("iget: gouldn't get super-block\n\t"); + iput(inode); + return NULL; + } inode->i_dev = dev; - inode->i_num = nr; + inode->i_ino = nr; read_inode(inode); return inode; } - -static void read_inode(struct m_inode * inode) -{ - struct super_block * sb; - struct buffer_head * bh; - int block; - - lock_inode(inode); - sb=get_super(inode->i_dev); - block = 2 + sb->s_imap_blocks + sb->s_zmap_blocks + - (inode->i_num-1)/INODES_PER_BLOCK; - if (!(bh=bread(inode->i_dev,block))) - panic("unable to read i-node block"); - *(struct d_inode *)inode = - ((struct d_inode *)bh->b_data) - [(inode->i_num-1)%INODES_PER_BLOCK]; - brelse(bh); - unlock_inode(inode); -} - -static void write_inode(struct m_inode * inode) -{ - struct super_block * sb; - struct buffer_head * bh; - int block; - - lock_inode(inode); - sb=get_super(inode->i_dev); - block = 2 + sb->s_imap_blocks + sb->s_zmap_blocks + - (inode->i_num-1)/INODES_PER_BLOCK; - if (!(bh=bread(inode->i_dev,block))) - panic("unable to read i-node block"); - ((struct d_inode *)bh->b_data) - [(inode->i_num-1)%INODES_PER_BLOCK] = - *(struct d_inode *)inode; - bh->b_dirt=1; - inode->i_dirt=0; - brelse(bh); - unlock_inode(inode); -}