blob: fcc498ec9b337afdcb96582fce07734c9a3cb46d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/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>
Lubomir Rintel46c23d72010-06-29 15:05:38 -070028#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "sysv.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]; */
38static inline sysv_ino_t *
39sv_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 /* 512 byte Xenix FS */
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
55struct sysv_inode *
56sysv_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
70static 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);
96out:
97 return i;
98}
99
100void 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 clear_inode(inode);
117 if (!raw_inode) {
118 printk("sysv_free_inode: unable to read inode block on device "
119 "%s\n", inode->i_sb->s_id);
120 return;
121 }
122 lock_super(sb);
123 count = fs16_to_cpu(sbi, *sbi->s_sb_fic_count);
124 if (count < sbi->s_fic_size) {
125 *sv_sb_fic_inode(sb,count++) = cpu_to_fs16(sbi, ino);
126 *sbi->s_sb_fic_count = cpu_to_fs16(sbi, count);
127 }
128 fs16_add(sbi, sbi->s_sb_total_free_inodes, 1);
129 dirty_sb(sb);
130 memset(raw_inode, 0, sizeof(struct sysv_inode));
131 mark_buffer_dirty(bh);
132 unlock_super(sb);
133 brelse(bh);
134}
135
136struct inode * sysv_new_inode(const struct inode * dir, mode_t mode)
137{
138 struct super_block *sb = dir->i_sb;
139 struct sysv_sb_info *sbi = SYSV_SB(sb);
140 struct inode *inode;
141 sysv_ino_t ino;
142 unsigned count;
Lubomir Rintel46c23d72010-06-29 15:05:38 -0700143 struct writeback_control wbc = {
144 .sync_mode = WB_SYNC_NONE
145 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 inode = new_inode(sb);
148 if (!inode)
149 return ERR_PTR(-ENOMEM);
150
151 lock_super(sb);
152 count = fs16_to_cpu(sbi, *sbi->s_sb_fic_count);
153 if (count == 0 || (*sv_sb_fic_inode(sb,count-1) == 0)) {
154 count = refill_free_cache(sb);
155 if (count == 0) {
156 iput(inode);
157 unlock_super(sb);
158 return ERR_PTR(-ENOSPC);
159 }
160 }
161 /* Now count > 0. */
162 ino = *sv_sb_fic_inode(sb,--count);
163 *sbi->s_sb_fic_count = cpu_to_fs16(sbi, count);
164 fs16_add(sbi, sbi->s_sb_total_free_inodes, -1);
165 dirty_sb(sb);
Dmitry Monakhov85640bd2010-03-04 17:32:20 +0300166 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 inode->i_ino = fs16_to_cpu(sbi, ino);
168 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
Theodore Ts'oba52de12006-09-27 01:50:49 -0700169 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 memset(SYSV_I(inode)->i_data, 0, sizeof(SYSV_I(inode)->i_data));
171 SYSV_I(inode)->i_dir_start_lookup = 0;
172 insert_inode_hash(inode);
173 mark_inode_dirty(inode);
174
Lubomir Rintel46c23d72010-06-29 15:05:38 -0700175 sysv_write_inode(inode, &wbc); /* ensure inode not allocated again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 mark_inode_dirty(inode); /* cleared by sysv_write_inode() */
177 /* That's it. */
178 unlock_super(sb);
179 return inode;
180}
181
182unsigned long sysv_count_free_inodes(struct super_block * sb)
183{
184 struct sysv_sb_info *sbi = SYSV_SB(sb);
185 struct buffer_head * bh;
186 struct sysv_inode * raw_inode;
187 int ino, count, sb_count;
188
189 lock_super(sb);
190
191 sb_count = fs16_to_cpu(sbi, *sbi->s_sb_total_free_inodes);
192
193 if (0)
194 goto trust_sb;
195
196 /* this causes a lot of disk traffic ... */
197 count = 0;
198 ino = SYSV_ROOT_INO+1;
199 raw_inode = sysv_raw_inode(sb, ino, &bh);
200 if (!raw_inode)
201 goto Eio;
202 while (ino <= sbi->s_ninodes) {
203 if (raw_inode->i_mode == 0 && raw_inode->i_nlink == 0)
204 count++;
205 if ((ino++ & sbi->s_inodes_per_block_1) == 0) {
206 brelse(bh);
207 raw_inode = sysv_raw_inode(sb, ino, &bh);
208 if (!raw_inode)
209 goto Eio;
210 } else
211 raw_inode++;
212 }
213 brelse(bh);
214 if (count != sb_count)
215 goto Einval;
216out:
217 unlock_super(sb);
218 return count;
219
220Einval:
221 printk("sysv_count_free_inodes: "
222 "free inode count was %d, correcting to %d\n",
223 sb_count, count);
224 if (!(sb->s_flags & MS_RDONLY)) {
225 *sbi->s_sb_total_free_inodes = cpu_to_fs16(SYSV_SB(sb), count);
226 dirty_sb(sb);
227 }
228 goto out;
229
230Eio:
231 printk("sysv_count_free_inodes: unable to read inode table\n");
232trust_sb:
233 count = sb_count;
234 goto out;
235}