USB: remove duplicate device id from usb_storage
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / hpfs / alloc.c
blob5503e2c28910102bb0a2a22b97d8e2a7bf20cbad
1 /*
2 * linux/fs/hpfs/alloc.c
4 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
6 * HPFS bitmap operations
7 */
9 #include "hpfs_fn.h"
11 static int hpfs_alloc_if_possible_nolock(struct super_block *s, secno sec);
14 * Check if a sector is allocated in bitmap
15 * This is really slow. Turned on only if chk==2
18 static int chk_if_allocated(struct super_block *s, secno sec, char *msg)
20 struct quad_buffer_head qbh;
21 unsigned *bmp;
22 if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "chk"))) goto fail;
23 if ((bmp[(sec & 0x3fff) >> 5] >> (sec & 0x1f)) & 1) {
24 hpfs_error(s, "sector '%s' - %08x not allocated in bitmap", msg, sec);
25 goto fail1;
27 hpfs_brelse4(&qbh);
28 if (sec >= hpfs_sb(s)->sb_dirband_start && sec < hpfs_sb(s)->sb_dirband_start + hpfs_sb(s)->sb_dirband_size) {
29 unsigned ssec = (sec - hpfs_sb(s)->sb_dirband_start) / 4;
30 if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) goto fail;
31 if ((bmp[ssec >> 5] >> (ssec & 0x1f)) & 1) {
32 hpfs_error(s, "sector '%s' - %08x not allocated in directory bitmap", msg, sec);
33 goto fail1;
35 hpfs_brelse4(&qbh);
37 return 0;
38 fail1:
39 hpfs_brelse4(&qbh);
40 fail:
41 return 1;
45 * Check if sector(s) have proper number and additionally check if they're
46 * allocated in bitmap.
49 int hpfs_chk_sectors(struct super_block *s, secno start, int len, char *msg)
51 if (start + len < start || start < 0x12 ||
52 start + len > hpfs_sb(s)->sb_fs_size) {
53 hpfs_error(s, "sector(s) '%s' badly placed at %08x", msg, start);
54 return 1;
56 if (hpfs_sb(s)->sb_chk>=2) {
57 int i;
58 for (i = 0; i < len; i++)
59 if (chk_if_allocated(s, start + i, msg)) return 1;
61 return 0;
64 static secno alloc_in_bmp(struct super_block *s, secno near, unsigned n, unsigned forward)
66 struct quad_buffer_head qbh;
67 unsigned *bmp;
68 unsigned bs = near & ~0x3fff;
69 unsigned nr = (near & 0x3fff) & ~(n - 1);
70 /*unsigned mnr;*/
71 unsigned i, q;
72 int a, b;
73 secno ret = 0;
74 if (n != 1 && n != 4) {
75 hpfs_error(s, "Bad allocation size: %d", n);
76 return 0;
78 lock_super(s);
79 if (bs != ~0x3fff) {
80 if (!(bmp = hpfs_map_bitmap(s, near >> 14, &qbh, "aib"))) goto uls;
81 } else {
82 if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) goto uls;
84 if (!tstbits(bmp, nr, n + forward)) {
85 ret = bs + nr;
86 goto rt;
88 /*if (!tstbits(bmp, nr + n, n + forward)) {
89 ret = bs + nr + n;
90 goto rt;
91 }*/
92 q = nr + n; b = 0;
93 while ((a = tstbits(bmp, q, n + forward)) != 0) {
94 q += a;
95 if (n != 1) q = ((q-1)&~(n-1))+n;
96 if (!b) {
97 if (q>>5 != nr>>5) {
98 b = 1;
99 q = nr & 0x1f;
101 } else if (q > nr) break;
103 if (!a) {
104 ret = bs + q;
105 goto rt;
107 nr >>= 5;
108 /*for (i = nr + 1; i != nr; i++, i &= 0x1ff) {*/
109 i = nr;
110 do {
111 if (!bmp[i]) goto cont;
112 if (n + forward >= 0x3f && bmp[i] != -1) goto cont;
113 q = i<<5;
114 if (i > 0) {
115 unsigned k = bmp[i-1];
116 while (k & 0x80000000) {
117 q--; k <<= 1;
120 if (n != 1) q = ((q-1)&~(n-1))+n;
121 while ((a = tstbits(bmp, q, n + forward)) != 0) {
122 q += a;
123 if (n != 1) q = ((q-1)&~(n-1))+n;
124 if (q>>5 > i) break;
126 if (!a) {
127 ret = bs + q;
128 goto rt;
130 cont:
131 i++, i &= 0x1ff;
132 } while (i != nr);
134 if (ret) {
135 if (hpfs_sb(s)->sb_chk && ((ret >> 14) != (bs >> 14) || (bmp[(ret & 0x3fff) >> 5] | ~(((1 << n) - 1) << (ret & 0x1f))) != 0xffffffff)) {
136 hpfs_error(s, "Allocation doesn't work! Wanted %d, allocated at %08x", n, ret);
137 ret = 0;
138 goto b;
140 bmp[(ret & 0x3fff) >> 5] &= ~(((1 << n) - 1) << (ret & 0x1f));
141 hpfs_mark_4buffers_dirty(&qbh);
144 hpfs_brelse4(&qbh);
145 uls:
146 unlock_super(s);
147 return ret;
151 * Allocation strategy: 1) search place near the sector specified
152 * 2) search bitmap where free sectors last found
153 * 3) search all bitmaps
154 * 4) search all bitmaps ignoring number of pre-allocated
155 * sectors
158 secno hpfs_alloc_sector(struct super_block *s, secno near, unsigned n, int forward, int lock)
160 secno sec;
161 int i;
162 unsigned n_bmps;
163 struct hpfs_sb_info *sbi = hpfs_sb(s);
164 int f_p = 0;
165 int near_bmp;
166 if (forward < 0) {
167 forward = -forward;
168 f_p = 1;
170 if (lock) hpfs_lock_creation(s);
171 n_bmps = (sbi->sb_fs_size + 0x4000 - 1) >> 14;
172 if (near && near < sbi->sb_fs_size) {
173 if ((sec = alloc_in_bmp(s, near, n, f_p ? forward : forward/4))) goto ret;
174 near_bmp = near >> 14;
175 } else near_bmp = n_bmps / 2;
177 if (b != -1) {
178 if ((sec = alloc_in_bmp(s, b<<14, n, f_p ? forward : forward/2))) {
179 b &= 0x0fffffff;
180 goto ret;
182 if (b > 0x10000000) if ((sec = alloc_in_bmp(s, (b&0xfffffff)<<14, n, f_p ? forward : 0))) goto ret;
184 if (!f_p) if (forward > sbi->sb_max_fwd_alloc) forward = sbi->sb_max_fwd_alloc;
185 less_fwd:
186 for (i = 0; i < n_bmps; i++) {
187 if (near_bmp+i < n_bmps && ((sec = alloc_in_bmp(s, (near_bmp+i) << 14, n, forward)))) {
188 sbi->sb_c_bitmap = near_bmp+i;
189 goto ret;
191 if (!forward) {
192 if (near_bmp-i-1 >= 0 && ((sec = alloc_in_bmp(s, (near_bmp-i-1) << 14, n, forward)))) {
193 sbi->sb_c_bitmap = near_bmp-i-1;
194 goto ret;
196 } else {
197 if (near_bmp+i >= n_bmps && ((sec = alloc_in_bmp(s, (near_bmp+i-n_bmps) << 14, n, forward)))) {
198 sbi->sb_c_bitmap = near_bmp+i-n_bmps;
199 goto ret;
202 if (i == 1 && sbi->sb_c_bitmap != -1 && ((sec = alloc_in_bmp(s, (sbi->sb_c_bitmap) << 14, n, forward)))) {
203 goto ret;
206 if (!f_p) {
207 if (forward) {
208 sbi->sb_max_fwd_alloc = forward * 3 / 4;
209 forward /= 2;
210 goto less_fwd;
213 sec = 0;
214 ret:
215 if (sec && f_p) {
216 for (i = 0; i < forward; i++) {
217 if (!hpfs_alloc_if_possible_nolock(s, sec + i + 1)) {
218 hpfs_error(s, "Prealloc doesn't work! Wanted %d, allocated at %08x, can't allocate %d", forward, sec, i);
219 sec = 0;
220 break;
224 if (lock) hpfs_unlock_creation(s);
225 return sec;
228 static secno alloc_in_dirband(struct super_block *s, secno near, int lock)
230 unsigned nr = near;
231 secno sec;
232 struct hpfs_sb_info *sbi = hpfs_sb(s);
233 if (nr < sbi->sb_dirband_start)
234 nr = sbi->sb_dirband_start;
235 if (nr >= sbi->sb_dirband_start + sbi->sb_dirband_size)
236 nr = sbi->sb_dirband_start + sbi->sb_dirband_size - 4;
237 nr -= sbi->sb_dirband_start;
238 nr >>= 2;
239 if (lock) hpfs_lock_creation(s);
240 sec = alloc_in_bmp(s, (~0x3fff) | nr, 1, 0);
241 if (lock) hpfs_unlock_creation(s);
242 if (!sec) return 0;
243 return ((sec & 0x3fff) << 2) + sbi->sb_dirband_start;
246 /* Alloc sector if it's free */
248 static int hpfs_alloc_if_possible_nolock(struct super_block *s, secno sec)
250 struct quad_buffer_head qbh;
251 unsigned *bmp;
252 lock_super(s);
253 if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "aip"))) goto end;
254 if (bmp[(sec & 0x3fff) >> 5] & (1 << (sec & 0x1f))) {
255 bmp[(sec & 0x3fff) >> 5] &= ~(1 << (sec & 0x1f));
256 hpfs_mark_4buffers_dirty(&qbh);
257 hpfs_brelse4(&qbh);
258 unlock_super(s);
259 return 1;
261 hpfs_brelse4(&qbh);
262 end:
263 unlock_super(s);
264 return 0;
267 int hpfs_alloc_if_possible(struct super_block *s, secno sec)
269 int r;
270 hpfs_lock_creation(s);
271 r = hpfs_alloc_if_possible_nolock(s, sec);
272 hpfs_unlock_creation(s);
273 return r;
276 /* Free sectors in bitmaps */
278 void hpfs_free_sectors(struct super_block *s, secno sec, unsigned n)
280 struct quad_buffer_head qbh;
281 unsigned *bmp;
282 struct hpfs_sb_info *sbi = hpfs_sb(s);
283 /*printk("2 - ");*/
284 if (!n) return;
285 if (sec < 0x12) {
286 hpfs_error(s, "Trying to free reserved sector %08x", sec);
287 return;
289 lock_super(s);
290 sbi->sb_max_fwd_alloc += n > 0xffff ? 0xffff : n;
291 if (sbi->sb_max_fwd_alloc > 0xffffff) sbi->sb_max_fwd_alloc = 0xffffff;
292 new_map:
293 if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "free"))) {
294 unlock_super(s);
295 return;
297 new_tst:
298 if ((bmp[(sec & 0x3fff) >> 5] >> (sec & 0x1f) & 1)) {
299 hpfs_error(s, "sector %08x not allocated", sec);
300 hpfs_brelse4(&qbh);
301 unlock_super(s);
302 return;
304 bmp[(sec & 0x3fff) >> 5] |= 1 << (sec & 0x1f);
305 if (!--n) {
306 hpfs_mark_4buffers_dirty(&qbh);
307 hpfs_brelse4(&qbh);
308 unlock_super(s);
309 return;
311 if (!(++sec & 0x3fff)) {
312 hpfs_mark_4buffers_dirty(&qbh);
313 hpfs_brelse4(&qbh);
314 goto new_map;
316 goto new_tst;
320 * Check if there are at least n free dnodes on the filesystem.
321 * Called before adding to dnode. If we run out of space while
322 * splitting dnodes, it would corrupt dnode tree.
325 int hpfs_check_free_dnodes(struct super_block *s, int n)
327 int n_bmps = (hpfs_sb(s)->sb_fs_size + 0x4000 - 1) >> 14;
328 int b = hpfs_sb(s)->sb_c_bitmap & 0x0fffffff;
329 int i, j;
330 unsigned *bmp;
331 struct quad_buffer_head qbh;
332 if ((bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
333 for (j = 0; j < 512; j++) {
334 unsigned k;
335 if (!bmp[j]) continue;
336 for (k = bmp[j]; k; k >>= 1) if (k & 1) if (!--n) {
337 hpfs_brelse4(&qbh);
338 return 0;
342 hpfs_brelse4(&qbh);
343 i = 0;
344 if (hpfs_sb(s)->sb_c_bitmap != -1) {
345 bmp = hpfs_map_bitmap(s, b, &qbh, "chkdn1");
346 goto chk_bmp;
348 chk_next:
349 if (i == b) i++;
350 if (i >= n_bmps) return 1;
351 bmp = hpfs_map_bitmap(s, i, &qbh, "chkdn2");
352 chk_bmp:
353 if (bmp) {
354 for (j = 0; j < 512; j++) {
355 unsigned k;
356 if (!bmp[j]) continue;
357 for (k = 0xf; k; k <<= 4)
358 if ((bmp[j] & k) == k) {
359 if (!--n) {
360 hpfs_brelse4(&qbh);
361 return 0;
365 hpfs_brelse4(&qbh);
367 i++;
368 goto chk_next;
371 void hpfs_free_dnode(struct super_block *s, dnode_secno dno)
373 if (hpfs_sb(s)->sb_chk) if (dno & 3) {
374 hpfs_error(s, "hpfs_free_dnode: dnode %08x not aligned", dno);
375 return;
377 if (dno < hpfs_sb(s)->sb_dirband_start ||
378 dno >= hpfs_sb(s)->sb_dirband_start + hpfs_sb(s)->sb_dirband_size) {
379 hpfs_free_sectors(s, dno, 4);
380 } else {
381 struct quad_buffer_head qbh;
382 unsigned *bmp;
383 unsigned ssec = (dno - hpfs_sb(s)->sb_dirband_start) / 4;
384 lock_super(s);
385 if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
386 unlock_super(s);
387 return;
389 bmp[ssec >> 5] |= 1 << (ssec & 0x1f);
390 hpfs_mark_4buffers_dirty(&qbh);
391 hpfs_brelse4(&qbh);
392 unlock_super(s);
396 struct dnode *hpfs_alloc_dnode(struct super_block *s, secno near,
397 dnode_secno *dno, struct quad_buffer_head *qbh,
398 int lock)
400 struct dnode *d;
401 if (hpfs_count_one_bitmap(s, hpfs_sb(s)->sb_dmap) > FREE_DNODES_ADD) {
402 if (!(*dno = alloc_in_dirband(s, near, lock)))
403 if (!(*dno = hpfs_alloc_sector(s, near, 4, 0, lock))) return NULL;
404 } else {
405 if (!(*dno = hpfs_alloc_sector(s, near, 4, 0, lock)))
406 if (!(*dno = alloc_in_dirband(s, near, lock))) return NULL;
408 if (!(d = hpfs_get_4sectors(s, *dno, qbh))) {
409 hpfs_free_dnode(s, *dno);
410 return NULL;
412 memset(d, 0, 2048);
413 d->magic = DNODE_MAGIC;
414 d->first_free = 52;
415 d->dirent[0] = 32;
416 d->dirent[2] = 8;
417 d->dirent[30] = 1;
418 d->dirent[31] = 255;
419 d->self = *dno;
420 return d;
423 struct fnode *hpfs_alloc_fnode(struct super_block *s, secno near, fnode_secno *fno,
424 struct buffer_head **bh)
426 struct fnode *f;
427 if (!(*fno = hpfs_alloc_sector(s, near, 1, FNODE_ALLOC_FWD, 1))) return NULL;
428 if (!(f = hpfs_get_sector(s, *fno, bh))) {
429 hpfs_free_sectors(s, *fno, 1);
430 return NULL;
432 memset(f, 0, 512);
433 f->magic = FNODE_MAGIC;
434 f->ea_offs = 0xc4;
435 f->btree.n_free_nodes = 8;
436 f->btree.first_free = 8;
437 return f;
440 struct anode *hpfs_alloc_anode(struct super_block *s, secno near, anode_secno *ano,
441 struct buffer_head **bh)
443 struct anode *a;
444 if (!(*ano = hpfs_alloc_sector(s, near, 1, ANODE_ALLOC_FWD, 1))) return NULL;
445 if (!(a = hpfs_get_sector(s, *ano, bh))) {
446 hpfs_free_sectors(s, *ano, 1);
447 return NULL;
449 memset(a, 0, 512);
450 a->magic = ANODE_MAGIC;
451 a->self = *ano;
452 a->btree.n_free_nodes = 40;
453 a->btree.n_used_nodes = 0;
454 a->btree.first_free = 8;
455 return a;