blob: 98f7211599ffe747f15a9d916ccdb14e0cd3592a [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct ufs_cg_private_info * ucpi;
61 struct ufs_cylinder_group * ucg;
62 int is_directory;
63 unsigned ino, cg, bit;
64
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070065 UFSD("ENTER, ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 sb = inode->i_sb;
68 uspi = UFS_SB(sb)->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 ino = inode->i_ino;
71
Marco Stornellib6963322012-10-06 12:42:28 +020072 mutex_lock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 if (!((ino > 1) && (ino < (uspi->s_ncg * uspi->s_ipg )))) {
75 ufs_warning(sb, "ufs_free_inode", "reserved inode or nonexistent inode %u\n", ino);
Marco Stornellib6963322012-10-06 12:42:28 +020076 mutex_unlock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return;
78 }
79
80 cg = ufs_inotocg (ino);
81 bit = ufs_inotocgoff (ino);
82 ucpi = ufs_load_cylinder (sb, cg);
83 if (!ucpi) {
Marco Stornellib6963322012-10-06 12:42:28 +020084 mutex_unlock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return;
86 }
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -070087 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (!ufs_cg_chkmagic(sb, ucg))
89 ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number");
90
91 ucg->cg_time = cpu_to_fs32(sb, get_seconds());
92
93 is_directory = S_ISDIR(inode->i_mode);
94
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -070095 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino);
97 else {
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -070098 ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (ino < ucpi->c_irotor)
100 ucpi->c_irotor = ino;
101 fs32_add(sb, &ucg->cg_cs.cs_nifree, 1);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700102 uspi->cs_total.cs_nifree++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 fs32_add(sb, &UFS_SB(sb)->fs_cs(cg).cs_nifree, 1);
104
105 if (is_directory) {
106 fs32_sub(sb, &ucg->cg_cs.cs_ndir, 1);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700107 uspi->cs_total.cs_ndir--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 fs32_sub(sb, &UFS_SB(sb)->fs_cs(cg).cs_ndir, 1);
109 }
110 }
111
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700112 ubh_mark_buffer_dirty (USPI_UBH(uspi));
113 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
Christoph Hellwig9cb569d2010-08-11 17:06:24 +0200114 if (sb->s_flags & MS_SYNCHRONOUS)
115 ubh_sync_block(UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300117 ufs_mark_sb_dirty(sb);
Marco Stornellib6963322012-10-06 12:42:28 +0200118 mutex_unlock(&UFS_SB(sb)->s_lock);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700119 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122/*
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800123 * Nullify new chunk of inodes,
124 * BSD people also set ui_gen field of inode
125 * during nullification, but we not care about
126 * that because of linux ufs do not support NFS
127 */
128static void ufs2_init_inodes_chunk(struct super_block *sb,
129 struct ufs_cg_private_info *ucpi,
130 struct ufs_cylinder_group *ucg)
131{
132 struct buffer_head *bh;
133 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
134 sector_t beg = uspi->s_sbbase +
135 ufs_inotofsba(ucpi->c_cgx * uspi->s_ipg +
136 fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk));
137 sector_t end = beg + uspi->s_fpb;
138
139 UFSD("ENTER cgno %d\n", ucpi->c_cgx);
140
141 for (; beg < end; ++beg) {
142 bh = sb_getblk(sb, beg);
143 lock_buffer(bh);
144 memset(bh->b_data, 0, sb->s_blocksize);
145 set_buffer_uptodate(bh);
146 mark_buffer_dirty(bh);
147 unlock_buffer(bh);
148 if (sb->s_flags & MS_SYNCHRONOUS)
149 sync_dirty_buffer(bh);
150 brelse(bh);
151 }
152
153 fs32_add(sb, &ucg->cg_u.cg_u2.cg_initediblk, uspi->s_inopb);
154 ubh_mark_buffer_dirty(UCPI_UBH(ucpi));
Christoph Hellwig9cb569d2010-08-11 17:06:24 +0200155 if (sb->s_flags & MS_SYNCHRONOUS)
156 ubh_sync_block(UCPI_UBH(ucpi));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800157
158 UFSD("EXIT\n");
159}
160
161/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * There are two policies for allocating an inode. If the new inode is
163 * a directory, then a forward search is made for a block group with both
164 * free space and a low directory-to-inode ratio; if that fails, then of
165 * the groups with above-average free space, that group with the fewest
166 * directories already is chosen.
167 *
168 * For other inodes, search forward from the parent directory's block
169 * group to find a free inode.
170 */
Al Viro6a9a06d2011-07-26 02:49:13 -0400171struct inode *ufs_new_inode(struct inode *dir, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 struct super_block * sb;
174 struct ufs_sb_info * sbi;
175 struct ufs_sb_private_info * uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 struct ufs_cg_private_info * ucpi;
177 struct ufs_cylinder_group * ucg;
178 struct inode * inode;
179 unsigned cg, bit, i, j, start;
180 struct ufs_inode_info *ufsi;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800181 int err = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700183 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 /* Cannot create files in a deleted directory */
186 if (!dir || !dir->i_nlink)
187 return ERR_PTR(-EPERM);
188 sb = dir->i_sb;
189 inode = new_inode(sb);
190 if (!inode)
191 return ERR_PTR(-ENOMEM);
192 ufsi = UFS_I(inode);
193 sbi = UFS_SB(sb);
194 uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Marco Stornellib6963322012-10-06 12:42:28 +0200196 mutex_lock(&sbi->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 /*
199 * Try to place the inode in its parent directory
200 */
201 i = ufs_inotocg(dir->i_ino);
202 if (sbi->fs_cs(i).cs_nifree) {
203 cg = i;
204 goto cg_found;
205 }
206
207 /*
208 * Use a quadratic hash to find a group with a free inode
209 */
210 for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
211 i += j;
212 if (i >= uspi->s_ncg)
213 i -= uspi->s_ncg;
214 if (sbi->fs_cs(i).cs_nifree) {
215 cg = i;
216 goto cg_found;
217 }
218 }
219
220 /*
221 * That failed: try linear search for a free inode
222 */
223 i = ufs_inotocg(dir->i_ino) + 1;
224 for (j = 2; j < uspi->s_ncg; j++) {
225 i++;
226 if (i >= uspi->s_ncg)
227 i = 0;
228 if (sbi->fs_cs(i).cs_nifree) {
229 cg = i;
230 goto cg_found;
231 }
232 }
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 goto failed;
235
236cg_found:
237 ucpi = ufs_load_cylinder (sb, cg);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800238 if (!ucpi) {
239 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 goto failed;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800241 }
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700242 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (!ufs_cg_chkmagic(sb, ucg))
244 ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
245
246 start = ucpi->c_irotor;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700247 bit = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, uspi->s_ipg, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (!(bit < uspi->s_ipg)) {
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700249 bit = ubh_find_first_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (!(bit < start)) {
251 ufs_error (sb, "ufs_new_inode",
252 "cylinder group %u corrupted - error in inode bitmap\n", cg);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800253 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto failed;
255 }
256 }
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700257 UFSD("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg);
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700258 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
259 ubh_setbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 else {
261 ufs_panic (sb, "ufs_new_inode", "internal error");
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800262 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 goto failed;
264 }
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800265
266 if (uspi->fs_magic == UFS2_MAGIC) {
267 u32 initediblk = fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk);
268
269 if (bit + uspi->s_inopb > initediblk &&
270 initediblk < fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_niblk))
271 ufs2_init_inodes_chunk(sb, ucpi, ucg);
272 }
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700275 uspi->cs_total.cs_nifree--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1);
277
278 if (S_ISDIR(mode)) {
279 fs32_add(sb, &ucg->cg_cs.cs_ndir, 1);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700280 uspi->cs_total.cs_ndir++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1);
282 }
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700283 ubh_mark_buffer_dirty (USPI_UBH(uspi));
284 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
Christoph Hellwig9cb569d2010-08-11 17:06:24 +0200285 if (sb->s_flags & MS_SYNCHRONOUS)
286 ubh_sync_block(UCPI_UBH(ucpi));
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300287 ufs_mark_sb_dirty(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800289 inode->i_ino = cg * uspi->s_ipg + bit;
Dmitry Monakhovbe8ded52010-03-04 17:32:23 +0300290 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 inode->i_blocks = 0;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800292 inode->i_generation = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
294 ufsi->i_flags = UFS_I(dir)->i_flags;
295 ufsi->i_lastfrag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 ufsi->i_shadow = 0;
297 ufsi->i_osync = 0;
298 ufsi->i_oeftflag = 0;
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700299 ufsi->i_dir_start_lookup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 insert_inode_hash(inode);
302 mark_inode_dirty(inode);
303
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800304 if (uspi->fs_magic == UFS2_MAGIC) {
305 struct buffer_head *bh;
306 struct ufs2_inode *ufs2_inode;
307
308 /*
309 * setup birth date, we do it here because of there is no sense
310 * to hold it in struct ufs_inode_info, and lose 64 bit
311 */
312 bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
313 if (!bh) {
314 ufs_warning(sb, "ufs_read_inode",
315 "unable to read inode %lu\n",
316 inode->i_ino);
317 err = -EIO;
318 goto fail_remove_inode;
319 }
320 lock_buffer(bh);
321 ufs2_inode = (struct ufs2_inode *)bh->b_data;
322 ufs2_inode += ufs_inotofsbo(inode->i_ino);
Evgeniy Dushistov21898502007-03-16 13:38:07 -0800323 ufs2_inode->ui_birthtime = cpu_to_fs64(sb, CURRENT_TIME.tv_sec);
324 ufs2_inode->ui_birthnsec = cpu_to_fs32(sb, CURRENT_TIME.tv_nsec);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800325 mark_buffer_dirty(bh);
326 unlock_buffer(bh);
327 if (sb->s_flags & MS_SYNCHRONOUS)
328 sync_dirty_buffer(bh);
329 brelse(bh);
330 }
331
Marco Stornellib6963322012-10-06 12:42:28 +0200332 mutex_unlock(&sbi->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700334 UFSD("allocating inode %lu\n", inode->i_ino);
335 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return inode;
337
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800338fail_remove_inode:
Marco Stornellib6963322012-10-06 12:42:28 +0200339 mutex_unlock(&sbi->s_lock);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200340 clear_nlink(inode);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800341 iput(inode);
342 UFSD("EXIT (FAILED): err %d\n", err);
343 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344failed:
Marco Stornellib6963322012-10-06 12:42:28 +0200345 mutex_unlock(&sbi->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 make_bad_inode(inode);
347 iput (inode);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800348 UFSD("EXIT (FAILED): err %d\n", err);
349 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}