x86: convert cpuinfo_x86 array to a per_cpu array
[wrt350n-kernel.git] / fs / ufs / ialloc.c
blob7e260bc0d94f66719f40a71bebdfea19fb09c859
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/ufs_fs.h>
28 #include <linux/time.h>
29 #include <linux/stat.h>
30 #include <linux/string.h>
31 #include <linux/quotaops.h>
32 #include <linux/buffer_head.h>
33 #include <linux/sched.h>
34 #include <linux/bitops.h>
35 #include <asm/byteorder.h>
37 #include "ufs.h"
38 #include "swab.h"
39 #include "util.h"
42 * NOTE! When we get the inode, we're the only people
43 * that have access to it, and as such there are no
44 * race conditions we have to worry about. The inode
45 * is not on the hash-lists, and it cannot be reached
46 * through the filesystem because the directory entry
47 * has been deleted earlier.
49 * HOWEVER: we must make sure that we get no aliases,
50 * which means that we have to call "clear_inode()"
51 * _before_ we mark the inode not in use in the inode
52 * bitmaps. Otherwise a newly created file might use
53 * the same inode number (not actually the same pointer
54 * though), and then we'd have two inodes sharing the
55 * same inode number and space on the harddisk.
57 void ufs_free_inode (struct inode * inode)
59 struct super_block * sb;
60 struct ufs_sb_private_info * uspi;
61 struct ufs_super_block_first * usb1;
62 struct ufs_cg_private_info * ucpi;
63 struct ufs_cylinder_group * ucg;
64 int is_directory;
65 unsigned ino, cg, bit;
67 UFSD("ENTER, ino %lu\n", inode->i_ino);
69 sb = inode->i_sb;
70 uspi = UFS_SB(sb)->s_uspi;
71 usb1 = ubh_get_usb_first(uspi);
73 ino = inode->i_ino;
75 lock_super (sb);
77 if (!((ino > 1) && (ino < (uspi->s_ncg * uspi->s_ipg )))) {
78 ufs_warning(sb, "ufs_free_inode", "reserved inode or nonexistent inode %u\n", ino);
79 unlock_super (sb);
80 return;
83 cg = ufs_inotocg (ino);
84 bit = ufs_inotocgoff (ino);
85 ucpi = ufs_load_cylinder (sb, cg);
86 if (!ucpi) {
87 unlock_super (sb);
88 return;
90 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
91 if (!ufs_cg_chkmagic(sb, ucg))
92 ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number");
94 ucg->cg_time = cpu_to_fs32(sb, get_seconds());
96 is_directory = S_ISDIR(inode->i_mode);
98 DQUOT_FREE_INODE(inode);
99 DQUOT_DROP(inode);
101 clear_inode (inode);
103 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
104 ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino);
105 else {
106 ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
107 if (ino < ucpi->c_irotor)
108 ucpi->c_irotor = ino;
109 fs32_add(sb, &ucg->cg_cs.cs_nifree, 1);
110 uspi->cs_total.cs_nifree++;
111 fs32_add(sb, &UFS_SB(sb)->fs_cs(cg).cs_nifree, 1);
113 if (is_directory) {
114 fs32_sub(sb, &ucg->cg_cs.cs_ndir, 1);
115 uspi->cs_total.cs_ndir--;
116 fs32_sub(sb, &UFS_SB(sb)->fs_cs(cg).cs_ndir, 1);
120 ubh_mark_buffer_dirty (USPI_UBH(uspi));
121 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
122 if (sb->s_flags & MS_SYNCHRONOUS) {
123 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
124 ubh_wait_on_buffer (UCPI_UBH(ucpi));
127 sb->s_dirt = 1;
128 unlock_super (sb);
129 UFSD("EXIT\n");
133 * Nullify new chunk of inodes,
134 * BSD people also set ui_gen field of inode
135 * during nullification, but we not care about
136 * that because of linux ufs do not support NFS
138 static void ufs2_init_inodes_chunk(struct super_block *sb,
139 struct ufs_cg_private_info *ucpi,
140 struct ufs_cylinder_group *ucg)
142 struct buffer_head *bh;
143 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
144 sector_t beg = uspi->s_sbbase +
145 ufs_inotofsba(ucpi->c_cgx * uspi->s_ipg +
146 fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk));
147 sector_t end = beg + uspi->s_fpb;
149 UFSD("ENTER cgno %d\n", ucpi->c_cgx);
151 for (; beg < end; ++beg) {
152 bh = sb_getblk(sb, beg);
153 lock_buffer(bh);
154 memset(bh->b_data, 0, sb->s_blocksize);
155 set_buffer_uptodate(bh);
156 mark_buffer_dirty(bh);
157 unlock_buffer(bh);
158 if (sb->s_flags & MS_SYNCHRONOUS)
159 sync_dirty_buffer(bh);
160 brelse(bh);
163 fs32_add(sb, &ucg->cg_u.cg_u2.cg_initediblk, uspi->s_inopb);
164 ubh_mark_buffer_dirty(UCPI_UBH(ucpi));
165 if (sb->s_flags & MS_SYNCHRONOUS) {
166 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
167 ubh_wait_on_buffer(UCPI_UBH(ucpi));
170 UFSD("EXIT\n");
174 * There are two policies for allocating an inode. If the new inode is
175 * a directory, then a forward search is made for a block group with both
176 * free space and a low directory-to-inode ratio; if that fails, then of
177 * the groups with above-average free space, that group with the fewest
178 * directories already is chosen.
180 * For other inodes, search forward from the parent directory's block
181 * group to find a free inode.
183 struct inode * ufs_new_inode(struct inode * dir, int mode)
185 struct super_block * sb;
186 struct ufs_sb_info * sbi;
187 struct ufs_sb_private_info * uspi;
188 struct ufs_super_block_first * usb1;
189 struct ufs_cg_private_info * ucpi;
190 struct ufs_cylinder_group * ucg;
191 struct inode * inode;
192 unsigned cg, bit, i, j, start;
193 struct ufs_inode_info *ufsi;
194 int err = -ENOSPC;
196 UFSD("ENTER\n");
198 /* Cannot create files in a deleted directory */
199 if (!dir || !dir->i_nlink)
200 return ERR_PTR(-EPERM);
201 sb = dir->i_sb;
202 inode = new_inode(sb);
203 if (!inode)
204 return ERR_PTR(-ENOMEM);
205 ufsi = UFS_I(inode);
206 sbi = UFS_SB(sb);
207 uspi = sbi->s_uspi;
208 usb1 = ubh_get_usb_first(uspi);
210 lock_super (sb);
213 * Try to place the inode in its parent directory
215 i = ufs_inotocg(dir->i_ino);
216 if (sbi->fs_cs(i).cs_nifree) {
217 cg = i;
218 goto cg_found;
222 * Use a quadratic hash to find a group with a free inode
224 for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
225 i += j;
226 if (i >= uspi->s_ncg)
227 i -= uspi->s_ncg;
228 if (sbi->fs_cs(i).cs_nifree) {
229 cg = i;
230 goto cg_found;
235 * That failed: try linear search for a free inode
237 i = ufs_inotocg(dir->i_ino) + 1;
238 for (j = 2; j < uspi->s_ncg; j++) {
239 i++;
240 if (i >= uspi->s_ncg)
241 i = 0;
242 if (sbi->fs_cs(i).cs_nifree) {
243 cg = i;
244 goto cg_found;
248 goto failed;
250 cg_found:
251 ucpi = ufs_load_cylinder (sb, cg);
252 if (!ucpi) {
253 err = -EIO;
254 goto failed;
256 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
257 if (!ufs_cg_chkmagic(sb, ucg))
258 ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
260 start = ucpi->c_irotor;
261 bit = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, uspi->s_ipg, start);
262 if (!(bit < uspi->s_ipg)) {
263 bit = ubh_find_first_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, start);
264 if (!(bit < start)) {
265 ufs_error (sb, "ufs_new_inode",
266 "cylinder group %u corrupted - error in inode bitmap\n", cg);
267 err = -EIO;
268 goto failed;
271 UFSD("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg);
272 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
273 ubh_setbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
274 else {
275 ufs_panic (sb, "ufs_new_inode", "internal error");
276 err = -EIO;
277 goto failed;
280 if (uspi->fs_magic == UFS2_MAGIC) {
281 u32 initediblk = fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk);
283 if (bit + uspi->s_inopb > initediblk &&
284 initediblk < fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_niblk))
285 ufs2_init_inodes_chunk(sb, ucpi, ucg);
288 fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1);
289 uspi->cs_total.cs_nifree--;
290 fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1);
292 if (S_ISDIR(mode)) {
293 fs32_add(sb, &ucg->cg_cs.cs_ndir, 1);
294 uspi->cs_total.cs_ndir++;
295 fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1);
297 ubh_mark_buffer_dirty (USPI_UBH(uspi));
298 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
299 if (sb->s_flags & MS_SYNCHRONOUS) {
300 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
301 ubh_wait_on_buffer (UCPI_UBH(ucpi));
303 sb->s_dirt = 1;
305 inode->i_ino = cg * uspi->s_ipg + bit;
306 inode->i_mode = mode;
307 inode->i_uid = current->fsuid;
308 if (dir->i_mode & S_ISGID) {
309 inode->i_gid = dir->i_gid;
310 if (S_ISDIR(mode))
311 inode->i_mode |= S_ISGID;
312 } else
313 inode->i_gid = current->fsgid;
315 inode->i_blocks = 0;
316 inode->i_generation = 0;
317 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
318 ufsi->i_flags = UFS_I(dir)->i_flags;
319 ufsi->i_lastfrag = 0;
320 ufsi->i_shadow = 0;
321 ufsi->i_osync = 0;
322 ufsi->i_oeftflag = 0;
323 ufsi->i_dir_start_lookup = 0;
324 memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1));
325 insert_inode_hash(inode);
326 mark_inode_dirty(inode);
328 if (uspi->fs_magic == UFS2_MAGIC) {
329 struct buffer_head *bh;
330 struct ufs2_inode *ufs2_inode;
333 * setup birth date, we do it here because of there is no sense
334 * to hold it in struct ufs_inode_info, and lose 64 bit
336 bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
337 if (!bh) {
338 ufs_warning(sb, "ufs_read_inode",
339 "unable to read inode %lu\n",
340 inode->i_ino);
341 err = -EIO;
342 goto fail_remove_inode;
344 lock_buffer(bh);
345 ufs2_inode = (struct ufs2_inode *)bh->b_data;
346 ufs2_inode += ufs_inotofsbo(inode->i_ino);
347 ufs2_inode->ui_birthtime = cpu_to_fs64(sb, CURRENT_TIME.tv_sec);
348 ufs2_inode->ui_birthnsec = cpu_to_fs32(sb, CURRENT_TIME.tv_nsec);
349 mark_buffer_dirty(bh);
350 unlock_buffer(bh);
351 if (sb->s_flags & MS_SYNCHRONOUS)
352 sync_dirty_buffer(bh);
353 brelse(bh);
356 unlock_super (sb);
358 if (DQUOT_ALLOC_INODE(inode)) {
359 DQUOT_DROP(inode);
360 err = -EDQUOT;
361 goto fail_without_unlock;
364 UFSD("allocating inode %lu\n", inode->i_ino);
365 UFSD("EXIT\n");
366 return inode;
368 fail_remove_inode:
369 unlock_super(sb);
370 fail_without_unlock:
371 inode->i_flags |= S_NOQUOTA;
372 inode->i_nlink = 0;
373 iput(inode);
374 UFSD("EXIT (FAILED): err %d\n", err);
375 return ERR_PTR(err);
376 failed:
377 unlock_super (sb);
378 make_bad_inode(inode);
379 iput (inode);
380 UFSD("EXIT (FAILED): err %d\n", err);
381 return ERR_PTR(err);