Import 2.3.1pre2
[davej-history.git] / fs / ufs / ialloc.c
blobfa4dfa06afdd32621efb9c460d0b1e14cd2547ba
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/sched.h>
26 #include <linux/stat.h>
27 #include <linux/string.h>
28 #include <linux/locks.h>
29 #include <linux/quotaops.h>
30 #include <asm/bitops.h>
31 #include <asm/byteorder.h>
33 #include "swab.h"
34 #include "util.h"
36 #undef UFS_IALLOC_DEBUG
38 #ifdef UFS_IALLOC_DEBUG
39 #define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x;
40 #else
41 #define UFSD(x)
42 #endif
45 * NOTE! When we get the inode, we're the only people
46 * that have access to it, and as such there are no
47 * race conditions we have to worry about. The inode
48 * is not on the hash-lists, and it cannot be reached
49 * through the filesystem because the directory entry
50 * has been deleted earlier.
52 * HOWEVER: we must make sure that we get no aliases,
53 * which means that we have to call "clear_inode()"
54 * _before_ we mark the inode not in use in the inode
55 * bitmaps. Otherwise a newly created file might use
56 * the same inode number (not actually the same pointer
57 * though), and then we'd have two inodes sharing the
58 * same inode number and space on the harddisk.
60 void ufs_free_inode (struct inode * inode)
62 struct super_block * sb;
63 struct ufs_sb_private_info * uspi;
64 struct ufs_super_block_first * usb1;
65 struct ufs_cg_private_info * ucpi;
66 struct ufs_cylinder_group * ucg;
67 int is_directory;
68 unsigned ino, cg, bit;
69 unsigned swab;
71 UFSD(("ENTER, ino %lu\n", inode->i_ino))
73 if (!inode)
74 return;
75 sb = inode->i_sb;
76 swab = sb->u.ufs_sb.s_swab;
77 uspi = sb->u.ufs_sb.s_uspi;
78 usb1 = ubh_get_usb_first(USPI_UBH);
80 if (inode->i_count > 1) {
81 ufs_warning(sb, "ufs_free_inode", "inode has count=%d\n", inode->i_count);
82 return;
84 if (inode->i_nlink) {
85 ufs_warning(sb, "ufs_free_inode", "inode has nlink=%d\n", inode->i_nlink);
86 return;
89 ino = inode->i_ino;
91 lock_super (sb);
93 if (!((ino > 1) && (ino < (uspi->s_ncg * uspi->s_ipg )))) {
94 ufs_warning(sb, "ufs_free_inode", "reserved inode or nonexistent inode %u\n", ino);
95 unlock_super (sb);
96 return;
99 cg = ufs_inotocg (ino);
100 bit = ufs_inotocgoff (ino);
101 ucpi = ufs_load_cylinder (sb, cg);
102 if (!ucpi) {
103 unlock_super (sb);
104 return;
106 ucg = ubh_get_ucg(UCPI_UBH);
107 if (!ufs_cg_chkmagic(ucg))
108 ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number");
110 ucg->cg_time = SWAB32(CURRENT_TIME);
112 is_directory = S_ISDIR(inode->i_mode);
114 DQUOT_FREE_INODE(sb, inode);
116 clear_inode (inode);
118 if (ubh_isclr (UCPI_UBH, ucpi->c_iusedoff, bit))
119 ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino);
120 else {
121 ubh_clrbit (UCPI_UBH, ucpi->c_iusedoff, bit);
122 if (ino < ucpi->c_irotor)
123 ucpi->c_irotor = ino;
124 INC_SWAB32(ucg->cg_cs.cs_nifree);
125 INC_SWAB32(usb1->fs_cstotal.cs_nifree);
126 INC_SWAB32(sb->fs_cs(cg).cs_nifree);
128 if (is_directory) {
129 DEC_SWAB32(ucg->cg_cs.cs_ndir);
130 DEC_SWAB32(usb1->fs_cstotal.cs_ndir);
131 DEC_SWAB32(sb->fs_cs(cg).cs_ndir);
134 ubh_mark_buffer_dirty (USPI_UBH, 1);
135 ubh_mark_buffer_dirty (UCPI_UBH, 1);
136 if (sb->s_flags & MS_SYNCHRONOUS) {
137 ubh_ll_rw_block (WRITE, 1, (struct ufs_buffer_head **) &ucpi);
138 ubh_wait_on_buffer (UCPI_UBH);
141 sb->s_dirt = 1;
142 unlock_super (sb);
143 UFSD(("EXIT\n"))
147 * There are two policies for allocating an inode. If the new inode is
148 * a directory, then a forward search is made for a block group with both
149 * free space and a low directory-to-inode ratio; if that fails, then of
150 * the groups with above-average free space, that group with the fewest
151 * directories already is chosen.
153 * For other inodes, search forward from the parent directory's block
154 * group to find a free inode.
156 struct inode * ufs_new_inode (const struct inode * dir, int mode, int * err )
158 struct super_block * sb;
159 struct ufs_sb_private_info * uspi;
160 struct ufs_super_block_first * usb1;
161 struct ufs_cg_private_info * ucpi;
162 struct ufs_cylinder_group * ucg;
163 struct inode * inode;
164 unsigned cg, bit, i, j, start;
165 unsigned swab;
167 UFSD(("ENTER\n"))
169 /* Cannot create files in a deleted directory */
170 if (!dir || !dir->i_nlink) {
171 *err = -EPERM;
172 return NULL;
174 inode = get_empty_inode ();
175 if (!inode) {
176 *err = -ENOMEM;
177 return NULL;
179 sb = dir->i_sb;
180 swab = sb->u.ufs_sb.s_swab;
181 uspi = sb->u.ufs_sb.s_uspi;
182 usb1 = ubh_get_usb_first(USPI_UBH);
184 inode->i_sb = sb;
185 inode->i_flags = 0;
187 lock_super (sb);
189 *err = -ENOSPC;
192 * Try to place the inode in its parent directory
194 i = ufs_inotocg(dir->i_ino);
195 if (SWAB32(sb->fs_cs(i).cs_nifree)) {
196 cg = i;
197 goto cg_found;
201 * Use a quadratic hash to find a group with a free inode
203 for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
204 i += j;
205 if (i >= uspi->s_ncg)
206 i -= uspi->s_ncg;
207 if (SWAB32(sb->fs_cs(i).cs_nifree)) {
208 cg = i;
209 goto cg_found;
214 * That failed: try linear search for a free inode
216 i = ufs_inotocg(dir->i_ino) + 1;
217 for (j = 2; j < uspi->s_ncg; j++) {
218 i++;
219 if (i >= uspi->s_ncg)
220 i = 0;
221 if (SWAB32(sb->fs_cs(i).cs_nifree)) {
222 cg = i;
223 goto cg_found;
227 goto failed;
229 cg_found:
230 ucpi = ufs_load_cylinder (sb, cg);
231 if (!ucpi)
232 goto failed;
233 ucg = ubh_get_ucg(UCPI_UBH);
234 if (!ufs_cg_chkmagic(ucg))
235 ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
237 start = ucpi->c_irotor;
238 bit = ubh_find_next_zero_bit (UCPI_UBH, ucpi->c_iusedoff, uspi->s_ipg, start);
239 if (!(bit < uspi->s_ipg)) {
240 bit = ubh_find_first_zero_bit (UCPI_UBH, ucpi->c_iusedoff, start);
241 if (!(bit < start)) {
242 ufs_error (sb, "ufs_new_inode",
243 "cylinder group %u corrupted - error in inode bitmap\n", cg);
244 goto failed;
247 UFSD(("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg))
248 if (ubh_isclr (UCPI_UBH, ucpi->c_iusedoff, bit))
249 ubh_setbit (UCPI_UBH, ucpi->c_iusedoff, bit);
250 else {
251 ufs_panic (sb, "ufs_new_inode", "internal error");
252 goto failed;
255 DEC_SWAB32(ucg->cg_cs.cs_nifree);
256 DEC_SWAB32(usb1->fs_cstotal.cs_nifree);
257 DEC_SWAB32(sb->fs_cs(cg).cs_nifree);
259 if (S_ISDIR(mode)) {
260 INC_SWAB32(ucg->cg_cs.cs_ndir);
261 INC_SWAB32(usb1->fs_cstotal.cs_ndir);
262 INC_SWAB32(sb->fs_cs(cg).cs_ndir);
265 ubh_mark_buffer_dirty (USPI_UBH, 1);
266 ubh_mark_buffer_dirty (UCPI_UBH, 1);
267 if (sb->s_flags & MS_SYNCHRONOUS) {
268 ubh_ll_rw_block (WRITE, 1, (struct ufs_buffer_head **) &ucpi);
269 ubh_wait_on_buffer (UCPI_UBH);
271 sb->s_dirt = 1;
273 inode->i_mode = mode;
274 inode->i_sb = sb;
275 inode->i_nlink = 1;
276 inode->i_dev = sb->s_dev;
277 inode->i_uid = current->fsuid;
278 if (test_opt (sb, GRPID))
279 inode->i_gid = dir->i_gid;
280 else if (dir->i_mode & S_ISGID) {
281 inode->i_gid = dir->i_gid;
282 if (S_ISDIR(mode))
283 mode |= S_ISGID;
284 } else
285 inode->i_gid = current->fsgid;
287 inode->i_ino = cg * uspi->s_ipg + bit;
288 inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */
289 inode->i_blocks = 0;
290 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
291 inode->u.ufs_i.i_flags = dir->u.ufs_i.i_flags;
292 inode->u.ufs_i.i_uid = inode->i_uid;
293 inode->u.ufs_i.i_gid = inode->i_gid;
294 inode->u.ufs_i.i_lastfrag = 0;
295 inode->i_op = NULL;
297 insert_inode_hash(inode);
298 mark_inode_dirty(inode);
300 unlock_super (sb);
302 if(DQUOT_ALLOC_INODE(sb, inode)) {
303 sb->dq_op->drop(inode);
304 inode->i_nlink = 0;
305 iput(inode);
306 *err = -EDQUOT;
307 return NULL;
310 UFSD(("allocating inode %lu\n", inode->i_ino))
311 *err = 0;
312 UFSD(("EXIT\n"))
313 return inode;
315 failed:
316 unlock_super (sb);
317 iput (inode);
318 UFSD(("EXIT (FAILED)\n"))
319 return NULL;