blob: 594480e537d2b3a6c548f4531b48cf0e10e25cd0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ufs/ialloc.c
3 *
4 * Copyright (c) 1998
5 * Daniel Pirkl <daniel.pirkl@email.cz>
6 * Charles University, Faculty of Mathematics and Physics
7 *
8 * from
9 *
10 * linux/fs/ext2/ialloc.c
11 *
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)
16 *
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
Evgeniy Dushistov3313e292007-02-12 00:54:31 -080021 *
22 * UFS2 write support added by
23 * Evgeniy Dushistov <dushistov@mail.ru>, 2007
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
26#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/time.h>
28#include <linux/stat.h>
29#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/buffer_head.h>
31#include <linux/sched.h>
32#include <linux/bitops.h>
33#include <asm/byteorder.h>
34
Mike Frysingere5420592008-02-08 04:21:31 -080035#include "ufs_fs.h"
Christoph Hellwigbcd6d4e2007-10-16 23:26:51 -070036#include "ufs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "swab.h"
38#include "util.h"
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
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.
47 *
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.
55 */
56void ufs_free_inode (struct inode * inode)
57{
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;
65
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070066 UFSD("ENTER, ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 sb = inode->i_sb;
69 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +030070 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 ino = inode->i_ino;
73
74 lock_super (sb);
75
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;
80 }
81
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;
88 }
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -070089 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (!ufs_cg_chkmagic(sb, ucg))
91 ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number");
92
93 ucg->cg_time = cpu_to_fs32(sb, get_seconds());
94
95 is_directory = S_ISDIR(inode->i_mode);
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 clear_inode (inode);
98
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -070099 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino);
101 else {
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700102 ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (ino < ucpi->c_irotor)
104 ucpi->c_irotor = ino;
105 fs32_add(sb, &ucg->cg_cs.cs_nifree, 1);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700106 uspi->cs_total.cs_nifree++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 fs32_add(sb, &UFS_SB(sb)->fs_cs(cg).cs_nifree, 1);
108
109 if (is_directory) {
110 fs32_sub(sb, &ucg->cg_cs.cs_ndir, 1);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700111 uspi->cs_total.cs_ndir--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 fs32_sub(sb, &UFS_SB(sb)->fs_cs(cg).cs_ndir, 1);
113 }
114 }
115
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700116 ubh_mark_buffer_dirty (USPI_UBH(uspi));
117 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (sb->s_flags & MS_SYNCHRONOUS) {
Evgeniy Dushistov098d5af2006-06-25 05:47:31 -0700119 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700120 ubh_wait_on_buffer (UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
123 sb->s_dirt = 1;
124 unlock_super (sb);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700125 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128/*
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800129 * Nullify new chunk of inodes,
130 * BSD people also set ui_gen field of inode
131 * during nullification, but we not care about
132 * that because of linux ufs do not support NFS
133 */
134static void ufs2_init_inodes_chunk(struct super_block *sb,
135 struct ufs_cg_private_info *ucpi,
136 struct ufs_cylinder_group *ucg)
137{
138 struct buffer_head *bh;
139 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
140 sector_t beg = uspi->s_sbbase +
141 ufs_inotofsba(ucpi->c_cgx * uspi->s_ipg +
142 fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk));
143 sector_t end = beg + uspi->s_fpb;
144
145 UFSD("ENTER cgno %d\n", ucpi->c_cgx);
146
147 for (; beg < end; ++beg) {
148 bh = sb_getblk(sb, beg);
149 lock_buffer(bh);
150 memset(bh->b_data, 0, sb->s_blocksize);
151 set_buffer_uptodate(bh);
152 mark_buffer_dirty(bh);
153 unlock_buffer(bh);
154 if (sb->s_flags & MS_SYNCHRONOUS)
155 sync_dirty_buffer(bh);
156 brelse(bh);
157 }
158
159 fs32_add(sb, &ucg->cg_u.cg_u2.cg_initediblk, uspi->s_inopb);
160 ubh_mark_buffer_dirty(UCPI_UBH(ucpi));
161 if (sb->s_flags & MS_SYNCHRONOUS) {
162 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
163 ubh_wait_on_buffer(UCPI_UBH(ucpi));
164 }
165
166 UFSD("EXIT\n");
167}
168
169/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * There are two policies for allocating an inode. If the new inode is
171 * a directory, then a forward search is made for a block group with both
172 * free space and a low directory-to-inode ratio; if that fails, then of
173 * the groups with above-average free space, that group with the fewest
174 * directories already is chosen.
175 *
176 * For other inodes, search forward from the parent directory's block
177 * group to find a free inode.
178 */
179struct inode * ufs_new_inode(struct inode * dir, int mode)
180{
181 struct super_block * sb;
182 struct ufs_sb_info * sbi;
183 struct ufs_sb_private_info * uspi;
184 struct ufs_super_block_first * usb1;
185 struct ufs_cg_private_info * ucpi;
186 struct ufs_cylinder_group * ucg;
187 struct inode * inode;
188 unsigned cg, bit, i, j, start;
189 struct ufs_inode_info *ufsi;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800190 int err = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700192 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 /* Cannot create files in a deleted directory */
195 if (!dir || !dir->i_nlink)
196 return ERR_PTR(-EPERM);
197 sb = dir->i_sb;
198 inode = new_inode(sb);
199 if (!inode)
200 return ERR_PTR(-ENOMEM);
201 ufsi = UFS_I(inode);
202 sbi = UFS_SB(sb);
203 uspi = sbi->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300204 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 lock_super (sb);
207
208 /*
209 * Try to place the inode in its parent directory
210 */
211 i = ufs_inotocg(dir->i_ino);
212 if (sbi->fs_cs(i).cs_nifree) {
213 cg = i;
214 goto cg_found;
215 }
216
217 /*
218 * Use a quadratic hash to find a group with a free inode
219 */
220 for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
221 i += j;
222 if (i >= uspi->s_ncg)
223 i -= uspi->s_ncg;
224 if (sbi->fs_cs(i).cs_nifree) {
225 cg = i;
226 goto cg_found;
227 }
228 }
229
230 /*
231 * That failed: try linear search for a free inode
232 */
233 i = ufs_inotocg(dir->i_ino) + 1;
234 for (j = 2; j < uspi->s_ncg; j++) {
235 i++;
236 if (i >= uspi->s_ncg)
237 i = 0;
238 if (sbi->fs_cs(i).cs_nifree) {
239 cg = i;
240 goto cg_found;
241 }
242 }
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 goto failed;
245
246cg_found:
247 ucpi = ufs_load_cylinder (sb, cg);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800248 if (!ucpi) {
249 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 goto failed;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800251 }
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700252 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!ufs_cg_chkmagic(sb, ucg))
254 ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
255
256 start = ucpi->c_irotor;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700257 bit = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, uspi->s_ipg, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (!(bit < uspi->s_ipg)) {
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700259 bit = ubh_find_first_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (!(bit < start)) {
261 ufs_error (sb, "ufs_new_inode",
262 "cylinder group %u corrupted - error in inode bitmap\n", cg);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800263 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 goto failed;
265 }
266 }
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700267 UFSD("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg);
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700268 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
269 ubh_setbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 else {
271 ufs_panic (sb, "ufs_new_inode", "internal error");
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800272 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 goto failed;
274 }
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800275
276 if (uspi->fs_magic == UFS2_MAGIC) {
277 u32 initediblk = fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk);
278
279 if (bit + uspi->s_inopb > initediblk &&
280 initediblk < fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_niblk))
281 ufs2_init_inodes_chunk(sb, ucpi, ucg);
282 }
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700285 uspi->cs_total.cs_nifree--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1);
287
288 if (S_ISDIR(mode)) {
289 fs32_add(sb, &ucg->cg_cs.cs_ndir, 1);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700290 uspi->cs_total.cs_ndir++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1);
292 }
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700293 ubh_mark_buffer_dirty (USPI_UBH(uspi));
294 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (sb->s_flags & MS_SYNCHRONOUS) {
Evgeniy Dushistov098d5af2006-06-25 05:47:31 -0700296 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700297 ubh_wait_on_buffer (UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299 sb->s_dirt = 1;
300
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800301 inode->i_ino = cg * uspi->s_ipg + bit;
Dmitry Monakhovbe8ded52010-03-04 17:32:23 +0300302 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 inode->i_blocks = 0;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800304 inode->i_generation = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
306 ufsi->i_flags = UFS_I(dir)->i_flags;
307 ufsi->i_lastfrag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 ufsi->i_shadow = 0;
309 ufsi->i_osync = 0;
310 ufsi->i_oeftflag = 0;
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700311 ufsi->i_dir_start_lookup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 insert_inode_hash(inode);
314 mark_inode_dirty(inode);
315
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800316 if (uspi->fs_magic == UFS2_MAGIC) {
317 struct buffer_head *bh;
318 struct ufs2_inode *ufs2_inode;
319
320 /*
321 * setup birth date, we do it here because of there is no sense
322 * to hold it in struct ufs_inode_info, and lose 64 bit
323 */
324 bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
325 if (!bh) {
326 ufs_warning(sb, "ufs_read_inode",
327 "unable to read inode %lu\n",
328 inode->i_ino);
329 err = -EIO;
330 goto fail_remove_inode;
331 }
332 lock_buffer(bh);
333 ufs2_inode = (struct ufs2_inode *)bh->b_data;
334 ufs2_inode += ufs_inotofsbo(inode->i_ino);
Evgeniy Dushistov21898502007-03-16 13:38:07 -0800335 ufs2_inode->ui_birthtime = cpu_to_fs64(sb, CURRENT_TIME.tv_sec);
336 ufs2_inode->ui_birthnsec = cpu_to_fs32(sb, CURRENT_TIME.tv_nsec);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800337 mark_buffer_dirty(bh);
338 unlock_buffer(bh);
339 if (sb->s_flags & MS_SYNCHRONOUS)
340 sync_dirty_buffer(bh);
341 brelse(bh);
342 }
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 unlock_super (sb);
345
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700346 UFSD("allocating inode %lu\n", inode->i_ino);
347 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return inode;
349
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800350fail_remove_inode:
351 unlock_super(sb);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800352 inode->i_nlink = 0;
353 iput(inode);
354 UFSD("EXIT (FAILED): err %d\n", err);
355 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356failed:
357 unlock_super (sb);
358 make_bad_inode(inode);
359 iput (inode);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800360 UFSD("EXIT (FAILED): err %d\n", err);
361 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}