blob: 2ad1259c6ecaacd7f6130859c42cb8654fb0e991 [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
21 */
22
23#include <linux/fs.h>
24#include <linux/ufs_fs.h>
25#include <linux/time.h>
26#include <linux/stat.h>
27#include <linux/string.h>
28#include <linux/quotaops.h>
29#include <linux/buffer_head.h>
30#include <linux/sched.h>
31#include <linux/bitops.h>
32#include <asm/byteorder.h>
33
34#include "swab.h"
35#include "util.h"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * NOTE! When we get the inode, we're the only people
39 * that have access to it, and as such there are no
40 * race conditions we have to worry about. The inode
41 * is not on the hash-lists, and it cannot be reached
42 * through the filesystem because the directory entry
43 * has been deleted earlier.
44 *
45 * HOWEVER: we must make sure that we get no aliases,
46 * which means that we have to call "clear_inode()"
47 * _before_ we mark the inode not in use in the inode
48 * bitmaps. Otherwise a newly created file might use
49 * the same inode number (not actually the same pointer
50 * though), and then we'd have two inodes sharing the
51 * same inode number and space on the harddisk.
52 */
53void ufs_free_inode (struct inode * inode)
54{
55 struct super_block * sb;
56 struct ufs_sb_private_info * uspi;
57 struct ufs_super_block_first * usb1;
58 struct ufs_cg_private_info * ucpi;
59 struct ufs_cylinder_group * ucg;
60 int is_directory;
61 unsigned ino, cg, bit;
62
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070063 UFSD("ENTER, ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 sb = inode->i_sb;
66 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +030067 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 ino = inode->i_ino;
70
71 lock_super (sb);
72
73 if (!((ino > 1) && (ino < (uspi->s_ncg * uspi->s_ipg )))) {
74 ufs_warning(sb, "ufs_free_inode", "reserved inode or nonexistent inode %u\n", ino);
75 unlock_super (sb);
76 return;
77 }
78
79 cg = ufs_inotocg (ino);
80 bit = ufs_inotocgoff (ino);
81 ucpi = ufs_load_cylinder (sb, cg);
82 if (!ucpi) {
83 unlock_super (sb);
84 return;
85 }
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -070086 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (!ufs_cg_chkmagic(sb, ucg))
88 ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number");
89
90 ucg->cg_time = cpu_to_fs32(sb, get_seconds());
91
92 is_directory = S_ISDIR(inode->i_mode);
93
94 DQUOT_FREE_INODE(inode);
95 DQUOT_DROP(inode);
96
97 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/*
129 * There are two policies for allocating an inode. If the new inode is
130 * a directory, then a forward search is made for a block group with both
131 * free space and a low directory-to-inode ratio; if that fails, then of
132 * the groups with above-average free space, that group with the fewest
133 * directories already is chosen.
134 *
135 * For other inodes, search forward from the parent directory's block
136 * group to find a free inode.
137 */
138struct inode * ufs_new_inode(struct inode * dir, int mode)
139{
140 struct super_block * sb;
141 struct ufs_sb_info * sbi;
142 struct ufs_sb_private_info * uspi;
143 struct ufs_super_block_first * usb1;
144 struct ufs_cg_private_info * ucpi;
145 struct ufs_cylinder_group * ucg;
146 struct inode * inode;
147 unsigned cg, bit, i, j, start;
148 struct ufs_inode_info *ufsi;
149
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700150 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 /* Cannot create files in a deleted directory */
153 if (!dir || !dir->i_nlink)
154 return ERR_PTR(-EPERM);
155 sb = dir->i_sb;
156 inode = new_inode(sb);
157 if (!inode)
158 return ERR_PTR(-ENOMEM);
159 ufsi = UFS_I(inode);
160 sbi = UFS_SB(sb);
161 uspi = sbi->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300162 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 lock_super (sb);
165
166 /*
167 * Try to place the inode in its parent directory
168 */
169 i = ufs_inotocg(dir->i_ino);
170 if (sbi->fs_cs(i).cs_nifree) {
171 cg = i;
172 goto cg_found;
173 }
174
175 /*
176 * Use a quadratic hash to find a group with a free inode
177 */
178 for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
179 i += j;
180 if (i >= uspi->s_ncg)
181 i -= uspi->s_ncg;
182 if (sbi->fs_cs(i).cs_nifree) {
183 cg = i;
184 goto cg_found;
185 }
186 }
187
188 /*
189 * That failed: try linear search for a free inode
190 */
191 i = ufs_inotocg(dir->i_ino) + 1;
192 for (j = 2; j < uspi->s_ncg; j++) {
193 i++;
194 if (i >= uspi->s_ncg)
195 i = 0;
196 if (sbi->fs_cs(i).cs_nifree) {
197 cg = i;
198 goto cg_found;
199 }
200 }
201
202 goto failed;
203
204cg_found:
205 ucpi = ufs_load_cylinder (sb, cg);
206 if (!ucpi)
207 goto failed;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700208 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (!ufs_cg_chkmagic(sb, ucg))
210 ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
211
212 start = ucpi->c_irotor;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700213 bit = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, uspi->s_ipg, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (!(bit < uspi->s_ipg)) {
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700215 bit = ubh_find_first_zero_bit (UCPI_UBH(ucpi), ucpi->c_iusedoff, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (!(bit < start)) {
217 ufs_error (sb, "ufs_new_inode",
218 "cylinder group %u corrupted - error in inode bitmap\n", cg);
219 goto failed;
220 }
221 }
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700222 UFSD("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg);
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700223 if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit))
224 ubh_setbit (UCPI_UBH(ucpi), ucpi->c_iusedoff, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 else {
226 ufs_panic (sb, "ufs_new_inode", "internal error");
227 goto failed;
228 }
229
230 fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700231 uspi->cs_total.cs_nifree--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1);
233
234 if (S_ISDIR(mode)) {
235 fs32_add(sb, &ucg->cg_cs.cs_ndir, 1);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700236 uspi->cs_total.cs_ndir++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1);
238 }
239
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700240 ubh_mark_buffer_dirty (USPI_UBH(uspi));
241 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (sb->s_flags & MS_SYNCHRONOUS) {
Evgeniy Dushistov098d5af2006-06-25 05:47:31 -0700243 ubh_ll_rw_block(SWRITE, UCPI_UBH(ucpi));
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700244 ubh_wait_on_buffer (UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246 sb->s_dirt = 1;
247
248 inode->i_mode = mode;
249 inode->i_uid = current->fsuid;
250 if (dir->i_mode & S_ISGID) {
251 inode->i_gid = dir->i_gid;
252 if (S_ISDIR(mode))
253 inode->i_mode |= S_ISGID;
254 } else
255 inode->i_gid = current->fsgid;
256
257 inode->i_ino = cg * uspi->s_ipg + bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 inode->i_blocks = 0;
259 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
260 ufsi->i_flags = UFS_I(dir)->i_flags;
261 ufsi->i_lastfrag = 0;
262 ufsi->i_gen = 0;
263 ufsi->i_shadow = 0;
264 ufsi->i_osync = 0;
265 ufsi->i_oeftflag = 0;
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700266 ufsi->i_dir_start_lookup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1));
268
269 insert_inode_hash(inode);
270 mark_inode_dirty(inode);
271
272 unlock_super (sb);
273
274 if (DQUOT_ALLOC_INODE(inode)) {
275 DQUOT_DROP(inode);
276 inode->i_flags |= S_NOQUOTA;
277 inode->i_nlink = 0;
278 iput(inode);
279 return ERR_PTR(-EDQUOT);
280 }
281
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700282 UFSD("allocating inode %lu\n", inode->i_ino);
283 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return inode;
285
286failed:
287 unlock_super (sb);
288 make_bad_inode(inode);
289 iput (inode);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700290 UFSD("EXIT (FAILED)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return ERR_PTR(-ENOSPC);
292}