FRV: Use generic show_interrupts()
[cris-mirror.git] / fs / ufs / ialloc.c
blob2eabf04af3de12e98d0fbf812879825e7247e619
1 /*
2 * linux/fs/ufs/ialloc.c
4 * Copyright (c) 1998
5 * Daniel Pirkl <daniel.pirkl@email.cz>
6 * Charles University, Faculty of Mathematics and Physics
8 * from
10 * linux/fs/ext2/ialloc.c
12 * Copyright (C) 1992, 1993, 1994, 1995
13 * Remy Card (card@masi.ibp.fr)
14 * Laboratoire MASI - Institut Blaise Pascal
15 * Universite Pierre et Marie Curie (Paris VI)
17 * BSD ufs-inspired inode and directory allocation by
18 * Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
19 * Big-endian to little-endian byte-swapping/bitmaps by
20 * David S. Miller (davem@caip.rutgers.edu), 1995
22 * UFS2 write support added by
23 * Evgeniy Dushistov <dushistov@mail.ru>, 2007
26 #include <linux/fs.h>
27 #include <linux/time.h>
28 #include <linux/stat.h>
29 #include <linux/string.h>
30 #include <linux/buffer_head.h>
31 #include <linux/sched.h>
32 #include <linux/bitops.h>
33 #include <asm/byteorder.h>
35 #include "ufs_fs.h"
36 #include "ufs.h"
37 #include "swab.h"
38 #include "util.h"
41 * NOTE! When we get the inode, we're the only people
42 * that have access to it, and as such there are no
43 * race conditions we have to worry about. The inode
44 * is not on the hash-lists, and it cannot be reached
45 * through the filesystem because the directory entry
46 * has been deleted earlier.
48 * HOWEVER: we must make sure that we get no aliases,
49 * which means that we have to call "clear_inode()"
50 * _before_ we mark the inode not in use in the inode
51 * bitmaps. Otherwise a newly created file might use
52 * the same inode number (not actually the same pointer
53 * though), and then we'd have two inodes sharing the
54 * same inode number and space on the harddisk.
56 void ufs_free_inode (struct inode * inode)
58 struct super_block * sb;
59 struct ufs_sb_private_info * uspi;
60 struct ufs_super_block_first * usb1;
61 struct ufs_cg_private_info * ucpi;
62 struct ufs_cylinder_group * ucg;
63 int is_directory;
64 unsigned ino, cg, bit;
66 UFSD("ENTER, ino %lu\n", inode->i_ino);
68 sb = inode->i_sb;
69 uspi = UFS_SB(sb)->s_uspi;
70 usb1 = ubh_get_usb_first(uspi);
72 ino = inode->i_ino;
74 lock_super (sb);
76 if (!((ino > 1) && (ino < (uspi->s_ncg * uspi->s_ipg )))) {
77 ufs_warning(sb, "ufs_free_inode", "reserved inode or nonexistent inode %u\n", ino);
78 unlock_super (sb);
79 return;
82 cg = ufs_inotocg (ino);
83 bit = ufs_inotocgoff (ino);
84 ucpi = ufs_load_cylinder (sb, cg);
85 if (!ucpi) {
86 unlock_super (sb);
87 return;
89 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
90 if (!ufs_cg_chkmagic(sb, ucg))
91 ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number");
93 ucg->cg_time = cpu_to_fs32(sb, get_seconds());
95 is_directory = S_ISDIR(inode->i_mode);
97 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
98 ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino);
99 else {
100 ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
101 if (ino < ucpi->c_irotor)
102 ucpi->c_irotor = ino;
103 fs32_add(sb, &ucg->cg_cs.cs_nifree, 1);
104 uspi->cs_total.cs_nifree++;
105 fs32_add(sb, &UFS_SB(sb)->fs_cs(cg).cs_nifree, 1);
107 if (is_directory) {
108 fs32_sub(sb, &ucg->cg_cs.cs_ndir, 1);
109 uspi->cs_total.cs_ndir--;
110 fs32_sub(sb, &UFS_SB(sb)->fs_cs(cg).cs_ndir, 1);
114 ubh_mark_buffer_dirty (USPI_UBH(uspi));
115 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
116 if (sb->s_flags & MS_SYNCHRONOUS)
117 ubh_sync_block(UCPI_UBH(ucpi));
119 sb->s_dirt = 1;
120 unlock_super (sb);
121 UFSD("EXIT\n");
125 * Nullify new chunk of inodes,
126 * BSD people also set ui_gen field of inode
127 * during nullification, but we not care about
128 * that because of linux ufs do not support NFS
130 static void ufs2_init_inodes_chunk(struct super_block *sb,
131 struct ufs_cg_private_info *ucpi,
132 struct ufs_cylinder_group *ucg)
134 struct buffer_head *bh;
135 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
136 sector_t beg = uspi->s_sbbase +
137 ufs_inotofsba(ucpi->c_cgx * uspi->s_ipg +
138 fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk));
139 sector_t end = beg + uspi->s_fpb;
141 UFSD("ENTER cgno %d\n", ucpi->c_cgx);
143 for (; beg < end; ++beg) {
144 bh = sb_getblk(sb, beg);
145 lock_buffer(bh);
146 memset(bh->b_data, 0, sb->s_blocksize);
147 set_buffer_uptodate(bh);
148 mark_buffer_dirty(bh);
149 unlock_buffer(bh);
150 if (sb->s_flags & MS_SYNCHRONOUS)
151 sync_dirty_buffer(bh);
152 brelse(bh);
155 fs32_add(sb, &ucg->cg_u.cg_u2.cg_initediblk, uspi->s_inopb);
156 ubh_mark_buffer_dirty(UCPI_UBH(ucpi));
157 if (sb->s_flags & MS_SYNCHRONOUS)
158 ubh_sync_block(UCPI_UBH(ucpi));
160 UFSD("EXIT\n");
164 * There are two policies for allocating an inode. If the new inode is
165 * a directory, then a forward search is made for a block group with both
166 * free space and a low directory-to-inode ratio; if that fails, then of
167 * the groups with above-average free space, that group with the fewest
168 * directories already is chosen.
170 * For other inodes, search forward from the parent directory's block
171 * group to find a free inode.
173 struct inode * ufs_new_inode(struct inode * dir, int mode)
175 struct super_block * sb;
176 struct ufs_sb_info * sbi;
177 struct ufs_sb_private_info * uspi;
178 struct ufs_super_block_first * usb1;
179 struct ufs_cg_private_info * ucpi;
180 struct ufs_cylinder_group * ucg;
181 struct inode * inode;
182 unsigned cg, bit, i, j, start;
183 struct ufs_inode_info *ufsi;
184 int err = -ENOSPC;
186 UFSD("ENTER\n");
188 /* Cannot create files in a deleted directory */
189 if (!dir || !dir->i_nlink)
190 return ERR_PTR(-EPERM);
191 sb = dir->i_sb;
192 inode = new_inode(sb);
193 if (!inode)
194 return ERR_PTR(-ENOMEM);
195 ufsi = UFS_I(inode);
196 sbi = UFS_SB(sb);
197 uspi = sbi->s_uspi;
198 usb1 = ubh_get_usb_first(uspi);
200 lock_super (sb);
203 * Try to place the inode in its parent directory
205 i = ufs_inotocg(dir->i_ino);
206 if (sbi->fs_cs(i).cs_nifree) {
207 cg = i;
208 goto cg_found;
212 * Use a quadratic hash to find a group with a free inode
214 for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
215 i += j;
216 if (i >= uspi->s_ncg)
217 i -= uspi->s_ncg;
218 if (sbi->fs_cs(i).cs_nifree) {
219 cg = i;
220 goto cg_found;
225 * That failed: try linear search for a free inode
227 i = ufs_inotocg(dir->i_ino) + 1;
228 for (j = 2; j < uspi->s_ncg; j++) {
229 i++;
230 if (i >= uspi->s_ncg)
231 i = 0;
232 if (sbi->fs_cs(i).cs_nifree) {
233 cg = i;
234 goto cg_found;
238 goto failed;
240 cg_found:
241 ucpi = ufs_load_cylinder (sb, cg);
242 if (!ucpi) {
243 err = -EIO;
244 goto failed;
246 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
247 if (!ufs_cg_chkmagic(sb, ucg))
248 ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
250 start = ucpi->c_irotor;
251 bit = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, uspi->s_ipg, start);
252 if (!(bit < uspi->s_ipg)) {
253 bit = ubh_find_first_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, start);
254 if (!(bit < start)) {
255 ufs_error (sb, "ufs_new_inode",
256 "cylinder group %u corrupted - error in inode bitmap\n", cg);
257 err = -EIO;
258 goto failed;
261 UFSD("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg);
262 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
263 ubh_setbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
264 else {
265 ufs_panic (sb, "ufs_new_inode", "internal error");
266 err = -EIO;
267 goto failed;
270 if (uspi->fs_magic == UFS2_MAGIC) {
271 u32 initediblk = fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk);
273 if (bit + uspi->s_inopb > initediblk &&
274 initediblk < fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_niblk))
275 ufs2_init_inodes_chunk(sb, ucpi, ucg);
278 fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1);
279 uspi->cs_total.cs_nifree--;
280 fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1);
282 if (S_ISDIR(mode)) {
283 fs32_add(sb, &ucg->cg_cs.cs_ndir, 1);
284 uspi->cs_total.cs_ndir++;
285 fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1);
287 ubh_mark_buffer_dirty (USPI_UBH(uspi));
288 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
289 if (sb->s_flags & MS_SYNCHRONOUS)
290 ubh_sync_block(UCPI_UBH(ucpi));
291 sb->s_dirt = 1;
293 inode->i_ino = cg * uspi->s_ipg + bit;
294 inode_init_owner(inode, dir, mode);
295 inode->i_blocks = 0;
296 inode->i_generation = 0;
297 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
298 ufsi->i_flags = UFS_I(dir)->i_flags;
299 ufsi->i_lastfrag = 0;
300 ufsi->i_shadow = 0;
301 ufsi->i_osync = 0;
302 ufsi->i_oeftflag = 0;
303 ufsi->i_dir_start_lookup = 0;
304 memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1));
305 insert_inode_hash(inode);
306 mark_inode_dirty(inode);
308 if (uspi->fs_magic == UFS2_MAGIC) {
309 struct buffer_head *bh;
310 struct ufs2_inode *ufs2_inode;
313 * setup birth date, we do it here because of there is no sense
314 * to hold it in struct ufs_inode_info, and lose 64 bit
316 bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
317 if (!bh) {
318 ufs_warning(sb, "ufs_read_inode",
319 "unable to read inode %lu\n",
320 inode->i_ino);
321 err = -EIO;
322 goto fail_remove_inode;
324 lock_buffer(bh);
325 ufs2_inode = (struct ufs2_inode *)bh->b_data;
326 ufs2_inode += ufs_inotofsbo(inode->i_ino);
327 ufs2_inode->ui_birthtime = cpu_to_fs64(sb, CURRENT_TIME.tv_sec);
328 ufs2_inode->ui_birthnsec = cpu_to_fs32(sb, CURRENT_TIME.tv_nsec);
329 mark_buffer_dirty(bh);
330 unlock_buffer(bh);
331 if (sb->s_flags & MS_SYNCHRONOUS)
332 sync_dirty_buffer(bh);
333 brelse(bh);
336 unlock_super (sb);
338 UFSD("allocating inode %lu\n", inode->i_ino);
339 UFSD("EXIT\n");
340 return inode;
342 fail_remove_inode:
343 unlock_super(sb);
344 inode->i_nlink = 0;
345 iput(inode);
346 UFSD("EXIT (FAILED): err %d\n", err);
347 return ERR_PTR(err);
348 failed:
349 unlock_super (sb);
350 make_bad_inode(inode);
351 iput (inode);
352 UFSD("EXIT (FAILED): err %d\n", err);
353 return ERR_PTR(err);