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