]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/ocfs2/suballoc.c
d7a6f928c31726731ee0d58a839f5b6daf427676
[linux-2.6-omap-h63xx.git] / fs / ocfs2 / suballoc.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * suballoc.c
5  *
6  * metadata alloc and free
7  * Inspired by ext3 block groups.
8  *
9  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public
22  * License along with this program; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 021110-1307, USA.
25  */
26
27 #include <linux/fs.h>
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.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 #include "uptodate.h"
46
47 #include "buffer_head_io.h"
48
49 #define NOT_ALLOC_NEW_GROUP             0
50 #define ALLOC_NEW_GROUP                 1
51
52 #define OCFS2_MAX_INODES_TO_STEAL       1024
53
54 static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
55 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
56 static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
57 static int ocfs2_block_group_fill(handle_t *handle,
58                                   struct inode *alloc_inode,
59                                   struct buffer_head *bg_bh,
60                                   u64 group_blkno,
61                                   u16 my_chain,
62                                   struct ocfs2_chain_list *cl);
63 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
64                                    struct inode *alloc_inode,
65                                    struct buffer_head *bh,
66                                    u64 max_block);
67
68 static int ocfs2_cluster_group_search(struct inode *inode,
69                                       struct buffer_head *group_bh,
70                                       u32 bits_wanted, u32 min_bits,
71                                       u64 max_block,
72                                       u16 *bit_off, u16 *bits_found);
73 static int ocfs2_block_group_search(struct inode *inode,
74                                     struct buffer_head *group_bh,
75                                     u32 bits_wanted, u32 min_bits,
76                                     u64 max_block,
77                                     u16 *bit_off, u16 *bits_found);
78 static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
79                                      struct ocfs2_alloc_context *ac,
80                                      handle_t *handle,
81                                      u32 bits_wanted,
82                                      u32 min_bits,
83                                      u16 *bit_off,
84                                      unsigned int *num_bits,
85                                      u64 *bg_blkno);
86 static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
87                                          int nr);
88 static inline int ocfs2_block_group_set_bits(handle_t *handle,
89                                              struct inode *alloc_inode,
90                                              struct ocfs2_group_desc *bg,
91                                              struct buffer_head *group_bh,
92                                              unsigned int bit_off,
93                                              unsigned int num_bits);
94 static inline int ocfs2_block_group_clear_bits(handle_t *handle,
95                                                struct inode *alloc_inode,
96                                                struct ocfs2_group_desc *bg,
97                                                struct buffer_head *group_bh,
98                                                unsigned int bit_off,
99                                                unsigned int num_bits);
100
101 static int ocfs2_relink_block_group(handle_t *handle,
102                                     struct inode *alloc_inode,
103                                     struct buffer_head *fe_bh,
104                                     struct buffer_head *bg_bh,
105                                     struct buffer_head *prev_bg_bh,
106                                     u16 chain);
107 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
108                                                      u32 wanted);
109 static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
110                                                    u64 bg_blkno,
111                                                    u16 bg_bit_off);
112 static inline void ocfs2_block_to_cluster_group(struct inode *inode,
113                                                 u64 data_blkno,
114                                                 u64 *bg_blkno,
115                                                 u16 *bg_bit_off);
116 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
117                                              u32 bits_wanted, u64 max_block,
118                                              struct ocfs2_alloc_context **ac);
119
120 void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
121 {
122         struct inode *inode = ac->ac_inode;
123
124         if (inode) {
125                 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
126                         ocfs2_inode_unlock(inode, 1);
127
128                 mutex_unlock(&inode->i_mutex);
129
130                 iput(inode);
131                 ac->ac_inode = NULL;
132         }
133         if (ac->ac_bh) {
134                 brelse(ac->ac_bh);
135                 ac->ac_bh = NULL;
136         }
137 }
138
139 void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
140 {
141         ocfs2_free_ac_resource(ac);
142         kfree(ac);
143 }
144
145 static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
146 {
147         return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
148 }
149
150 /* somewhat more expensive than our other checks, so use sparingly. */
151 int ocfs2_check_group_descriptor(struct super_block *sb,
152                                  struct ocfs2_dinode *di,
153                                  struct ocfs2_group_desc *gd)
154 {
155         unsigned int max_bits;
156
157         if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
158                 OCFS2_RO_ON_INVALID_GROUP_DESC(sb, gd);
159                 return -EIO;
160         }
161
162         if (di->i_blkno != gd->bg_parent_dinode) {
163                 ocfs2_error(sb, "Group descriptor # %llu has bad parent "
164                             "pointer (%llu, expected %llu)",
165                             (unsigned long long)le64_to_cpu(gd->bg_blkno),
166                             (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
167                             (unsigned long long)le64_to_cpu(di->i_blkno));
168                 return -EIO;
169         }
170
171         max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
172         if (le16_to_cpu(gd->bg_bits) > max_bits) {
173                 ocfs2_error(sb, "Group descriptor # %llu has bit count of %u",
174                             (unsigned long long)le64_to_cpu(gd->bg_blkno),
175                             le16_to_cpu(gd->bg_bits));
176                 return -EIO;
177         }
178
179         if (le16_to_cpu(gd->bg_chain) >=
180             le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) {
181                 ocfs2_error(sb, "Group descriptor # %llu has bad chain %u",
182                             (unsigned long long)le64_to_cpu(gd->bg_blkno),
183                             le16_to_cpu(gd->bg_chain));
184                 return -EIO;
185         }
186
187         if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
188                 ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
189                             "claims that %u are free",
190                             (unsigned long long)le64_to_cpu(gd->bg_blkno),
191                             le16_to_cpu(gd->bg_bits),
192                             le16_to_cpu(gd->bg_free_bits_count));
193                 return -EIO;
194         }
195
196         if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
197                 ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
198                             "max bitmap bits of %u",
199                             (unsigned long long)le64_to_cpu(gd->bg_blkno),
200                             le16_to_cpu(gd->bg_bits),
201                             8 * le16_to_cpu(gd->bg_size));
202                 return -EIO;
203         }
204
205         return 0;
206 }
207
208 static int ocfs2_block_group_fill(handle_t *handle,
209                                   struct inode *alloc_inode,
210                                   struct buffer_head *bg_bh,
211                                   u64 group_blkno,
212                                   u16 my_chain,
213                                   struct ocfs2_chain_list *cl)
214 {
215         int status = 0;
216         struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
217         struct super_block * sb = alloc_inode->i_sb;
218
219         mlog_entry_void();
220
221         if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
222                 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
223                             "b_blocknr (%llu)",
224                             (unsigned long long)group_blkno,
225                             (unsigned long long) bg_bh->b_blocknr);
226                 status = -EIO;
227                 goto bail;
228         }
229
230         status = ocfs2_journal_access(handle,
231                                       alloc_inode,
232                                       bg_bh,
233                                       OCFS2_JOURNAL_ACCESS_CREATE);
234         if (status < 0) {
235                 mlog_errno(status);
236                 goto bail;
237         }
238
239         memset(bg, 0, sb->s_blocksize);
240         strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
241         bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
242         bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
243         bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
244         bg->bg_chain = cpu_to_le16(my_chain);
245         bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
246         bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
247         bg->bg_blkno = cpu_to_le64(group_blkno);
248         /* set the 1st bit in the bitmap to account for the descriptor block */
249         ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
250         bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
251
252         status = ocfs2_journal_dirty(handle, bg_bh);
253         if (status < 0)
254                 mlog_errno(status);
255
256         /* There is no need to zero out or otherwise initialize the
257          * other blocks in a group - All valid FS metadata in a block
258          * group stores the superblock fs_generation value at
259          * allocation time. */
260
261 bail:
262         mlog_exit(status);
263         return status;
264 }
265
266 static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
267 {
268         u16 curr, best;
269
270         best = curr = 0;
271         while (curr < le16_to_cpu(cl->cl_count)) {
272                 if (le32_to_cpu(cl->cl_recs[best].c_total) >
273                     le32_to_cpu(cl->cl_recs[curr].c_total))
274                         best = curr;
275                 curr++;
276         }
277         return best;
278 }
279
280 /*
281  * We expect the block group allocator to already be locked.
282  */
283 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
284                                    struct inode *alloc_inode,
285                                    struct buffer_head *bh,
286                                    u64 max_block)
287 {
288         int status, credits;
289         struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
290         struct ocfs2_chain_list *cl;
291         struct ocfs2_alloc_context *ac = NULL;
292         handle_t *handle = NULL;
293         u32 bit_off, num_bits;
294         u16 alloc_rec;
295         u64 bg_blkno;
296         struct buffer_head *bg_bh = NULL;
297         struct ocfs2_group_desc *bg;
298
299         BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
300
301         mlog_entry_void();
302
303         cl = &fe->id2.i_chain;
304         status = ocfs2_reserve_clusters_with_limit(osb,
305                                                    le16_to_cpu(cl->cl_cpg),
306                                                    max_block, &ac);
307         if (status < 0) {
308                 if (status != -ENOSPC)
309                         mlog_errno(status);
310                 goto bail;
311         }
312
313         credits = ocfs2_calc_group_alloc_credits(osb->sb,
314                                                  le16_to_cpu(cl->cl_cpg));
315         handle = ocfs2_start_trans(osb, credits);
316         if (IS_ERR(handle)) {
317                 status = PTR_ERR(handle);
318                 handle = NULL;
319                 mlog_errno(status);
320                 goto bail;
321         }
322
323         status = ocfs2_claim_clusters(osb,
324                                       handle,
325                                       ac,
326                                       le16_to_cpu(cl->cl_cpg),
327                                       &bit_off,
328                                       &num_bits);
329         if (status < 0) {
330                 if (status != -ENOSPC)
331                         mlog_errno(status);
332                 goto bail;
333         }
334
335         alloc_rec = ocfs2_find_smallest_chain(cl);
336
337         /* setup the group */
338         bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
339         mlog(0, "new descriptor, record %u, at block %llu\n",
340              alloc_rec, (unsigned long long)bg_blkno);
341
342         bg_bh = sb_getblk(osb->sb, bg_blkno);
343         if (!bg_bh) {
344                 status = -EIO;
345                 mlog_errno(status);
346                 goto bail;
347         }
348         ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
349
350         status = ocfs2_block_group_fill(handle,
351                                         alloc_inode,
352                                         bg_bh,
353                                         bg_blkno,
354                                         alloc_rec,
355                                         cl);
356         if (status < 0) {
357                 mlog_errno(status);
358                 goto bail;
359         }
360
361         bg = (struct ocfs2_group_desc *) bg_bh->b_data;
362
363         status = ocfs2_journal_access(handle, alloc_inode,
364                                       bh, OCFS2_JOURNAL_ACCESS_WRITE);
365         if (status < 0) {
366                 mlog_errno(status);
367                 goto bail;
368         }
369
370         le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
371                      le16_to_cpu(bg->bg_free_bits_count));
372         le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
373         cl->cl_recs[alloc_rec].c_blkno  = cpu_to_le64(bg_blkno);
374         if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
375                 le16_add_cpu(&cl->cl_next_free_rec, 1);
376
377         le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
378                                         le16_to_cpu(bg->bg_free_bits_count));
379         le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
380         le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
381
382         status = ocfs2_journal_dirty(handle, bh);
383         if (status < 0) {
384                 mlog_errno(status);
385                 goto bail;
386         }
387
388         spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
389         OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
390         fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
391                                              le32_to_cpu(fe->i_clusters)));
392         spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
393         i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
394         alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
395
396         status = 0;
397 bail:
398         if (handle)
399                 ocfs2_commit_trans(osb, handle);
400
401         if (ac)
402                 ocfs2_free_alloc_context(ac);
403
404         if (bg_bh)
405                 brelse(bg_bh);
406
407         mlog_exit(status);
408         return status;
409 }
410
411 static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
412                                        struct ocfs2_alloc_context *ac,
413                                        int type,
414                                        u32 slot,
415                                        int alloc_new_group)
416 {
417         int status;
418         u32 bits_wanted = ac->ac_bits_wanted;
419         struct inode *alloc_inode;
420         struct buffer_head *bh = NULL;
421         struct ocfs2_dinode *fe;
422         u32 free_bits;
423
424         mlog_entry_void();
425
426         alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
427         if (!alloc_inode) {
428                 mlog_errno(-EINVAL);
429                 return -EINVAL;
430         }
431
432         mutex_lock(&alloc_inode->i_mutex);
433
434         status = ocfs2_inode_lock(alloc_inode, &bh, 1);
435         if (status < 0) {
436                 mutex_unlock(&alloc_inode->i_mutex);
437                 iput(alloc_inode);
438
439                 mlog_errno(status);
440                 return status;
441         }
442
443         ac->ac_inode = alloc_inode;
444         ac->ac_alloc_slot = slot;
445
446         fe = (struct ocfs2_dinode *) bh->b_data;
447         if (!OCFS2_IS_VALID_DINODE(fe)) {
448                 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
449                 status = -EIO;
450                 goto bail;
451         }
452         if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
453                 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
454                             (unsigned long long)le64_to_cpu(fe->i_blkno));
455                 status = -EIO;
456                 goto bail;
457         }
458
459         free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
460                 le32_to_cpu(fe->id1.bitmap1.i_used);
461
462         if (bits_wanted > free_bits) {
463                 /* cluster bitmap never grows */
464                 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
465                         mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
466                              bits_wanted, free_bits);
467                         status = -ENOSPC;
468                         goto bail;
469                 }
470
471                 if (alloc_new_group != ALLOC_NEW_GROUP) {
472                         mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
473                              "and we don't alloc a new group for it.\n",
474                              slot, bits_wanted, free_bits);
475                         status = -ENOSPC;
476                         goto bail;
477                 }
478
479                 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
480                                                  ac->ac_max_block);
481                 if (status < 0) {
482                         if (status != -ENOSPC)
483                                 mlog_errno(status);
484                         goto bail;
485                 }
486                 atomic_inc(&osb->alloc_stats.bg_extends);
487
488                 /* You should never ask for this much metadata */
489                 BUG_ON(bits_wanted >
490                        (le32_to_cpu(fe->id1.bitmap1.i_total)
491                         - le32_to_cpu(fe->id1.bitmap1.i_used)));
492         }
493
494         get_bh(bh);
495         ac->ac_bh = bh;
496 bail:
497         if (bh)
498                 brelse(bh);
499
500         mlog_exit(status);
501         return status;
502 }
503
504 int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
505                                       int blocks,
506                                       struct ocfs2_alloc_context **ac)
507 {
508         int status;
509         u32 slot;
510
511         *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
512         if (!(*ac)) {
513                 status = -ENOMEM;
514                 mlog_errno(status);
515                 goto bail;
516         }
517
518         (*ac)->ac_bits_wanted = blocks;
519         (*ac)->ac_which = OCFS2_AC_USE_META;
520         slot = osb->slot_num;
521         (*ac)->ac_group_search = ocfs2_block_group_search;
522
523         status = ocfs2_reserve_suballoc_bits(osb, (*ac),
524                                              EXTENT_ALLOC_SYSTEM_INODE,
525                                              slot, ALLOC_NEW_GROUP);
526         if (status < 0) {
527                 if (status != -ENOSPC)
528                         mlog_errno(status);
529                 goto bail;
530         }
531
532         status = 0;
533 bail:
534         if ((status < 0) && *ac) {
535                 ocfs2_free_alloc_context(*ac);
536                 *ac = NULL;
537         }
538
539         mlog_exit(status);
540         return status;
541 }
542
543 int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
544                                struct ocfs2_extent_list *root_el,
545                                struct ocfs2_alloc_context **ac)
546 {
547         return ocfs2_reserve_new_metadata_blocks(osb,
548                                         ocfs2_extend_meta_needed(root_el),
549                                         ac);
550 }
551
552 static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super *osb,
553                                               struct ocfs2_alloc_context *ac)
554 {
555         int i, status = -ENOSPC;
556         s16 slot = ocfs2_get_inode_steal_slot(osb);
557
558         /* Start to steal inodes from the first slot after ours. */
559         if (slot == OCFS2_INVALID_SLOT)
560                 slot = osb->slot_num + 1;
561
562         for (i = 0; i < osb->max_slots; i++, slot++) {
563                 if (slot == osb->max_slots)
564                         slot = 0;
565
566                 if (slot == osb->slot_num)
567                         continue;
568
569                 status = ocfs2_reserve_suballoc_bits(osb, ac,
570                                                      INODE_ALLOC_SYSTEM_INODE,
571                                                      slot, NOT_ALLOC_NEW_GROUP);
572                 if (status >= 0) {
573                         ocfs2_set_inode_steal_slot(osb, slot);
574                         break;
575                 }
576
577                 ocfs2_free_ac_resource(ac);
578         }
579
580         return status;
581 }
582
583 int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
584                             struct ocfs2_alloc_context **ac)
585 {
586         int status;
587         s16 slot = ocfs2_get_inode_steal_slot(osb);
588
589         *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
590         if (!(*ac)) {
591                 status = -ENOMEM;
592                 mlog_errno(status);
593                 goto bail;
594         }
595
596         (*ac)->ac_bits_wanted = 1;
597         (*ac)->ac_which = OCFS2_AC_USE_INODE;
598
599         (*ac)->ac_group_search = ocfs2_block_group_search;
600
601         /*
602          * stat(2) can't handle i_ino > 32bits, so we tell the
603          * lower levels not to allocate us a block group past that
604          * limit.  The 'inode64' mount option avoids this behavior.
605          */
606         if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
607                 (*ac)->ac_max_block = (u32)~0U;
608
609         /*
610          * slot is set when we successfully steal inode from other nodes.
611          * It is reset in 3 places:
612          * 1. when we flush the truncate log
613          * 2. when we complete local alloc recovery.
614          * 3. when we successfully allocate from our own slot.
615          * After it is set, we will go on stealing inodes until we find the
616          * need to check our slots to see whether there is some space for us.
617          */
618         if (slot != OCFS2_INVALID_SLOT &&
619             atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_INODES_TO_STEAL)
620                 goto inode_steal;
621
622         atomic_set(&osb->s_num_inodes_stolen, 0);
623         status = ocfs2_reserve_suballoc_bits(osb, *ac,
624                                              INODE_ALLOC_SYSTEM_INODE,
625                                              osb->slot_num, ALLOC_NEW_GROUP);
626         if (status >= 0) {
627                 status = 0;
628
629                 /*
630                  * Some inodes must be freed by us, so try to allocate
631                  * from our own next time.
632                  */
633                 if (slot != OCFS2_INVALID_SLOT)
634                         ocfs2_init_inode_steal_slot(osb);
635                 goto bail;
636         } else if (status < 0 && status != -ENOSPC) {
637                 mlog_errno(status);
638                 goto bail;
639         }
640
641         ocfs2_free_ac_resource(*ac);
642
643 inode_steal:
644         status = ocfs2_steal_inode_from_other_nodes(osb, *ac);
645         atomic_inc(&osb->s_num_inodes_stolen);
646         if (status < 0) {
647                 if (status != -ENOSPC)
648                         mlog_errno(status);
649                 goto bail;
650         }
651
652         status = 0;
653 bail:
654         if ((status < 0) && *ac) {
655                 ocfs2_free_alloc_context(*ac);
656                 *ac = NULL;
657         }
658
659         mlog_exit(status);
660         return status;
661 }
662
663 /* local alloc code has to do the same thing, so rather than do this
664  * twice.. */
665 int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
666                                       struct ocfs2_alloc_context *ac)
667 {
668         int status;
669
670         ac->ac_which = OCFS2_AC_USE_MAIN;
671         ac->ac_group_search = ocfs2_cluster_group_search;
672
673         status = ocfs2_reserve_suballoc_bits(osb, ac,
674                                              GLOBAL_BITMAP_SYSTEM_INODE,
675                                              OCFS2_INVALID_SLOT,
676                                              ALLOC_NEW_GROUP);
677         if (status < 0 && status != -ENOSPC) {
678                 mlog_errno(status);
679                 goto bail;
680         }
681
682 bail:
683         return status;
684 }
685
686 /* Callers don't need to care which bitmap (local alloc or main) to
687  * use so we figure it out for them, but unfortunately this clutters
688  * things a bit. */
689 static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
690                                              u32 bits_wanted, u64 max_block,
691                                              struct ocfs2_alloc_context **ac)
692 {
693         int status;
694
695         mlog_entry_void();
696
697         *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
698         if (!(*ac)) {
699                 status = -ENOMEM;
700                 mlog_errno(status);
701                 goto bail;
702         }
703
704         (*ac)->ac_bits_wanted = bits_wanted;
705         (*ac)->ac_max_block = max_block;
706
707         status = -ENOSPC;
708         if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
709                 status = ocfs2_reserve_local_alloc_bits(osb,
710                                                         bits_wanted,
711                                                         *ac);
712                 if (status == -EFBIG) {
713                         /* The local alloc window is outside ac_max_block.
714                          * use the main bitmap. */
715                         status = -ENOSPC;
716                 } else if ((status < 0) && (status != -ENOSPC)) {
717                         mlog_errno(status);
718                         goto bail;
719                 }
720         }
721
722         if (status == -ENOSPC) {
723                 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
724                 if (status < 0) {
725                         if (status != -ENOSPC)
726                                 mlog_errno(status);
727                         goto bail;
728                 }
729         }
730
731         status = 0;
732 bail:
733         if ((status < 0) && *ac) {
734                 ocfs2_free_alloc_context(*ac);
735                 *ac = NULL;
736         }
737
738         mlog_exit(status);
739         return status;
740 }
741
742 int ocfs2_reserve_clusters(struct ocfs2_super *osb,
743                            u32 bits_wanted,
744                            struct ocfs2_alloc_context **ac)
745 {
746         return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0, ac);
747 }
748
749 /*
750  * More or less lifted from ext3. I'll leave their description below:
751  *
752  * "For ext3 allocations, we must not reuse any blocks which are
753  * allocated in the bitmap buffer's "last committed data" copy.  This
754  * prevents deletes from freeing up the page for reuse until we have
755  * committed the delete transaction.
756  *
757  * If we didn't do this, then deleting something and reallocating it as
758  * data would allow the old block to be overwritten before the
759  * transaction committed (because we force data to disk before commit).
760  * This would lead to corruption if we crashed between overwriting the
761  * data and committing the delete.
762  *
763  * @@@ We may want to make this allocation behaviour conditional on
764  * data-writes at some point, and disable it for metadata allocations or
765  * sync-data inodes."
766  *
767  * Note: OCFS2 already does this differently for metadata vs data
768  * allocations, as those bitmaps are separate and undo access is never
769  * called on a metadata group descriptor.
770  */
771 static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
772                                          int nr)
773 {
774         struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
775
776         if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
777                 return 0;
778         if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
779                 return 1;
780
781         bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
782         return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
783 }
784
785 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
786                                              struct buffer_head *bg_bh,
787                                              unsigned int bits_wanted,
788                                              unsigned int total_bits,
789                                              u16 *bit_off,
790                                              u16 *bits_found)
791 {
792         void *bitmap;
793         u16 best_offset, best_size;
794         int offset, start, found, status = 0;
795         struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
796
797         if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
798                 OCFS2_RO_ON_INVALID_GROUP_DESC(osb->sb, bg);
799                 return -EIO;
800         }
801
802         found = start = best_offset = best_size = 0;
803         bitmap = bg->bg_bitmap;
804
805         while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
806                 if (offset == total_bits)
807                         break;
808
809                 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
810                         /* We found a zero, but we can't use it as it
811                          * hasn't been put to disk yet! */
812                         found = 0;
813                         start = offset + 1;
814                 } else if (offset == start) {
815                         /* we found a zero */
816                         found++;
817                         /* move start to the next bit to test */
818                         start++;
819                 } else {
820                         /* got a zero after some ones */
821                         found = 1;
822                         start = offset + 1;
823                 }
824                 if (found > best_size) {
825                         best_size = found;
826                         best_offset = start - found;
827                 }
828                 /* we got everything we needed */
829                 if (found == bits_wanted) {
830                         /* mlog(0, "Found it all!\n"); */
831                         break;
832                 }
833         }
834
835         /* XXX: I think the first clause is equivalent to the second
836          *      - jlbec */
837         if (found == bits_wanted) {
838                 *bit_off = start - found;
839                 *bits_found = found;
840         } else if (best_size) {
841                 *bit_off = best_offset;
842                 *bits_found = best_size;
843         } else {
844                 status = -ENOSPC;
845                 /* No error log here -- see the comment above
846                  * ocfs2_test_bg_bit_allocatable */
847         }
848
849         return status;
850 }
851
852 static inline int ocfs2_block_group_set_bits(handle_t *handle,
853                                              struct inode *alloc_inode,
854                                              struct ocfs2_group_desc *bg,
855                                              struct buffer_head *group_bh,
856                                              unsigned int bit_off,
857                                              unsigned int num_bits)
858 {
859         int status;
860         void *bitmap = bg->bg_bitmap;
861         int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
862
863         mlog_entry_void();
864
865         if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
866                 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
867                 status = -EIO;
868                 goto bail;
869         }
870         BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
871
872         mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
873              num_bits);
874
875         if (ocfs2_is_cluster_bitmap(alloc_inode))
876                 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
877
878         status = ocfs2_journal_access(handle,
879                                       alloc_inode,
880                                       group_bh,
881                                       journal_type);
882         if (status < 0) {
883                 mlog_errno(status);
884                 goto bail;
885         }
886
887         le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
888
889         while(num_bits--)
890                 ocfs2_set_bit(bit_off++, bitmap);
891
892         status = ocfs2_journal_dirty(handle,
893                                      group_bh);
894         if (status < 0) {
895                 mlog_errno(status);
896                 goto bail;
897         }
898
899 bail:
900         mlog_exit(status);
901         return status;
902 }
903
904 /* find the one with the most empty bits */
905 static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
906 {
907         u16 curr, best;
908
909         BUG_ON(!cl->cl_next_free_rec);
910
911         best = curr = 0;
912         while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
913                 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
914                     le32_to_cpu(cl->cl_recs[best].c_free))
915                         best = curr;
916                 curr++;
917         }
918
919         BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
920         return best;
921 }
922
923 static int ocfs2_relink_block_group(handle_t *handle,
924                                     struct inode *alloc_inode,
925                                     struct buffer_head *fe_bh,
926                                     struct buffer_head *bg_bh,
927                                     struct buffer_head *prev_bg_bh,
928                                     u16 chain)
929 {
930         int status;
931         /* there is a really tiny chance the journal calls could fail,
932          * but we wouldn't want inconsistent blocks in *any* case. */
933         u64 fe_ptr, bg_ptr, prev_bg_ptr;
934         struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
935         struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
936         struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
937
938         if (!OCFS2_IS_VALID_DINODE(fe)) {
939                 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
940                 status = -EIO;
941                 goto out;
942         }
943         if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
944                 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
945                 status = -EIO;
946                 goto out;
947         }
948         if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg)) {
949                 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, prev_bg);
950                 status = -EIO;
951                 goto out;
952         }
953
954         mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
955              (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
956              (unsigned long long)le64_to_cpu(bg->bg_blkno),
957              (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
958
959         fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
960         bg_ptr = le64_to_cpu(bg->bg_next_group);
961         prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
962
963         status = ocfs2_journal_access(handle, alloc_inode, prev_bg_bh,
964                                       OCFS2_JOURNAL_ACCESS_WRITE);
965         if (status < 0) {
966                 mlog_errno(status);
967                 goto out_rollback;
968         }
969
970         prev_bg->bg_next_group = bg->bg_next_group;
971
972         status = ocfs2_journal_dirty(handle, prev_bg_bh);
973         if (status < 0) {
974                 mlog_errno(status);
975                 goto out_rollback;
976         }
977
978         status = ocfs2_journal_access(handle, alloc_inode, bg_bh,
979                                       OCFS2_JOURNAL_ACCESS_WRITE);
980         if (status < 0) {
981                 mlog_errno(status);
982                 goto out_rollback;
983         }
984
985         bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
986
987         status = ocfs2_journal_dirty(handle, bg_bh);
988         if (status < 0) {
989                 mlog_errno(status);
990                 goto out_rollback;
991         }
992
993         status = ocfs2_journal_access(handle, alloc_inode, fe_bh,
994                                       OCFS2_JOURNAL_ACCESS_WRITE);
995         if (status < 0) {
996                 mlog_errno(status);
997                 goto out_rollback;
998         }
999
1000         fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
1001
1002         status = ocfs2_journal_dirty(handle, fe_bh);
1003         if (status < 0) {
1004                 mlog_errno(status);
1005                 goto out_rollback;
1006         }
1007
1008         status = 0;
1009 out_rollback:
1010         if (status < 0) {
1011                 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1012                 bg->bg_next_group = cpu_to_le64(bg_ptr);
1013                 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1014         }
1015 out:
1016         mlog_exit(status);
1017         return status;
1018 }
1019
1020 static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1021                                                      u32 wanted)
1022 {
1023         return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1024 }
1025
1026 /* return 0 on success, -ENOSPC to keep searching and any other < 0
1027  * value on error. */
1028 static int ocfs2_cluster_group_search(struct inode *inode,
1029                                       struct buffer_head *group_bh,
1030                                       u32 bits_wanted, u32 min_bits,
1031                                       u64 max_block,
1032                                       u16 *bit_off, u16 *bits_found)
1033 {
1034         int search = -ENOSPC;
1035         int ret;
1036         u64 blkoff;
1037         struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
1038         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1039         u16 tmp_off, tmp_found;
1040         unsigned int max_bits, gd_cluster_off;
1041
1042         BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1043
1044         if (gd->bg_free_bits_count) {
1045                 max_bits = le16_to_cpu(gd->bg_bits);
1046
1047                 /* Tail groups in cluster bitmaps which aren't cpg
1048                  * aligned are prone to partial extention by a failed
1049                  * fs resize. If the file system resize never got to
1050                  * update the dinode cluster count, then we don't want
1051                  * to trust any clusters past it, regardless of what
1052                  * the group descriptor says. */
1053                 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1054                                                           le64_to_cpu(gd->bg_blkno));
1055                 if ((gd_cluster_off + max_bits) >
1056                     OCFS2_I(inode)->ip_clusters) {
1057                         max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1058                         mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1059                              (unsigned long long)le64_to_cpu(gd->bg_blkno),
1060                              le16_to_cpu(gd->bg_bits),
1061                              OCFS2_I(inode)->ip_clusters, max_bits);
1062                 }
1063
1064                 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1065                                                         group_bh, bits_wanted,
1066                                                         max_bits,
1067                                                         &tmp_off, &tmp_found);
1068                 if (ret)
1069                         return ret;
1070
1071                 if (max_block) {
1072                         blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1073                                                           gd_cluster_off +
1074                                                           tmp_off + tmp_found);
1075                         mlog(0, "Checking %llu against %llu\n",
1076                              (unsigned long long)blkoff,
1077                              (unsigned long long)max_block);
1078                         if (blkoff > max_block)
1079                                 return -ENOSPC;
1080                 }
1081
1082                 /* ocfs2_block_group_find_clear_bits() might
1083                  * return success, but we still want to return
1084                  * -ENOSPC unless it found the minimum number
1085                  * of bits. */
1086                 if (min_bits <= tmp_found) {
1087                         *bit_off = tmp_off;
1088                         *bits_found = tmp_found;
1089                         search = 0; /* success */
1090                 } else if (tmp_found) {
1091                         /*
1092                          * Don't show bits which we'll be returning
1093                          * for allocation to the local alloc bitmap.
1094                          */
1095                         ocfs2_local_alloc_seen_free_bits(osb, tmp_found);
1096                 }
1097         }
1098
1099         return search;
1100 }
1101
1102 static int ocfs2_block_group_search(struct inode *inode,
1103                                     struct buffer_head *group_bh,
1104                                     u32 bits_wanted, u32 min_bits,
1105                                     u64 max_block,
1106                                     u16 *bit_off, u16 *bits_found)
1107 {
1108         int ret = -ENOSPC;
1109         u64 blkoff;
1110         struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1111
1112         BUG_ON(min_bits != 1);
1113         BUG_ON(ocfs2_is_cluster_bitmap(inode));
1114
1115         if (bg->bg_free_bits_count) {
1116                 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1117                                                         group_bh, bits_wanted,
1118                                                         le16_to_cpu(bg->bg_bits),
1119                                                         bit_off, bits_found);
1120                 if (!ret && max_block) {
1121                         blkoff = le64_to_cpu(bg->bg_blkno) + *bit_off +
1122                                 *bits_found;
1123                         mlog(0, "Checking %llu against %llu\n",
1124                              (unsigned long long)blkoff,
1125                              (unsigned long long)max_block);
1126                         if (blkoff > max_block)
1127                                 ret = -ENOSPC;
1128                 }
1129         }
1130
1131         return ret;
1132 }
1133
1134 static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
1135                                        handle_t *handle,
1136                                        struct buffer_head *di_bh,
1137                                        u32 num_bits,
1138                                        u16 chain)
1139 {
1140         int ret;
1141         u32 tmp_used;
1142         struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1143         struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1144
1145         ret = ocfs2_journal_access(handle, inode, di_bh,
1146                                    OCFS2_JOURNAL_ACCESS_WRITE);
1147         if (ret < 0) {
1148                 mlog_errno(ret);
1149                 goto out;
1150         }
1151
1152         tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1153         di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1154         le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1155
1156         ret = ocfs2_journal_dirty(handle, di_bh);
1157         if (ret < 0)
1158                 mlog_errno(ret);
1159
1160 out:
1161         return ret;
1162 }
1163
1164 static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
1165                                   handle_t *handle,
1166                                   u32 bits_wanted,
1167                                   u32 min_bits,
1168                                   u16 *bit_off,
1169                                   unsigned int *num_bits,
1170                                   u64 gd_blkno,
1171                                   u16 *bits_left)
1172 {
1173         int ret;
1174         u16 found;
1175         struct buffer_head *group_bh = NULL;
1176         struct ocfs2_group_desc *gd;
1177         struct inode *alloc_inode = ac->ac_inode;
1178
1179         ret = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb), gd_blkno,
1180                                &group_bh, OCFS2_BH_CACHED, alloc_inode);
1181         if (ret < 0) {
1182                 mlog_errno(ret);
1183                 return ret;
1184         }
1185
1186         gd = (struct ocfs2_group_desc *) group_bh->b_data;
1187         if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
1188                 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, gd);
1189                 ret = -EIO;
1190                 goto out;
1191         }
1192
1193         ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
1194                                   ac->ac_max_block, bit_off, &found);
1195         if (ret < 0) {
1196                 if (ret != -ENOSPC)
1197                         mlog_errno(ret);
1198                 goto out;
1199         }
1200
1201         *num_bits = found;
1202
1203         ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1204                                                *num_bits,
1205                                                le16_to_cpu(gd->bg_chain));
1206         if (ret < 0) {
1207                 mlog_errno(ret);
1208                 goto out;
1209         }
1210
1211         ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1212                                          *bit_off, *num_bits);
1213         if (ret < 0)
1214                 mlog_errno(ret);
1215
1216         *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1217
1218 out:
1219         brelse(group_bh);
1220
1221         return ret;
1222 }
1223
1224 static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1225                               handle_t *handle,
1226                               u32 bits_wanted,
1227                               u32 min_bits,
1228                               u16 *bit_off,
1229                               unsigned int *num_bits,
1230                               u64 *bg_blkno,
1231                               u16 *bits_left)
1232 {
1233         int status;
1234         u16 chain, tmp_bits;
1235         u32 tmp_used;
1236         u64 next_group;
1237         struct inode *alloc_inode = ac->ac_inode;
1238         struct buffer_head *group_bh = NULL;
1239         struct buffer_head *prev_group_bh = NULL;
1240         struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1241         struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1242         struct ocfs2_group_desc *bg;
1243
1244         chain = ac->ac_chain;
1245         mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1246              bits_wanted, chain,
1247              (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
1248
1249         status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1250                                   le64_to_cpu(cl->cl_recs[chain].c_blkno),
1251                                   &group_bh, OCFS2_BH_CACHED, alloc_inode);
1252         if (status < 0) {
1253                 mlog_errno(status);
1254                 goto bail;
1255         }
1256         bg = (struct ocfs2_group_desc *) group_bh->b_data;
1257         status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
1258         if (status) {
1259                 mlog_errno(status);
1260                 goto bail;
1261         }
1262
1263         status = -ENOSPC;
1264         /* for now, the chain search is a bit simplistic. We just use
1265          * the 1st group with any empty bits. */
1266         while ((status = ac->ac_group_search(alloc_inode, group_bh,
1267                                              bits_wanted, min_bits,
1268                                              ac->ac_max_block, bit_off,
1269                                              &tmp_bits)) == -ENOSPC) {
1270                 if (!bg->bg_next_group)
1271                         break;
1272                 if (prev_group_bh) {
1273                         brelse(prev_group_bh);
1274                         prev_group_bh = NULL;
1275                 }
1276                 next_group = le64_to_cpu(bg->bg_next_group);
1277                 prev_group_bh = group_bh;
1278                 group_bh = NULL;
1279                 status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1280                                           next_group, &group_bh,
1281                                           OCFS2_BH_CACHED, alloc_inode);
1282                 if (status < 0) {
1283                         mlog_errno(status);
1284                         goto bail;
1285                 }
1286                 bg = (struct ocfs2_group_desc *) group_bh->b_data;
1287                 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
1288                 if (status) {
1289                         mlog_errno(status);
1290                         goto bail;
1291                 }
1292         }
1293         if (status < 0) {
1294                 if (status != -ENOSPC)
1295                         mlog_errno(status);
1296                 goto bail;
1297         }
1298
1299         mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
1300              tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
1301
1302         *num_bits = tmp_bits;
1303
1304         BUG_ON(*num_bits == 0);
1305
1306         /*
1307          * Keep track of previous block descriptor read. When
1308          * we find a target, if we have read more than X
1309          * number of descriptors, and the target is reasonably
1310          * empty, relink him to top of his chain.
1311          *
1312          * We've read 0 extra blocks and only send one more to
1313          * the transaction, yet the next guy to search has a
1314          * much easier time.
1315          *
1316          * Do this *after* figuring out how many bits we're taking out
1317          * of our target group.
1318          */
1319         if (ac->ac_allow_chain_relink &&
1320             (prev_group_bh) &&
1321             (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1322                 status = ocfs2_relink_block_group(handle, alloc_inode,
1323                                                   ac->ac_bh, group_bh,
1324                                                   prev_group_bh, chain);
1325                 if (status < 0) {
1326                         mlog_errno(status);
1327                         goto bail;
1328                 }
1329         }
1330
1331         /* Ok, claim our bits now: set the info on dinode, chainlist
1332          * and then the group */
1333         status = ocfs2_journal_access(handle,
1334                                       alloc_inode,
1335                                       ac->ac_bh,
1336                                       OCFS2_JOURNAL_ACCESS_WRITE);
1337         if (status < 0) {
1338                 mlog_errno(status);
1339                 goto bail;
1340         }
1341
1342         tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1343         fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1344         le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
1345
1346         status = ocfs2_journal_dirty(handle,
1347                                      ac->ac_bh);
1348         if (status < 0) {
1349                 mlog_errno(status);
1350                 goto bail;
1351         }
1352
1353         status = ocfs2_block_group_set_bits(handle,
1354                                             alloc_inode,
1355                                             bg,
1356                                             group_bh,
1357                                             *bit_off,
1358                                             *num_bits);
1359         if (status < 0) {
1360                 mlog_errno(status);
1361                 goto bail;
1362         }
1363
1364         mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
1365              (unsigned long long)le64_to_cpu(fe->i_blkno));
1366
1367         *bg_blkno = le64_to_cpu(bg->bg_blkno);
1368         *bits_left = le16_to_cpu(bg->bg_free_bits_count);
1369 bail:
1370         if (group_bh)
1371                 brelse(group_bh);
1372         if (prev_group_bh)
1373                 brelse(prev_group_bh);
1374
1375         mlog_exit(status);
1376         return status;
1377 }
1378
1379 /* will give out up to bits_wanted contiguous bits. */
1380 static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1381                                      struct ocfs2_alloc_context *ac,
1382                                      handle_t *handle,
1383                                      u32 bits_wanted,
1384                                      u32 min_bits,
1385                                      u16 *bit_off,
1386                                      unsigned int *num_bits,
1387                                      u64 *bg_blkno)
1388 {
1389         int status;
1390         u16 victim, i;
1391         u16 bits_left = 0;
1392         u64 hint_blkno = ac->ac_last_group;
1393         struct ocfs2_chain_list *cl;
1394         struct ocfs2_dinode *fe;
1395
1396         mlog_entry_void();
1397
1398         BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1399         BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1400         BUG_ON(!ac->ac_bh);
1401
1402         fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1403         if (!OCFS2_IS_VALID_DINODE(fe)) {
1404                 OCFS2_RO_ON_INVALID_DINODE(osb->sb, fe);
1405                 status = -EIO;
1406                 goto bail;
1407         }
1408         if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1409             le32_to_cpu(fe->id1.bitmap1.i_total)) {
1410                 ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1411                             "bits but only %u total.",
1412                             (unsigned long long)le64_to_cpu(fe->i_blkno),
1413                             le32_to_cpu(fe->id1.bitmap1.i_used),
1414                             le32_to_cpu(fe->id1.bitmap1.i_total));
1415                 status = -EIO;
1416                 goto bail;
1417         }
1418
1419         if (hint_blkno) {
1420                 /* Attempt to short-circuit the usual search mechanism
1421                  * by jumping straight to the most recently used
1422                  * allocation group. This helps us mantain some
1423                  * contiguousness across allocations. */
1424                 status = ocfs2_search_one_group(ac, handle, bits_wanted,
1425                                                 min_bits, bit_off, num_bits,
1426                                                 hint_blkno, &bits_left);
1427                 if (!status) {
1428                         /* Be careful to update *bg_blkno here as the
1429                          * caller is expecting it to be filled in, and
1430                          * ocfs2_search_one_group() won't do that for
1431                          * us. */
1432                         *bg_blkno = hint_blkno;
1433                         goto set_hint;
1434                 }
1435                 if (status < 0 && status != -ENOSPC) {
1436                         mlog_errno(status);
1437                         goto bail;
1438                 }
1439         }
1440
1441         cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1442
1443         victim = ocfs2_find_victim_chain(cl);
1444         ac->ac_chain = victim;
1445         ac->ac_allow_chain_relink = 1;
1446
1447         status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
1448                                     num_bits, bg_blkno, &bits_left);
1449         if (!status)
1450                 goto set_hint;
1451         if (status < 0 && status != -ENOSPC) {
1452                 mlog_errno(status);
1453                 goto bail;
1454         }
1455
1456         mlog(0, "Search of victim chain %u came up with nothing, "
1457              "trying all chains now.\n", victim);
1458
1459         /* If we didn't pick a good victim, then just default to
1460          * searching each chain in order. Don't allow chain relinking
1461          * because we only calculate enough journal credits for one
1462          * relink per alloc. */
1463         ac->ac_allow_chain_relink = 0;
1464         for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1465                 if (i == victim)
1466                         continue;
1467                 if (!cl->cl_recs[i].c_free)
1468                         continue;
1469
1470                 ac->ac_chain = i;
1471                 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1472                                             bit_off, num_bits, bg_blkno,
1473                                             &bits_left);
1474                 if (!status)
1475                         break;
1476                 if (status < 0 && status != -ENOSPC) {
1477                         mlog_errno(status);
1478                         goto bail;
1479                 }
1480         }
1481
1482 set_hint:
1483         if (status != -ENOSPC) {
1484                 /* If the next search of this group is not likely to
1485                  * yield a suitable extent, then we reset the last
1486                  * group hint so as to not waste a disk read */
1487                 if (bits_left < min_bits)
1488                         ac->ac_last_group = 0;
1489                 else
1490                         ac->ac_last_group = *bg_blkno;
1491         }
1492
1493 bail:
1494         mlog_exit(status);
1495         return status;
1496 }
1497
1498 int ocfs2_claim_metadata(struct ocfs2_super *osb,
1499                          handle_t *handle,
1500                          struct ocfs2_alloc_context *ac,
1501                          u32 bits_wanted,
1502                          u16 *suballoc_bit_start,
1503                          unsigned int *num_bits,
1504                          u64 *blkno_start)
1505 {
1506         int status;
1507         u64 bg_blkno;
1508
1509         BUG_ON(!ac);
1510         BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1511         BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
1512
1513         status = ocfs2_claim_suballoc_bits(osb,
1514                                            ac,
1515                                            handle,
1516                                            bits_wanted,
1517                                            1,
1518                                            suballoc_bit_start,
1519                                            num_bits,
1520                                            &bg_blkno);
1521         if (status < 0) {
1522                 mlog_errno(status);
1523                 goto bail;
1524         }
1525         atomic_inc(&osb->alloc_stats.bg_allocs);
1526
1527         *blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1528         ac->ac_bits_given += (*num_bits);
1529         status = 0;
1530 bail:
1531         mlog_exit(status);
1532         return status;
1533 }
1534
1535 int ocfs2_claim_new_inode(struct ocfs2_super *osb,
1536                           handle_t *handle,
1537                           struct ocfs2_alloc_context *ac,
1538                           u16 *suballoc_bit,
1539                           u64 *fe_blkno)
1540 {
1541         int status;
1542         unsigned int num_bits;
1543         u64 bg_blkno;
1544
1545         mlog_entry_void();
1546
1547         BUG_ON(!ac);
1548         BUG_ON(ac->ac_bits_given != 0);
1549         BUG_ON(ac->ac_bits_wanted != 1);
1550         BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
1551
1552         status = ocfs2_claim_suballoc_bits(osb,
1553                                            ac,
1554                                            handle,
1555                                            1,
1556                                            1,
1557                                            suballoc_bit,
1558                                            &num_bits,
1559                                            &bg_blkno);
1560         if (status < 0) {
1561                 mlog_errno(status);
1562                 goto bail;
1563         }
1564         atomic_inc(&osb->alloc_stats.bg_allocs);
1565
1566         BUG_ON(num_bits != 1);
1567
1568         *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1569         ac->ac_bits_given++;
1570         status = 0;
1571 bail:
1572         mlog_exit(status);
1573         return status;
1574 }
1575
1576 /* translate a group desc. blkno and it's bitmap offset into
1577  * disk cluster offset. */
1578 static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1579                                                    u64 bg_blkno,
1580                                                    u16 bg_bit_off)
1581 {
1582         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1583         u32 cluster = 0;
1584
1585         BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1586
1587         if (bg_blkno != osb->first_cluster_group_blkno)
1588                 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1589         cluster += (u32) bg_bit_off;
1590         return cluster;
1591 }
1592
1593 /* given a cluster offset, calculate which block group it belongs to
1594  * and return that block offset. */
1595 u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
1596 {
1597         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1598         u32 group_no;
1599
1600         BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1601
1602         group_no = cluster / osb->bitmap_cpg;
1603         if (!group_no)
1604                 return osb->first_cluster_group_blkno;
1605         return ocfs2_clusters_to_blocks(inode->i_sb,
1606                                         group_no * osb->bitmap_cpg);
1607 }
1608
1609 /* given the block number of a cluster start, calculate which cluster
1610  * group and descriptor bitmap offset that corresponds to. */
1611 static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1612                                                 u64 data_blkno,
1613                                                 u64 *bg_blkno,
1614                                                 u16 *bg_bit_off)
1615 {
1616         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1617         u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1618
1619         BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1620
1621         *bg_blkno = ocfs2_which_cluster_group(inode,
1622                                               data_cluster);
1623
1624         if (*bg_blkno == osb->first_cluster_group_blkno)
1625                 *bg_bit_off = (u16) data_cluster;
1626         else
1627                 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1628                                                              data_blkno - *bg_blkno);
1629 }
1630
1631 /*
1632  * min_bits - minimum contiguous chunk from this total allocation we
1633  * can handle. set to what we asked for originally for a full
1634  * contig. allocation, set to '1' to indicate we can deal with extents
1635  * of any size.
1636  */
1637 int __ocfs2_claim_clusters(struct ocfs2_super *osb,
1638                            handle_t *handle,
1639                            struct ocfs2_alloc_context *ac,
1640                            u32 min_clusters,
1641                            u32 max_clusters,
1642                            u32 *cluster_start,
1643                            u32 *num_clusters)
1644 {
1645         int status;
1646         unsigned int bits_wanted = max_clusters;
1647         u64 bg_blkno = 0;
1648         u16 bg_bit_off;
1649
1650         mlog_entry_void();
1651
1652         BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1653
1654         BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1655                && ac->ac_which != OCFS2_AC_USE_MAIN);
1656
1657         if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1658                 status = ocfs2_claim_local_alloc_bits(osb,
1659                                                       handle,
1660                                                       ac,
1661                                                       bits_wanted,
1662                                                       cluster_start,
1663                                                       num_clusters);
1664                 if (!status)
1665                         atomic_inc(&osb->alloc_stats.local_data);
1666         } else {
1667                 if (min_clusters > (osb->bitmap_cpg - 1)) {
1668                         /* The only paths asking for contiguousness
1669                          * should know about this already. */
1670                         mlog(ML_ERROR, "minimum allocation requested %u exceeds "
1671                              "group bitmap size %u!\n", min_clusters,
1672                              osb->bitmap_cpg);
1673                         status = -ENOSPC;
1674                         goto bail;
1675                 }
1676                 /* clamp the current request down to a realistic size. */
1677                 if (bits_wanted > (osb->bitmap_cpg - 1))
1678                         bits_wanted = osb->bitmap_cpg - 1;
1679
1680                 status = ocfs2_claim_suballoc_bits(osb,
1681                                                    ac,
1682                                                    handle,
1683                                                    bits_wanted,
1684                                                    min_clusters,
1685                                                    &bg_bit_off,
1686                                                    num_clusters,
1687                                                    &bg_blkno);
1688                 if (!status) {
1689                         *cluster_start =
1690                                 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1691                                                                  bg_blkno,
1692                                                                  bg_bit_off);
1693                         atomic_inc(&osb->alloc_stats.bitmap_data);
1694                 }
1695         }
1696         if (status < 0) {
1697                 if (status != -ENOSPC)
1698                         mlog_errno(status);
1699                 goto bail;
1700         }
1701
1702         ac->ac_bits_given += *num_clusters;
1703
1704 bail:
1705         mlog_exit(status);
1706         return status;
1707 }
1708
1709 int ocfs2_claim_clusters(struct ocfs2_super *osb,
1710                          handle_t *handle,
1711                          struct ocfs2_alloc_context *ac,
1712                          u32 min_clusters,
1713                          u32 *cluster_start,
1714                          u32 *num_clusters)
1715 {
1716         unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1717
1718         return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
1719                                       bits_wanted, cluster_start, num_clusters);
1720 }
1721
1722 static inline int ocfs2_block_group_clear_bits(handle_t *handle,
1723                                                struct inode *alloc_inode,
1724                                                struct ocfs2_group_desc *bg,
1725                                                struct buffer_head *group_bh,
1726                                                unsigned int bit_off,
1727                                                unsigned int num_bits)
1728 {
1729         int status;
1730         unsigned int tmp;
1731         int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1732         struct ocfs2_group_desc *undo_bg = NULL;
1733
1734         mlog_entry_void();
1735
1736         if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
1737                 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
1738                 status = -EIO;
1739                 goto bail;
1740         }
1741
1742         mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1743
1744         if (ocfs2_is_cluster_bitmap(alloc_inode))
1745                 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1746
1747         status = ocfs2_journal_access(handle, alloc_inode, group_bh,
1748                                       journal_type);
1749         if (status < 0) {
1750                 mlog_errno(status);
1751                 goto bail;
1752         }
1753
1754         if (ocfs2_is_cluster_bitmap(alloc_inode))
1755                 undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
1756
1757         tmp = num_bits;
1758         while(tmp--) {
1759                 ocfs2_clear_bit((bit_off + tmp),
1760                                 (unsigned long *) bg->bg_bitmap);
1761                 if (ocfs2_is_cluster_bitmap(alloc_inode))
1762                         ocfs2_set_bit(bit_off + tmp,
1763                                       (unsigned long *) undo_bg->bg_bitmap);
1764         }
1765         le16_add_cpu(&bg->bg_free_bits_count, num_bits);
1766
1767         status = ocfs2_journal_dirty(handle, group_bh);
1768         if (status < 0)
1769                 mlog_errno(status);
1770 bail:
1771         return status;
1772 }
1773
1774 /*
1775  * expects the suballoc inode to already be locked.
1776  */
1777 int ocfs2_free_suballoc_bits(handle_t *handle,
1778                              struct inode *alloc_inode,
1779                              struct buffer_head *alloc_bh,
1780                              unsigned int start_bit,
1781                              u64 bg_blkno,
1782                              unsigned int count)
1783 {
1784         int status = 0;
1785         u32 tmp_used;
1786         struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
1787         struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
1788         struct ocfs2_chain_list *cl = &fe->id2.i_chain;
1789         struct buffer_head *group_bh = NULL;
1790         struct ocfs2_group_desc *group;
1791
1792         mlog_entry_void();
1793
1794         if (!OCFS2_IS_VALID_DINODE(fe)) {
1795                 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
1796                 status = -EIO;
1797                 goto bail;
1798         }
1799         BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
1800
1801         mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1802              (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
1803              (unsigned long long)bg_blkno, start_bit);
1804
1805         status = ocfs2_read_block(osb, bg_blkno, &group_bh, OCFS2_BH_CACHED,
1806                                   alloc_inode);
1807         if (status < 0) {
1808                 mlog_errno(status);
1809                 goto bail;
1810         }
1811
1812         group = (struct ocfs2_group_desc *) group_bh->b_data;
1813         status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, group);
1814         if (status) {
1815                 mlog_errno(status);
1816                 goto bail;
1817         }
1818         BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
1819
1820         status = ocfs2_block_group_clear_bits(handle, alloc_inode,
1821                                               group, group_bh,
1822                                               start_bit, count);
1823         if (status < 0) {
1824                 mlog_errno(status);
1825                 goto bail;
1826         }
1827
1828         status = ocfs2_journal_access(handle, alloc_inode, alloc_bh,
1829                                       OCFS2_JOURNAL_ACCESS_WRITE);
1830         if (status < 0) {
1831                 mlog_errno(status);
1832                 goto bail;
1833         }
1834
1835         le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
1836                      count);
1837         tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1838         fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
1839
1840         status = ocfs2_journal_dirty(handle, alloc_bh);
1841         if (status < 0) {
1842                 mlog_errno(status);
1843                 goto bail;
1844         }
1845
1846 bail:
1847         if (group_bh)
1848                 brelse(group_bh);
1849
1850         mlog_exit(status);
1851         return status;
1852 }
1853
1854 int ocfs2_free_dinode(handle_t *handle,
1855                       struct inode *inode_alloc_inode,
1856                       struct buffer_head *inode_alloc_bh,
1857                       struct ocfs2_dinode *di)
1858 {
1859         u64 blk = le64_to_cpu(di->i_blkno);
1860         u16 bit = le16_to_cpu(di->i_suballoc_bit);
1861         u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1862
1863         return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
1864                                         inode_alloc_bh, bit, bg_blkno, 1);
1865 }
1866
1867 int ocfs2_free_clusters(handle_t *handle,
1868                        struct inode *bitmap_inode,
1869                        struct buffer_head *bitmap_bh,
1870                        u64 start_blk,
1871                        unsigned int num_clusters)
1872 {
1873         int status;
1874         u16 bg_start_bit;
1875         u64 bg_blkno;
1876         struct ocfs2_dinode *fe;
1877
1878         /* You can't ever have a contiguous set of clusters
1879          * bigger than a block group bitmap so we never have to worry
1880          * about looping on them. */
1881
1882         mlog_entry_void();
1883
1884         /* This is expensive. We can safely remove once this stuff has
1885          * gotten tested really well. */
1886         BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
1887
1888         fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
1889
1890         ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
1891                                      &bg_start_bit);
1892
1893         mlog(0, "want to free %u clusters starting at block %llu\n",
1894              num_clusters, (unsigned long long)start_blk);
1895         mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1896              (unsigned long long)bg_blkno, bg_start_bit);
1897
1898         status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
1899                                           bg_start_bit, bg_blkno,
1900                                           num_clusters);
1901         if (status < 0) {
1902                 mlog_errno(status);
1903                 goto out;
1904         }
1905
1906         ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
1907                                          num_clusters);
1908
1909 out:
1910         mlog_exit(status);
1911         return status;
1912 }
1913
1914 static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
1915 {
1916         printk("Block Group:\n");
1917         printk("bg_signature:       %s\n", bg->bg_signature);
1918         printk("bg_size:            %u\n", bg->bg_size);
1919         printk("bg_bits:            %u\n", bg->bg_bits);
1920         printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
1921         printk("bg_chain:           %u\n", bg->bg_chain);
1922         printk("bg_generation:      %u\n", le32_to_cpu(bg->bg_generation));
1923         printk("bg_next_group:      %llu\n",
1924                (unsigned long long)bg->bg_next_group);
1925         printk("bg_parent_dinode:   %llu\n",
1926                (unsigned long long)bg->bg_parent_dinode);
1927         printk("bg_blkno:           %llu\n",
1928                (unsigned long long)bg->bg_blkno);
1929 }
1930
1931 static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
1932 {
1933         int i;
1934
1935         printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
1936         printk("i_signature:                  %s\n", fe->i_signature);
1937         printk("i_size:                       %llu\n",
1938                (unsigned long long)fe->i_size);
1939         printk("i_clusters:                   %u\n", fe->i_clusters);
1940         printk("i_generation:                 %u\n",
1941                le32_to_cpu(fe->i_generation));
1942         printk("id1.bitmap1.i_used:           %u\n",
1943                le32_to_cpu(fe->id1.bitmap1.i_used));
1944         printk("id1.bitmap1.i_total:          %u\n",
1945                le32_to_cpu(fe->id1.bitmap1.i_total));
1946         printk("id2.i_chain.cl_cpg:           %u\n", fe->id2.i_chain.cl_cpg);
1947         printk("id2.i_chain.cl_bpc:           %u\n", fe->id2.i_chain.cl_bpc);
1948         printk("id2.i_chain.cl_count:         %u\n", fe->id2.i_chain.cl_count);
1949         printk("id2.i_chain.cl_next_free_rec: %u\n",
1950                fe->id2.i_chain.cl_next_free_rec);
1951         for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
1952                 printk("fe->id2.i_chain.cl_recs[%d].c_free:  %u\n", i,
1953                        fe->id2.i_chain.cl_recs[i].c_free);
1954                 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
1955                        fe->id2.i_chain.cl_recs[i].c_total);
1956                 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
1957                        (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
1958         }
1959 }
1960
1961 /*
1962  * For a given allocation, determine which allocators will need to be
1963  * accessed, and lock them, reserving the appropriate number of bits.
1964  *
1965  * Sparse file systems call this from ocfs2_write_begin_nolock()
1966  * and ocfs2_allocate_unwritten_extents().
1967  *
1968  * File systems which don't support holes call this from
1969  * ocfs2_extend_allocation().
1970  */
1971 int ocfs2_lock_allocators(struct inode *inode,
1972                           struct ocfs2_extent_tree *et,
1973                           u32 clusters_to_add, u32 extents_to_split,
1974                           struct ocfs2_alloc_context **data_ac,
1975                           struct ocfs2_alloc_context **meta_ac)
1976 {
1977         int ret = 0, num_free_extents;
1978         unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
1979         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1980
1981         *meta_ac = NULL;
1982         if (data_ac)
1983                 *data_ac = NULL;
1984
1985         BUG_ON(clusters_to_add != 0 && data_ac == NULL);
1986
1987         num_free_extents = ocfs2_num_free_extents(osb, inode, et);
1988         if (num_free_extents < 0) {
1989                 ret = num_free_extents;
1990                 mlog_errno(ret);
1991                 goto out;
1992         }
1993
1994         /*
1995          * Sparse allocation file systems need to be more conservative
1996          * with reserving room for expansion - the actual allocation
1997          * happens while we've got a journal handle open so re-taking
1998          * a cluster lock (because we ran out of room for another
1999          * extent) will violate ordering rules.
2000          *
2001          * Most of the time we'll only be seeing this 1 cluster at a time
2002          * anyway.
2003          *
2004          * Always lock for any unwritten extents - we might want to
2005          * add blocks during a split.
2006          */
2007         if (!num_free_extents ||
2008             (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
2009                 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
2010                 if (ret < 0) {
2011                         if (ret != -ENOSPC)
2012                                 mlog_errno(ret);
2013                         goto out;
2014                 }
2015         }
2016
2017         if (clusters_to_add == 0)
2018                 goto out;
2019
2020         ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2021         if (ret < 0) {
2022                 if (ret != -ENOSPC)
2023                         mlog_errno(ret);
2024                 goto out;
2025         }
2026
2027 out:
2028         if (ret) {
2029                 if (*meta_ac) {
2030                         ocfs2_free_alloc_context(*meta_ac);
2031                         *meta_ac = NULL;
2032                 }
2033
2034                 /*
2035                  * We cannot have an error and a non null *data_ac.
2036                  */
2037         }
2038
2039         return ret;
2040 }