root/fs/inode.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. inode_init
  2. fs_may_mount
  3. fs_may_umount
  4. __wait_on_inode
  5. wait_on_inode
  6. lock_inode
  7. unlock_inode
  8. write_inode
  9. read_inode
  10. bmap
  11. invalidate_inodes
  12. sync_inodes
  13. iput
  14. get_empty_inode
  15. get_pipe_inode
  16. iget

   1 /*
   2  *  linux/fs/inode.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 #include <linux/stat.h>
   8 #include <linux/sched.h>
   9 #include <linux/kernel.h>
  10 #include <linux/mm.h>
  11 #include <linux/string.h>
  12 
  13 #include <asm/system.h>
  14 
  15 static struct inode inode_table[NR_INODE];
  16 
  17 void inode_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  18 {
  19         memset(inode_table,0,sizeof(inode_table));      
  20 }
  21 
  22 int fs_may_mount(dev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
  23 {
  24         struct inode * inode;
  25 
  26         for (inode = inode_table+0 ; inode < inode_table+NR_INODE ; inode++) {
  27                 if (inode->i_dev != dev)
  28                         continue;
  29                 if (inode->i_count || inode->i_dirt || inode->i_lock)
  30                         return 0;
  31                 inode->i_dev = 0;
  32         }
  33         return 1;
  34 }
  35 
  36 int fs_may_umount(dev_t dev, struct inode * mount_root)
     /* [previous][next][first][last][top][bottom][index][help] */
  37 {
  38         struct inode * inode;
  39 
  40         for (inode = inode_table+0 ; inode < inode_table+NR_INODE ; inode++)
  41                 if (inode->i_dev==dev && inode->i_count)
  42                         if (inode == mount_root && inode->i_count == 1)
  43                                 continue;
  44                         else
  45                                 return 0;
  46         return 1;
  47 }
  48 
  49 /*
  50  * The "new" scheduling primitives (new as of 0.97 or so) allow this to
  51  * be done without disabling interrupts (other than in the actual queue
  52  * updating things: only a couple of 386 instructions). This should be
  53  * much better for interrupt latency.
  54  */
  55 static void __wait_on_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  56 {
  57         add_wait_queue(&inode->i_wait,&current->wait);
  58 repeat:
  59         current->state = TASK_UNINTERRUPTIBLE;
  60         if (inode->i_lock) {
  61                 schedule();
  62                 goto repeat;
  63         }
  64         remove_wait_queue(&inode->i_wait,&current->wait);
  65         current->state = TASK_RUNNING;
  66 }
  67 
  68 static inline void wait_on_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  69 {
  70         if (inode->i_lock)
  71                 __wait_on_inode(inode);
  72 }
  73 
  74 static inline void lock_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  75 {
  76         wait_on_inode(inode);
  77         inode->i_lock = 1;
  78 }
  79 
  80 static inline void unlock_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  81 {
  82         inode->i_lock = 0;
  83         wake_up(&inode->i_wait);
  84 }
  85 
  86 static void write_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  87 {
  88         if (!inode->i_dirt)
  89                 return;
  90         lock_inode(inode);
  91         inode->i_dirt = 0;
  92         if (inode->i_dev && inode->i_sb &&
  93             inode->i_sb->s_op && inode->i_sb->s_op->write_inode)
  94                 inode->i_sb->s_op->write_inode(inode);
  95         unlock_inode(inode);
  96 }
  97 
  98 static void read_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  99 {
 100         lock_inode(inode);
 101         if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->read_inode)
 102                 inode->i_sb->s_op->read_inode(inode);
 103         unlock_inode(inode);
 104 }
 105 
 106 /*
 107  * bmap is needed for demand-loading and paging: if this function
 108  * doesn't exist for a filesystem, then those things are impossible:
 109  * executables cannot be run from the filesystem etc...
 110  *
 111  * This isn't as bad as it sounds: the read-routines might still work,
 112  * so the filesystem would be otherwise ok (for example, you might have
 113  * a DOS filesystem, which doesn't lend itself to bmap very well, but
 114  * you could still transfer files to/from the filesystem)
 115  */
 116 int bmap(struct inode * inode, int block)
     /* [previous][next][first][last][top][bottom][index][help] */
 117 {
 118         if (inode->i_op && inode->i_op->bmap)
 119                 return inode->i_op->bmap(inode,block);
 120         return 0;
 121 }
 122 
 123 void invalidate_inodes(dev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 124 {
 125         int i;
 126         struct inode * inode;
 127 
 128         inode = 0+inode_table;
 129         for(i=0 ; i<NR_INODE ; i++,inode++) {
 130                 wait_on_inode(inode);
 131                 if (inode->i_dev == dev) {
 132                         if (inode->i_count) {
 133                                 printk("inode in use on removed disk\n\r");
 134                                 continue;
 135                         }
 136                         inode->i_dev = inode->i_dirt = 0;
 137                 }
 138         }
 139 }
 140 
 141 void sync_inodes(dev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 142 {
 143         int i;
 144         struct inode * inode;
 145 
 146         inode = 0+inode_table;
 147         for(i=0 ; i<NR_INODE ; i++,inode++) {
 148                 wait_on_inode(inode);
 149                 if (inode->i_dirt)
 150                         write_inode(inode);
 151         }
 152 }
 153 
 154 void iput(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 155 {
 156         if (!inode)
 157                 return;
 158         wait_on_inode(inode);
 159         if (!inode->i_count) {
 160                 printk("iput: trying to free free inode\n");
 161                 printk("device %04x, inode %d, mode=%07o\n",inode->i_rdev,
 162                         inode->i_ino,inode->i_mode);
 163                 return;
 164         }
 165         if (inode->i_pipe) {
 166                 wake_up(&PIPE_READ_WAIT(*inode));
 167                 wake_up(&PIPE_WRITE_WAIT(*inode));
 168         }
 169 repeat:
 170         if (inode->i_count>1) {
 171                 inode->i_count--;
 172                 return;
 173         }
 174         if (inode->i_pipe) {
 175                 unsigned long page = (unsigned long) PIPE_BASE(*inode);
 176                 PIPE_BASE(*inode) = NULL;
 177                 free_page(page);
 178         }
 179         if (!inode->i_dev) {
 180                 inode->i_count--;
 181                 return;
 182         }
 183         if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->put_inode) {
 184                 inode->i_sb->s_op->put_inode(inode);
 185                 if (!inode->i_nlink)
 186                         return;
 187         }
 188         if (inode->i_dirt) {
 189                 write_inode(inode);     /* we can sleep - so do again */
 190                 wait_on_inode(inode);
 191                 goto repeat;
 192         }
 193         inode->i_count--;
 194         return;
 195 }
 196 
 197 struct inode * get_empty_inode(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 198 {
 199         struct inode * inode;
 200         static struct inode * last_inode = inode_table;
 201         int i;
 202 
 203         do {
 204                 inode = NULL;
 205                 for (i = NR_INODE; i ; i--) {
 206                         if (++last_inode >= inode_table + NR_INODE)
 207                                 last_inode = inode_table;
 208                         if (!last_inode->i_count) {
 209                                 inode = last_inode;
 210                                 if (!inode->i_dirt && !inode->i_lock)
 211                                         break;
 212                         }
 213                 }
 214                 if (!inode) {
 215                         for (i=0 ; i<NR_INODE ; i++)
 216                                 printk("(%04x: %d (%o)) ",inode_table[i].i_dev,
 217                                         inode_table[i].i_ino,inode_table[i].i_mode);
 218                         panic("No free inodes in mem");
 219                 }
 220                 wait_on_inode(inode);
 221                 while (inode->i_dirt) {
 222                         write_inode(inode);
 223                         wait_on_inode(inode);
 224                 }
 225         } while (inode->i_count);
 226         memset(inode,0,sizeof(*inode));
 227         inode->i_count = 1;
 228         return inode;
 229 }
 230 
 231 struct inode * get_pipe_inode(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 232 {
 233         struct inode * inode;
 234 
 235         if (!(inode = get_empty_inode()))
 236                 return NULL;
 237         if (!(PIPE_BASE(*inode) = (char *) get_free_page(GFP_USER))) {
 238                 inode->i_count = 0;
 239                 return NULL;
 240         }
 241         inode->i_count = 2;     /* sum of readers/writers */
 242         PIPE_READ_WAIT(*inode) = PIPE_WRITE_WAIT(*inode) = NULL;
 243         PIPE_HEAD(*inode) = PIPE_TAIL(*inode) = 0;
 244         PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
 245         inode->i_pipe = 1;
 246         return inode;
 247 }
 248 
 249 struct inode * iget(struct super_block * sb,int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 250 {
 251         struct inode * inode, * empty;
 252 
 253         if (!sb)
 254                 panic("iget with sb==NULL");
 255         empty = get_empty_inode();
 256         inode = inode_table;
 257         while (inode < NR_INODE+inode_table) {
 258                 if (inode->i_sb != sb || inode->i_ino != nr) {
 259                         inode++;
 260                         continue;
 261                 }
 262                 wait_on_inode(inode);
 263                 if (inode->i_sb != sb || inode->i_ino != nr) {
 264                         inode = inode_table;
 265                         continue;
 266                 }
 267                 inode->i_count++;
 268                 if (inode->i_mount) {
 269                         int i;
 270 
 271                         for (i = 0 ; i<NR_SUPER ; i++)
 272                                 if (super_block[i].s_covered==inode)
 273                                         break;
 274                         if (i >= NR_SUPER) {
 275                                 printk("Mounted inode hasn't got sb\n");
 276                                 if (empty)
 277                                         iput(empty);
 278                                 return inode;
 279                         }
 280                         iput(inode);
 281                         if (!(inode = super_block[i].s_mounted))
 282                                 printk("iget: mounted dev has no rootinode\n");
 283                         else {
 284                                 inode->i_count++;
 285                                 wait_on_inode(inode);
 286                         }
 287                 }
 288                 if (empty)
 289                         iput(empty);
 290                 return inode;
 291         }
 292         if (!empty)
 293                 return (NULL);
 294         inode = empty;
 295         inode->i_sb = sb;
 296         inode->i_dev = sb->s_dev;
 297         inode->i_ino = nr;
 298         inode->i_flags = sb->s_flags;
 299         read_inode(inode);
 300         return inode;
 301 }

/* [previous][next][first][last][top][bottom][index][help] */