ocfs2: don't pass handle to ocfs2_meta_lock in ocfs2_rename()
[deliverable/linux.git] / fs / ocfs2 / localalloc.c
CommitLineData
ccd979bd
MF
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * localalloc.c
5 *
6 * Node local data allocation
7 *
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
26#include <linux/fs.h>
27#include <linux/types.h>
28#include <linux/slab.h>
29#include <linux/highmem.h>
30#include <linux/bitops.h>
31
32#define MLOG_MASK_PREFIX ML_DISK_ALLOC
33#include <cluster/masklog.h>
34
35#include "ocfs2.h"
36
37#include "alloc.h"
38#include "dlmglue.h"
39#include "inode.h"
40#include "journal.h"
41#include "localalloc.h"
42#include "suballoc.h"
43#include "super.h"
44#include "sysfile.h"
45
46#include "buffer_head_io.h"
47
48#define OCFS2_LOCAL_ALLOC(dinode) (&((dinode)->id2.i_lab))
49
50static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb);
51
52static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc);
53
54static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
55 struct ocfs2_dinode *alloc,
56 u32 numbits);
57
58static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc);
59
60static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
61 struct ocfs2_journal_handle *handle,
62 struct ocfs2_dinode *alloc,
63 struct inode *main_bm_inode,
64 struct buffer_head *main_bm_bh);
65
66static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
67 struct ocfs2_journal_handle *handle,
68 struct ocfs2_alloc_context **ac,
69 struct inode **bitmap_inode,
70 struct buffer_head **bitmap_bh);
71
72static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
73 struct ocfs2_journal_handle *handle,
74 struct ocfs2_alloc_context *ac);
75
76static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
77 struct inode *local_alloc_inode);
78
79/*
80 * Determine how large our local alloc window should be, in bits.
81 *
82 * These values (and the behavior in ocfs2_alloc_should_use_local) have
83 * been chosen so that most allocations, including new block groups go
84 * through local alloc.
85 */
86static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb)
87{
88 BUG_ON(osb->s_clustersize_bits < 12);
89
90 return 2048 >> (osb->s_clustersize_bits - 12);
91}
92
93/*
94 * Tell us whether a given allocation should use the local alloc
95 * file. Otherwise, it has to go to the main bitmap.
96 */
97int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
98{
99 int la_bits = ocfs2_local_alloc_window_bits(osb);
100
101 if (osb->local_alloc_state != OCFS2_LA_ENABLED)
102 return 0;
103
104 /* la_bits should be at least twice the size (in clusters) of
105 * a new block group. We want to be sure block group
106 * allocations go through the local alloc, so allow an
107 * allocation to take up to half the bitmap. */
108 if (bits > (la_bits / 2))
109 return 0;
110
111 return 1;
112}
113
114int ocfs2_load_local_alloc(struct ocfs2_super *osb)
115{
116 int status = 0;
117 struct ocfs2_dinode *alloc = NULL;
118 struct buffer_head *alloc_bh = NULL;
119 u32 num_used;
120 struct inode *inode = NULL;
121 struct ocfs2_local_alloc *la;
122
123 mlog_entry_void();
124
125 /* read the alloc off disk */
126 inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
127 osb->slot_num);
128 if (!inode) {
129 status = -EINVAL;
130 mlog_errno(status);
131 goto bail;
132 }
133
134 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
135 &alloc_bh, 0, inode);
136 if (status < 0) {
137 mlog_errno(status);
138 goto bail;
139 }
140
141 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
142 la = OCFS2_LOCAL_ALLOC(alloc);
143
144 if (!(le32_to_cpu(alloc->i_flags) &
145 (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
b0697053
MF
146 mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
147 (unsigned long long)OCFS2_I(inode)->ip_blkno);
ccd979bd
MF
148 status = -EINVAL;
149 goto bail;
150 }
151
152 if ((la->la_size == 0) ||
153 (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) {
154 mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n",
155 le16_to_cpu(la->la_size));
156 status = -EINVAL;
157 goto bail;
158 }
159
160 /* do a little verification. */
161 num_used = ocfs2_local_alloc_count_bits(alloc);
162
163 /* hopefully the local alloc has always been recovered before
164 * we load it. */
165 if (num_used
166 || alloc->id1.bitmap1.i_used
167 || alloc->id1.bitmap1.i_total
168 || la->la_bm_off)
169 mlog(ML_ERROR, "Local alloc hasn't been recovered!\n"
170 "found = %u, set = %u, taken = %u, off = %u\n",
171 num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
172 le32_to_cpu(alloc->id1.bitmap1.i_total),
173 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
174
175 osb->local_alloc_bh = alloc_bh;
176 osb->local_alloc_state = OCFS2_LA_ENABLED;
177
178bail:
179 if (status < 0)
180 if (alloc_bh)
181 brelse(alloc_bh);
182 if (inode)
183 iput(inode);
184
185 mlog_exit(status);
186 return status;
187}
188
189/*
190 * return any unused bits to the bitmap and write out a clean
191 * local_alloc.
192 *
193 * local_alloc_bh is optional. If not passed, we will simply use the
194 * one off osb. If you do pass it however, be warned that it *will* be
195 * returned brelse'd and NULL'd out.*/
196void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
197{
198 int status;
8898a5a5 199 struct ocfs2_journal_handle *handle;
ccd979bd
MF
200 struct inode *local_alloc_inode = NULL;
201 struct buffer_head *bh = NULL;
202 struct buffer_head *main_bm_bh = NULL;
203 struct inode *main_bm_inode = NULL;
204 struct ocfs2_dinode *alloc_copy = NULL;
205 struct ocfs2_dinode *alloc = NULL;
206
207 mlog_entry_void();
208
209 if (osb->local_alloc_state == OCFS2_LA_UNUSED)
8898a5a5 210 goto out;
ccd979bd
MF
211
212 local_alloc_inode =
213 ocfs2_get_system_file_inode(osb,
214 LOCAL_ALLOC_SYSTEM_INODE,
215 osb->slot_num);
216 if (!local_alloc_inode) {
217 status = -ENOENT;
218 mlog_errno(status);
8898a5a5 219 goto out;
ccd979bd
MF
220 }
221
222 osb->local_alloc_state = OCFS2_LA_DISABLED;
223
ccd979bd
MF
224 main_bm_inode = ocfs2_get_system_file_inode(osb,
225 GLOBAL_BITMAP_SYSTEM_INODE,
226 OCFS2_INVALID_SLOT);
227 if (!main_bm_inode) {
228 status = -EINVAL;
229 mlog_errno(status);
8898a5a5 230 goto out;
ccd979bd
MF
231 }
232
8898a5a5
MF
233 mutex_lock(&main_bm_inode->i_mutex);
234
235 status = ocfs2_meta_lock(main_bm_inode, NULL, &main_bm_bh, 1);
ccd979bd
MF
236 if (status < 0) {
237 mlog_errno(status);
8898a5a5 238 goto out_mutex;
ccd979bd
MF
239 }
240
241 /* WINDOW_MOVE_CREDITS is a bit heavy... */
8898a5a5 242 handle = ocfs2_start_trans(osb, NULL, OCFS2_WINDOW_MOVE_CREDITS);
ccd979bd
MF
243 if (IS_ERR(handle)) {
244 mlog_errno(PTR_ERR(handle));
245 handle = NULL;
8898a5a5 246 goto out_unlock;
ccd979bd
MF
247 }
248
249 bh = osb->local_alloc_bh;
250 alloc = (struct ocfs2_dinode *) bh->b_data;
251
252 alloc_copy = kmalloc(bh->b_size, GFP_KERNEL);
253 if (!alloc_copy) {
254 status = -ENOMEM;
8898a5a5 255 goto out_commit;
ccd979bd
MF
256 }
257 memcpy(alloc_copy, alloc, bh->b_size);
258
259 status = ocfs2_journal_access(handle, local_alloc_inode, bh,
260 OCFS2_JOURNAL_ACCESS_WRITE);
261 if (status < 0) {
262 mlog_errno(status);
8898a5a5 263 goto out_commit;
ccd979bd
MF
264 }
265
266 ocfs2_clear_local_alloc(alloc);
267
268 status = ocfs2_journal_dirty(handle, bh);
269 if (status < 0) {
270 mlog_errno(status);
8898a5a5 271 goto out_commit;
ccd979bd
MF
272 }
273
274 brelse(bh);
275 osb->local_alloc_bh = NULL;
276 osb->local_alloc_state = OCFS2_LA_UNUSED;
277
278 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
279 main_bm_inode, main_bm_bh);
280 if (status < 0)
281 mlog_errno(status);
282
8898a5a5
MF
283out_commit:
284 ocfs2_commit_trans(handle);
ccd979bd 285
8898a5a5 286out_unlock:
ccd979bd
MF
287 if (main_bm_bh)
288 brelse(main_bm_bh);
289
8898a5a5 290 ocfs2_meta_unlock(main_bm_inode, 1);
ccd979bd 291
8898a5a5
MF
292out_mutex:
293 mutex_unlock(&main_bm_inode->i_mutex);
294 iput(main_bm_inode);
295
296out:
ccd979bd
MF
297 if (local_alloc_inode)
298 iput(local_alloc_inode);
299
300 if (alloc_copy)
301 kfree(alloc_copy);
302
303 mlog_exit_void();
304}
305
306/*
307 * We want to free the bitmap bits outside of any recovery context as
308 * we'll need a cluster lock to do so, but we must clear the local
309 * alloc before giving up the recovered nodes journal. To solve this,
310 * we kmalloc a copy of the local alloc before it's change for the
311 * caller to process with ocfs2_complete_local_alloc_recovery
312 */
313int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
314 int slot_num,
315 struct ocfs2_dinode **alloc_copy)
316{
317 int status = 0;
318 struct buffer_head *alloc_bh = NULL;
319 struct inode *inode = NULL;
320 struct ocfs2_dinode *alloc;
321
322 mlog_entry("(slot_num = %d)\n", slot_num);
323
324 *alloc_copy = NULL;
325
326 inode = ocfs2_get_system_file_inode(osb,
327 LOCAL_ALLOC_SYSTEM_INODE,
328 slot_num);
329 if (!inode) {
330 status = -EINVAL;
331 mlog_errno(status);
332 goto bail;
333 }
334
1b1dcc1b 335 mutex_lock(&inode->i_mutex);
ccd979bd
MF
336
337 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
338 &alloc_bh, 0, inode);
339 if (status < 0) {
340 mlog_errno(status);
341 goto bail;
342 }
343
344 *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL);
345 if (!(*alloc_copy)) {
346 status = -ENOMEM;
347 goto bail;
348 }
349 memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
350
351 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
352 ocfs2_clear_local_alloc(alloc);
353
354 status = ocfs2_write_block(osb, alloc_bh, inode);
355 if (status < 0)
356 mlog_errno(status);
357
358bail:
359 if ((status < 0) && (*alloc_copy)) {
360 kfree(*alloc_copy);
361 *alloc_copy = NULL;
362 }
363
364 if (alloc_bh)
365 brelse(alloc_bh);
366
367 if (inode) {
1b1dcc1b 368 mutex_unlock(&inode->i_mutex);
ccd979bd
MF
369 iput(inode);
370 }
371
372 mlog_exit(status);
373 return status;
374}
375
376/*
377 * Step 2: By now, we've completed the journal recovery, we've stamped
378 * a clean local alloc on disk and dropped the node out of the
379 * recovery map. Dlm locks will no longer stall, so lets clear out the
380 * main bitmap.
381 */
382int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
383 struct ocfs2_dinode *alloc)
384{
385 int status;
8898a5a5 386 struct ocfs2_journal_handle *handle;
ccd979bd 387 struct buffer_head *main_bm_bh = NULL;
8898a5a5 388 struct inode *main_bm_inode;
ccd979bd
MF
389
390 mlog_entry_void();
391
ccd979bd
MF
392 main_bm_inode = ocfs2_get_system_file_inode(osb,
393 GLOBAL_BITMAP_SYSTEM_INODE,
394 OCFS2_INVALID_SLOT);
395 if (!main_bm_inode) {
396 status = -EINVAL;
397 mlog_errno(status);
8898a5a5 398 goto out;
ccd979bd
MF
399 }
400
8898a5a5
MF
401 mutex_lock(&main_bm_inode->i_mutex);
402
403 status = ocfs2_meta_lock(main_bm_inode, NULL, &main_bm_bh, 1);
ccd979bd
MF
404 if (status < 0) {
405 mlog_errno(status);
8898a5a5 406 goto out_mutex;
ccd979bd
MF
407 }
408
8898a5a5 409 handle = ocfs2_start_trans(osb, NULL, OCFS2_WINDOW_MOVE_CREDITS);
ccd979bd
MF
410 if (IS_ERR(handle)) {
411 status = PTR_ERR(handle);
412 handle = NULL;
413 mlog_errno(status);
8898a5a5 414 goto out_unlock;
ccd979bd
MF
415 }
416
417 /* we want the bitmap change to be recorded on disk asap */
c161f89b 418 handle->k_handle->h_sync = 1;
ccd979bd
MF
419
420 status = ocfs2_sync_local_to_main(osb, handle, alloc,
421 main_bm_inode, main_bm_bh);
422 if (status < 0)
423 mlog_errno(status);
424
8898a5a5
MF
425 ocfs2_commit_trans(handle);
426
427out_unlock:
428 ocfs2_meta_unlock(main_bm_inode, 1);
429
430out_mutex:
431 mutex_unlock(&main_bm_inode->i_mutex);
ccd979bd
MF
432
433 if (main_bm_bh)
434 brelse(main_bm_bh);
435
8898a5a5 436 iput(main_bm_inode);
ccd979bd 437
8898a5a5 438out:
ccd979bd
MF
439 mlog_exit(status);
440 return status;
441}
442
443/*
444 * make sure we've got at least bitswanted contiguous bits in the
1b1dcc1b 445 * local alloc. You lose them when you drop i_mutex.
ccd979bd
MF
446 *
447 * We will add ourselves to the transaction passed in, but may start
448 * our own in order to shift windows.
449 */
450int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
451 struct ocfs2_journal_handle *passed_handle,
452 u32 bits_wanted,
453 struct ocfs2_alloc_context *ac)
454{
455 int status;
456 struct ocfs2_dinode *alloc;
457 struct inode *local_alloc_inode;
458 unsigned int free_bits;
459
460 mlog_entry_void();
461
462 BUG_ON(!passed_handle);
463 BUG_ON(!ac);
c161f89b 464 BUG_ON(passed_handle->k_handle);
ccd979bd
MF
465
466 local_alloc_inode =
467 ocfs2_get_system_file_inode(osb,
468 LOCAL_ALLOC_SYSTEM_INODE,
469 osb->slot_num);
470 if (!local_alloc_inode) {
471 status = -ENOENT;
472 mlog_errno(status);
473 goto bail;
474 }
475 ocfs2_handle_add_inode(passed_handle, local_alloc_inode);
476
477 if (osb->local_alloc_state != OCFS2_LA_ENABLED) {
478 status = -ENOSPC;
479 goto bail;
480 }
481
482 if (bits_wanted > ocfs2_local_alloc_window_bits(osb)) {
483 mlog(0, "Asking for more than my max window size!\n");
484 status = -ENOSPC;
485 goto bail;
486 }
487
488 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
489
490 if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
491 ocfs2_local_alloc_count_bits(alloc)) {
b0697053 492 ocfs2_error(osb->sb, "local alloc inode %llu says it has "
ccd979bd 493 "%u free bits, but a count shows %u",
b0697053 494 (unsigned long long)le64_to_cpu(alloc->i_blkno),
ccd979bd
MF
495 le32_to_cpu(alloc->id1.bitmap1.i_used),
496 ocfs2_local_alloc_count_bits(alloc));
497 status = -EIO;
498 goto bail;
499 }
500
501 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
502 le32_to_cpu(alloc->id1.bitmap1.i_used);
503 if (bits_wanted > free_bits) {
504 /* uhoh, window change time. */
505 status =
506 ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
507 if (status < 0) {
508 if (status != -ENOSPC)
509 mlog_errno(status);
510 goto bail;
511 }
512 }
513
514 ac->ac_inode = igrab(local_alloc_inode);
515 get_bh(osb->local_alloc_bh);
516 ac->ac_bh = osb->local_alloc_bh;
517 ac->ac_which = OCFS2_AC_USE_LOCAL;
518 status = 0;
519bail:
520 if (local_alloc_inode)
521 iput(local_alloc_inode);
522
523 mlog_exit(status);
524 return status;
525}
526
527int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
528 struct ocfs2_journal_handle *handle,
529 struct ocfs2_alloc_context *ac,
530 u32 min_bits,
531 u32 *bit_off,
532 u32 *num_bits)
533{
534 int status, start;
535 struct inode *local_alloc_inode;
536 u32 bits_wanted;
537 void *bitmap;
538 struct ocfs2_dinode *alloc;
539 struct ocfs2_local_alloc *la;
540
541 mlog_entry_void();
542 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
543
544 bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
545 local_alloc_inode = ac->ac_inode;
546 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
547 la = OCFS2_LOCAL_ALLOC(alloc);
548
549 start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted);
550 if (start == -1) {
551 /* TODO: Shouldn't we just BUG here? */
552 status = -ENOSPC;
553 mlog_errno(status);
554 goto bail;
555 }
556
557 bitmap = la->la_bitmap;
558 *bit_off = le32_to_cpu(la->la_bm_off) + start;
559 /* local alloc is always contiguous by nature -- we never
560 * delete bits from it! */
561 *num_bits = bits_wanted;
562
563 status = ocfs2_journal_access(handle, local_alloc_inode,
564 osb->local_alloc_bh,
565 OCFS2_JOURNAL_ACCESS_WRITE);
566 if (status < 0) {
567 mlog_errno(status);
568 goto bail;
569 }
570
571 while(bits_wanted--)
572 ocfs2_set_bit(start++, bitmap);
573
574 alloc->id1.bitmap1.i_used = cpu_to_le32(*num_bits +
575 le32_to_cpu(alloc->id1.bitmap1.i_used));
576
577 status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
578 if (status < 0) {
579 mlog_errno(status);
580 goto bail;
581 }
582
583 status = 0;
584bail:
585 mlog_exit(status);
586 return status;
587}
588
589static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
590{
591 int i;
592 u8 *buffer;
593 u32 count = 0;
594 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
595
596 mlog_entry_void();
597
598 buffer = la->la_bitmap;
599 for (i = 0; i < le16_to_cpu(la->la_size); i++)
600 count += hweight8(buffer[i]);
601
602 mlog_exit(count);
603 return count;
604}
605
606static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
607 struct ocfs2_dinode *alloc,
608 u32 numbits)
609{
610 int numfound, bitoff, left, startoff, lastzero;
611 void *bitmap = NULL;
612
613 mlog_entry("(numbits wanted = %u)\n", numbits);
614
615 if (!alloc->id1.bitmap1.i_total) {
616 mlog(0, "No bits in my window!\n");
617 bitoff = -1;
618 goto bail;
619 }
620
621 bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
622
623 numfound = bitoff = startoff = 0;
624 lastzero = -1;
625 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
626 while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
627 if (bitoff == left) {
628 /* mlog(0, "bitoff (%d) == left", bitoff); */
629 break;
630 }
631 /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
632 "numfound = %d\n", bitoff, startoff, numfound);*/
633
634 /* Ok, we found a zero bit... is it contig. or do we
635 * start over?*/
636 if (bitoff == startoff) {
637 /* we found a zero */
638 numfound++;
639 startoff++;
640 } else {
641 /* got a zero after some ones */
642 numfound = 1;
643 startoff = bitoff+1;
644 }
645 /* we got everything we needed */
646 if (numfound == numbits) {
647 /* mlog(0, "Found it all!\n"); */
648 break;
649 }
650 }
651
652 mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff,
653 numfound);
654
655 if (numfound == numbits)
656 bitoff = startoff - numfound;
657 else
658 bitoff = -1;
659
660bail:
661 mlog_exit(bitoff);
662 return bitoff;
663}
664
665static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
666{
667 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
668 int i;
669 mlog_entry_void();
670
671 alloc->id1.bitmap1.i_total = 0;
672 alloc->id1.bitmap1.i_used = 0;
673 la->la_bm_off = 0;
674 for(i = 0; i < le16_to_cpu(la->la_size); i++)
675 la->la_bitmap[i] = 0;
676
677 mlog_exit_void();
678}
679
680#if 0
681/* turn this on and uncomment below to aid debugging window shifts. */
682static void ocfs2_verify_zero_bits(unsigned long *bitmap,
683 unsigned int start,
684 unsigned int count)
685{
686 unsigned int tmp = count;
687 while(tmp--) {
688 if (ocfs2_test_bit(start + tmp, bitmap)) {
689 printk("ocfs2_verify_zero_bits: start = %u, count = "
690 "%u\n", start, count);
691 printk("ocfs2_verify_zero_bits: bit %u is set!",
692 start + tmp);
693 BUG();
694 }
695 }
696}
697#endif
698
699/*
700 * sync the local alloc to main bitmap.
701 *
702 * assumes you've already locked the main bitmap -- the bitmap inode
703 * passed is used for caching.
704 */
705static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
706 struct ocfs2_journal_handle *handle,
707 struct ocfs2_dinode *alloc,
708 struct inode *main_bm_inode,
709 struct buffer_head *main_bm_bh)
710{
711 int status = 0;
712 int bit_off, left, count, start;
713 u64 la_start_blk;
714 u64 blkno;
715 void *bitmap;
716 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
717
718 mlog_entry("total = %u, COUNT = %u, used = %u\n",
719 le32_to_cpu(alloc->id1.bitmap1.i_total),
720 ocfs2_local_alloc_count_bits(alloc),
721 le32_to_cpu(alloc->id1.bitmap1.i_used));
722
723 if (!alloc->id1.bitmap1.i_total) {
724 mlog(0, "nothing to sync!\n");
725 goto bail;
726 }
727
728 if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
729 le32_to_cpu(alloc->id1.bitmap1.i_total)) {
730 mlog(0, "all bits were taken!\n");
731 goto bail;
732 }
733
734 la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
735 le32_to_cpu(la->la_bm_off));
736 bitmap = la->la_bitmap;
737 start = count = bit_off = 0;
738 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
739
740 while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
741 != -1) {
742 if ((bit_off < left) && (bit_off == start)) {
743 count++;
744 start++;
745 continue;
746 }
747 if (count) {
748 blkno = la_start_blk +
749 ocfs2_clusters_to_blocks(osb->sb,
750 start - count);
751
b0697053
MF
752 mlog(0, "freeing %u bits starting at local alloc bit "
753 "%u (la_start_blk = %llu, blkno = %llu)\n",
754 count, start - count,
755 (unsigned long long)la_start_blk,
756 (unsigned long long)blkno);
ccd979bd
MF
757
758 status = ocfs2_free_clusters(handle, main_bm_inode,
759 main_bm_bh, blkno, count);
760 if (status < 0) {
761 mlog_errno(status);
762 goto bail;
763 }
764 }
765 if (bit_off >= left)
766 break;
767 count = 1;
768 start = bit_off + 1;
769 }
770
771bail:
772 mlog_exit(status);
773 return status;
774}
775
776static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
777 struct ocfs2_journal_handle *handle,
778 struct ocfs2_alloc_context **ac,
779 struct inode **bitmap_inode,
780 struct buffer_head **bitmap_bh)
781{
782 int status;
783
784 *ac = kcalloc(1, sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
785 if (!(*ac)) {
786 status = -ENOMEM;
787 mlog_errno(status);
788 goto bail;
789 }
790
791 (*ac)->ac_handle = handle;
792 (*ac)->ac_bits_wanted = ocfs2_local_alloc_window_bits(osb);
793
794 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
795 if (status < 0) {
796 if (status != -ENOSPC)
797 mlog_errno(status);
798 goto bail;
799 }
800
801 *bitmap_inode = (*ac)->ac_inode;
802 igrab(*bitmap_inode);
803 *bitmap_bh = (*ac)->ac_bh;
804 get_bh(*bitmap_bh);
805 status = 0;
806bail:
807 if ((status < 0) && *ac) {
808 ocfs2_free_alloc_context(*ac);
809 *ac = NULL;
810 }
811
812 mlog_exit(status);
813 return status;
814}
815
816/*
817 * pass it the bitmap lock in lock_bh if you have it.
818 */
819static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
820 struct ocfs2_journal_handle *handle,
821 struct ocfs2_alloc_context *ac)
822{
823 int status = 0;
824 u32 cluster_off, cluster_count;
825 struct ocfs2_dinode *alloc = NULL;
826 struct ocfs2_local_alloc *la;
827
828 mlog_entry_void();
829
830 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
831 la = OCFS2_LOCAL_ALLOC(alloc);
832
833 if (alloc->id1.bitmap1.i_total)
834 mlog(0, "asking me to alloc a new window over a non-empty "
835 "one\n");
836
837 mlog(0, "Allocating %u clusters for a new window.\n",
838 ocfs2_local_alloc_window_bits(osb));
883d4cae
MF
839
840 /* Instruct the allocation code to try the most recently used
841 * cluster group. We'll re-record the group used this pass
842 * below. */
843 ac->ac_last_group = osb->la_last_gd;
844
ccd979bd
MF
845 /* we used the generic suballoc reserve function, but we set
846 * everything up nicely, so there's no reason why we can't use
847 * the more specific cluster api to claim bits. */
848 status = ocfs2_claim_clusters(osb, handle, ac,
849 ocfs2_local_alloc_window_bits(osb),
850 &cluster_off, &cluster_count);
851 if (status < 0) {
852 if (status != -ENOSPC)
853 mlog_errno(status);
854 goto bail;
855 }
856
883d4cae
MF
857 osb->la_last_gd = ac->ac_last_group;
858
ccd979bd
MF
859 la->la_bm_off = cpu_to_le32(cluster_off);
860 alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
861 /* just in case... In the future when we find space ourselves,
862 * we don't have to get all contiguous -- but we'll have to
863 * set all previously used bits in bitmap and update
864 * la_bits_set before setting the bits in the main bitmap. */
865 alloc->id1.bitmap1.i_used = 0;
866 memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
867 le16_to_cpu(la->la_size));
868
869 mlog(0, "New window allocated:\n");
870 mlog(0, "window la_bm_off = %u\n",
871 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
872 mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total));
873
874bail:
875 mlog_exit(status);
876 return status;
877}
878
879/* Note that we do *NOT* lock the local alloc inode here as
880 * it's been locked already for us. */
881static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
882 struct inode *local_alloc_inode)
883{
884 int status = 0;
885 struct buffer_head *main_bm_bh = NULL;
886 struct inode *main_bm_inode = NULL;
887 struct ocfs2_journal_handle *handle = NULL;
888 struct ocfs2_dinode *alloc;
889 struct ocfs2_dinode *alloc_copy = NULL;
890 struct ocfs2_alloc_context *ac = NULL;
891
892 mlog_entry_void();
893
894 handle = ocfs2_alloc_handle(osb);
895 if (!handle) {
896 status = -ENOMEM;
897 mlog_errno(status);
898 goto bail;
899 }
900
901 /* This will lock the main bitmap for us. */
902 status = ocfs2_local_alloc_reserve_for_window(osb,
903 handle,
904 &ac,
905 &main_bm_inode,
906 &main_bm_bh);
907 if (status < 0) {
908 if (status != -ENOSPC)
909 mlog_errno(status);
910 goto bail;
911 }
912
913 handle = ocfs2_start_trans(osb, handle, OCFS2_WINDOW_MOVE_CREDITS);
914 if (IS_ERR(handle)) {
915 status = PTR_ERR(handle);
916 handle = NULL;
917 mlog_errno(status);
918 goto bail;
919 }
920
921 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
922
923 /* We want to clear the local alloc before doing anything
924 * else, so that if we error later during this operation,
925 * local alloc shutdown won't try to double free main bitmap
926 * bits. Make a copy so the sync function knows which bits to
927 * free. */
928 alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_KERNEL);
929 if (!alloc_copy) {
930 status = -ENOMEM;
931 mlog_errno(status);
932 goto bail;
933 }
934 memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
935
936 status = ocfs2_journal_access(handle, local_alloc_inode,
937 osb->local_alloc_bh,
938 OCFS2_JOURNAL_ACCESS_WRITE);
939 if (status < 0) {
940 mlog_errno(status);
941 goto bail;
942 }
943
944 ocfs2_clear_local_alloc(alloc);
945
946 status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
947 if (status < 0) {
948 mlog_errno(status);
949 goto bail;
950 }
951
952 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
953 main_bm_inode, main_bm_bh);
954 if (status < 0) {
955 mlog_errno(status);
956 goto bail;
957 }
958
959 status = ocfs2_local_alloc_new_window(osb, handle, ac);
960 if (status < 0) {
961 if (status != -ENOSPC)
962 mlog_errno(status);
963 goto bail;
964 }
965
966 atomic_inc(&osb->alloc_stats.moves);
967
968 status = 0;
969bail:
970 if (handle)
971 ocfs2_commit_trans(handle);
972
973 if (main_bm_bh)
974 brelse(main_bm_bh);
975
976 if (main_bm_inode)
977 iput(main_bm_inode);
978
979 if (alloc_copy)
980 kfree(alloc_copy);
981
982 if (ac)
983 ocfs2_free_alloc_context(ac);
984
985 mlog_exit(status);
986 return status;
987}
988
This page took 0.187572 seconds and 5 git commands to generate.