OSDN Git Service

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