[PATCH] ext3 balloc: fix _with_rsv freeze
[linux-2.6.22.y-op.git] / fs / ufs / ialloc.c
blob2ad1259c6ecaacd7f6130859c42cb8654fb0e991
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
23 #include <linux/fs.h>
24 #include <linux/ufs_fs.h>
25 #include <linux/time.h>
26 #include <linux/stat.h>
27 #include <linux/string.h>
28 #include <linux/quotaops.h>
29 #include <linux/buffer_head.h>
30 #include <linux/sched.h>
31 #include <linux/bitops.h>
32 #include <asm/byteorder.h>
34 #include "swab.h"
35 #include "util.h"
38 * NOTE! When we get the inode, we're the only people
39 * that have access to it, and as such there are no
40 * race conditions we have to worry about. The inode
41 * is not on the hash-lists, and it cannot be reached
42 * through the filesystem because the directory entry
43 * has been deleted earlier.
45 * HOWEVER: we must make sure that we get no aliases,
46 * which means that we have to call "clear_inode()"
47 * _before_ we mark the inode not in use in the inode
48 * bitmaps. Otherwise a newly created file might use
49 * the same inode number (not actually the same pointer
50 * though), and then we'd have two inodes sharing the
51 * same inode number and space on the harddisk.
53 void ufs_free_inode (struct inode * inode)
55 struct super_block * sb;
56 struct ufs_sb_private_info * uspi;
57 struct ufs_super_block_first * usb1;
58 struct ufs_cg_private_info * ucpi;
59 struct ufs_cylinder_group * ucg;
60 int is_directory;
61 unsigned ino, cg, bit;
63 UFSD("ENTER, ino %lu\n", inode->i_ino);
65 sb = inode->i_sb;
66 uspi = UFS_SB(sb)->s_uspi;
67 usb1 = ubh_get_usb_first(uspi);
69 ino = inode->i_ino;
71 lock_super (sb);
73 if (!((ino > 1) && (ino < (uspi->s_ncg * uspi->s_ipg )))) {
74 ufs_warning(sb, "ufs_free_inode", "reserved inode or nonexistent inode %u\n", ino);
75 unlock_super (sb);
76 return;
79 cg = ufs_inotocg (ino);
80 bit = ufs_inotocgoff (ino);
81 ucpi = ufs_load_cylinder (sb, cg);
82 if (!ucpi) {
83 unlock_super (sb);
84 return;
86 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
87 if (!ufs_cg_chkmagic(sb, ucg))
88 ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number");
90 ucg->cg_time = cpu_to_fs32(sb, get_seconds());
92 is_directory = S_ISDIR(inode->i_mode);
94 DQUOT_FREE_INODE(inode);
95 DQUOT_DROP(inode);
97 clear_inode (inode);
99 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
100 ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino);
101 else {
102 ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
103 if (ino < ucpi->c_irotor)
104 ucpi->c_irotor = ino;
105 fs32_add(sb, &ucg->cg_cs.cs_nifree, 1);
106 uspi->cs_total.cs_nifree++;
107 fs32_add(sb, &UFS_SB(sb)->fs_cs(cg).cs_nifree, 1);
109 if (is_directory) {
110 fs32_sub(sb, &ucg->cg_cs.cs_ndir, 1);
111 uspi->cs_total.cs_ndir--;
112 fs32_sub(sb, &UFS_SB(sb)->fs_cs(cg).cs_ndir, 1);
116 ubh_mark_buffer_dirty (USPI_UBH(uspi));
117 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
118 if (sb->s_flags & MS_SYNCHRONOUS) {
119 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
120 ubh_wait_on_buffer (UCPI_UBH(ucpi));
123 sb->s_dirt = 1;
124 unlock_super (sb);
125 UFSD("EXIT\n");
129 * There are two policies for allocating an inode. If the new inode is
130 * a directory, then a forward search is made for a block group with both
131 * free space and a low directory-to-inode ratio; if that fails, then of
132 * the groups with above-average free space, that group with the fewest
133 * directories already is chosen.
135 * For other inodes, search forward from the parent directory's block
136 * group to find a free inode.
138 struct inode * ufs_new_inode(struct inode * dir, int mode)
140 struct super_block * sb;
141 struct ufs_sb_info * sbi;
142 struct ufs_sb_private_info * uspi;
143 struct ufs_super_block_first * usb1;
144 struct ufs_cg_private_info * ucpi;
145 struct ufs_cylinder_group * ucg;
146 struct inode * inode;
147 unsigned cg, bit, i, j, start;
148 struct ufs_inode_info *ufsi;
150 UFSD("ENTER\n");
152 /* Cannot create files in a deleted directory */
153 if (!dir || !dir->i_nlink)
154 return ERR_PTR(-EPERM);
155 sb = dir->i_sb;
156 inode = new_inode(sb);
157 if (!inode)
158 return ERR_PTR(-ENOMEM);
159 ufsi = UFS_I(inode);
160 sbi = UFS_SB(sb);
161 uspi = sbi->s_uspi;
162 usb1 = ubh_get_usb_first(uspi);
164 lock_super (sb);
167 * Try to place the inode in its parent directory
169 i = ufs_inotocg(dir->i_ino);
170 if (sbi->fs_cs(i).cs_nifree) {
171 cg = i;
172 goto cg_found;
176 * Use a quadratic hash to find a group with a free inode
178 for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
179 i += j;
180 if (i >= uspi->s_ncg)
181 i -= uspi->s_ncg;
182 if (sbi->fs_cs(i).cs_nifree) {
183 cg = i;
184 goto cg_found;
189 * That failed: try linear search for a free inode
191 i = ufs_inotocg(dir->i_ino) + 1;
192 for (j = 2; j < uspi->s_ncg; j++) {
193 i++;
194 if (i >= uspi->s_ncg)
195 i = 0;
196 if (sbi->fs_cs(i).cs_nifree) {
197 cg = i;
198 goto cg_found;
202 goto failed;
204 cg_found:
205 ucpi = ufs_load_cylinder (sb, cg);
206 if (!ucpi)
207 goto failed;
208 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
209 if (!ufs_cg_chkmagic(sb, ucg))
210 ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
212 start = ucpi->c_irotor;
213 bit = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, uspi->s_ipg, start);
214 if (!(bit < uspi->s_ipg)) {
215 bit = ubh_find_first_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, start);
216 if (!(bit < start)) {
217 ufs_error (sb, "ufs_new_inode",
218 "cylinder group %u corrupted - error in inode bitmap\n", cg);
219 goto failed;
222 UFSD("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg);
223 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
224 ubh_setbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
225 else {
226 ufs_panic (sb, "ufs_new_inode", "internal error");
227 goto failed;
230 fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1);
231 uspi->cs_total.cs_nifree--;
232 fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1);
234 if (S_ISDIR(mode)) {
235 fs32_add(sb, &ucg->cg_cs.cs_ndir, 1);
236 uspi->cs_total.cs_ndir++;
237 fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1);
240 ubh_mark_buffer_dirty (USPI_UBH(uspi));
241 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
242 if (sb->s_flags & MS_SYNCHRONOUS) {
243 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
244 ubh_wait_on_buffer (UCPI_UBH(ucpi));
246 sb->s_dirt = 1;
248 inode->i_mode = mode;
249 inode->i_uid = current->fsuid;
250 if (dir->i_mode & S_ISGID) {
251 inode->i_gid = dir->i_gid;
252 if (S_ISDIR(mode))
253 inode->i_mode |= S_ISGID;
254 } else
255 inode->i_gid = current->fsgid;
257 inode->i_ino = cg * uspi->s_ipg + bit;
258 inode->i_blocks = 0;
259 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
260 ufsi->i_flags = UFS_I(dir)->i_flags;
261 ufsi->i_lastfrag = 0;
262 ufsi->i_gen = 0;
263 ufsi->i_shadow = 0;
264 ufsi->i_osync = 0;
265 ufsi->i_oeftflag = 0;
266 ufsi->i_dir_start_lookup = 0;
267 memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1));
269 insert_inode_hash(inode);
270 mark_inode_dirty(inode);
272 unlock_super (sb);
274 if (DQUOT_ALLOC_INODE(inode)) {
275 DQUOT_DROP(inode);
276 inode->i_flags |= S_NOQUOTA;
277 inode->i_nlink = 0;
278 iput(inode);
279 return ERR_PTR(-EDQUOT);
282 UFSD("allocating inode %lu\n", inode->i_ino);
283 UFSD("EXIT\n");
284 return inode;
286 failed:
287 unlock_super (sb);
288 make_bad_inode(inode);
289 iput (inode);
290 UFSD("EXIT (FAILED)\n");
291 return ERR_PTR(-ENOSPC);