blob: 7e260bc0d94f66719f40a71bebdfea19fb09c859 [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
Christoph Hellwigbcd6d4e2007-10-16 23:26:51 -070037#include "ufs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "swab.h"
39#include "util.h"
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/*
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.
48 *
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.
56 */
57void ufs_free_inode (struct inode * inode)
58{
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;
66
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070067 UFSD("ENTER, ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 sb = inode->i_sb;
70 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +030071 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 ino = inode->i_ino;
74
75 lock_super (sb);
76
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;
81 }
82
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;
89 }
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -070090 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (!ufs_cg_chkmagic(sb, ucg))
92 ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number");
93
94 ucg->cg_time = cpu_to_fs32(sb, get_seconds());
95
96 is_directory = S_ISDIR(inode->i_mode);
97
98 DQUOT_FREE_INODE(inode);
99 DQUOT_DROP(inode);
100
101 clear_inode (inode);
102
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700103 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino);
105 else {
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700106 ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (ino < ucpi->c_irotor)
108 ucpi->c_irotor = ino;
109 fs32_add(sb, &ucg->cg_cs.cs_nifree, 1);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700110 uspi->cs_total.cs_nifree++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 fs32_add(sb, &UFS_SB(sb)->fs_cs(cg).cs_nifree, 1);
112
113 if (is_directory) {
114 fs32_sub(sb, &ucg->cg_cs.cs_ndir, 1);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700115 uspi->cs_total.cs_ndir--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 fs32_sub(sb, &UFS_SB(sb)->fs_cs(cg).cs_ndir, 1);
117 }
118 }
119
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700120 ubh_mark_buffer_dirty (USPI_UBH(uspi));
121 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (sb->s_flags & MS_SYNCHRONOUS) {
Evgeniy Dushistov098d5af2006-06-25 05:47:31 -0700123 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700124 ubh_wait_on_buffer (UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126
127 sb->s_dirt = 1;
128 unlock_super (sb);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700129 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132/*
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800133 * 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
137 */
138static void ufs2_init_inodes_chunk(struct super_block *sb,
139 struct ufs_cg_private_info *ucpi,
140 struct ufs_cylinder_group *ucg)
141{
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;
148
149 UFSD("ENTER cgno %d\n", ucpi->c_cgx);
150
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);
161 }
162
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));
168 }
169
170 UFSD("EXIT\n");
171}
172
173/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * 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.
179 *
180 * For other inodes, search forward from the parent directory's block
181 * group to find a free inode.
182 */
183struct inode * ufs_new_inode(struct inode * dir, int mode)
184{
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;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800194 int err = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700196 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
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;
Evgeniy7b4ee732006-01-14 11:42:06 +0300208 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 lock_super (sb);
211
212 /*
213 * Try to place the inode in its parent directory
214 */
215 i = ufs_inotocg(dir->i_ino);
216 if (sbi->fs_cs(i).cs_nifree) {
217 cg = i;
218 goto cg_found;
219 }
220
221 /*
222 * Use a quadratic hash to find a group with a free inode
223 */
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;
231 }
232 }
233
234 /*
235 * That failed: try linear search for a free inode
236 */
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;
245 }
246 }
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto failed;
249
250cg_found:
251 ucpi = ufs_load_cylinder (sb, cg);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800252 if (!ucpi) {
253 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto failed;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800255 }
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700256 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (!ufs_cg_chkmagic(sb, ucg))
258 ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
259
260 start = ucpi->c_irotor;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700261 bit = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, uspi->s_ipg, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (!(bit < uspi->s_ipg)) {
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700263 bit = ubh_find_first_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (!(bit < start)) {
265 ufs_error (sb, "ufs_new_inode",
266 "cylinder group %u corrupted - error in inode bitmap\n", cg);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800267 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 goto failed;
269 }
270 }
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700271 UFSD("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg);
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700272 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
273 ubh_setbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 else {
275 ufs_panic (sb, "ufs_new_inode", "internal error");
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800276 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 goto failed;
278 }
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800279
280 if (uspi->fs_magic == UFS2_MAGIC) {
281 u32 initediblk = fs32_to_cpu(sb, ucg->cg_u.cg_u2.cg_initediblk);
282
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);
286 }
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700289 uspi->cs_total.cs_nifree--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1);
291
292 if (S_ISDIR(mode)) {
293 fs32_add(sb, &ucg->cg_cs.cs_ndir, 1);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700294 uspi->cs_total.cs_ndir++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1);
296 }
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700297 ubh_mark_buffer_dirty (USPI_UBH(uspi));
298 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (sb->s_flags & MS_SYNCHRONOUS) {
Evgeniy Dushistov098d5af2006-06-25 05:47:31 -0700300 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700301 ubh_wait_on_buffer (UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303 sb->s_dirt = 1;
304
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800305 inode->i_ino = cg * uspi->s_ipg + bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 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;
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 inode->i_blocks = 0;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800316 inode->i_generation = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 ufsi->i_shadow = 0;
321 ufsi->i_osync = 0;
322 ufsi->i_oeftflag = 0;
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700323 ufsi->i_dir_start_lookup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 insert_inode_hash(inode);
326 mark_inode_dirty(inode);
327
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800328 if (uspi->fs_magic == UFS2_MAGIC) {
329 struct buffer_head *bh;
330 struct ufs2_inode *ufs2_inode;
331
332 /*
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
335 */
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;
343 }
344 lock_buffer(bh);
345 ufs2_inode = (struct ufs2_inode *)bh->b_data;
346 ufs2_inode += ufs_inotofsbo(inode->i_ino);
Evgeniy Dushistov21898502007-03-16 13:38:07 -0800347 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);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800349 mark_buffer_dirty(bh);
350 unlock_buffer(bh);
351 if (sb->s_flags & MS_SYNCHRONOUS)
352 sync_dirty_buffer(bh);
353 brelse(bh);
354 }
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 unlock_super (sb);
357
358 if (DQUOT_ALLOC_INODE(inode)) {
359 DQUOT_DROP(inode);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800360 err = -EDQUOT;
361 goto fail_without_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700364 UFSD("allocating inode %lu\n", inode->i_ino);
365 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return inode;
367
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800368fail_remove_inode:
369 unlock_super(sb);
370fail_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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376failed:
377 unlock_super (sb);
378 make_bad_inode(inode);
379 iput (inode);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800380 UFSD("EXIT (FAILED): err %d\n", err);
381 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}