Linux-2.6.12-rc2
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / ufs / ialloc.c
1 /*
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
37 #undef UFS_IALLOC_DEBUG
38
39 #ifdef UFS_IALLOC_DEBUG
40 #define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x;
41 #else
42 #define UFSD(x)
43 #endif
44
45 /*
46 * NOTE! When we get the inode, we're the only people
47 * that have access to it, and as such there are no
48 * race conditions we have to worry about. The inode
49 * is not on the hash-lists, and it cannot be reached
50 * through the filesystem because the directory entry
51 * has been deleted earlier.
52 *
53 * HOWEVER: we must make sure that we get no aliases,
54 * which means that we have to call "clear_inode()"
55 * _before_ we mark the inode not in use in the inode
56 * bitmaps. Otherwise a newly created file might use
57 * the same inode number (not actually the same pointer
58 * though), and then we'd have two inodes sharing the
59 * same inode number and space on the harddisk.
60 */
61 void ufs_free_inode (struct inode * inode)
62 {
63 struct super_block * sb;
64 struct ufs_sb_private_info * uspi;
65 struct ufs_super_block_first * usb1;
66 struct ufs_cg_private_info * ucpi;
67 struct ufs_cylinder_group * ucg;
68 int is_directory;
69 unsigned ino, cg, bit;
70
71 UFSD(("ENTER, ino %lu\n", inode->i_ino))
72
73 sb = inode->i_sb;
74 uspi = UFS_SB(sb)->s_uspi;
75 usb1 = ubh_get_usb_first(USPI_UBH);
76
77 ino = inode->i_ino;
78
79 lock_super (sb);
80
81 if (!((ino > 1) && (ino < (uspi->s_ncg * uspi->s_ipg )))) {
82 ufs_warning(sb, "ufs_free_inode", "reserved inode or nonexistent inode %u\n", ino);
83 unlock_super (sb);
84 return;
85 }
86
87 cg = ufs_inotocg (ino);
88 bit = ufs_inotocgoff (ino);
89 ucpi = ufs_load_cylinder (sb, cg);
90 if (!ucpi) {
91 unlock_super (sb);
92 return;
93 }
94 ucg = ubh_get_ucg(UCPI_UBH);
95 if (!ufs_cg_chkmagic(sb, ucg))
96 ufs_panic (sb, "ufs_free_fragments", "internal error, bad cg magic number");
97
98 ucg->cg_time = cpu_to_fs32(sb, get_seconds());
99
100 is_directory = S_ISDIR(inode->i_mode);
101
102 DQUOT_FREE_INODE(inode);
103 DQUOT_DROP(inode);
104
105 clear_inode (inode);
106
107 if (ubh_isclr (UCPI_UBH, ucpi->c_iusedoff, bit))
108 ufs_error(sb, "ufs_free_inode", "bit already cleared for inode %u", ino);
109 else {
110 ubh_clrbit (UCPI_UBH, ucpi->c_iusedoff, bit);
111 if (ino < ucpi->c_irotor)
112 ucpi->c_irotor = ino;
113 fs32_add(sb, &ucg->cg_cs.cs_nifree, 1);
114 fs32_add(sb, &usb1->fs_cstotal.cs_nifree, 1);
115 fs32_add(sb, &UFS_SB(sb)->fs_cs(cg).cs_nifree, 1);
116
117 if (is_directory) {
118 fs32_sub(sb, &ucg->cg_cs.cs_ndir, 1);
119 fs32_sub(sb, &usb1->fs_cstotal.cs_ndir, 1);
120 fs32_sub(sb, &UFS_SB(sb)->fs_cs(cg).cs_ndir, 1);
121 }
122 }
123
124 ubh_mark_buffer_dirty (USPI_UBH);
125 ubh_mark_buffer_dirty (UCPI_UBH);
126 if (sb->s_flags & MS_SYNCHRONOUS) {
127 ubh_wait_on_buffer (UCPI_UBH);
128 ubh_ll_rw_block (WRITE, 1, (struct ufs_buffer_head **) &ucpi);
129 ubh_wait_on_buffer (UCPI_UBH);
130 }
131
132 sb->s_dirt = 1;
133 unlock_super (sb);
134 UFSD(("EXIT\n"))
135 }
136
137 /*
138 * There are two policies for allocating an inode. If the new inode is
139 * a directory, then a forward search is made for a block group with both
140 * free space and a low directory-to-inode ratio; if that fails, then of
141 * the groups with above-average free space, that group with the fewest
142 * directories already is chosen.
143 *
144 * For other inodes, search forward from the parent directory's block
145 * group to find a free inode.
146 */
147 struct inode * ufs_new_inode(struct inode * dir, int mode)
148 {
149 struct super_block * sb;
150 struct ufs_sb_info * sbi;
151 struct ufs_sb_private_info * uspi;
152 struct ufs_super_block_first * usb1;
153 struct ufs_cg_private_info * ucpi;
154 struct ufs_cylinder_group * ucg;
155 struct inode * inode;
156 unsigned cg, bit, i, j, start;
157 struct ufs_inode_info *ufsi;
158
159 UFSD(("ENTER\n"))
160
161 /* Cannot create files in a deleted directory */
162 if (!dir || !dir->i_nlink)
163 return ERR_PTR(-EPERM);
164 sb = dir->i_sb;
165 inode = new_inode(sb);
166 if (!inode)
167 return ERR_PTR(-ENOMEM);
168 ufsi = UFS_I(inode);
169 sbi = UFS_SB(sb);
170 uspi = sbi->s_uspi;
171 usb1 = ubh_get_usb_first(USPI_UBH);
172
173 lock_super (sb);
174
175 /*
176 * Try to place the inode in its parent directory
177 */
178 i = ufs_inotocg(dir->i_ino);
179 if (sbi->fs_cs(i).cs_nifree) {
180 cg = i;
181 goto cg_found;
182 }
183
184 /*
185 * Use a quadratic hash to find a group with a free inode
186 */
187 for ( j = 1; j < uspi->s_ncg; j <<= 1 ) {
188 i += j;
189 if (i >= uspi->s_ncg)
190 i -= uspi->s_ncg;
191 if (sbi->fs_cs(i).cs_nifree) {
192 cg = i;
193 goto cg_found;
194 }
195 }
196
197 /*
198 * That failed: try linear search for a free inode
199 */
200 i = ufs_inotocg(dir->i_ino) + 1;
201 for (j = 2; j < uspi->s_ncg; j++) {
202 i++;
203 if (i >= uspi->s_ncg)
204 i = 0;
205 if (sbi->fs_cs(i).cs_nifree) {
206 cg = i;
207 goto cg_found;
208 }
209 }
210
211 goto failed;
212
213 cg_found:
214 ucpi = ufs_load_cylinder (sb, cg);
215 if (!ucpi)
216 goto failed;
217 ucg = ubh_get_ucg(UCPI_UBH);
218 if (!ufs_cg_chkmagic(sb, ucg))
219 ufs_panic (sb, "ufs_new_inode", "internal error, bad cg magic number");
220
221 start = ucpi->c_irotor;
222 bit = ubh_find_next_zero_bit (UCPI_UBH, ucpi->c_iusedoff, uspi->s_ipg, start);
223 if (!(bit < uspi->s_ipg)) {
224 bit = ubh_find_first_zero_bit (UCPI_UBH, ucpi->c_iusedoff, start);
225 if (!(bit < start)) {
226 ufs_error (sb, "ufs_new_inode",
227 "cylinder group %u corrupted - error in inode bitmap\n", cg);
228 goto failed;
229 }
230 }
231 UFSD(("start = %u, bit = %u, ipg = %u\n", start, bit, uspi->s_ipg))
232 if (ubh_isclr (UCPI_UBH, ucpi->c_iusedoff, bit))
233 ubh_setbit (UCPI_UBH, ucpi->c_iusedoff, bit);
234 else {
235 ufs_panic (sb, "ufs_new_inode", "internal error");
236 goto failed;
237 }
238
239 fs32_sub(sb, &ucg->cg_cs.cs_nifree, 1);
240 fs32_sub(sb, &usb1->fs_cstotal.cs_nifree, 1);
241 fs32_sub(sb, &sbi->fs_cs(cg).cs_nifree, 1);
242
243 if (S_ISDIR(mode)) {
244 fs32_add(sb, &ucg->cg_cs.cs_ndir, 1);
245 fs32_add(sb, &usb1->fs_cstotal.cs_ndir, 1);
246 fs32_add(sb, &sbi->fs_cs(cg).cs_ndir, 1);
247 }
248
249 ubh_mark_buffer_dirty (USPI_UBH);
250 ubh_mark_buffer_dirty (UCPI_UBH);
251 if (sb->s_flags & MS_SYNCHRONOUS) {
252 ubh_wait_on_buffer (UCPI_UBH);
253 ubh_ll_rw_block (WRITE, 1, (struct ufs_buffer_head **) &ucpi);
254 ubh_wait_on_buffer (UCPI_UBH);
255 }
256 sb->s_dirt = 1;
257
258 inode->i_mode = mode;
259 inode->i_uid = current->fsuid;
260 if (dir->i_mode & S_ISGID) {
261 inode->i_gid = dir->i_gid;
262 if (S_ISDIR(mode))
263 inode->i_mode |= S_ISGID;
264 } else
265 inode->i_gid = current->fsgid;
266
267 inode->i_ino = cg * uspi->s_ipg + bit;
268 inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */
269 inode->i_blocks = 0;
270 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
271 ufsi->i_flags = UFS_I(dir)->i_flags;
272 ufsi->i_lastfrag = 0;
273 ufsi->i_gen = 0;
274 ufsi->i_shadow = 0;
275 ufsi->i_osync = 0;
276 ufsi->i_oeftflag = 0;
277 memset(&ufsi->i_u1, 0, sizeof(ufsi->i_u1));
278
279 insert_inode_hash(inode);
280 mark_inode_dirty(inode);
281
282 unlock_super (sb);
283
284 if (DQUOT_ALLOC_INODE(inode)) {
285 DQUOT_DROP(inode);
286 inode->i_flags |= S_NOQUOTA;
287 inode->i_nlink = 0;
288 iput(inode);
289 return ERR_PTR(-EDQUOT);
290 }
291
292 UFSD(("allocating inode %lu\n", inode->i_ino))
293 UFSD(("EXIT\n"))
294 return inode;
295
296 failed:
297 unlock_super (sb);
298 make_bad_inode(inode);
299 iput (inode);
300 UFSD(("EXIT (FAILED)\n"))
301 return ERR_PTR(-ENOSPC);
302 }