| 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 ([email protected])
|
| 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/kernel.h>
|
| 23 | #include <linux/stddef.h>
|
| 24 | #include <linux/sched.h>
|
| 25 | #include <linux/stat.h>
|
| 26 | #include <linux/string.h>
|
| 27 | #include <linux/buffer_head.h>
|
| 28 | #include <linux/writeback.h>
|
| 29 | #include "sysv.h"
|
| 30 |
|
| 31 | |
| 32 | sb->sv_sbd2->s_tinode = *sb->sv_sb_total_free_inodes
|
| 33 | but we nevertheless keep it up to date.
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 | static inline sysv_ino_t *
|
| 39 | sv_sb_fic_inode(struct super_block * sb, unsigned int i)
|
| 40 | {
|
| 41 | struct sysv_sb_info *sbi = SYSV_SB(sb);
|
| 42 |
|
| 43 | if (sbi->s_bh1 == sbi->s_bh2)
|
| 44 | return &sbi->s_sb_fic_inodes[i];
|
| 45 | else {
|
| 46 |
|
| 47 | unsigned int offset = offsetof(struct xenix_super_block, s_inode[i]);
|
| 48 | if (offset < 512)
|
| 49 | return (sysv_ino_t*)(sbi->s_sbd1 + offset);
|
| 50 | else
|
| 51 | return (sysv_ino_t*)(sbi->s_sbd2 + offset);
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | struct sysv_inode *
|
| 56 | sysv_raw_inode(struct super_block *sb, unsigned ino, struct buffer_head **bh)
|
| 57 | {
|
| 58 | struct sysv_sb_info *sbi = SYSV_SB(sb);
|
| 59 | struct sysv_inode *res;
|
| 60 | int block = sbi->s_firstinodezone + sbi->s_block_base;
|
| 61 |
|
| 62 | block += (ino-1) >> sbi->s_inodes_per_block_bits;
|
| 63 | *bh = sb_bread(sb, block);
|
| 64 | if (!*bh)
|
| 65 | return NULL;
|
| 66 | res = (struct sysv_inode *)(*bh)->b_data;
|
| 67 | return res + ((ino-1) & sbi->s_inodes_per_block_1);
|
| 68 | }
|
| 69 |
|
| 70 | static int refill_free_cache(struct super_block *sb)
|
| 71 | {
|
| 72 | struct sysv_sb_info *sbi = SYSV_SB(sb);
|
| 73 | struct buffer_head * bh;
|
| 74 | struct sysv_inode * raw_inode;
|
| 75 | int i = 0, ino;
|
| 76 |
|
| 77 | ino = SYSV_ROOT_INO+1;
|
| 78 | raw_inode = sysv_raw_inode(sb, ino, &bh);
|
| 79 | if (!raw_inode)
|
| 80 | goto out;
|
| 81 | while (ino <= sbi->s_ninodes) {
|
| 82 | if (raw_inode->i_mode == 0 && raw_inode->i_nlink == 0) {
|
| 83 | *sv_sb_fic_inode(sb,i++) = cpu_to_fs16(SYSV_SB(sb), ino);
|
| 84 | if (i == sbi->s_fic_size)
|
| 85 | break;
|
| 86 | }
|
| 87 | if ((ino++ & sbi->s_inodes_per_block_1) == 0) {
|
| 88 | brelse(bh);
|
| 89 | raw_inode = sysv_raw_inode(sb, ino, &bh);
|
| 90 | if (!raw_inode)
|
| 91 | goto out;
|
| 92 | } else
|
| 93 | raw_inode++;
|
| 94 | }
|
| 95 | brelse(bh);
|
| 96 | out:
|
| 97 | return i;
|
| 98 | }
|
| 99 |
|
| 100 | void sysv_free_inode(struct inode * inode)
|
| 101 | {
|
| 102 | struct super_block *sb = inode->i_sb;
|
| 103 | struct sysv_sb_info *sbi = SYSV_SB(sb);
|
| 104 | unsigned int ino;
|
| 105 | struct buffer_head * bh;
|
| 106 | struct sysv_inode * raw_inode;
|
| 107 | unsigned count;
|
| 108 |
|
| 109 | sb = inode->i_sb;
|
| 110 | ino = inode->i_ino;
|
| 111 | if (ino <= SYSV_ROOT_INO || ino > sbi->s_ninodes) {
|
| 112 | printk("sysv_free_inode: inode 0,1,2 or nonexistent inode\n");
|
| 113 | return;
|
| 114 | }
|
| 115 | raw_inode = sysv_raw_inode(sb, ino, &bh);
|
| 116 | if (!raw_inode) {
|
| 117 | printk("sysv_free_inode: unable to read inode block on device "
|
| 118 | "%s\n", inode->i_sb->s_id);
|
| 119 | return;
|
| 120 | }
|
| 121 | mutex_lock(&sbi->s_lock);
|
| 122 | count = fs16_to_cpu(sbi, *sbi->s_sb_fic_count);
|
| 123 | if (count < sbi->s_fic_size) {
|
| 124 | *sv_sb_fic_inode(sb,count++) = cpu_to_fs16(sbi, ino);
|
| 125 | *sbi->s_sb_fic_count = cpu_to_fs16(sbi, count);
|
| 126 | }
|
| 127 | fs16_add(sbi, sbi->s_sb_total_free_inodes, 1);
|
| 128 | dirty_sb(sb);
|
| 129 | memset(raw_inode, 0, sizeof(struct sysv_inode));
|
| 130 | mark_buffer_dirty(bh);
|
| 131 | mutex_unlock(&sbi->s_lock);
|
| 132 | brelse(bh);
|
| 133 | }
|
| 134 |
|
| 135 | struct inode * sysv_new_inode(const struct inode * dir, umode_t mode)
|
| 136 | {
|
| 137 | struct super_block *sb = dir->i_sb;
|
| 138 | struct sysv_sb_info *sbi = SYSV_SB(sb);
|
| 139 | struct inode *inode;
|
| 140 | sysv_ino_t ino;
|
| 141 | unsigned count;
|
| 142 | struct writeback_control wbc = {
|
| 143 | .sync_mode = WB_SYNC_NONE
|
| 144 | };
|
| 145 |
|
| 146 | inode = new_inode(sb);
|
| 147 | if (!inode)
|
| 148 | return ERR_PTR(-ENOMEM);
|
| 149 |
|
| 150 | mutex_lock(&sbi->s_lock);
|
| 151 | count = fs16_to_cpu(sbi, *sbi->s_sb_fic_count);
|
| 152 | if (count == 0 || (*sv_sb_fic_inode(sb,count-1) == 0)) {
|
| 153 | count = refill_free_cache(sb);
|
| 154 | if (count == 0) {
|
| 155 | iput(inode);
|
| 156 | mutex_unlock(&sbi->s_lock);
|
| 157 | return ERR_PTR(-ENOSPC);
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | ino = *sv_sb_fic_inode(sb,--count);
|
| 162 | *sbi->s_sb_fic_count = cpu_to_fs16(sbi, count);
|
| 163 | fs16_add(sbi, sbi->s_sb_total_free_inodes, -1);
|
| 164 | dirty_sb(sb);
|
| 165 | inode_init_owner(inode, dir, mode);
|
| 166 | inode->i_ino = fs16_to_cpu(sbi, ino);
|
| 167 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
|
| 168 | inode->i_blocks = 0;
|
| 169 | memset(SYSV_I(inode)->i_data, 0, sizeof(SYSV_I(inode)->i_data));
|
| 170 | SYSV_I(inode)->i_dir_start_lookup = 0;
|
| 171 | insert_inode_hash(inode);
|
| 172 | mark_inode_dirty(inode);
|
| 173 |
|
| 174 | sysv_write_inode(inode, &wbc);
|
| 175 | mark_inode_dirty(inode);
|
| 176 |
|
| 177 | mutex_unlock(&sbi->s_lock);
|
| 178 | return inode;
|
| 179 | }
|
| 180 |
|
| 181 | unsigned long sysv_count_free_inodes(struct super_block * sb)
|
| 182 | {
|
| 183 | struct sysv_sb_info *sbi = SYSV_SB(sb);
|
| 184 | struct buffer_head * bh;
|
| 185 | struct sysv_inode * raw_inode;
|
| 186 | int ino, count, sb_count;
|
| 187 |
|
| 188 | mutex_lock(&sbi->s_lock);
|
| 189 |
|
| 190 | sb_count = fs16_to_cpu(sbi, *sbi->s_sb_total_free_inodes);
|
| 191 |
|
| 192 | if (0)
|
| 193 | goto trust_sb;
|
| 194 |
|
| 195 |
|
| 196 | count = 0;
|
| 197 | ino = SYSV_ROOT_INO+1;
|
| 198 | raw_inode = sysv_raw_inode(sb, ino, &bh);
|
| 199 | if (!raw_inode)
|
| 200 | goto Eio;
|
| 201 | while (ino <= sbi->s_ninodes) {
|
| 202 | if (raw_inode->i_mode == 0 && raw_inode->i_nlink == 0)
|
| 203 | count++;
|
| 204 | if ((ino++ & sbi->s_inodes_per_block_1) == 0) {
|
| 205 | brelse(bh);
|
| 206 | raw_inode = sysv_raw_inode(sb, ino, &bh);
|
| 207 | if (!raw_inode)
|
| 208 | goto Eio;
|
| 209 | } else
|
| 210 | raw_inode++;
|
| 211 | }
|
| 212 | brelse(bh);
|
| 213 | if (count != sb_count)
|
| 214 | goto Einval;
|
| 215 | out:
|
| 216 | mutex_unlock(&sbi->s_lock);
|
| 217 | return count;
|
| 218 |
|
| 219 | Einval:
|
| 220 | printk("sysv_count_free_inodes: "
|
| 221 | "free inode count was %d, correcting to %d\n",
|
| 222 | sb_count, count);
|
| 223 | if (!(sb->s_flags & MS_RDONLY)) {
|
| 224 | *sbi->s_sb_total_free_inodes = cpu_to_fs16(SYSV_SB(sb), count);
|
| 225 | dirty_sb(sb);
|
| 226 | }
|
| 227 | goto out;
|
| 228 |
|
| 229 | Eio:
|
| 230 | printk("sysv_count_free_inodes: unable to read inode table\n");
|
| 231 | trust_sb:
|
| 232 | count = sb_count;
|
| 233 | goto out;
|
| 234 | }
|