root/fs/sysv/ialloc.c

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

DEFINITIONS

This source file includes following definitions.
  1. sv_sb_fic_inode
  2. sysv_free_inode
  3. sysv_new_inode
  4. sysv_count_free_inodes

   1 /*
   2  *  linux/fs/sysv/ialloc.c
   3  *
   4  *  minix/bitmap.c
   5  *  Copyright (C) 1991, 1992  Linus Torvalds
   6  *
   7  *  ext/freelists.c
   8  *  Copyright (C) 1992  Remy Card (card@masi.ibp.fr)
   9  *
  10  *  xenix/alloc.c
  11  *  Copyright (C) 1992  Doug Evans
  12  *
  13  *  coh/alloc.c
  14  *  Copyright (C) 1993  Pascal Haible, Bruno Haible
  15  *
  16  *  sysv/ialloc.c
  17  *  Copyright (C) 1993  Bruno Haible
  18  *
  19  *  This file contains code for allocating/freeing inodes.
  20  */
  21 
  22 #include <linux/sched.h>
  23 #include <linux/kernel.h>
  24 #include <linux/fs.h>
  25 #include <linux/sysv_fs.h>
  26 #include <linux/stddef.h>
  27 #include <linux/stat.h>
  28 #include <linux/string.h>
  29 #include <linux/locks.h>
  30 
  31 /* We don't trust the value of
  32    sb->sv_sbd2->s_tinode = *sb->sv_sb_total_free_inodes
  33    but we nevertheless keep it up to date. */
  34 
  35 /* An inode on disk is considered free if both i_mode == 0 and i_nlink == 0. */
  36 
  37 /* return &sb->sv_sb_fic_inodes[i] = &sbd->s_inode[i]; */
  38 static inline sysv_ino_t * sv_sb_fic_inode (struct super_block * sb, unsigned int i)
     /* [previous][next][first][last][top][bottom][index][help] */
  39 {
  40         if (sb->sv_bh1 == sb->sv_bh2)
  41                 return &sb->sv_sb_fic_inodes[i];
  42         else {
  43                 /* 512 byte Xenix FS */
  44                 unsigned int offset = offsetof(struct xenix_super_block, s_inode[i]);
  45                 if (offset < 512)
  46                         return (sysv_ino_t*)(sb->sv_sbd1 + offset);
  47                 else
  48                         return (sysv_ino_t*)(sb->sv_sbd2 + offset);
  49         }
  50 }
  51 
  52 void sysv_free_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  53 {
  54         struct super_block * sb;
  55         unsigned int ino;
  56         struct buffer_head * bh;
  57         struct sysv_inode * raw_inode;
  58 
  59         if (!inode)
  60                 return;
  61         if (!inode->i_dev) {
  62                 printk("sysv_free_inode: inode has no device\n");
  63                 return;
  64         }
  65         if (inode->i_count != 1) {
  66                 printk("sysv_free_inode: inode has count=%d\n", inode->i_count);
  67                 return;
  68         }
  69         if (inode->i_nlink) {
  70                 printk("sysv_free_inode: inode has nlink=%d\n", inode->i_nlink);
  71                 return;
  72         }
  73         if (!(sb = inode->i_sb)) {
  74                 printk("sysv_free_inode: inode on nonexistent device\n");
  75                 return;
  76         }
  77         ino = inode->i_ino;
  78         if (ino <= SYSV_ROOT_INO || ino > sb->sv_ninodes) {
  79                 printk("sysv_free_inode: inode 0,1,2 or nonexistent inode\n");
  80                 return;
  81         }
  82         if (!(bh = sv_bread(sb, inode->i_dev, sb->sv_firstinodezone + ((ino-1) >> sb->sv_inodes_per_block_bits)))) {
  83                 printk("sysv_free_inode: unable to read inode block on device %d/%d\n",MAJOR(inode->i_dev),MINOR(inode->i_dev));
  84                 clear_inode(inode);
  85                 return;
  86         }
  87         raw_inode = (struct sysv_inode *) bh->b_data + ((ino-1) & sb->sv_inodes_per_block_1);
  88         lock_super(sb);
  89         if (*sb->sv_sb_fic_count < sb->sv_fic_size)
  90                 *sv_sb_fic_inode(sb,(*sb->sv_sb_fic_count)++) = ino;
  91         (*sb->sv_sb_total_free_inodes)++;
  92         mark_buffer_dirty(sb->sv_bh1, 1); /* super-block has been modified */
  93         if (sb->sv_bh1 != sb->sv_bh2) mark_buffer_dirty(sb->sv_bh2, 1);
  94         sb->s_dirt = 1; /* and needs time stamp */
  95         memset(raw_inode, 0, sizeof(struct sysv_inode));
  96         mark_buffer_dirty(bh, 1);
  97         unlock_super(sb);
  98         brelse(bh);
  99         clear_inode(inode);
 100 }
 101 
 102 struct inode * sysv_new_inode(const struct inode * dir)
     /* [previous][next][first][last][top][bottom][index][help] */
 103 {
 104         struct inode * inode;
 105         struct super_block * sb;
 106         struct buffer_head * bh;
 107         struct sysv_inode * raw_inode;
 108         int i,j,ino,block;
 109 
 110         if (!dir || !(inode = get_empty_inode()))
 111                 return NULL;
 112         sb = dir->i_sb;
 113         inode->i_sb = sb;
 114         inode->i_flags = inode->i_sb->s_flags;
 115         lock_super(sb);         /* protect against task switches */
 116         if ((*sb->sv_sb_fic_count == 0)
 117             || (*sv_sb_fic_inode(sb,(*sb->sv_sb_fic_count)-1) == 0) /* Applies only to SystemV2 FS */
 118            ) {
 119                 /* Rebuild cache of free inodes: */
 120                 /* i : index into cache slot being filled            */
 121                 /* ino : inode we are trying                         */
 122                 /* block : firstinodezone + (ino-1)/inodes_per_block */
 123                 /* j : (ino-1)%inodes_per_block                      */
 124                 /* bh : buffer for block                             */
 125                 /* raw_inode : pointer to inode ino in the block     */
 126                 for (i = 0, ino = SYSV_ROOT_INO+1, block = sb->sv_firstinodezone, j = SYSV_ROOT_INO ; i < sb->sv_fic_size && block < sb->sv_firstdatazone ; block++, j = 0) {
 127                         if (!(bh = sv_bread(sb, sb->s_dev, block))) {
 128                                 printk("sysv_new_inode: unable to read inode table\n");
 129                                 break;  /* go with what we've got */
 130                                 /* FIXME: Perhaps try the next block? */
 131                         }
 132                         raw_inode = (struct sysv_inode *) bh->b_data + j;
 133                         for (; j < sb->sv_inodes_per_block && i < sb->sv_fic_size; ino++, j++, raw_inode++) {
 134                                 if (raw_inode->i_mode == 0 && raw_inode->i_nlink == 0)
 135                                         *sv_sb_fic_inode(sb,i++) = ino;
 136                         }
 137                         brelse(bh);
 138                 }
 139                 if (i == 0) {
 140                         iput(inode);
 141                         unlock_super(sb);
 142                         return NULL;    /* no inodes available */
 143                 }
 144                 *sb->sv_sb_fic_count = i;
 145         }
 146         /* Now *sb->sv_sb_fic_count > 0. */
 147         ino = *sv_sb_fic_inode(sb,--(*sb->sv_sb_fic_count));
 148         mark_buffer_dirty(sb->sv_bh1, 1); /* super-block has been modified */
 149         if (sb->sv_bh1 != sb->sv_bh2) mark_buffer_dirty(sb->sv_bh2, 1);
 150         sb->s_dirt = 1; /* and needs time stamp */
 151         inode->i_count = 1;
 152         inode->i_nlink = 1;
 153         inode->i_dev = sb->s_dev;
 154         inode->i_uid = current->fsuid;
 155         inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
 156         inode->i_dirt = 1;
 157         inode->i_ino = ino;
 158         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
 159         inode->i_op = NULL;
 160         inode->i_blocks = inode->i_blksize = 0;
 161         insert_inode_hash(inode);
 162         /* Change directory entry: */
 163         inode->i_mode = 0;              /* for sysv_write_inode() */
 164         inode->i_size = 0;              /* ditto */
 165         sysv_write_inode(inode);        /* ensure inode not allocated again */
 166                                         /* FIXME: caller may call this too. */
 167         inode->i_dirt = 1;              /* cleared by sysv_write_inode() */
 168         /* That's it. */
 169         (*sb->sv_sb_total_free_inodes)--;
 170         mark_buffer_dirty(sb->sv_bh2, 1); /* super-block has been modified again */
 171         sb->s_dirt = 1; /* and needs time stamp again */
 172         unlock_super(sb);
 173         return inode;
 174 }
 175 
 176 unsigned long sysv_count_free_inodes(struct super_block * sb)
     /* [previous][next][first][last][top][bottom][index][help] */
 177 {
 178 #if 1 /* test */
 179         struct buffer_head * bh;
 180         struct sysv_inode * raw_inode;
 181         int j,block,count;
 182 
 183         /* this causes a lot of disk traffic ... */
 184         count = 0;
 185         lock_super(sb);
 186         /* i : index into cache slot being filled            */
 187         /* ino : inode we are trying                         */
 188         /* block : firstinodezone + (ino-1)/inodes_per_block */
 189         /* j : (ino-1)%inodes_per_block                      */
 190         /* bh : buffer for block                             */
 191         /* raw_inode : pointer to inode ino in the block     */
 192         for (block = sb->sv_firstinodezone, j = SYSV_ROOT_INO ; block < sb->sv_firstdatazone ; block++, j = 0) {
 193                 if (!(bh = sv_bread(sb, sb->s_dev, block))) {
 194                         printk("sysv_count_free_inodes: unable to read inode table\n");
 195                         break;  /* go with what we've got */
 196                         /* FIXME: Perhaps try the next block? */
 197                 }
 198                 raw_inode = (struct sysv_inode *) bh->b_data + j;
 199                 for (; j < sb->sv_inodes_per_block ; j++, raw_inode++)
 200                         if (raw_inode->i_mode == 0 && raw_inode->i_nlink == 0)
 201                                 count++;
 202                 brelse(bh);
 203         }
 204         if (count != *sb->sv_sb_total_free_inodes) {
 205                 printk("sysv_count_free_inodes: free inode count was %d, correcting to %d\n",(short)(*sb->sv_sb_total_free_inodes),count);
 206                 if (!(sb->s_flags & MS_RDONLY)) {
 207                         *sb->sv_sb_total_free_inodes = count;
 208                         mark_buffer_dirty(sb->sv_bh2, 1); /* super-block has been modified */
 209                         sb->s_dirt = 1; /* and needs time stamp */
 210                 }
 211         }
 212         unlock_super(sb);
 213         return count;
 214 #else
 215         return *sb->sv_sb_total_free_inodes;
 216 #endif
 217 }
 218 

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