blob: c28a8b6f2feb65695dca3be4cd7d954871425d8f [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>
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>
36
37#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
97 DQUOT_FREE_INODE(inode);
98 DQUOT_DROP(inode);
99
100 clear_inode (inode);
101
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700102 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino);
104 else {
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700105 ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (ino < ucpi->c_irotor)
107 ucpi->c_irotor = ino;
108 fs32_add(sb, &ucg->cg_cs.cs_nifree, 1);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700109 uspi->cs_total.cs_nifree++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 fs32_add(sb, &UFS_SB(sb)->fs_cs(cg).cs_nifree, 1);
111
112 if (is_directory) {
113 fs32_sub(sb, &ucg->cg_cs.cs_ndir, 1);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700114 uspi->cs_total.cs_ndir--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 fs32_sub(sb, &UFS_SB(sb)->fs_cs(cg).cs_ndir, 1);
116 }
117 }
118
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700119 ubh_mark_buffer_dirty (USPI_UBH(uspi));
120 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (sb->s_flags & MS_SYNCHRONOUS) {
Evgeniy Dushistov098d5af2006-06-25 05:47:31 -0700122 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700123 ubh_wait_on_buffer (UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125
126 sb->s_dirt = 1;
127 unlock_super (sb);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700128 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131/*
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800132 * Nullify new chunk of inodes,
133 * BSD people also set ui_gen field of inode
134 * during nullification, but we not care about
135 * that because of linux ufs do not support NFS
136 */
137static void ufs2_init_inodes_chunk(struct super_block *sb,
138 struct ufs_cg_private_info *ucpi,
139 struct ufs_cylinder_group *ucg)
140{
141 struct buffer_head *bh;
142 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
143 sector_t beg = uspi->s_sbbase +
144 ufs_inotofsba(ucpi->c_cgx * uspi->s_ipg +
145 fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk));
146 sector_t end = beg + uspi->s_fpb;
147
148 UFSD("ENTER cgno %d\n", ucpi->c_cgx);
149
150 for (; beg < end; ++beg) {
151 bh = sb_getblk(sb, beg);
152 lock_buffer(bh);
153 memset(bh->b_data, 0, sb->s_blocksize);
154 set_buffer_uptodate(bh);
155 mark_buffer_dirty(bh);
156 unlock_buffer(bh);
157 if (sb->s_flags & MS_SYNCHRONOUS)
158 sync_dirty_buffer(bh);
159 brelse(bh);
160 }
161
162 fs32_add(sb, &ucg->cg_u.cg_u2.cg_initediblk, uspi->s_inopb);
163 ubh_mark_buffer_dirty(UCPI_UBH(ucpi));
164 if (sb->s_flags & MS_SYNCHRONOUS) {
165 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
166 ubh_wait_on_buffer(UCPI_UBH(ucpi));
167 }
168
169 UFSD("EXIT\n");
170}
171
172/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * There are two policies for allocating an inode. If the new inode is
174 * a directory, then a forward search is made for a block group with both
175 * free space and a low directory-to-inode ratio; if that fails, then of
176 * the groups with above-average free space, that group with the fewest
177 * directories already is chosen.
178 *
179 * For other inodes, search forward from the parent directory's block
180 * group to find a free inode.
181 */
182struct inode * ufs_new_inode(struct inode * dir, int mode)
183{
184 struct super_block * sb;
185 struct ufs_sb_info * sbi;
186 struct ufs_sb_private_info * uspi;
187 struct ufs_super_block_first * usb1;
188 struct ufs_cg_private_info * ucpi;
189 struct ufs_cylinder_group * ucg;
190 struct inode * inode;
191 unsigned cg, bit, i, j, start;
192 struct ufs_inode_info *ufsi;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800193 int err = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700195 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 /* Cannot create files in a deleted directory */
198 if (!dir || !dir->i_nlink)
199 return ERR_PTR(-EPERM);
200 sb = dir->i_sb;
201 inode = new_inode(sb);
202 if (!inode)
203 return ERR_PTR(-ENOMEM);
204 ufsi = UFS_I(inode);
205 sbi = UFS_SB(sb);
206 uspi = sbi->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300207 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 lock_super (sb);
210
211 /*
212 * Try to place the inode in its parent directory
213 */
214 i = ufs_inotocg(dir->i_ino);
215 if (sbi->fs_cs(i).cs_nifree) {
216 cg = i;
217 goto cg_found;
218 }
219
220 /*
221 * Use a quadratic hash to find a group with a free inode
222 */
223 for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
224 i += j;
225 if (i >= uspi->s_ncg)
226 i -= uspi->s_ncg;
227 if (sbi->fs_cs(i).cs_nifree) {
228 cg = i;
229 goto cg_found;
230 }
231 }
232
233 /*
234 * That failed: try linear search for a free inode
235 */
236 i = ufs_inotocg(dir->i_ino) + 1;
237 for (j = 2; j < uspi->s_ncg; j++) {
238 i++;
239 if (i >= uspi->s_ncg)
240 i = 0;
241 if (sbi->fs_cs(i).cs_nifree) {
242 cg = i;
243 goto cg_found;
244 }
245 }
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 goto failed;
248
249cg_found:
250 ucpi = ufs_load_cylinder (sb, cg);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800251 if (!ucpi) {
252 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 goto failed;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800254 }
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700255 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (!ufs_cg_chkmagic(sb, ucg))
257 ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
258
259 start = ucpi->c_irotor;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700260 bit = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, uspi->s_ipg, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (!(bit < uspi->s_ipg)) {
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700262 bit = ubh_find_first_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (!(bit < start)) {
264 ufs_error (sb, "ufs_new_inode",
265 "cylinder group %u corrupted - error in inode bitmap\n", cg);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800266 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 goto failed;
268 }
269 }
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700270 UFSD("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg);
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700271 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
272 ubh_setbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 else {
274 ufs_panic (sb, "ufs_new_inode", "internal error");
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800275 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 goto failed;
277 }
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800278
279 if (uspi->fs_magic == UFS2_MAGIC) {
280 u32 initediblk = fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk);
281
282 if (bit + uspi->s_inopb > initediblk &&
283 initediblk < fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_niblk))
284 ufs2_init_inodes_chunk(sb, ucpi, ucg);
285 }
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700288 uspi->cs_total.cs_nifree--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1);
290
291 if (S_ISDIR(mode)) {
292 fs32_add(sb, &ucg->cg_cs.cs_ndir, 1);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700293 uspi->cs_total.cs_ndir++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1);
295 }
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700296 ubh_mark_buffer_dirty (USPI_UBH(uspi));
297 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (sb->s_flags & MS_SYNCHRONOUS) {
Evgeniy Dushistov098d5af2006-06-25 05:47:31 -0700299 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700300 ubh_wait_on_buffer (UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302 sb->s_dirt = 1;
303
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800304 inode->i_ino = cg * uspi->s_ipg + bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 inode->i_mode = mode;
306 inode->i_uid = current->fsuid;
307 if (dir->i_mode & S_ISGID) {
308 inode->i_gid = dir->i_gid;
309 if (S_ISDIR(mode))
310 inode->i_mode |= S_ISGID;
311 } else
312 inode->i_gid = current->fsgid;
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 inode->i_blocks = 0;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800315 inode->i_generation = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
317 ufsi->i_flags = UFS_I(dir)->i_flags;
318 ufsi->i_lastfrag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 ufsi->i_shadow = 0;
320 ufsi->i_osync = 0;
321 ufsi->i_oeftflag = 0;
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700322 ufsi->i_dir_start_lookup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 insert_inode_hash(inode);
325 mark_inode_dirty(inode);
326
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800327 if (uspi->fs_magic == UFS2_MAGIC) {
328 struct buffer_head *bh;
329 struct ufs2_inode *ufs2_inode;
330
331 /*
332 * setup birth date, we do it here because of there is no sense
333 * to hold it in struct ufs_inode_info, and lose 64 bit
334 */
335 bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
336 if (!bh) {
337 ufs_warning(sb, "ufs_read_inode",
338 "unable to read inode %lu\n",
339 inode->i_ino);
340 err = -EIO;
341 goto fail_remove_inode;
342 }
343 lock_buffer(bh);
344 ufs2_inode = (struct ufs2_inode *)bh->b_data;
345 ufs2_inode += ufs_inotofsbo(inode->i_ino);
Evgeniy Dushistov21898502007-03-16 13:38:07 -0800346 ufs2_inode->ui_birthtime = cpu_to_fs64(sb, CURRENT_TIME.tv_sec);
347 ufs2_inode->ui_birthnsec = cpu_to_fs32(sb, CURRENT_TIME.tv_nsec);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800348 mark_buffer_dirty(bh);
349 unlock_buffer(bh);
350 if (sb->s_flags & MS_SYNCHRONOUS)
351 sync_dirty_buffer(bh);
352 brelse(bh);
353 }
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 unlock_super (sb);
356
357 if (DQUOT_ALLOC_INODE(inode)) {
358 DQUOT_DROP(inode);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800359 err = -EDQUOT;
360 goto fail_without_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700363 UFSD("allocating inode %lu\n", inode->i_ino);
364 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return inode;
366
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800367fail_remove_inode:
368 unlock_super(sb);
369fail_without_unlock:
370 inode->i_flags |= S_NOQUOTA;
371 inode->i_nlink = 0;
372 iput(inode);
373 UFSD("EXIT (FAILED): err %d\n", err);
374 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375failed:
376 unlock_super (sb);
377 make_bad_inode(inode);
378 iput (inode);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800379 UFSD("EXIT (FAILED): err %d\n", err);
380 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}