1328970deSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2fa60ce2cSMasahiro Yamada /*
3ccd979bdSMark Fasheh * suballoc.c
4ccd979bdSMark Fasheh *
5ccd979bdSMark Fasheh * metadata alloc and free
6ccd979bdSMark Fasheh * Inspired by ext3 block groups.
7ccd979bdSMark Fasheh *
8ccd979bdSMark Fasheh * Copyright (C) 2002, 2004 Oracle. All rights reserved.
9ccd979bdSMark Fasheh */
10ccd979bdSMark Fasheh
11ccd979bdSMark Fasheh #include <linux/fs.h>
12ccd979bdSMark Fasheh #include <linux/types.h>
13ccd979bdSMark Fasheh #include <linux/slab.h>
14ccd979bdSMark Fasheh #include <linux/highmem.h>
15ccd979bdSMark Fasheh
16ccd979bdSMark Fasheh #include <cluster/masklog.h>
17ccd979bdSMark Fasheh
18ccd979bdSMark Fasheh #include "ocfs2.h"
19ccd979bdSMark Fasheh
20ccd979bdSMark Fasheh #include "alloc.h"
21d6b32bbbSJoel Becker #include "blockcheck.h"
22ccd979bdSMark Fasheh #include "dlmglue.h"
23ccd979bdSMark Fasheh #include "inode.h"
24ccd979bdSMark Fasheh #include "journal.h"
25ccd979bdSMark Fasheh #include "localalloc.h"
26ccd979bdSMark Fasheh #include "suballoc.h"
27ccd979bdSMark Fasheh #include "super.h"
28ccd979bdSMark Fasheh #include "sysfile.h"
29ccd979bdSMark Fasheh #include "uptodate.h"
302f73e135STao Ma #include "ocfs2_trace.h"
31ccd979bdSMark Fasheh
32ccd979bdSMark Fasheh #include "buffer_head_io.h"
33ccd979bdSMark Fasheh
34ffda89a3STao Ma #define NOT_ALLOC_NEW_GROUP 0
3560ca81e8STao Ma #define ALLOC_NEW_GROUP 0x1
3660ca81e8STao Ma #define ALLOC_GROUPS_FROM_GLOBAL 0x2
37ffda89a3STao Ma
38b89c5428STiger Yang #define OCFS2_MAX_TO_STEAL 1024
394d0ddb2cSTao Ma
407d1fe093SJoel Becker struct ocfs2_suballoc_result {
412b6cb576SJoel Becker u64 sr_bg_blkno; /* The bg we allocated from. Set
422b6cb576SJoel Becker to 0 when a block group is
432b6cb576SJoel Becker contiguous. */
44e49e2767SMark Fasheh u64 sr_bg_stable_blkno; /*
45e49e2767SMark Fasheh * Doesn't change, always
46e49e2767SMark Fasheh * set to target block
47e49e2767SMark Fasheh * group descriptor
48e49e2767SMark Fasheh * block.
49e49e2767SMark Fasheh */
50ba206635SJoel Becker u64 sr_blkno; /* The first allocated block */
517d1fe093SJoel Becker unsigned int sr_bit_offset; /* The bit in the bg */
527d1fe093SJoel Becker unsigned int sr_bits; /* How many bits we claimed */
537d1fe093SJoel Becker };
547d1fe093SJoel Becker
ocfs2_group_from_res(struct ocfs2_suballoc_result * res)55b2b6ebf5SMark Fasheh static u64 ocfs2_group_from_res(struct ocfs2_suballoc_result *res)
56b2b6ebf5SMark Fasheh {
57b2b6ebf5SMark Fasheh if (res->sr_blkno == 0)
58b2b6ebf5SMark Fasheh return 0;
59b2b6ebf5SMark Fasheh
60b2b6ebf5SMark Fasheh if (res->sr_bg_blkno)
61b2b6ebf5SMark Fasheh return res->sr_bg_blkno;
62b2b6ebf5SMark Fasheh
63b2b6ebf5SMark Fasheh return ocfs2_which_suballoc_group(res->sr_blkno, res->sr_bit_offset);
64b2b6ebf5SMark Fasheh }
65b2b6ebf5SMark Fasheh
66ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
671fabe148SMark Fasheh static int ocfs2_block_group_fill(handle_t *handle,
68ccd979bdSMark Fasheh struct inode *alloc_inode,
69ccd979bdSMark Fasheh struct buffer_head *bg_bh,
70ccd979bdSMark Fasheh u64 group_blkno,
71798db35fSJoel Becker unsigned int group_clusters,
72ccd979bdSMark Fasheh u16 my_chain,
73ccd979bdSMark Fasheh struct ocfs2_chain_list *cl);
74ccd979bdSMark Fasheh static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
75ccd979bdSMark Fasheh struct inode *alloc_inode,
761187c968SJoel Becker struct buffer_head *bh,
7760ca81e8STao Ma u64 max_block,
78feb473a6STao Ma u64 *last_alloc_group,
7960ca81e8STao Ma int flags);
80ccd979bdSMark Fasheh
81ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode,
82ccd979bdSMark Fasheh struct buffer_head *group_bh,
83ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits,
841187c968SJoel Becker u64 max_block,
857d1fe093SJoel Becker struct ocfs2_suballoc_result *res);
86ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode,
87ccd979bdSMark Fasheh struct buffer_head *group_bh,
88ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits,
891187c968SJoel Becker u64 max_block,
907d1fe093SJoel Becker struct ocfs2_suballoc_result *res);
91aa8f8e93SJoel Becker static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
921fabe148SMark Fasheh handle_t *handle,
93ccd979bdSMark Fasheh u32 bits_wanted,
94ccd979bdSMark Fasheh u32 min_bits,
957d1fe093SJoel Becker struct ocfs2_suballoc_result *res);
96ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
97ccd979bdSMark Fasheh int nr);
981fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle,
99ccd979bdSMark Fasheh struct inode *alloc_inode,
100ccd979bdSMark Fasheh struct buffer_head *fe_bh,
101ccd979bdSMark Fasheh struct buffer_head *bg_bh,
102ccd979bdSMark Fasheh struct buffer_head *prev_bg_bh,
103ccd979bdSMark Fasheh u16 chain);
104ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
105ccd979bdSMark Fasheh u32 wanted);
106ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
107ccd979bdSMark Fasheh u64 bg_blkno,
108ccd979bdSMark Fasheh u16 bg_bit_off);
109ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode,
110ccd979bdSMark Fasheh u64 data_blkno,
111ccd979bdSMark Fasheh u64 *bg_blkno,
112ccd979bdSMark Fasheh u16 *bg_bit_off);
1131187c968SJoel Becker static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
1141187c968SJoel Becker u32 bits_wanted, u64 max_block,
11560ca81e8STao Ma int flags,
1161187c968SJoel Becker struct ocfs2_alloc_context **ac);
117ccd979bdSMark Fasheh
ocfs2_free_ac_resource(struct ocfs2_alloc_context * ac)1189c7af40bSMark Fasheh void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
119ccd979bdSMark Fasheh {
120da5cbf2fSMark Fasheh struct inode *inode = ac->ac_inode;
121da5cbf2fSMark Fasheh
122da5cbf2fSMark Fasheh if (inode) {
123da5cbf2fSMark Fasheh if (ac->ac_which != OCFS2_AC_USE_LOCAL)
124e63aecb6SMark Fasheh ocfs2_inode_unlock(inode, 1);
125da5cbf2fSMark Fasheh
1265955102cSAl Viro inode_unlock(inode);
127da5cbf2fSMark Fasheh
128da5cbf2fSMark Fasheh iput(inode);
1294d0ddb2cSTao Ma ac->ac_inode = NULL;
130da5cbf2fSMark Fasheh }
131ccd979bdSMark Fasheh brelse(ac->ac_bh);
1324d0ddb2cSTao Ma ac->ac_bh = NULL;
133e3b4a97dSMark Fasheh ac->ac_resv = NULL;
134e49e2767SMark Fasheh kfree(ac->ac_find_loc_priv);
135e49e2767SMark Fasheh ac->ac_find_loc_priv = NULL;
136e49e2767SMark Fasheh }
1374d0ddb2cSTao Ma
ocfs2_free_alloc_context(struct ocfs2_alloc_context * ac)1384d0ddb2cSTao Ma void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
1394d0ddb2cSTao Ma {
1404d0ddb2cSTao Ma ocfs2_free_ac_resource(ac);
141ccd979bdSMark Fasheh kfree(ac);
142ccd979bdSMark Fasheh }
143ccd979bdSMark Fasheh
ocfs2_bits_per_group(struct ocfs2_chain_list * cl)144ccd979bdSMark Fasheh static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
145ccd979bdSMark Fasheh {
146ccd979bdSMark Fasheh return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
147ccd979bdSMark Fasheh }
148ccd979bdSMark Fasheh
14957e3e797SJoel Becker #define do_error(fmt, ...) \
15057e3e797SJoel Becker do { \
15178c37eb0STao Ma if (resize) \
1527ecef14aSJoe Perches mlog(ML_ERROR, fmt, ##__VA_ARGS__); \
15357e3e797SJoel Becker else \
15417a5b9abSGoldwyn Rodrigues return ocfs2_error(sb, fmt, ##__VA_ARGS__); \
15557e3e797SJoel Becker } while (0)
15657e3e797SJoel Becker
ocfs2_validate_gd_self(struct super_block * sb,struct buffer_head * bh,int resize)157970e4936SJoel Becker static int ocfs2_validate_gd_self(struct super_block *sb,
158970e4936SJoel Becker struct buffer_head *bh,
15978c37eb0STao Ma int resize)
160970e4936SJoel Becker {
161970e4936SJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
162970e4936SJoel Becker
1637bf72edeSMark Fasheh if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
1647ecef14aSJoe Perches do_error("Group descriptor #%llu has bad signature %.*s\n",
16568f64d47SJoel Becker (unsigned long long)bh->b_blocknr, 7,
16657e3e797SJoel Becker gd->bg_signature);
1677bf72edeSMark Fasheh }
1687bf72edeSMark Fasheh
16968f64d47SJoel Becker if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
1707ecef14aSJoe Perches do_error("Group descriptor #%llu has an invalid bg_blkno of %llu\n",
17168f64d47SJoel Becker (unsigned long long)bh->b_blocknr,
17268f64d47SJoel Becker (unsigned long long)le64_to_cpu(gd->bg_blkno));
17368f64d47SJoel Becker }
17468f64d47SJoel Becker
17568f64d47SJoel Becker if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
1767ecef14aSJoe Perches do_error("Group descriptor #%llu has an invalid fs_generation of #%u\n",
17768f64d47SJoel Becker (unsigned long long)bh->b_blocknr,
17868f64d47SJoel Becker le32_to_cpu(gd->bg_generation));
17968f64d47SJoel Becker }
18068f64d47SJoel Becker
181970e4936SJoel Becker if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
1827ecef14aSJoe Perches do_error("Group descriptor #%llu has bit count %u but claims that %u are free\n",
183970e4936SJoel Becker (unsigned long long)bh->b_blocknr,
184970e4936SJoel Becker le16_to_cpu(gd->bg_bits),
185970e4936SJoel Becker le16_to_cpu(gd->bg_free_bits_count));
186970e4936SJoel Becker }
187970e4936SJoel Becker
188970e4936SJoel Becker if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
1897ecef14aSJoe Perches do_error("Group descriptor #%llu has bit count %u but max bitmap bits of %u\n",
190970e4936SJoel Becker (unsigned long long)bh->b_blocknr,
191970e4936SJoel Becker le16_to_cpu(gd->bg_bits),
192970e4936SJoel Becker 8 * le16_to_cpu(gd->bg_size));
193970e4936SJoel Becker }
194970e4936SJoel Becker
195970e4936SJoel Becker return 0;
196970e4936SJoel Becker }
197970e4936SJoel Becker
ocfs2_validate_gd_parent(struct super_block * sb,struct ocfs2_dinode * di,struct buffer_head * bh,int resize)198970e4936SJoel Becker static int ocfs2_validate_gd_parent(struct super_block *sb,
199970e4936SJoel Becker struct ocfs2_dinode *di,
200970e4936SJoel Becker struct buffer_head *bh,
20178c37eb0STao Ma int resize)
202970e4936SJoel Becker {
203970e4936SJoel Becker unsigned int max_bits;
204970e4936SJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
205970e4936SJoel Becker
2067bf72edeSMark Fasheh if (di->i_blkno != gd->bg_parent_dinode) {
2077ecef14aSJoe Perches do_error("Group descriptor #%llu has bad parent pointer (%llu, expected %llu)\n",
20868f64d47SJoel Becker (unsigned long long)bh->b_blocknr,
2097bf72edeSMark Fasheh (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
2107bf72edeSMark Fasheh (unsigned long long)le64_to_cpu(di->i_blkno));
2117bf72edeSMark Fasheh }
2127bf72edeSMark Fasheh
2137bf72edeSMark Fasheh max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
2147bf72edeSMark Fasheh if (le16_to_cpu(gd->bg_bits) > max_bits) {
2157ecef14aSJoe Perches do_error("Group descriptor #%llu has bit count of %u\n",
21668f64d47SJoel Becker (unsigned long long)bh->b_blocknr,
2177bf72edeSMark Fasheh le16_to_cpu(gd->bg_bits));
2187bf72edeSMark Fasheh }
2197bf72edeSMark Fasheh
22078c37eb0STao Ma /* In resize, we may meet the case bg_chain == cl_next_free_rec. */
22178c37eb0STao Ma if ((le16_to_cpu(gd->bg_chain) >
22278c37eb0STao Ma le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) ||
22378c37eb0STao Ma ((le16_to_cpu(gd->bg_chain) ==
22478c37eb0STao Ma le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) {
2257ecef14aSJoe Perches do_error("Group descriptor #%llu has bad chain %u\n",
22668f64d47SJoel Becker (unsigned long long)bh->b_blocknr,
2277bf72edeSMark Fasheh le16_to_cpu(gd->bg_chain));
2287bf72edeSMark Fasheh }
2297bf72edeSMark Fasheh
230970e4936SJoel Becker return 0;
2317bf72edeSMark Fasheh }
2327bf72edeSMark Fasheh
23357e3e797SJoel Becker #undef do_error
2347bf72edeSMark Fasheh
235970e4936SJoel Becker /*
236970e4936SJoel Becker * This version only prints errors. It does not fail the filesystem, and
237970e4936SJoel Becker * exists only for resize.
238970e4936SJoel Becker */
ocfs2_check_group_descriptor(struct super_block * sb,struct ocfs2_dinode * di,struct buffer_head * bh)239970e4936SJoel Becker int ocfs2_check_group_descriptor(struct super_block *sb,
240970e4936SJoel Becker struct ocfs2_dinode *di,
241970e4936SJoel Becker struct buffer_head *bh)
242970e4936SJoel Becker {
243970e4936SJoel Becker int rc;
244d6b32bbbSJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
245970e4936SJoel Becker
246d6b32bbbSJoel Becker BUG_ON(!buffer_uptodate(bh));
247d6b32bbbSJoel Becker
248d6b32bbbSJoel Becker /*
249d6b32bbbSJoel Becker * If the ecc fails, we return the error but otherwise
250d6b32bbbSJoel Becker * leave the filesystem running. We know any error is
251d6b32bbbSJoel Becker * local to this block.
252d6b32bbbSJoel Becker */
253d6b32bbbSJoel Becker rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
25413723d00SJoel Becker if (rc) {
25513723d00SJoel Becker mlog(ML_ERROR,
25613723d00SJoel Becker "Checksum failed for group descriptor %llu\n",
25713723d00SJoel Becker (unsigned long long)bh->b_blocknr);
25813723d00SJoel Becker } else
259970e4936SJoel Becker rc = ocfs2_validate_gd_self(sb, bh, 1);
260970e4936SJoel Becker if (!rc)
261970e4936SJoel Becker rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
262970e4936SJoel Becker
263970e4936SJoel Becker return rc;
264970e4936SJoel Becker }
265970e4936SJoel Becker
ocfs2_validate_group_descriptor(struct super_block * sb,struct buffer_head * bh)266970e4936SJoel Becker static int ocfs2_validate_group_descriptor(struct super_block *sb,
267970e4936SJoel Becker struct buffer_head *bh)
268970e4936SJoel Becker {
269d6b32bbbSJoel Becker int rc;
270d6b32bbbSJoel Becker struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
271d6b32bbbSJoel Becker
2722f73e135STao Ma trace_ocfs2_validate_group_descriptor(
273970e4936SJoel Becker (unsigned long long)bh->b_blocknr);
274970e4936SJoel Becker
275d6b32bbbSJoel Becker BUG_ON(!buffer_uptodate(bh));
276d6b32bbbSJoel Becker
277d6b32bbbSJoel Becker /*
278d6b32bbbSJoel Becker * If the ecc fails, we return the error but otherwise
279d6b32bbbSJoel Becker * leave the filesystem running. We know any error is
280d6b32bbbSJoel Becker * local to this block.
281d6b32bbbSJoel Becker */
282d6b32bbbSJoel Becker rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
283d6b32bbbSJoel Becker if (rc)
284d6b32bbbSJoel Becker return rc;
285d6b32bbbSJoel Becker
286d6b32bbbSJoel Becker /*
287d6b32bbbSJoel Becker * Errors after here are fatal.
288d6b32bbbSJoel Becker */
289d6b32bbbSJoel Becker
290970e4936SJoel Becker return ocfs2_validate_gd_self(sb, bh, 0);
2917bf72edeSMark Fasheh }
2927bf72edeSMark Fasheh
ocfs2_read_group_descriptor(struct inode * inode,struct ocfs2_dinode * di,u64 gd_blkno,struct buffer_head ** bh)29368f64d47SJoel Becker int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
29468f64d47SJoel Becker u64 gd_blkno, struct buffer_head **bh)
29568f64d47SJoel Becker {
29668f64d47SJoel Becker int rc;
29768f64d47SJoel Becker struct buffer_head *tmp = *bh;
29868f64d47SJoel Becker
2998cb471e8SJoel Becker rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
300970e4936SJoel Becker ocfs2_validate_group_descriptor);
30168f64d47SJoel Becker if (rc)
30268f64d47SJoel Becker goto out;
30368f64d47SJoel Becker
304970e4936SJoel Becker rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
30568f64d47SJoel Becker if (rc) {
30668f64d47SJoel Becker brelse(tmp);
30768f64d47SJoel Becker goto out;
30868f64d47SJoel Becker }
30968f64d47SJoel Becker
31068f64d47SJoel Becker /* If ocfs2_read_block() got us a new bh, pass it up. */
31168f64d47SJoel Becker if (!*bh)
31268f64d47SJoel Becker *bh = tmp;
31368f64d47SJoel Becker
31468f64d47SJoel Becker out:
31568f64d47SJoel Becker return rc;
31668f64d47SJoel Becker }
31768f64d47SJoel Becker
ocfs2_bg_discontig_add_extent(struct ocfs2_super * osb,struct ocfs2_group_desc * bg,struct ocfs2_chain_list * cl,u64 p_blkno,unsigned int clusters)318798db35fSJoel Becker static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb,
319798db35fSJoel Becker struct ocfs2_group_desc *bg,
320798db35fSJoel Becker struct ocfs2_chain_list *cl,
32147dea423STao Ma u64 p_blkno, unsigned int clusters)
322798db35fSJoel Becker {
323798db35fSJoel Becker struct ocfs2_extent_list *el = &bg->bg_list;
324798db35fSJoel Becker struct ocfs2_extent_rec *rec;
325798db35fSJoel Becker
3264711954eSTao Ma BUG_ON(!ocfs2_supports_discontig_bg(osb));
327798db35fSJoel Becker if (!el->l_next_free_rec)
328798db35fSJoel Becker el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb));
329798db35fSJoel Becker rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)];
3304711954eSTao Ma rec->e_blkno = cpu_to_le64(p_blkno);
331798db35fSJoel Becker rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) /
332798db35fSJoel Becker le16_to_cpu(cl->cl_bpc));
33347dea423STao Ma rec->e_leaf_clusters = cpu_to_le16(clusters);
334798db35fSJoel Becker le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc));
3354711954eSTao Ma le16_add_cpu(&bg->bg_free_bits_count,
3364711954eSTao Ma clusters * le16_to_cpu(cl->cl_bpc));
337798db35fSJoel Becker le16_add_cpu(&el->l_next_free_rec, 1);
338798db35fSJoel Becker }
339798db35fSJoel Becker
ocfs2_block_group_fill(handle_t * handle,struct inode * alloc_inode,struct buffer_head * bg_bh,u64 group_blkno,unsigned int group_clusters,u16 my_chain,struct ocfs2_chain_list * cl)3401fabe148SMark Fasheh static int ocfs2_block_group_fill(handle_t *handle,
341ccd979bdSMark Fasheh struct inode *alloc_inode,
342ccd979bdSMark Fasheh struct buffer_head *bg_bh,
343ccd979bdSMark Fasheh u64 group_blkno,
344798db35fSJoel Becker unsigned int group_clusters,
345ccd979bdSMark Fasheh u16 my_chain,
346ccd979bdSMark Fasheh struct ocfs2_chain_list *cl)
347ccd979bdSMark Fasheh {
348ccd979bdSMark Fasheh int status = 0;
349798db35fSJoel Becker struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
350ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
351ccd979bdSMark Fasheh struct super_block * sb = alloc_inode->i_sb;
352ccd979bdSMark Fasheh
353ccd979bdSMark Fasheh if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
3547ecef14aSJoe Perches status = ocfs2_error(alloc_inode->i_sb,
3557ecef14aSJoe Perches "group block (%llu) != b_blocknr (%llu)\n",
356b0697053SMark Fasheh (unsigned long long)group_blkno,
357ccd979bdSMark Fasheh (unsigned long long) bg_bh->b_blocknr);
358ccd979bdSMark Fasheh goto bail;
359ccd979bdSMark Fasheh }
360ccd979bdSMark Fasheh
36113723d00SJoel Becker status = ocfs2_journal_access_gd(handle,
3620cf2f763SJoel Becker INODE_CACHE(alloc_inode),
363ccd979bdSMark Fasheh bg_bh,
364ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_CREATE);
365ccd979bdSMark Fasheh if (status < 0) {
366ccd979bdSMark Fasheh mlog_errno(status);
367ccd979bdSMark Fasheh goto bail;
368ccd979bdSMark Fasheh }
369ccd979bdSMark Fasheh
370ccd979bdSMark Fasheh memset(bg, 0, sb->s_blocksize);
371ccd979bdSMark Fasheh strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
3721119d3c0Spiaojun bg->bg_generation = cpu_to_le32(osb->fs_generation);
3738571882cSTao Ma bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1,
3748571882cSTao Ma osb->s_feature_incompat));
375ccd979bdSMark Fasheh bg->bg_chain = cpu_to_le16(my_chain);
376ccd979bdSMark Fasheh bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
377ccd979bdSMark Fasheh bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
378ccd979bdSMark Fasheh bg->bg_blkno = cpu_to_le64(group_blkno);
379798db35fSJoel Becker if (group_clusters == le16_to_cpu(cl->cl_cpg))
380798db35fSJoel Becker bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
381798db35fSJoel Becker else
3824711954eSTao Ma ocfs2_bg_discontig_add_extent(osb, bg, cl, group_blkno,
383798db35fSJoel Becker group_clusters);
384798db35fSJoel Becker
385ccd979bdSMark Fasheh /* set the 1st bit in the bitmap to account for the descriptor block */
386ccd979bdSMark Fasheh ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
387ccd979bdSMark Fasheh bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
388ccd979bdSMark Fasheh
389ec20cec7SJoel Becker ocfs2_journal_dirty(handle, bg_bh);
390ccd979bdSMark Fasheh
391ccd979bdSMark Fasheh /* There is no need to zero out or otherwise initialize the
392ccd979bdSMark Fasheh * other blocks in a group - All valid FS metadata in a block
393ccd979bdSMark Fasheh * group stores the superblock fs_generation value at
394ccd979bdSMark Fasheh * allocation time. */
395ccd979bdSMark Fasheh
396ccd979bdSMark Fasheh bail:
397c1e8d35eSTao Ma if (status)
398c1e8d35eSTao Ma mlog_errno(status);
399ccd979bdSMark Fasheh return status;
400ccd979bdSMark Fasheh }
401ccd979bdSMark Fasheh
ocfs2_find_smallest_chain(struct ocfs2_chain_list * cl)402ccd979bdSMark Fasheh static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
403ccd979bdSMark Fasheh {
404ccd979bdSMark Fasheh u16 curr, best;
405ccd979bdSMark Fasheh
406ccd979bdSMark Fasheh best = curr = 0;
407ccd979bdSMark Fasheh while (curr < le16_to_cpu(cl->cl_count)) {
408ccd979bdSMark Fasheh if (le32_to_cpu(cl->cl_recs[best].c_total) >
409ccd979bdSMark Fasheh le32_to_cpu(cl->cl_recs[curr].c_total))
410ccd979bdSMark Fasheh best = curr;
411ccd979bdSMark Fasheh curr++;
412ccd979bdSMark Fasheh }
413ccd979bdSMark Fasheh return best;
414ccd979bdSMark Fasheh }
415ccd979bdSMark Fasheh
416798db35fSJoel Becker static struct buffer_head *
ocfs2_block_group_alloc_contig(struct ocfs2_super * osb,handle_t * handle,struct inode * alloc_inode,struct ocfs2_alloc_context * ac,struct ocfs2_chain_list * cl)417798db35fSJoel Becker ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle,
418798db35fSJoel Becker struct inode *alloc_inode,
419798db35fSJoel Becker struct ocfs2_alloc_context *ac,
420798db35fSJoel Becker struct ocfs2_chain_list *cl)
421798db35fSJoel Becker {
422798db35fSJoel Becker int status;
423798db35fSJoel Becker u32 bit_off, num_bits;
424798db35fSJoel Becker u64 bg_blkno;
425798db35fSJoel Becker struct buffer_head *bg_bh;
426798db35fSJoel Becker unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
427798db35fSJoel Becker
4281ed9b777SJoel Becker status = ocfs2_claim_clusters(handle, ac,
429798db35fSJoel Becker le16_to_cpu(cl->cl_cpg), &bit_off,
430798db35fSJoel Becker &num_bits);
431798db35fSJoel Becker if (status < 0) {
432798db35fSJoel Becker if (status != -ENOSPC)
433798db35fSJoel Becker mlog_errno(status);
434798db35fSJoel Becker goto bail;
435798db35fSJoel Becker }
436798db35fSJoel Becker
437798db35fSJoel Becker /* setup the group */
438798db35fSJoel Becker bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4392f73e135STao Ma trace_ocfs2_block_group_alloc_contig(
4402f73e135STao Ma (unsigned long long)bg_blkno, alloc_rec);
441798db35fSJoel Becker
442798db35fSJoel Becker bg_bh = sb_getblk(osb->sb, bg_blkno);
443798db35fSJoel Becker if (!bg_bh) {
4447391a294SRui Xiang status = -ENOMEM;
445798db35fSJoel Becker mlog_errno(status);
446798db35fSJoel Becker goto bail;
447798db35fSJoel Becker }
448798db35fSJoel Becker ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
449798db35fSJoel Becker
450798db35fSJoel Becker status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
451798db35fSJoel Becker bg_blkno, num_bits, alloc_rec, cl);
452798db35fSJoel Becker if (status < 0) {
453798db35fSJoel Becker brelse(bg_bh);
454798db35fSJoel Becker mlog_errno(status);
455798db35fSJoel Becker }
456798db35fSJoel Becker
457798db35fSJoel Becker bail:
458798db35fSJoel Becker return status ? ERR_PTR(status) : bg_bh;
459798db35fSJoel Becker }
460798db35fSJoel Becker
ocfs2_block_group_claim_bits(struct ocfs2_super * osb,handle_t * handle,struct ocfs2_alloc_context * ac,unsigned int min_bits,u32 * bit_off,u32 * num_bits)461798db35fSJoel Becker static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb,
462798db35fSJoel Becker handle_t *handle,
463798db35fSJoel Becker struct ocfs2_alloc_context *ac,
464798db35fSJoel Becker unsigned int min_bits,
465798db35fSJoel Becker u32 *bit_off, u32 *num_bits)
466798db35fSJoel Becker {
46718d3a98fSJoel Becker int status = 0;
468798db35fSJoel Becker
469798db35fSJoel Becker while (min_bits) {
4701ed9b777SJoel Becker status = ocfs2_claim_clusters(handle, ac, min_bits,
471798db35fSJoel Becker bit_off, num_bits);
472798db35fSJoel Becker if (status != -ENOSPC)
473798db35fSJoel Becker break;
474798db35fSJoel Becker
475798db35fSJoel Becker min_bits >>= 1;
476798db35fSJoel Becker }
477798db35fSJoel Becker
478798db35fSJoel Becker return status;
479798db35fSJoel Becker }
480798db35fSJoel Becker
ocfs2_block_group_grow_discontig(handle_t * handle,struct inode * alloc_inode,struct buffer_head * bg_bh,struct ocfs2_alloc_context * ac,struct ocfs2_chain_list * cl,unsigned int min_bits)481798db35fSJoel Becker static int ocfs2_block_group_grow_discontig(handle_t *handle,
482798db35fSJoel Becker struct inode *alloc_inode,
483798db35fSJoel Becker struct buffer_head *bg_bh,
484798db35fSJoel Becker struct ocfs2_alloc_context *ac,
485798db35fSJoel Becker struct ocfs2_chain_list *cl,
486798db35fSJoel Becker unsigned int min_bits)
487798db35fSJoel Becker {
488798db35fSJoel Becker int status;
489798db35fSJoel Becker struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
490798db35fSJoel Becker struct ocfs2_group_desc *bg =
491798db35fSJoel Becker (struct ocfs2_group_desc *)bg_bh->b_data;
4924711954eSTao Ma unsigned int needed = le16_to_cpu(cl->cl_cpg) -
4934711954eSTao Ma le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
494798db35fSJoel Becker u32 p_cpos, clusters;
495798db35fSJoel Becker u64 p_blkno;
496798db35fSJoel Becker struct ocfs2_extent_list *el = &bg->bg_list;
497798db35fSJoel Becker
498798db35fSJoel Becker status = ocfs2_journal_access_gd(handle,
499798db35fSJoel Becker INODE_CACHE(alloc_inode),
500798db35fSJoel Becker bg_bh,
501798db35fSJoel Becker OCFS2_JOURNAL_ACCESS_CREATE);
502798db35fSJoel Becker if (status < 0) {
503798db35fSJoel Becker mlog_errno(status);
504798db35fSJoel Becker goto bail;
505798db35fSJoel Becker }
506798db35fSJoel Becker
507798db35fSJoel Becker while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) <
508798db35fSJoel Becker le16_to_cpu(el->l_count))) {
509798db35fSJoel Becker if (min_bits > needed)
510798db35fSJoel Becker min_bits = needed;
511798db35fSJoel Becker status = ocfs2_block_group_claim_bits(osb, handle, ac,
512798db35fSJoel Becker min_bits, &p_cpos,
513798db35fSJoel Becker &clusters);
514798db35fSJoel Becker if (status < 0) {
515798db35fSJoel Becker if (status != -ENOSPC)
516798db35fSJoel Becker mlog_errno(status);
517798db35fSJoel Becker goto bail;
518798db35fSJoel Becker }
519798db35fSJoel Becker p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos);
520798db35fSJoel Becker ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno,
521798db35fSJoel Becker clusters);
522798db35fSJoel Becker
523798db35fSJoel Becker min_bits = clusters;
5244711954eSTao Ma needed = le16_to_cpu(cl->cl_cpg) -
5254711954eSTao Ma le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
526798db35fSJoel Becker }
527798db35fSJoel Becker
528798db35fSJoel Becker if (needed > 0) {
5294711954eSTao Ma /*
5304711954eSTao Ma * We have used up all the extent rec but can't fill up
5314711954eSTao Ma * the cpg. So bail out.
5324711954eSTao Ma */
5334711954eSTao Ma status = -ENOSPC;
5344711954eSTao Ma goto bail;
535798db35fSJoel Becker }
536798db35fSJoel Becker
537798db35fSJoel Becker ocfs2_journal_dirty(handle, bg_bh);
538798db35fSJoel Becker
539798db35fSJoel Becker bail:
540798db35fSJoel Becker return status;
541798db35fSJoel Becker }
542798db35fSJoel Becker
ocfs2_bg_alloc_cleanup(handle_t * handle,struct ocfs2_alloc_context * cluster_ac,struct inode * alloc_inode,struct buffer_head * bg_bh)5438b06bc59SJoel Becker static void ocfs2_bg_alloc_cleanup(handle_t *handle,
5448b06bc59SJoel Becker struct ocfs2_alloc_context *cluster_ac,
5458b06bc59SJoel Becker struct inode *alloc_inode,
5468b06bc59SJoel Becker struct buffer_head *bg_bh)
547798db35fSJoel Becker {
5488b06bc59SJoel Becker int i, ret;
549798db35fSJoel Becker struct ocfs2_group_desc *bg;
550798db35fSJoel Becker struct ocfs2_extent_list *el;
551798db35fSJoel Becker struct ocfs2_extent_rec *rec;
552798db35fSJoel Becker
553798db35fSJoel Becker if (!bg_bh)
554798db35fSJoel Becker return;
555798db35fSJoel Becker
556798db35fSJoel Becker bg = (struct ocfs2_group_desc *)bg_bh->b_data;
557798db35fSJoel Becker el = &bg->bg_list;
558798db35fSJoel Becker for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
559798db35fSJoel Becker rec = &el->l_recs[i];
5608b06bc59SJoel Becker ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode,
5618b06bc59SJoel Becker cluster_ac->ac_bh,
562798db35fSJoel Becker le64_to_cpu(rec->e_blkno),
56372094e43SAl Viro le16_to_cpu(rec->e_leaf_clusters));
5648b06bc59SJoel Becker if (ret)
5658b06bc59SJoel Becker mlog_errno(ret);
5668b06bc59SJoel Becker /* Try all the clusters to free */
567798db35fSJoel Becker }
568798db35fSJoel Becker
569798db35fSJoel Becker ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh);
570798db35fSJoel Becker brelse(bg_bh);
571798db35fSJoel Becker }
572798db35fSJoel Becker
573798db35fSJoel Becker static struct buffer_head *
ocfs2_block_group_alloc_discontig(handle_t * handle,struct inode * alloc_inode,struct ocfs2_alloc_context * ac,struct ocfs2_chain_list * cl)574798db35fSJoel Becker ocfs2_block_group_alloc_discontig(handle_t *handle,
575798db35fSJoel Becker struct inode *alloc_inode,
576798db35fSJoel Becker struct ocfs2_alloc_context *ac,
5778b06bc59SJoel Becker struct ocfs2_chain_list *cl)
578798db35fSJoel Becker {
579798db35fSJoel Becker int status;
580798db35fSJoel Becker u32 bit_off, num_bits;
581798db35fSJoel Becker u64 bg_blkno;
582798db35fSJoel Becker unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1;
583798db35fSJoel Becker struct buffer_head *bg_bh = NULL;
584798db35fSJoel Becker unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
585798db35fSJoel Becker struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
586798db35fSJoel Becker
5874711954eSTao Ma if (!ocfs2_supports_discontig_bg(osb)) {
588798db35fSJoel Becker status = -ENOSPC;
589798db35fSJoel Becker goto bail;
590798db35fSJoel Becker }
591798db35fSJoel Becker
5928b06bc59SJoel Becker status = ocfs2_extend_trans(handle,
5938b06bc59SJoel Becker ocfs2_calc_bg_discontig_credits(osb->sb));
5948b06bc59SJoel Becker if (status) {
5958b06bc59SJoel Becker mlog_errno(status);
5968b06bc59SJoel Becker goto bail;
5978b06bc59SJoel Becker }
5988b06bc59SJoel Becker
59995ec0adfSJoel Becker /*
60095ec0adfSJoel Becker * We're going to be grabbing from multiple cluster groups.
60195ec0adfSJoel Becker * We don't have enough credits to relink them all, and the
60295ec0adfSJoel Becker * cluster groups will be staying in cache for the duration of
60395ec0adfSJoel Becker * this operation.
60495ec0adfSJoel Becker */
605309a85b6SXiaowei.Hu ac->ac_disable_chain_relink = 1;
60695ec0adfSJoel Becker
607798db35fSJoel Becker /* Claim the first region */
608798db35fSJoel Becker status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits,
609798db35fSJoel Becker &bit_off, &num_bits);
610798db35fSJoel Becker if (status < 0) {
611798db35fSJoel Becker if (status != -ENOSPC)
612798db35fSJoel Becker mlog_errno(status);
613798db35fSJoel Becker goto bail;
614798db35fSJoel Becker }
615798db35fSJoel Becker min_bits = num_bits;
616798db35fSJoel Becker
617798db35fSJoel Becker /* setup the group */
618798db35fSJoel Becker bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
6192f73e135STao Ma trace_ocfs2_block_group_alloc_discontig(
6202f73e135STao Ma (unsigned long long)bg_blkno, alloc_rec);
621798db35fSJoel Becker
622798db35fSJoel Becker bg_bh = sb_getblk(osb->sb, bg_blkno);
623798db35fSJoel Becker if (!bg_bh) {
6247391a294SRui Xiang status = -ENOMEM;
625798db35fSJoel Becker mlog_errno(status);
626798db35fSJoel Becker goto bail;
627798db35fSJoel Becker }
628798db35fSJoel Becker ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
629798db35fSJoel Becker
630798db35fSJoel Becker status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
631798db35fSJoel Becker bg_blkno, num_bits, alloc_rec, cl);
632798db35fSJoel Becker if (status < 0) {
633798db35fSJoel Becker mlog_errno(status);
634798db35fSJoel Becker goto bail;
635798db35fSJoel Becker }
636798db35fSJoel Becker
637798db35fSJoel Becker status = ocfs2_block_group_grow_discontig(handle, alloc_inode,
638798db35fSJoel Becker bg_bh, ac, cl, min_bits);
639798db35fSJoel Becker if (status)
640798db35fSJoel Becker mlog_errno(status);
641798db35fSJoel Becker
642798db35fSJoel Becker bail:
643798db35fSJoel Becker if (status)
6448b06bc59SJoel Becker ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh);
645798db35fSJoel Becker return status ? ERR_PTR(status) : bg_bh;
646798db35fSJoel Becker }
647798db35fSJoel Becker
648ccd979bdSMark Fasheh /*
649ccd979bdSMark Fasheh * We expect the block group allocator to already be locked.
650ccd979bdSMark Fasheh */
ocfs2_block_group_alloc(struct ocfs2_super * osb,struct inode * alloc_inode,struct buffer_head * bh,u64 max_block,u64 * last_alloc_group,int flags)651ccd979bdSMark Fasheh static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
652ccd979bdSMark Fasheh struct inode *alloc_inode,
6531187c968SJoel Becker struct buffer_head *bh,
65460ca81e8STao Ma u64 max_block,
655feb473a6STao Ma u64 *last_alloc_group,
65660ca81e8STao Ma int flags)
657ccd979bdSMark Fasheh {
658ccd979bdSMark Fasheh int status, credits;
659ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
660ccd979bdSMark Fasheh struct ocfs2_chain_list *cl;
661ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac = NULL;
6621fabe148SMark Fasheh handle_t *handle = NULL;
6634711954eSTao Ma u16 alloc_rec;
664ccd979bdSMark Fasheh struct buffer_head *bg_bh = NULL;
665ccd979bdSMark Fasheh struct ocfs2_group_desc *bg;
666ccd979bdSMark Fasheh
667ccd979bdSMark Fasheh BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
668ccd979bdSMark Fasheh
669ccd979bdSMark Fasheh cl = &fe->id2.i_chain;
6701187c968SJoel Becker status = ocfs2_reserve_clusters_with_limit(osb,
671ccd979bdSMark Fasheh le16_to_cpu(cl->cl_cpg),
67260ca81e8STao Ma max_block, flags, &ac);
673ccd979bdSMark Fasheh if (status < 0) {
674ccd979bdSMark Fasheh if (status != -ENOSPC)
675ccd979bdSMark Fasheh mlog_errno(status);
676ccd979bdSMark Fasheh goto bail;
677ccd979bdSMark Fasheh }
678ccd979bdSMark Fasheh
679ccd979bdSMark Fasheh credits = ocfs2_calc_group_alloc_credits(osb->sb,
680ccd979bdSMark Fasheh le16_to_cpu(cl->cl_cpg));
68165eff9ccSMark Fasheh handle = ocfs2_start_trans(osb, credits);
682ccd979bdSMark Fasheh if (IS_ERR(handle)) {
683ccd979bdSMark Fasheh status = PTR_ERR(handle);
684ccd979bdSMark Fasheh handle = NULL;
685ccd979bdSMark Fasheh mlog_errno(status);
686ccd979bdSMark Fasheh goto bail;
687ccd979bdSMark Fasheh }
688ccd979bdSMark Fasheh
689feb473a6STao Ma if (last_alloc_group && *last_alloc_group != 0) {
6902f73e135STao Ma trace_ocfs2_block_group_alloc(
691feb473a6STao Ma (unsigned long long)*last_alloc_group);
692feb473a6STao Ma ac->ac_last_group = *last_alloc_group;
693feb473a6STao Ma }
694798db35fSJoel Becker
695798db35fSJoel Becker bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode,
696798db35fSJoel Becker ac, cl);
69745586c70SMasahiro Yamada if (PTR_ERR(bg_bh) == -ENOSPC)
698798db35fSJoel Becker bg_bh = ocfs2_block_group_alloc_discontig(handle,
699798db35fSJoel Becker alloc_inode,
7008b06bc59SJoel Becker ac, cl);
701798db35fSJoel Becker if (IS_ERR(bg_bh)) {
702798db35fSJoel Becker status = PTR_ERR(bg_bh);
703798db35fSJoel Becker bg_bh = NULL;
704ccd979bdSMark Fasheh if (status != -ENOSPC)
705ccd979bdSMark Fasheh mlog_errno(status);
706ccd979bdSMark Fasheh goto bail;
707ccd979bdSMark Fasheh }
708ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) bg_bh->b_data;
709ccd979bdSMark Fasheh
7100cf2f763SJoel Becker status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
711ccd979bdSMark Fasheh bh, OCFS2_JOURNAL_ACCESS_WRITE);
712ccd979bdSMark Fasheh if (status < 0) {
713ccd979bdSMark Fasheh mlog_errno(status);
714ccd979bdSMark Fasheh goto bail;
715ccd979bdSMark Fasheh }
716ccd979bdSMark Fasheh
7174711954eSTao Ma alloc_rec = le16_to_cpu(bg->bg_chain);
7184711954eSTao Ma le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
719ccd979bdSMark Fasheh le16_to_cpu(bg->bg_free_bits_count));
7204711954eSTao Ma le32_add_cpu(&cl->cl_recs[alloc_rec].c_total,
721798db35fSJoel Becker le16_to_cpu(bg->bg_bits));
7220a463b74STao Ma cl->cl_recs[alloc_rec].c_blkno = bg->bg_blkno;
723ccd979bdSMark Fasheh if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
724ccd979bdSMark Fasheh le16_add_cpu(&cl->cl_next_free_rec, 1);
725ccd979bdSMark Fasheh
726ccd979bdSMark Fasheh le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
727ccd979bdSMark Fasheh le16_to_cpu(bg->bg_free_bits_count));
728ccd979bdSMark Fasheh le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
729ccd979bdSMark Fasheh le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
730ccd979bdSMark Fasheh
731ec20cec7SJoel Becker ocfs2_journal_dirty(handle, bh);
732ccd979bdSMark Fasheh
733ccd979bdSMark Fasheh spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
734ccd979bdSMark Fasheh OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
735ccd979bdSMark Fasheh fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
736ccd979bdSMark Fasheh le32_to_cpu(fe->i_clusters)));
737ccd979bdSMark Fasheh spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
738ccd979bdSMark Fasheh i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
7398110b073SMark Fasheh alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
7406fdb702dSDarrick J. Wong ocfs2_update_inode_fsync_trans(handle, alloc_inode, 0);
741ccd979bdSMark Fasheh
742ccd979bdSMark Fasheh status = 0;
743feb473a6STao Ma
744feb473a6STao Ma /* save the new last alloc group so that the caller can cache it. */
745feb473a6STao Ma if (last_alloc_group)
746feb473a6STao Ma *last_alloc_group = ac->ac_last_group;
747feb473a6STao Ma
748ccd979bdSMark Fasheh bail:
749ccd979bdSMark Fasheh if (handle)
75002dc1af4SMark Fasheh ocfs2_commit_trans(osb, handle);
751ccd979bdSMark Fasheh
752ccd979bdSMark Fasheh if (ac)
753ccd979bdSMark Fasheh ocfs2_free_alloc_context(ac);
754ccd979bdSMark Fasheh
755ccd979bdSMark Fasheh brelse(bg_bh);
756ccd979bdSMark Fasheh
757c1e8d35eSTao Ma if (status)
758c1e8d35eSTao Ma mlog_errno(status);
759ccd979bdSMark Fasheh return status;
760ccd979bdSMark Fasheh }
761ccd979bdSMark Fasheh
ocfs2_reserve_suballoc_bits(struct ocfs2_super * osb,struct ocfs2_alloc_context * ac,int type,u32 slot,u64 * last_alloc_group,int flags)762ccd979bdSMark Fasheh static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
763da5cbf2fSMark Fasheh struct ocfs2_alloc_context *ac,
764da5cbf2fSMark Fasheh int type,
765ffda89a3STao Ma u32 slot,
766feb473a6STao Ma u64 *last_alloc_group,
76760ca81e8STao Ma int flags)
768ccd979bdSMark Fasheh {
769ccd979bdSMark Fasheh int status;
770ccd979bdSMark Fasheh u32 bits_wanted = ac->ac_bits_wanted;
771da5cbf2fSMark Fasheh struct inode *alloc_inode;
772ccd979bdSMark Fasheh struct buffer_head *bh = NULL;
773ccd979bdSMark Fasheh struct ocfs2_dinode *fe;
774ccd979bdSMark Fasheh u32 free_bits;
775ccd979bdSMark Fasheh
776da5cbf2fSMark Fasheh alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
777da5cbf2fSMark Fasheh if (!alloc_inode) {
778da5cbf2fSMark Fasheh mlog_errno(-EINVAL);
779da5cbf2fSMark Fasheh return -EINVAL;
780ccd979bdSMark Fasheh }
781ccd979bdSMark Fasheh
7825955102cSAl Viro inode_lock(alloc_inode);
783da5cbf2fSMark Fasheh
784e63aecb6SMark Fasheh status = ocfs2_inode_lock(alloc_inode, &bh, 1);
785da5cbf2fSMark Fasheh if (status < 0) {
7865955102cSAl Viro inode_unlock(alloc_inode);
787da5cbf2fSMark Fasheh iput(alloc_inode);
788da5cbf2fSMark Fasheh
789da5cbf2fSMark Fasheh mlog_errno(status);
790da5cbf2fSMark Fasheh return status;
791da5cbf2fSMark Fasheh }
792da5cbf2fSMark Fasheh
793da5cbf2fSMark Fasheh ac->ac_inode = alloc_inode;
794a4a48911STao Ma ac->ac_alloc_slot = slot;
795da5cbf2fSMark Fasheh
796ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) bh->b_data;
79710995aa2SJoel Becker
79810995aa2SJoel Becker /* The bh was validated by the inode read inside
79910995aa2SJoel Becker * ocfs2_inode_lock(). Any corruption is a code bug. */
80010995aa2SJoel Becker BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
80110995aa2SJoel Becker
802ccd979bdSMark Fasheh if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
8037ecef14aSJoe Perches status = ocfs2_error(alloc_inode->i_sb,
8047ecef14aSJoe Perches "Invalid chain allocator %llu\n",
805b0697053SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno));
806ccd979bdSMark Fasheh goto bail;
807ccd979bdSMark Fasheh }
808ccd979bdSMark Fasheh
809ccd979bdSMark Fasheh free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
810ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_used);
811ccd979bdSMark Fasheh
812ccd979bdSMark Fasheh if (bits_wanted > free_bits) {
813ccd979bdSMark Fasheh /* cluster bitmap never grows */
814ccd979bdSMark Fasheh if (ocfs2_is_cluster_bitmap(alloc_inode)) {
8152f73e135STao Ma trace_ocfs2_reserve_suballoc_bits_nospc(bits_wanted,
8162f73e135STao Ma free_bits);
817ccd979bdSMark Fasheh status = -ENOSPC;
818ccd979bdSMark Fasheh goto bail;
819ccd979bdSMark Fasheh }
820ccd979bdSMark Fasheh
82160ca81e8STao Ma if (!(flags & ALLOC_NEW_GROUP)) {
8222f73e135STao Ma trace_ocfs2_reserve_suballoc_bits_no_new_group(
823ffda89a3STao Ma slot, bits_wanted, free_bits);
824ffda89a3STao Ma status = -ENOSPC;
825ffda89a3STao Ma goto bail;
826ffda89a3STao Ma }
827ffda89a3STao Ma
8281187c968SJoel Becker status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
829feb473a6STao Ma ac->ac_max_block,
830feb473a6STao Ma last_alloc_group, flags);
831ccd979bdSMark Fasheh if (status < 0) {
832ccd979bdSMark Fasheh if (status != -ENOSPC)
833ccd979bdSMark Fasheh mlog_errno(status);
834ccd979bdSMark Fasheh goto bail;
835ccd979bdSMark Fasheh }
836ccd979bdSMark Fasheh atomic_inc(&osb->alloc_stats.bg_extends);
837ccd979bdSMark Fasheh
838ccd979bdSMark Fasheh /* You should never ask for this much metadata */
839ccd979bdSMark Fasheh BUG_ON(bits_wanted >
840ccd979bdSMark Fasheh (le32_to_cpu(fe->id1.bitmap1.i_total)
841ccd979bdSMark Fasheh - le32_to_cpu(fe->id1.bitmap1.i_used)));
842ccd979bdSMark Fasheh }
843ccd979bdSMark Fasheh
844ccd979bdSMark Fasheh get_bh(bh);
845ccd979bdSMark Fasheh ac->ac_bh = bh;
846ccd979bdSMark Fasheh bail:
847ccd979bdSMark Fasheh brelse(bh);
848ccd979bdSMark Fasheh
849c1e8d35eSTao Ma if (status)
850c1e8d35eSTao Ma mlog_errno(status);
851ccd979bdSMark Fasheh return status;
852ccd979bdSMark Fasheh }
853ccd979bdSMark Fasheh
ocfs2_init_inode_steal_slot(struct ocfs2_super * osb)854b89c5428STiger Yang static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
855b89c5428STiger Yang {
856b89c5428STiger Yang spin_lock(&osb->osb_lock);
857b89c5428STiger Yang osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
858b89c5428STiger Yang spin_unlock(&osb->osb_lock);
859b89c5428STiger Yang atomic_set(&osb->s_num_inodes_stolen, 0);
860b89c5428STiger Yang }
861b89c5428STiger Yang
ocfs2_init_meta_steal_slot(struct ocfs2_super * osb)862b89c5428STiger Yang static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
863b89c5428STiger Yang {
864b89c5428STiger Yang spin_lock(&osb->osb_lock);
865b89c5428STiger Yang osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
866b89c5428STiger Yang spin_unlock(&osb->osb_lock);
867b89c5428STiger Yang atomic_set(&osb->s_num_meta_stolen, 0);
868b89c5428STiger Yang }
869b89c5428STiger Yang
ocfs2_init_steal_slots(struct ocfs2_super * osb)870b89c5428STiger Yang void ocfs2_init_steal_slots(struct ocfs2_super *osb)
871b89c5428STiger Yang {
872b89c5428STiger Yang ocfs2_init_inode_steal_slot(osb);
873b89c5428STiger Yang ocfs2_init_meta_steal_slot(osb);
874b89c5428STiger Yang }
875b89c5428STiger Yang
__ocfs2_set_steal_slot(struct ocfs2_super * osb,int slot,int type)876b89c5428STiger Yang static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
877b89c5428STiger Yang {
878b89c5428STiger Yang spin_lock(&osb->osb_lock);
879b89c5428STiger Yang if (type == INODE_ALLOC_SYSTEM_INODE)
88038d51b2dSJunxiao Bi osb->s_inode_steal_slot = (u16)slot;
881b89c5428STiger Yang else if (type == EXTENT_ALLOC_SYSTEM_INODE)
88238d51b2dSJunxiao Bi osb->s_meta_steal_slot = (u16)slot;
883b89c5428STiger Yang spin_unlock(&osb->osb_lock);
884b89c5428STiger Yang }
885b89c5428STiger Yang
__ocfs2_get_steal_slot(struct ocfs2_super * osb,int type)886b89c5428STiger Yang static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
887b89c5428STiger Yang {
888b89c5428STiger Yang int slot = OCFS2_INVALID_SLOT;
889b89c5428STiger Yang
890b89c5428STiger Yang spin_lock(&osb->osb_lock);
891b89c5428STiger Yang if (type == INODE_ALLOC_SYSTEM_INODE)
892b89c5428STiger Yang slot = osb->s_inode_steal_slot;
893b89c5428STiger Yang else if (type == EXTENT_ALLOC_SYSTEM_INODE)
894b89c5428STiger Yang slot = osb->s_meta_steal_slot;
895b89c5428STiger Yang spin_unlock(&osb->osb_lock);
896b89c5428STiger Yang
897b89c5428STiger Yang return slot;
898b89c5428STiger Yang }
899b89c5428STiger Yang
ocfs2_get_inode_steal_slot(struct ocfs2_super * osb)900b89c5428STiger Yang static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
901b89c5428STiger Yang {
902b89c5428STiger Yang return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
903b89c5428STiger Yang }
904b89c5428STiger Yang
ocfs2_get_meta_steal_slot(struct ocfs2_super * osb)905b89c5428STiger Yang static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
906b89c5428STiger Yang {
907b89c5428STiger Yang return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
908b89c5428STiger Yang }
909b89c5428STiger Yang
ocfs2_steal_resource(struct ocfs2_super * osb,struct ocfs2_alloc_context * ac,int type)910b89c5428STiger Yang static int ocfs2_steal_resource(struct ocfs2_super *osb,
911b89c5428STiger Yang struct ocfs2_alloc_context *ac,
912b89c5428STiger Yang int type)
913b89c5428STiger Yang {
914b89c5428STiger Yang int i, status = -ENOSPC;
915b89c5428STiger Yang int slot = __ocfs2_get_steal_slot(osb, type);
916b89c5428STiger Yang
917b89c5428STiger Yang /* Start to steal resource from the first slot after ours. */
918b89c5428STiger Yang if (slot == OCFS2_INVALID_SLOT)
919b89c5428STiger Yang slot = osb->slot_num + 1;
920b89c5428STiger Yang
921b89c5428STiger Yang for (i = 0; i < osb->max_slots; i++, slot++) {
922b89c5428STiger Yang if (slot == osb->max_slots)
923b89c5428STiger Yang slot = 0;
924b89c5428STiger Yang
925b89c5428STiger Yang if (slot == osb->slot_num)
926b89c5428STiger Yang continue;
927b89c5428STiger Yang
928b89c5428STiger Yang status = ocfs2_reserve_suballoc_bits(osb, ac,
929b89c5428STiger Yang type,
930b89c5428STiger Yang (u32)slot, NULL,
931b89c5428STiger Yang NOT_ALLOC_NEW_GROUP);
932b89c5428STiger Yang if (status >= 0) {
933b89c5428STiger Yang __ocfs2_set_steal_slot(osb, slot, type);
934b89c5428STiger Yang break;
935b89c5428STiger Yang }
936b89c5428STiger Yang
937b89c5428STiger Yang ocfs2_free_ac_resource(ac);
938b89c5428STiger Yang }
939b89c5428STiger Yang
940b89c5428STiger Yang return status;
941b89c5428STiger Yang }
942b89c5428STiger Yang
ocfs2_steal_inode(struct ocfs2_super * osb,struct ocfs2_alloc_context * ac)943b89c5428STiger Yang static int ocfs2_steal_inode(struct ocfs2_super *osb,
944b89c5428STiger Yang struct ocfs2_alloc_context *ac)
945b89c5428STiger Yang {
946b89c5428STiger Yang return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
947b89c5428STiger Yang }
948b89c5428STiger Yang
ocfs2_steal_meta(struct ocfs2_super * osb,struct ocfs2_alloc_context * ac)949b89c5428STiger Yang static int ocfs2_steal_meta(struct ocfs2_super *osb,
950b89c5428STiger Yang struct ocfs2_alloc_context *ac)
951b89c5428STiger Yang {
952b89c5428STiger Yang return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
953b89c5428STiger Yang }
954b89c5428STiger Yang
ocfs2_reserve_new_metadata_blocks(struct ocfs2_super * osb,int blocks,struct ocfs2_alloc_context ** ac)955cf1d6c76STiger Yang int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
956cf1d6c76STiger Yang int blocks,
957ccd979bdSMark Fasheh struct ocfs2_alloc_context **ac)
958ccd979bdSMark Fasheh {
959ccd979bdSMark Fasheh int status;
960b89c5428STiger Yang int slot = ocfs2_get_meta_steal_slot(osb);
961ccd979bdSMark Fasheh
962cd861280SRobert P. J. Day *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
963ccd979bdSMark Fasheh if (!(*ac)) {
964ccd979bdSMark Fasheh status = -ENOMEM;
965ccd979bdSMark Fasheh mlog_errno(status);
966ccd979bdSMark Fasheh goto bail;
967ccd979bdSMark Fasheh }
968ccd979bdSMark Fasheh
969cf1d6c76STiger Yang (*ac)->ac_bits_wanted = blocks;
970ccd979bdSMark Fasheh (*ac)->ac_which = OCFS2_AC_USE_META;
971ccd979bdSMark Fasheh (*ac)->ac_group_search = ocfs2_block_group_search;
972ccd979bdSMark Fasheh
973b89c5428STiger Yang if (slot != OCFS2_INVALID_SLOT &&
974b89c5428STiger Yang atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
975b89c5428STiger Yang goto extent_steal;
976b89c5428STiger Yang
977b89c5428STiger Yang atomic_set(&osb->s_num_meta_stolen, 0);
978da5cbf2fSMark Fasheh status = ocfs2_reserve_suballoc_bits(osb, (*ac),
979ffda89a3STao Ma EXTENT_ALLOC_SYSTEM_INODE,
980b89c5428STiger Yang (u32)osb->slot_num, NULL,
98133d5d380SMark Fasheh ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP);
982b89c5428STiger Yang
983b89c5428STiger Yang
984b89c5428STiger Yang if (status >= 0) {
985b89c5428STiger Yang status = 0;
986b89c5428STiger Yang if (slot != OCFS2_INVALID_SLOT)
987b89c5428STiger Yang ocfs2_init_meta_steal_slot(osb);
988b89c5428STiger Yang goto bail;
989b89c5428STiger Yang } else if (status < 0 && status != -ENOSPC) {
990b89c5428STiger Yang mlog_errno(status);
991b89c5428STiger Yang goto bail;
992b89c5428STiger Yang }
993b89c5428STiger Yang
994b89c5428STiger Yang ocfs2_free_ac_resource(*ac);
995b89c5428STiger Yang
996b89c5428STiger Yang extent_steal:
997b89c5428STiger Yang status = ocfs2_steal_meta(osb, *ac);
998b89c5428STiger Yang atomic_inc(&osb->s_num_meta_stolen);
999ccd979bdSMark Fasheh if (status < 0) {
1000ccd979bdSMark Fasheh if (status != -ENOSPC)
1001ccd979bdSMark Fasheh mlog_errno(status);
1002ccd979bdSMark Fasheh goto bail;
1003ccd979bdSMark Fasheh }
1004ccd979bdSMark Fasheh
1005ccd979bdSMark Fasheh status = 0;
1006ccd979bdSMark Fasheh bail:
1007ccd979bdSMark Fasheh if ((status < 0) && *ac) {
1008ccd979bdSMark Fasheh ocfs2_free_alloc_context(*ac);
1009ccd979bdSMark Fasheh *ac = NULL;
1010ccd979bdSMark Fasheh }
1011ccd979bdSMark Fasheh
1012c1e8d35eSTao Ma if (status)
1013c1e8d35eSTao Ma mlog_errno(status);
1014ccd979bdSMark Fasheh return status;
1015ccd979bdSMark Fasheh }
1016ccd979bdSMark Fasheh
ocfs2_reserve_new_metadata(struct ocfs2_super * osb,struct ocfs2_extent_list * root_el,struct ocfs2_alloc_context ** ac)1017cf1d6c76STiger Yang int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
1018cf1d6c76STiger Yang struct ocfs2_extent_list *root_el,
1019cf1d6c76STiger Yang struct ocfs2_alloc_context **ac)
1020cf1d6c76STiger Yang {
1021cf1d6c76STiger Yang return ocfs2_reserve_new_metadata_blocks(osb,
1022cf1d6c76STiger Yang ocfs2_extend_meta_needed(root_el),
1023cf1d6c76STiger Yang ac);
1024cf1d6c76STiger Yang }
1025cf1d6c76STiger Yang
ocfs2_reserve_new_inode(struct ocfs2_super * osb,struct ocfs2_alloc_context ** ac)1026ccd979bdSMark Fasheh int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
1027ccd979bdSMark Fasheh struct ocfs2_alloc_context **ac)
1028ccd979bdSMark Fasheh {
1029ccd979bdSMark Fasheh int status;
1030b89c5428STiger Yang int slot = ocfs2_get_inode_steal_slot(osb);
1031feb473a6STao Ma u64 alloc_group;
1032ccd979bdSMark Fasheh
1033cd861280SRobert P. J. Day *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
1034ccd979bdSMark Fasheh if (!(*ac)) {
1035ccd979bdSMark Fasheh status = -ENOMEM;
1036ccd979bdSMark Fasheh mlog_errno(status);
1037ccd979bdSMark Fasheh goto bail;
1038ccd979bdSMark Fasheh }
1039ccd979bdSMark Fasheh
1040ccd979bdSMark Fasheh (*ac)->ac_bits_wanted = 1;
1041ccd979bdSMark Fasheh (*ac)->ac_which = OCFS2_AC_USE_INODE;
1042ccd979bdSMark Fasheh
1043ccd979bdSMark Fasheh (*ac)->ac_group_search = ocfs2_block_group_search;
1044ccd979bdSMark Fasheh
10454d0ddb2cSTao Ma /*
10461187c968SJoel Becker * stat(2) can't handle i_ino > 32bits, so we tell the
10471187c968SJoel Becker * lower levels not to allocate us a block group past that
104812462f1dSJoel Becker * limit. The 'inode64' mount option avoids this behavior.
10491187c968SJoel Becker */
105012462f1dSJoel Becker if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
10511187c968SJoel Becker (*ac)->ac_max_block = (u32)~0U;
10521187c968SJoel Becker
10531187c968SJoel Becker /*
10544d0ddb2cSTao Ma * slot is set when we successfully steal inode from other nodes.
10554d0ddb2cSTao Ma * It is reset in 3 places:
10564d0ddb2cSTao Ma * 1. when we flush the truncate log
10574d0ddb2cSTao Ma * 2. when we complete local alloc recovery.
10584d0ddb2cSTao Ma * 3. when we successfully allocate from our own slot.
10594d0ddb2cSTao Ma * After it is set, we will go on stealing inodes until we find the
10604d0ddb2cSTao Ma * need to check our slots to see whether there is some space for us.
10614d0ddb2cSTao Ma */
10624d0ddb2cSTao Ma if (slot != OCFS2_INVALID_SLOT &&
1063b89c5428STiger Yang atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL)
10644d0ddb2cSTao Ma goto inode_steal;
10654d0ddb2cSTao Ma
10664d0ddb2cSTao Ma atomic_set(&osb->s_num_inodes_stolen, 0);
1067feb473a6STao Ma alloc_group = osb->osb_inode_alloc_group;
1068da5cbf2fSMark Fasheh status = ocfs2_reserve_suballoc_bits(osb, *ac,
1069da5cbf2fSMark Fasheh INODE_ALLOC_SYSTEM_INODE,
1070b89c5428STiger Yang (u32)osb->slot_num,
1071feb473a6STao Ma &alloc_group,
107260ca81e8STao Ma ALLOC_NEW_GROUP |
107360ca81e8STao Ma ALLOC_GROUPS_FROM_GLOBAL);
10744d0ddb2cSTao Ma if (status >= 0) {
10754d0ddb2cSTao Ma status = 0;
10764d0ddb2cSTao Ma
1077feb473a6STao Ma spin_lock(&osb->osb_lock);
1078feb473a6STao Ma osb->osb_inode_alloc_group = alloc_group;
1079feb473a6STao Ma spin_unlock(&osb->osb_lock);
10802f73e135STao Ma trace_ocfs2_reserve_new_inode_new_group(
10812f73e135STao Ma (unsigned long long)alloc_group);
1082feb473a6STao Ma
10834d0ddb2cSTao Ma /*
10844d0ddb2cSTao Ma * Some inodes must be freed by us, so try to allocate
10854d0ddb2cSTao Ma * from our own next time.
10864d0ddb2cSTao Ma */
10874d0ddb2cSTao Ma if (slot != OCFS2_INVALID_SLOT)
10884d0ddb2cSTao Ma ocfs2_init_inode_steal_slot(osb);
10894d0ddb2cSTao Ma goto bail;
10904d0ddb2cSTao Ma } else if (status < 0 && status != -ENOSPC) {
10914d0ddb2cSTao Ma mlog_errno(status);
10924d0ddb2cSTao Ma goto bail;
10934d0ddb2cSTao Ma }
10944d0ddb2cSTao Ma
10954d0ddb2cSTao Ma ocfs2_free_ac_resource(*ac);
10964d0ddb2cSTao Ma
10974d0ddb2cSTao Ma inode_steal:
1098b89c5428STiger Yang status = ocfs2_steal_inode(osb, *ac);
10994d0ddb2cSTao Ma atomic_inc(&osb->s_num_inodes_stolen);
1100ccd979bdSMark Fasheh if (status < 0) {
1101ccd979bdSMark Fasheh if (status != -ENOSPC)
1102ccd979bdSMark Fasheh mlog_errno(status);
1103ccd979bdSMark Fasheh goto bail;
1104ccd979bdSMark Fasheh }
1105ccd979bdSMark Fasheh
1106ccd979bdSMark Fasheh status = 0;
1107ccd979bdSMark Fasheh bail:
1108ccd979bdSMark Fasheh if ((status < 0) && *ac) {
1109ccd979bdSMark Fasheh ocfs2_free_alloc_context(*ac);
1110ccd979bdSMark Fasheh *ac = NULL;
1111ccd979bdSMark Fasheh }
1112ccd979bdSMark Fasheh
1113c1e8d35eSTao Ma if (status)
1114c1e8d35eSTao Ma mlog_errno(status);
1115ccd979bdSMark Fasheh return status;
1116ccd979bdSMark Fasheh }
1117ccd979bdSMark Fasheh
1118ccd979bdSMark Fasheh /* local alloc code has to do the same thing, so rather than do this
1119ccd979bdSMark Fasheh * twice.. */
ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super * osb,struct ocfs2_alloc_context * ac)1120ccd979bdSMark Fasheh int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
1121ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac)
1122ccd979bdSMark Fasheh {
1123ccd979bdSMark Fasheh int status;
1124ccd979bdSMark Fasheh
1125ccd979bdSMark Fasheh ac->ac_which = OCFS2_AC_USE_MAIN;
1126ccd979bdSMark Fasheh ac->ac_group_search = ocfs2_cluster_group_search;
1127ccd979bdSMark Fasheh
1128da5cbf2fSMark Fasheh status = ocfs2_reserve_suballoc_bits(osb, ac,
1129da5cbf2fSMark Fasheh GLOBAL_BITMAP_SYSTEM_INODE,
1130feb473a6STao Ma OCFS2_INVALID_SLOT, NULL,
1131ffda89a3STao Ma ALLOC_NEW_GROUP);
113247ee9d89SGuozhonghua if (status < 0 && status != -ENOSPC)
1133ccd979bdSMark Fasheh mlog_errno(status);
1134da5cbf2fSMark Fasheh
1135ccd979bdSMark Fasheh return status;
1136ccd979bdSMark Fasheh }
1137ccd979bdSMark Fasheh
1138ccd979bdSMark Fasheh /* Callers don't need to care which bitmap (local alloc or main) to
1139ccd979bdSMark Fasheh * use so we figure it out for them, but unfortunately this clutters
1140ccd979bdSMark Fasheh * things a bit. */
ocfs2_reserve_clusters_with_limit(struct ocfs2_super * osb,u32 bits_wanted,u64 max_block,int flags,struct ocfs2_alloc_context ** ac)11411187c968SJoel Becker static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
11421187c968SJoel Becker u32 bits_wanted, u64 max_block,
114360ca81e8STao Ma int flags,
1144ccd979bdSMark Fasheh struct ocfs2_alloc_context **ac)
1145ccd979bdSMark Fasheh {
11462070ad1aSEric Ren int status, ret = 0;
11472070ad1aSEric Ren int retried = 0;
1148ccd979bdSMark Fasheh
1149cd861280SRobert P. J. Day *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
1150ccd979bdSMark Fasheh if (!(*ac)) {
1151ccd979bdSMark Fasheh status = -ENOMEM;
1152ccd979bdSMark Fasheh mlog_errno(status);
1153ccd979bdSMark Fasheh goto bail;
1154ccd979bdSMark Fasheh }
1155ccd979bdSMark Fasheh
1156ccd979bdSMark Fasheh (*ac)->ac_bits_wanted = bits_wanted;
11571187c968SJoel Becker (*ac)->ac_max_block = max_block;
1158ccd979bdSMark Fasheh
1159ccd979bdSMark Fasheh status = -ENOSPC;
116060ca81e8STao Ma if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
116160ca81e8STao Ma ocfs2_alloc_should_use_local(osb, bits_wanted)) {
1162ccd979bdSMark Fasheh status = ocfs2_reserve_local_alloc_bits(osb,
1163ccd979bdSMark Fasheh bits_wanted,
1164ccd979bdSMark Fasheh *ac);
1165a57c8fd2SMark Fasheh if ((status < 0) && (status != -ENOSPC)) {
1166ccd979bdSMark Fasheh mlog_errno(status);
1167ccd979bdSMark Fasheh goto bail;
1168ccd979bdSMark Fasheh }
1169ccd979bdSMark Fasheh }
1170ccd979bdSMark Fasheh
1171ccd979bdSMark Fasheh if (status == -ENOSPC) {
11722070ad1aSEric Ren retry:
1173ccd979bdSMark Fasheh status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
11742070ad1aSEric Ren /* Retry if there is sufficient space cached in truncate log */
11752070ad1aSEric Ren if (status == -ENOSPC && !retried) {
11762070ad1aSEric Ren retried = 1;
11772070ad1aSEric Ren ocfs2_inode_unlock((*ac)->ac_inode, 1);
11782070ad1aSEric Ren inode_unlock((*ac)->ac_inode);
11792070ad1aSEric Ren
11802070ad1aSEric Ren ret = ocfs2_try_to_free_truncate_log(osb, bits_wanted);
11813bb8b653SJoseph Qi if (ret == 1) {
11823bb8b653SJoseph Qi iput((*ac)->ac_inode);
11833bb8b653SJoseph Qi (*ac)->ac_inode = NULL;
11842070ad1aSEric Ren goto retry;
11853bb8b653SJoseph Qi }
11862070ad1aSEric Ren
11872070ad1aSEric Ren if (ret < 0)
11882070ad1aSEric Ren mlog_errno(ret);
11892070ad1aSEric Ren
11902070ad1aSEric Ren inode_lock((*ac)->ac_inode);
11913bb8b653SJoseph Qi ret = ocfs2_inode_lock((*ac)->ac_inode, NULL, 1);
11923bb8b653SJoseph Qi if (ret < 0) {
11933bb8b653SJoseph Qi mlog_errno(ret);
11943bb8b653SJoseph Qi inode_unlock((*ac)->ac_inode);
11953bb8b653SJoseph Qi iput((*ac)->ac_inode);
11963bb8b653SJoseph Qi (*ac)->ac_inode = NULL;
11973bb8b653SJoseph Qi goto bail;
11983bb8b653SJoseph Qi }
11992070ad1aSEric Ren }
1200ccd979bdSMark Fasheh if (status < 0) {
1201ccd979bdSMark Fasheh if (status != -ENOSPC)
1202ccd979bdSMark Fasheh mlog_errno(status);
1203ccd979bdSMark Fasheh goto bail;
1204ccd979bdSMark Fasheh }
1205ccd979bdSMark Fasheh }
1206ccd979bdSMark Fasheh
1207ccd979bdSMark Fasheh status = 0;
1208ccd979bdSMark Fasheh bail:
1209ccd979bdSMark Fasheh if ((status < 0) && *ac) {
1210ccd979bdSMark Fasheh ocfs2_free_alloc_context(*ac);
1211ccd979bdSMark Fasheh *ac = NULL;
1212ccd979bdSMark Fasheh }
1213ccd979bdSMark Fasheh
1214c1e8d35eSTao Ma if (status)
1215c1e8d35eSTao Ma mlog_errno(status);
1216ccd979bdSMark Fasheh return status;
1217ccd979bdSMark Fasheh }
1218ccd979bdSMark Fasheh
ocfs2_reserve_clusters(struct ocfs2_super * osb,u32 bits_wanted,struct ocfs2_alloc_context ** ac)12191187c968SJoel Becker int ocfs2_reserve_clusters(struct ocfs2_super *osb,
12201187c968SJoel Becker u32 bits_wanted,
12211187c968SJoel Becker struct ocfs2_alloc_context **ac)
12221187c968SJoel Becker {
122360ca81e8STao Ma return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
122460ca81e8STao Ma ALLOC_NEW_GROUP, ac);
12251187c968SJoel Becker }
12261187c968SJoel Becker
1227ccd979bdSMark Fasheh /*
1228ccd979bdSMark Fasheh * More or less lifted from ext3. I'll leave their description below:
1229ccd979bdSMark Fasheh *
1230ccd979bdSMark Fasheh * "For ext3 allocations, we must not reuse any blocks which are
1231ccd979bdSMark Fasheh * allocated in the bitmap buffer's "last committed data" copy. This
1232ccd979bdSMark Fasheh * prevents deletes from freeing up the page for reuse until we have
1233ccd979bdSMark Fasheh * committed the delete transaction.
1234ccd979bdSMark Fasheh *
1235ccd979bdSMark Fasheh * If we didn't do this, then deleting something and reallocating it as
1236ccd979bdSMark Fasheh * data would allow the old block to be overwritten before the
1237ccd979bdSMark Fasheh * transaction committed (because we force data to disk before commit).
1238ccd979bdSMark Fasheh * This would lead to corruption if we crashed between overwriting the
1239ccd979bdSMark Fasheh * data and committing the delete.
1240ccd979bdSMark Fasheh *
1241ccd979bdSMark Fasheh * @@@ We may want to make this allocation behaviour conditional on
1242ccd979bdSMark Fasheh * data-writes at some point, and disable it for metadata allocations or
1243ccd979bdSMark Fasheh * sync-data inodes."
1244ccd979bdSMark Fasheh *
1245ccd979bdSMark Fasheh * Note: OCFS2 already does this differently for metadata vs data
1246c78bad11SJoe Perches * allocations, as those bitmaps are separate and undo access is never
1247ccd979bdSMark Fasheh * called on a metadata group descriptor.
1248ccd979bdSMark Fasheh */
ocfs2_test_bg_bit_allocatable(struct buffer_head * bg_bh,int nr)1249ccd979bdSMark Fasheh static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
1250ccd979bdSMark Fasheh int nr)
1251ccd979bdSMark Fasheh {
1252ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
125346417064SThomas Gleixner struct journal_head *jh;
1254*ddf4b773SJoseph Qi int ret;
1255ccd979bdSMark Fasheh
1256ccd979bdSMark Fasheh if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
1257ccd979bdSMark Fasheh return 0;
125894e41ecfSSunil Mushran
1259*ddf4b773SJoseph Qi jh = jbd2_journal_grab_journal_head(bg_bh);
1260*ddf4b773SJoseph Qi if (!jh)
1261ccd979bdSMark Fasheh return 1;
1262ccd979bdSMark Fasheh
126346417064SThomas Gleixner spin_lock(&jh->b_state_lock);
126446417064SThomas Gleixner bg = (struct ocfs2_group_desc *) jh->b_committed_data;
126594e41ecfSSunil Mushran if (bg)
126694e41ecfSSunil Mushran ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
126794e41ecfSSunil Mushran else
126894e41ecfSSunil Mushran ret = 1;
126946417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
1270*ddf4b773SJoseph Qi jbd2_journal_put_journal_head(jh);
127194e41ecfSSunil Mushran
127294e41ecfSSunil Mushran return ret;
1273ccd979bdSMark Fasheh }
1274ccd979bdSMark Fasheh
ocfs2_block_group_find_clear_bits(struct ocfs2_super * osb,struct buffer_head * bg_bh,unsigned int bits_wanted,unsigned int total_bits,struct ocfs2_suballoc_result * res)1275ccd979bdSMark Fasheh static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
1276ccd979bdSMark Fasheh struct buffer_head *bg_bh,
1277ccd979bdSMark Fasheh unsigned int bits_wanted,
12787bf72edeSMark Fasheh unsigned int total_bits,
12797d1fe093SJoel Becker struct ocfs2_suballoc_result *res)
1280ccd979bdSMark Fasheh {
1281ccd979bdSMark Fasheh void *bitmap;
1282ccd979bdSMark Fasheh u16 best_offset, best_size;
1283ccd979bdSMark Fasheh int offset, start, found, status = 0;
1284ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1285ccd979bdSMark Fasheh
128642035306SJoel Becker /* Callers got this descriptor from
128742035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
128842035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1289ccd979bdSMark Fasheh
1290ccd979bdSMark Fasheh found = start = best_offset = best_size = 0;
1291ccd979bdSMark Fasheh bitmap = bg->bg_bitmap;
1292ccd979bdSMark Fasheh
12937bf72edeSMark Fasheh while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
12947bf72edeSMark Fasheh if (offset == total_bits)
1295ccd979bdSMark Fasheh break;
1296ccd979bdSMark Fasheh
1297ccd979bdSMark Fasheh if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
1298ccd979bdSMark Fasheh /* We found a zero, but we can't use it as it
1299ccd979bdSMark Fasheh * hasn't been put to disk yet! */
1300ccd979bdSMark Fasheh found = 0;
1301ccd979bdSMark Fasheh start = offset + 1;
1302ccd979bdSMark Fasheh } else if (offset == start) {
1303ccd979bdSMark Fasheh /* we found a zero */
1304ccd979bdSMark Fasheh found++;
1305ccd979bdSMark Fasheh /* move start to the next bit to test */
1306ccd979bdSMark Fasheh start++;
1307ccd979bdSMark Fasheh } else {
1308ccd979bdSMark Fasheh /* got a zero after some ones */
1309ccd979bdSMark Fasheh found = 1;
1310ccd979bdSMark Fasheh start = offset + 1;
1311ccd979bdSMark Fasheh }
1312ccd979bdSMark Fasheh if (found > best_size) {
1313ccd979bdSMark Fasheh best_size = found;
1314ccd979bdSMark Fasheh best_offset = start - found;
1315ccd979bdSMark Fasheh }
1316ccd979bdSMark Fasheh /* we got everything we needed */
1317ccd979bdSMark Fasheh if (found == bits_wanted) {
1318ccd979bdSMark Fasheh /* mlog(0, "Found it all!\n"); */
1319ccd979bdSMark Fasheh break;
1320ccd979bdSMark Fasheh }
1321ccd979bdSMark Fasheh }
1322ccd979bdSMark Fasheh
13237d1fe093SJoel Becker if (best_size) {
13247d1fe093SJoel Becker res->sr_bit_offset = best_offset;
13257d1fe093SJoel Becker res->sr_bits = best_size;
1326ccd979bdSMark Fasheh } else {
1327ccd979bdSMark Fasheh status = -ENOSPC;
1328ccd979bdSMark Fasheh /* No error log here -- see the comment above
1329ccd979bdSMark Fasheh * ocfs2_test_bg_bit_allocatable */
1330ccd979bdSMark Fasheh }
1331ccd979bdSMark Fasheh
1332ccd979bdSMark Fasheh return status;
1333ccd979bdSMark Fasheh }
1334ccd979bdSMark Fasheh
ocfs2_block_group_set_bits(handle_t * handle,struct inode * alloc_inode,struct ocfs2_group_desc * bg,struct buffer_head * group_bh,unsigned int bit_off,unsigned int num_bits)13350a2fcd89SYounger Liu int ocfs2_block_group_set_bits(handle_t *handle,
1336ccd979bdSMark Fasheh struct inode *alloc_inode,
1337ccd979bdSMark Fasheh struct ocfs2_group_desc *bg,
1338ccd979bdSMark Fasheh struct buffer_head *group_bh,
1339ccd979bdSMark Fasheh unsigned int bit_off,
1340ccd979bdSMark Fasheh unsigned int num_bits)
1341ccd979bdSMark Fasheh {
1342ccd979bdSMark Fasheh int status;
1343ccd979bdSMark Fasheh void *bitmap = bg->bg_bitmap;
1344ccd979bdSMark Fasheh int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1345ccd979bdSMark Fasheh
134642035306SJoel Becker /* All callers get the descriptor via
134742035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
134842035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1349ccd979bdSMark Fasheh BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
1350ccd979bdSMark Fasheh
13512f73e135STao Ma trace_ocfs2_block_group_set_bits(bit_off, num_bits);
1352ccd979bdSMark Fasheh
1353ccd979bdSMark Fasheh if (ocfs2_is_cluster_bitmap(alloc_inode))
1354ccd979bdSMark Fasheh journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1355ccd979bdSMark Fasheh
135613723d00SJoel Becker status = ocfs2_journal_access_gd(handle,
13570cf2f763SJoel Becker INODE_CACHE(alloc_inode),
1358ccd979bdSMark Fasheh group_bh,
1359ccd979bdSMark Fasheh journal_type);
1360ccd979bdSMark Fasheh if (status < 0) {
1361ccd979bdSMark Fasheh mlog_errno(status);
1362ccd979bdSMark Fasheh goto bail;
1363ccd979bdSMark Fasheh }
1364ccd979bdSMark Fasheh
1365ccd979bdSMark Fasheh le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
13669b5cd10eSSrinivas Eeda if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) {
13677ecef14aSJoe Perches return ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit count %u but claims %u are freed. num_bits %d\n",
13689b5cd10eSSrinivas Eeda (unsigned long long)le64_to_cpu(bg->bg_blkno),
13699b5cd10eSSrinivas Eeda le16_to_cpu(bg->bg_bits),
13707ecef14aSJoe Perches le16_to_cpu(bg->bg_free_bits_count),
13717ecef14aSJoe Perches num_bits);
13729b5cd10eSSrinivas Eeda }
1373ccd979bdSMark Fasheh while(num_bits--)
1374ccd979bdSMark Fasheh ocfs2_set_bit(bit_off++, bitmap);
1375ccd979bdSMark Fasheh
1376ec20cec7SJoel Becker ocfs2_journal_dirty(handle, group_bh);
1377ccd979bdSMark Fasheh
1378ccd979bdSMark Fasheh bail:
1379ccd979bdSMark Fasheh return status;
1380ccd979bdSMark Fasheh }
1381ccd979bdSMark Fasheh
1382ccd979bdSMark Fasheh /* find the one with the most empty bits */
ocfs2_find_victim_chain(struct ocfs2_chain_list * cl)1383ccd979bdSMark Fasheh static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
1384ccd979bdSMark Fasheh {
1385ccd979bdSMark Fasheh u16 curr, best;
1386ccd979bdSMark Fasheh
1387ccd979bdSMark Fasheh BUG_ON(!cl->cl_next_free_rec);
1388ccd979bdSMark Fasheh
1389ccd979bdSMark Fasheh best = curr = 0;
1390ccd979bdSMark Fasheh while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
1391ccd979bdSMark Fasheh if (le32_to_cpu(cl->cl_recs[curr].c_free) >
1392ccd979bdSMark Fasheh le32_to_cpu(cl->cl_recs[best].c_free))
1393ccd979bdSMark Fasheh best = curr;
1394ccd979bdSMark Fasheh curr++;
1395ccd979bdSMark Fasheh }
1396ccd979bdSMark Fasheh
1397ccd979bdSMark Fasheh BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
1398ccd979bdSMark Fasheh return best;
1399ccd979bdSMark Fasheh }
1400ccd979bdSMark Fasheh
ocfs2_relink_block_group(handle_t * handle,struct inode * alloc_inode,struct buffer_head * fe_bh,struct buffer_head * bg_bh,struct buffer_head * prev_bg_bh,u16 chain)14011fabe148SMark Fasheh static int ocfs2_relink_block_group(handle_t *handle,
1402ccd979bdSMark Fasheh struct inode *alloc_inode,
1403ccd979bdSMark Fasheh struct buffer_head *fe_bh,
1404ccd979bdSMark Fasheh struct buffer_head *bg_bh,
1405ccd979bdSMark Fasheh struct buffer_head *prev_bg_bh,
1406ccd979bdSMark Fasheh u16 chain)
1407ccd979bdSMark Fasheh {
1408ccd979bdSMark Fasheh int status;
1409ccd979bdSMark Fasheh /* there is a really tiny chance the journal calls could fail,
1410ccd979bdSMark Fasheh * but we wouldn't want inconsistent blocks in *any* case. */
141149309841SJie Liu u64 bg_ptr, prev_bg_ptr;
1412ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
1413ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1414ccd979bdSMark Fasheh struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
1415ccd979bdSMark Fasheh
141642035306SJoel Becker /* The caller got these descriptors from
141742035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
141842035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
141942035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
1420ccd979bdSMark Fasheh
14212f73e135STao Ma trace_ocfs2_relink_block_group(
14221ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
14231ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(bg->bg_blkno),
14241ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
1425ccd979bdSMark Fasheh
1426ccd979bdSMark Fasheh bg_ptr = le64_to_cpu(bg->bg_next_group);
1427ccd979bdSMark Fasheh prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
1428ccd979bdSMark Fasheh
14290cf2f763SJoel Becker status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
14300cf2f763SJoel Becker prev_bg_bh,
1431ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE);
143225e28921SAndrew Morton if (status < 0)
143349309841SJie Liu goto out;
1434ccd979bdSMark Fasheh
1435ccd979bdSMark Fasheh prev_bg->bg_next_group = bg->bg_next_group;
1436ec20cec7SJoel Becker ocfs2_journal_dirty(handle, prev_bg_bh);
1437ccd979bdSMark Fasheh
14380cf2f763SJoel Becker status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
14390cf2f763SJoel Becker bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
144025e28921SAndrew Morton if (status < 0)
144149309841SJie Liu goto out_rollback_prev_bg;
1442ccd979bdSMark Fasheh
1443ccd979bdSMark Fasheh bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
1444ec20cec7SJoel Becker ocfs2_journal_dirty(handle, bg_bh);
1445ccd979bdSMark Fasheh
14460cf2f763SJoel Becker status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
14470cf2f763SJoel Becker fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
144825e28921SAndrew Morton if (status < 0)
144949309841SJie Liu goto out_rollback_bg;
1450ccd979bdSMark Fasheh
1451ccd979bdSMark Fasheh fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
1452ec20cec7SJoel Becker ocfs2_journal_dirty(handle, fe_bh);
1453ccd979bdSMark Fasheh
145449309841SJie Liu out:
145525e28921SAndrew Morton if (status < 0)
145625e28921SAndrew Morton mlog_errno(status);
145749309841SJie Liu return status;
145842035306SJoel Becker
145949309841SJie Liu out_rollback_bg:
146049309841SJie Liu bg->bg_next_group = cpu_to_le64(bg_ptr);
146149309841SJie Liu out_rollback_prev_bg:
146249309841SJie Liu prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
146325e28921SAndrew Morton goto out;
1464ccd979bdSMark Fasheh }
1465ccd979bdSMark Fasheh
ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc * bg,u32 wanted)1466ccd979bdSMark Fasheh static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1467ccd979bdSMark Fasheh u32 wanted)
1468ccd979bdSMark Fasheh {
1469ccd979bdSMark Fasheh return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1470ccd979bdSMark Fasheh }
1471ccd979bdSMark Fasheh
1472ccd979bdSMark Fasheh /* return 0 on success, -ENOSPC to keep searching and any other < 0
1473ccd979bdSMark Fasheh * value on error. */
ocfs2_cluster_group_search(struct inode * inode,struct buffer_head * group_bh,u32 bits_wanted,u32 min_bits,u64 max_block,struct ocfs2_suballoc_result * res)1474ccd979bdSMark Fasheh static int ocfs2_cluster_group_search(struct inode *inode,
1475ccd979bdSMark Fasheh struct buffer_head *group_bh,
1476ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits,
14771187c968SJoel Becker u64 max_block,
14787d1fe093SJoel Becker struct ocfs2_suballoc_result *res)
1479ccd979bdSMark Fasheh {
1480ccd979bdSMark Fasheh int search = -ENOSPC;
1481ccd979bdSMark Fasheh int ret;
14821187c968SJoel Becker u64 blkoff;
14837bf72edeSMark Fasheh struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
14849c7af40bSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
14857bf72edeSMark Fasheh unsigned int max_bits, gd_cluster_off;
1486ccd979bdSMark Fasheh
1487ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1488ccd979bdSMark Fasheh
14897bf72edeSMark Fasheh if (gd->bg_free_bits_count) {
14907bf72edeSMark Fasheh max_bits = le16_to_cpu(gd->bg_bits);
14917bf72edeSMark Fasheh
14927bf72edeSMark Fasheh /* Tail groups in cluster bitmaps which aren't cpg
149325985edcSLucas De Marchi * aligned are prone to partial extension by a failed
14947bf72edeSMark Fasheh * fs resize. If the file system resize never got to
14957bf72edeSMark Fasheh * update the dinode cluster count, then we don't want
14967bf72edeSMark Fasheh * to trust any clusters past it, regardless of what
14977bf72edeSMark Fasheh * the group descriptor says. */
14987bf72edeSMark Fasheh gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
14997bf72edeSMark Fasheh le64_to_cpu(gd->bg_blkno));
15007bf72edeSMark Fasheh if ((gd_cluster_off + max_bits) >
15017bf72edeSMark Fasheh OCFS2_I(inode)->ip_clusters) {
15027bf72edeSMark Fasheh max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
15032f73e135STao Ma trace_ocfs2_cluster_group_search_wrong_max_bits(
15047bf72edeSMark Fasheh (unsigned long long)le64_to_cpu(gd->bg_blkno),
15057bf72edeSMark Fasheh le16_to_cpu(gd->bg_bits),
15067bf72edeSMark Fasheh OCFS2_I(inode)->ip_clusters, max_bits);
15077bf72edeSMark Fasheh }
15087bf72edeSMark Fasheh
15091119d3c0Spiaojun ret = ocfs2_block_group_find_clear_bits(osb,
1510ccd979bdSMark Fasheh group_bh, bits_wanted,
15117d1fe093SJoel Becker max_bits, res);
1512ccd979bdSMark Fasheh if (ret)
1513ccd979bdSMark Fasheh return ret;
1514ccd979bdSMark Fasheh
15151187c968SJoel Becker if (max_block) {
15161187c968SJoel Becker blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
15171187c968SJoel Becker gd_cluster_off +
15187d1fe093SJoel Becker res->sr_bit_offset +
15197d1fe093SJoel Becker res->sr_bits);
15202f73e135STao Ma trace_ocfs2_cluster_group_search_max_block(
15211187c968SJoel Becker (unsigned long long)blkoff,
15221187c968SJoel Becker (unsigned long long)max_block);
15231187c968SJoel Becker if (blkoff > max_block)
15241187c968SJoel Becker return -ENOSPC;
15251187c968SJoel Becker }
15261187c968SJoel Becker
1527ccd979bdSMark Fasheh /* ocfs2_block_group_find_clear_bits() might
1528ccd979bdSMark Fasheh * return success, but we still want to return
1529ccd979bdSMark Fasheh * -ENOSPC unless it found the minimum number
1530ccd979bdSMark Fasheh * of bits. */
15317d1fe093SJoel Becker if (min_bits <= res->sr_bits)
1532ccd979bdSMark Fasheh search = 0; /* success */
15337d1fe093SJoel Becker else if (res->sr_bits) {
15349c7af40bSMark Fasheh /*
15359c7af40bSMark Fasheh * Don't show bits which we'll be returning
15369c7af40bSMark Fasheh * for allocation to the local alloc bitmap.
15379c7af40bSMark Fasheh */
15387d1fe093SJoel Becker ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits);
1539ccd979bdSMark Fasheh }
1540ccd979bdSMark Fasheh }
1541ccd979bdSMark Fasheh
1542ccd979bdSMark Fasheh return search;
1543ccd979bdSMark Fasheh }
1544ccd979bdSMark Fasheh
ocfs2_block_group_search(struct inode * inode,struct buffer_head * group_bh,u32 bits_wanted,u32 min_bits,u64 max_block,struct ocfs2_suballoc_result * res)1545ccd979bdSMark Fasheh static int ocfs2_block_group_search(struct inode *inode,
1546ccd979bdSMark Fasheh struct buffer_head *group_bh,
1547ccd979bdSMark Fasheh u32 bits_wanted, u32 min_bits,
15481187c968SJoel Becker u64 max_block,
15497d1fe093SJoel Becker struct ocfs2_suballoc_result *res)
1550ccd979bdSMark Fasheh {
1551ccd979bdSMark Fasheh int ret = -ENOSPC;
15521187c968SJoel Becker u64 blkoff;
1553ccd979bdSMark Fasheh struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1554ccd979bdSMark Fasheh
1555ccd979bdSMark Fasheh BUG_ON(min_bits != 1);
1556ccd979bdSMark Fasheh BUG_ON(ocfs2_is_cluster_bitmap(inode));
1557ccd979bdSMark Fasheh
15581187c968SJoel Becker if (bg->bg_free_bits_count) {
1559ccd979bdSMark Fasheh ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1560ccd979bdSMark Fasheh group_bh, bits_wanted,
15617bf72edeSMark Fasheh le16_to_cpu(bg->bg_bits),
15627d1fe093SJoel Becker res);
15631187c968SJoel Becker if (!ret && max_block) {
15647d1fe093SJoel Becker blkoff = le64_to_cpu(bg->bg_blkno) +
15657d1fe093SJoel Becker res->sr_bit_offset + res->sr_bits;
15662f73e135STao Ma trace_ocfs2_block_group_search_max_block(
15671187c968SJoel Becker (unsigned long long)blkoff,
15681187c968SJoel Becker (unsigned long long)max_block);
15691187c968SJoel Becker if (blkoff > max_block)
15701187c968SJoel Becker ret = -ENOSPC;
15711187c968SJoel Becker }
15721187c968SJoel Becker }
1573ccd979bdSMark Fasheh
1574ccd979bdSMark Fasheh return ret;
1575ccd979bdSMark Fasheh }
1576ccd979bdSMark Fasheh
ocfs2_alloc_dinode_update_counts(struct inode * inode,handle_t * handle,struct buffer_head * di_bh,u32 num_bits,u16 chain)15770a2fcd89SYounger Liu int ocfs2_alloc_dinode_update_counts(struct inode *inode,
15781fabe148SMark Fasheh handle_t *handle,
1579883d4caeSMark Fasheh struct buffer_head *di_bh,
1580883d4caeSMark Fasheh u32 num_bits,
1581883d4caeSMark Fasheh u16 chain)
1582883d4caeSMark Fasheh {
1583883d4caeSMark Fasheh int ret;
1584883d4caeSMark Fasheh u32 tmp_used;
1585883d4caeSMark Fasheh struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1586883d4caeSMark Fasheh struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1587883d4caeSMark Fasheh
15880cf2f763SJoel Becker ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
1589883d4caeSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE);
1590883d4caeSMark Fasheh if (ret < 0) {
1591883d4caeSMark Fasheh mlog_errno(ret);
1592883d4caeSMark Fasheh goto out;
1593883d4caeSMark Fasheh }
1594883d4caeSMark Fasheh
1595883d4caeSMark Fasheh tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1596883d4caeSMark Fasheh di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1597883d4caeSMark Fasheh le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1598ec20cec7SJoel Becker ocfs2_journal_dirty(handle, di_bh);
1599883d4caeSMark Fasheh
1600883d4caeSMark Fasheh out:
1601883d4caeSMark Fasheh return ret;
1602883d4caeSMark Fasheh }
1603883d4caeSMark Fasheh
ocfs2_rollback_alloc_dinode_counts(struct inode * inode,struct buffer_head * di_bh,u32 num_bits,u16 chain)1604db66c715SYounger Liu void ocfs2_rollback_alloc_dinode_counts(struct inode *inode,
1605db66c715SYounger Liu struct buffer_head *di_bh,
1606db66c715SYounger Liu u32 num_bits,
1607db66c715SYounger Liu u16 chain)
1608db66c715SYounger Liu {
1609db66c715SYounger Liu u32 tmp_used;
1610db66c715SYounger Liu struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1611db66c715SYounger Liu struct ocfs2_chain_list *cl;
1612db66c715SYounger Liu
1613db66c715SYounger Liu cl = (struct ocfs2_chain_list *)&di->id2.i_chain;
1614db66c715SYounger Liu tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1615db66c715SYounger Liu di->id1.bitmap1.i_used = cpu_to_le32(tmp_used - num_bits);
1616db66c715SYounger Liu le32_add_cpu(&cl->cl_recs[chain].c_free, num_bits);
1617db66c715SYounger Liu }
1618db66c715SYounger Liu
ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result * res,struct ocfs2_extent_rec * rec,struct ocfs2_chain_list * cl)1619ba206635SJoel Becker static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res,
162013e434cfSJoel Becker struct ocfs2_extent_rec *rec,
162113e434cfSJoel Becker struct ocfs2_chain_list *cl)
162213e434cfSJoel Becker {
162313e434cfSJoel Becker unsigned int bpc = le16_to_cpu(cl->cl_bpc);
162413e434cfSJoel Becker unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc;
162572094e43SAl Viro unsigned int bitcount = le16_to_cpu(rec->e_leaf_clusters) * bpc;
162613e434cfSJoel Becker
162713e434cfSJoel Becker if (res->sr_bit_offset < bitoff)
162813e434cfSJoel Becker return 0;
162913e434cfSJoel Becker if (res->sr_bit_offset >= (bitoff + bitcount))
163013e434cfSJoel Becker return 0;
1631ba206635SJoel Becker res->sr_blkno = le64_to_cpu(rec->e_blkno) +
1632ba206635SJoel Becker (res->sr_bit_offset - bitoff);
163313e434cfSJoel Becker if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount))
163413e434cfSJoel Becker res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset;
163513e434cfSJoel Becker return 1;
163613e434cfSJoel Becker }
163713e434cfSJoel Becker
ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context * ac,struct ocfs2_group_desc * bg,struct ocfs2_suballoc_result * res)1638ba206635SJoel Becker static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac,
163913e434cfSJoel Becker struct ocfs2_group_desc *bg,
164013e434cfSJoel Becker struct ocfs2_suballoc_result *res)
164113e434cfSJoel Becker {
164213e434cfSJoel Becker int i;
16432b6cb576SJoel Becker u64 bg_blkno = res->sr_bg_blkno; /* Save off */
164413e434cfSJoel Becker struct ocfs2_extent_rec *rec;
164513e434cfSJoel Becker struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
164613e434cfSJoel Becker struct ocfs2_chain_list *cl = &di->id2.i_chain;
164713e434cfSJoel Becker
1648ba206635SJoel Becker if (ocfs2_is_cluster_bitmap(ac->ac_inode)) {
1649ba206635SJoel Becker res->sr_blkno = 0;
165013e434cfSJoel Becker return;
1651ba206635SJoel Becker }
165213e434cfSJoel Becker
1653ba206635SJoel Becker res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset;
16542b6cb576SJoel Becker res->sr_bg_blkno = 0; /* Clear it for contig block groups */
16554711954eSTao Ma if (!ocfs2_supports_discontig_bg(OCFS2_SB(ac->ac_inode->i_sb)) ||
1656ba206635SJoel Becker !bg->bg_list.l_next_free_rec)
165713e434cfSJoel Becker return;
165813e434cfSJoel Becker
165913e434cfSJoel Becker for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) {
166013e434cfSJoel Becker rec = &bg->bg_list.l_recs[i];
16612b6cb576SJoel Becker if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) {
16622b6cb576SJoel Becker res->sr_bg_blkno = bg_blkno; /* Restore */
166313e434cfSJoel Becker break;
166413e434cfSJoel Becker }
166513e434cfSJoel Becker }
16662b6cb576SJoel Becker }
166713e434cfSJoel Becker
ocfs2_search_one_group(struct ocfs2_alloc_context * ac,handle_t * handle,u32 bits_wanted,u32 min_bits,struct ocfs2_suballoc_result * res,u16 * bits_left)1668883d4caeSMark Fasheh static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
16691fabe148SMark Fasheh handle_t *handle,
1670883d4caeSMark Fasheh u32 bits_wanted,
1671883d4caeSMark Fasheh u32 min_bits,
16727d1fe093SJoel Becker struct ocfs2_suballoc_result *res,
1673883d4caeSMark Fasheh u16 *bits_left)
1674883d4caeSMark Fasheh {
1675883d4caeSMark Fasheh int ret;
1676883d4caeSMark Fasheh struct buffer_head *group_bh = NULL;
1677883d4caeSMark Fasheh struct ocfs2_group_desc *gd;
167868f64d47SJoel Becker struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
1679883d4caeSMark Fasheh struct inode *alloc_inode = ac->ac_inode;
1680883d4caeSMark Fasheh
16817d1fe093SJoel Becker ret = ocfs2_read_group_descriptor(alloc_inode, di,
16827d1fe093SJoel Becker res->sr_bg_blkno, &group_bh);
1683883d4caeSMark Fasheh if (ret < 0) {
1684883d4caeSMark Fasheh mlog_errno(ret);
1685883d4caeSMark Fasheh return ret;
1686883d4caeSMark Fasheh }
1687883d4caeSMark Fasheh
1688883d4caeSMark Fasheh gd = (struct ocfs2_group_desc *) group_bh->b_data;
1689883d4caeSMark Fasheh ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
16907d1fe093SJoel Becker ac->ac_max_block, res);
1691883d4caeSMark Fasheh if (ret < 0) {
1692883d4caeSMark Fasheh if (ret != -ENOSPC)
1693883d4caeSMark Fasheh mlog_errno(ret);
1694883d4caeSMark Fasheh goto out;
1695883d4caeSMark Fasheh }
1696883d4caeSMark Fasheh
169713e434cfSJoel Becker if (!ret)
1698ba206635SJoel Becker ocfs2_bg_discontig_fix_result(ac, gd, res);
169913e434cfSJoel Becker
1700e49e2767SMark Fasheh /*
1701e49e2767SMark Fasheh * sr_bg_blkno might have been changed by
1702e49e2767SMark Fasheh * ocfs2_bg_discontig_fix_result
1703e49e2767SMark Fasheh */
1704e49e2767SMark Fasheh res->sr_bg_stable_blkno = group_bh->b_blocknr;
1705e49e2767SMark Fasheh
1706e49e2767SMark Fasheh if (ac->ac_find_loc_only)
1707e49e2767SMark Fasheh goto out_loc_only;
1708e49e2767SMark Fasheh
1709883d4caeSMark Fasheh ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
17107d1fe093SJoel Becker res->sr_bits,
1711883d4caeSMark Fasheh le16_to_cpu(gd->bg_chain));
1712883d4caeSMark Fasheh if (ret < 0) {
1713883d4caeSMark Fasheh mlog_errno(ret);
1714883d4caeSMark Fasheh goto out;
1715883d4caeSMark Fasheh }
1716883d4caeSMark Fasheh
1717883d4caeSMark Fasheh ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
17187d1fe093SJoel Becker res->sr_bit_offset, res->sr_bits);
1719db66c715SYounger Liu if (ret < 0) {
1720db66c715SYounger Liu ocfs2_rollback_alloc_dinode_counts(alloc_inode, ac->ac_bh,
1721db66c715SYounger Liu res->sr_bits,
1722db66c715SYounger Liu le16_to_cpu(gd->bg_chain));
1723883d4caeSMark Fasheh mlog_errno(ret);
1724db66c715SYounger Liu }
1725883d4caeSMark Fasheh
1726e49e2767SMark Fasheh out_loc_only:
1727883d4caeSMark Fasheh *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1728883d4caeSMark Fasheh
1729883d4caeSMark Fasheh out:
1730883d4caeSMark Fasheh brelse(group_bh);
1731883d4caeSMark Fasheh
1732883d4caeSMark Fasheh return ret;
1733883d4caeSMark Fasheh }
1734883d4caeSMark Fasheh
ocfs2_search_chain(struct ocfs2_alloc_context * ac,handle_t * handle,u32 bits_wanted,u32 min_bits,struct ocfs2_suballoc_result * res,u16 * bits_left)1735ccd979bdSMark Fasheh static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
17361fabe148SMark Fasheh handle_t *handle,
1737ccd979bdSMark Fasheh u32 bits_wanted,
1738ccd979bdSMark Fasheh u32 min_bits,
17397d1fe093SJoel Becker struct ocfs2_suballoc_result *res,
1740883d4caeSMark Fasheh u16 *bits_left)
1741ccd979bdSMark Fasheh {
1742ccd979bdSMark Fasheh int status;
17437d1fe093SJoel Becker u16 chain;
1744ccd979bdSMark Fasheh u64 next_group;
1745ccd979bdSMark Fasheh struct inode *alloc_inode = ac->ac_inode;
1746ccd979bdSMark Fasheh struct buffer_head *group_bh = NULL;
1747ccd979bdSMark Fasheh struct buffer_head *prev_group_bh = NULL;
1748ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1749ccd979bdSMark Fasheh struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1750ccd979bdSMark Fasheh struct ocfs2_group_desc *bg;
1751ccd979bdSMark Fasheh
1752ccd979bdSMark Fasheh chain = ac->ac_chain;
17532f73e135STao Ma trace_ocfs2_search_chain_begin(
17542f73e135STao Ma (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno,
17552f73e135STao Ma bits_wanted, chain);
1756ccd979bdSMark Fasheh
175768f64d47SJoel Becker status = ocfs2_read_group_descriptor(alloc_inode, fe,
1758ccd979bdSMark Fasheh le64_to_cpu(cl->cl_recs[chain].c_blkno),
17590fcaa56aSJoel Becker &group_bh);
1760ccd979bdSMark Fasheh if (status < 0) {
1761ccd979bdSMark Fasheh mlog_errno(status);
1762ccd979bdSMark Fasheh goto bail;
1763ccd979bdSMark Fasheh }
1764ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) group_bh->b_data;
1765ccd979bdSMark Fasheh
1766ccd979bdSMark Fasheh status = -ENOSPC;
1767ccd979bdSMark Fasheh /* for now, the chain search is a bit simplistic. We just use
1768ccd979bdSMark Fasheh * the 1st group with any empty bits. */
17691187c968SJoel Becker while ((status = ac->ac_group_search(alloc_inode, group_bh,
17701187c968SJoel Becker bits_wanted, min_bits,
17717d1fe093SJoel Becker ac->ac_max_block,
17727d1fe093SJoel Becker res)) == -ENOSPC) {
1773ccd979bdSMark Fasheh if (!bg->bg_next_group)
1774ccd979bdSMark Fasheh break;
1775a81cb88bSMark Fasheh
1776ccd979bdSMark Fasheh brelse(prev_group_bh);
1777ccd979bdSMark Fasheh prev_group_bh = NULL;
1778a81cb88bSMark Fasheh
1779ccd979bdSMark Fasheh next_group = le64_to_cpu(bg->bg_next_group);
1780ccd979bdSMark Fasheh prev_group_bh = group_bh;
1781ccd979bdSMark Fasheh group_bh = NULL;
178268f64d47SJoel Becker status = ocfs2_read_group_descriptor(alloc_inode, fe,
17830fcaa56aSJoel Becker next_group, &group_bh);
1784ccd979bdSMark Fasheh if (status < 0) {
1785ccd979bdSMark Fasheh mlog_errno(status);
1786ccd979bdSMark Fasheh goto bail;
1787ccd979bdSMark Fasheh }
1788ccd979bdSMark Fasheh bg = (struct ocfs2_group_desc *) group_bh->b_data;
1789ccd979bdSMark Fasheh }
1790ccd979bdSMark Fasheh if (status < 0) {
1791ccd979bdSMark Fasheh if (status != -ENOSPC)
1792ccd979bdSMark Fasheh mlog_errno(status);
1793ccd979bdSMark Fasheh goto bail;
1794ccd979bdSMark Fasheh }
1795ccd979bdSMark Fasheh
17962f73e135STao Ma trace_ocfs2_search_chain_succ(
17972f73e135STao Ma (unsigned long long)le64_to_cpu(bg->bg_blkno), res->sr_bits);
1798ccd979bdSMark Fasheh
17997d1fe093SJoel Becker res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
1800ccd979bdSMark Fasheh
18017d1fe093SJoel Becker BUG_ON(res->sr_bits == 0);
180213e434cfSJoel Becker if (!status)
1803ba206635SJoel Becker ocfs2_bg_discontig_fix_result(ac, bg, res);
180413e434cfSJoel Becker
1805e49e2767SMark Fasheh /*
1806e49e2767SMark Fasheh * sr_bg_blkno might have been changed by
1807e49e2767SMark Fasheh * ocfs2_bg_discontig_fix_result
1808e49e2767SMark Fasheh */
1809e49e2767SMark Fasheh res->sr_bg_stable_blkno = group_bh->b_blocknr;
1810ccd979bdSMark Fasheh
1811ccd979bdSMark Fasheh /*
1812ccd979bdSMark Fasheh * Keep track of previous block descriptor read. When
1813ccd979bdSMark Fasheh * we find a target, if we have read more than X
1814ccd979bdSMark Fasheh * number of descriptors, and the target is reasonably
1815ccd979bdSMark Fasheh * empty, relink him to top of his chain.
1816ccd979bdSMark Fasheh *
1817ccd979bdSMark Fasheh * We've read 0 extra blocks and only send one more to
1818ccd979bdSMark Fasheh * the transaction, yet the next guy to search has a
1819ccd979bdSMark Fasheh * much easier time.
1820ccd979bdSMark Fasheh *
1821ccd979bdSMark Fasheh * Do this *after* figuring out how many bits we're taking out
1822ccd979bdSMark Fasheh * of our target group.
1823ccd979bdSMark Fasheh */
1824309a85b6SXiaowei.Hu if (!ac->ac_disable_chain_relink &&
1825ccd979bdSMark Fasheh (prev_group_bh) &&
18267d1fe093SJoel Becker (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
1827ccd979bdSMark Fasheh status = ocfs2_relink_block_group(handle, alloc_inode,
1828ccd979bdSMark Fasheh ac->ac_bh, group_bh,
1829ccd979bdSMark Fasheh prev_group_bh, chain);
1830ccd979bdSMark Fasheh if (status < 0) {
1831ccd979bdSMark Fasheh mlog_errno(status);
1832ccd979bdSMark Fasheh goto bail;
1833ccd979bdSMark Fasheh }
1834ccd979bdSMark Fasheh }
1835ccd979bdSMark Fasheh
1836e49e2767SMark Fasheh if (ac->ac_find_loc_only)
1837e49e2767SMark Fasheh goto out_loc_only;
1838e49e2767SMark Fasheh
1839d5134982SMark Fasheh status = ocfs2_alloc_dinode_update_counts(alloc_inode, handle,
1840d5134982SMark Fasheh ac->ac_bh, res->sr_bits,
1841d5134982SMark Fasheh chain);
1842d5134982SMark Fasheh if (status) {
1843ccd979bdSMark Fasheh mlog_errno(status);
1844ccd979bdSMark Fasheh goto bail;
1845ccd979bdSMark Fasheh }
1846ccd979bdSMark Fasheh
1847ccd979bdSMark Fasheh status = ocfs2_block_group_set_bits(handle,
1848ccd979bdSMark Fasheh alloc_inode,
1849ccd979bdSMark Fasheh bg,
1850ccd979bdSMark Fasheh group_bh,
18517d1fe093SJoel Becker res->sr_bit_offset,
18527d1fe093SJoel Becker res->sr_bits);
1853ccd979bdSMark Fasheh if (status < 0) {
1854db66c715SYounger Liu ocfs2_rollback_alloc_dinode_counts(alloc_inode,
1855db66c715SYounger Liu ac->ac_bh, res->sr_bits, chain);
1856ccd979bdSMark Fasheh mlog_errno(status);
1857ccd979bdSMark Fasheh goto bail;
1858ccd979bdSMark Fasheh }
1859ccd979bdSMark Fasheh
18602f73e135STao Ma trace_ocfs2_search_chain_end(
18612f73e135STao Ma (unsigned long long)le64_to_cpu(fe->i_blkno),
18622f73e135STao Ma res->sr_bits);
1863ccd979bdSMark Fasheh
1864e49e2767SMark Fasheh out_loc_only:
1865883d4caeSMark Fasheh *bits_left = le16_to_cpu(bg->bg_free_bits_count);
1866ccd979bdSMark Fasheh bail:
1867ccd979bdSMark Fasheh brelse(group_bh);
1868ccd979bdSMark Fasheh brelse(prev_group_bh);
1869ccd979bdSMark Fasheh
1870c1e8d35eSTao Ma if (status)
1871c1e8d35eSTao Ma mlog_errno(status);
1872ccd979bdSMark Fasheh return status;
1873ccd979bdSMark Fasheh }
1874ccd979bdSMark Fasheh
1875ccd979bdSMark Fasheh /* will give out up to bits_wanted contiguous bits. */
ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context * ac,handle_t * handle,u32 bits_wanted,u32 min_bits,struct ocfs2_suballoc_result * res)1876aa8f8e93SJoel Becker static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
18771fabe148SMark Fasheh handle_t *handle,
1878ccd979bdSMark Fasheh u32 bits_wanted,
1879ccd979bdSMark Fasheh u32 min_bits,
18807d1fe093SJoel Becker struct ocfs2_suballoc_result *res)
1881ccd979bdSMark Fasheh {
1882ccd979bdSMark Fasheh int status;
1883ccd979bdSMark Fasheh u16 victim, i;
1884883d4caeSMark Fasheh u16 bits_left = 0;
1885b2b6ebf5SMark Fasheh u64 hint = ac->ac_last_group;
1886ccd979bdSMark Fasheh struct ocfs2_chain_list *cl;
1887ccd979bdSMark Fasheh struct ocfs2_dinode *fe;
1888ccd979bdSMark Fasheh
1889ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1890ccd979bdSMark Fasheh BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1891ccd979bdSMark Fasheh BUG_ON(!ac->ac_bh);
1892ccd979bdSMark Fasheh
1893ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
189410995aa2SJoel Becker
189510995aa2SJoel Becker /* The bh was validated by the inode read during
189610995aa2SJoel Becker * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
189710995aa2SJoel Becker BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
189810995aa2SJoel Becker
1899ccd979bdSMark Fasheh if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1900ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_total)) {
190117a5b9abSGoldwyn Rodrigues status = ocfs2_error(ac->ac_inode->i_sb,
19027ecef14aSJoe Perches "Chain allocator dinode %llu has %u used bits but only %u total\n",
1903b0697053SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno),
1904ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_used),
1905ccd979bdSMark Fasheh le32_to_cpu(fe->id1.bitmap1.i_total));
1906ccd979bdSMark Fasheh goto bail;
1907ccd979bdSMark Fasheh }
1908ccd979bdSMark Fasheh
1909b2b6ebf5SMark Fasheh res->sr_bg_blkno = hint;
19107d1fe093SJoel Becker if (res->sr_bg_blkno) {
1911883d4caeSMark Fasheh /* Attempt to short-circuit the usual search mechanism
1912883d4caeSMark Fasheh * by jumping straight to the most recently used
1913b595076aSUwe Kleine-König * allocation group. This helps us maintain some
1914883d4caeSMark Fasheh * contiguousness across allocations. */
1915da5cbf2fSMark Fasheh status = ocfs2_search_one_group(ac, handle, bits_wanted,
19167d1fe093SJoel Becker min_bits, res, &bits_left);
19177d1fe093SJoel Becker if (!status)
1918883d4caeSMark Fasheh goto set_hint;
1919883d4caeSMark Fasheh if (status < 0 && status != -ENOSPC) {
1920883d4caeSMark Fasheh mlog_errno(status);
1921883d4caeSMark Fasheh goto bail;
1922883d4caeSMark Fasheh }
1923883d4caeSMark Fasheh }
1924883d4caeSMark Fasheh
1925ccd979bdSMark Fasheh cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1926ccd979bdSMark Fasheh
1927ccd979bdSMark Fasheh victim = ocfs2_find_victim_chain(cl);
1928ccd979bdSMark Fasheh ac->ac_chain = victim;
1929ccd979bdSMark Fasheh
19307d1fe093SJoel Becker status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
19317d1fe093SJoel Becker res, &bits_left);
1932b2b6ebf5SMark Fasheh if (!status) {
19331d1aff8cSJoseph Qi if (ocfs2_is_cluster_bitmap(ac->ac_inode))
19341d1aff8cSJoseph Qi hint = res->sr_bg_blkno;
19351d1aff8cSJoseph Qi else
1936b2b6ebf5SMark Fasheh hint = ocfs2_group_from_res(res);
1937883d4caeSMark Fasheh goto set_hint;
1938b2b6ebf5SMark Fasheh }
1939ccd979bdSMark Fasheh if (status < 0 && status != -ENOSPC) {
1940ccd979bdSMark Fasheh mlog_errno(status);
1941ccd979bdSMark Fasheh goto bail;
1942ccd979bdSMark Fasheh }
1943ccd979bdSMark Fasheh
19442f73e135STao Ma trace_ocfs2_claim_suballoc_bits(victim);
1945ccd979bdSMark Fasheh
1946ccd979bdSMark Fasheh /* If we didn't pick a good victim, then just default to
1947ccd979bdSMark Fasheh * searching each chain in order. Don't allow chain relinking
1948ccd979bdSMark Fasheh * because we only calculate enough journal credits for one
1949ccd979bdSMark Fasheh * relink per alloc. */
1950309a85b6SXiaowei.Hu ac->ac_disable_chain_relink = 1;
1951ccd979bdSMark Fasheh for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1952ccd979bdSMark Fasheh if (i == victim)
1953ccd979bdSMark Fasheh continue;
1954ccd979bdSMark Fasheh if (!cl->cl_recs[i].c_free)
1955ccd979bdSMark Fasheh continue;
1956ccd979bdSMark Fasheh
1957ccd979bdSMark Fasheh ac->ac_chain = i;
1958da5cbf2fSMark Fasheh status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
19597d1fe093SJoel Becker res, &bits_left);
1960b2b6ebf5SMark Fasheh if (!status) {
1961b2b6ebf5SMark Fasheh hint = ocfs2_group_from_res(res);
1962ccd979bdSMark Fasheh break;
1963b2b6ebf5SMark Fasheh }
1964ccd979bdSMark Fasheh if (status < 0 && status != -ENOSPC) {
1965ccd979bdSMark Fasheh mlog_errno(status);
1966ccd979bdSMark Fasheh goto bail;
1967ccd979bdSMark Fasheh }
1968ccd979bdSMark Fasheh }
1969ccd979bdSMark Fasheh
1970883d4caeSMark Fasheh set_hint:
1971883d4caeSMark Fasheh if (status != -ENOSPC) {
1972883d4caeSMark Fasheh /* If the next search of this group is not likely to
1973883d4caeSMark Fasheh * yield a suitable extent, then we reset the last
1974883d4caeSMark Fasheh * group hint so as to not waste a disk read */
1975883d4caeSMark Fasheh if (bits_left < min_bits)
1976883d4caeSMark Fasheh ac->ac_last_group = 0;
1977883d4caeSMark Fasheh else
1978b2b6ebf5SMark Fasheh ac->ac_last_group = hint;
1979883d4caeSMark Fasheh }
1980883d4caeSMark Fasheh
1981883d4caeSMark Fasheh bail:
1982c1e8d35eSTao Ma if (status)
1983c1e8d35eSTao Ma mlog_errno(status);
1984ccd979bdSMark Fasheh return status;
1985ccd979bdSMark Fasheh }
1986ccd979bdSMark Fasheh
ocfs2_claim_metadata(handle_t * handle,struct ocfs2_alloc_context * ac,u32 bits_wanted,u64 * suballoc_loc,u16 * suballoc_bit_start,unsigned int * num_bits,u64 * blkno_start)19871ed9b777SJoel Becker int ocfs2_claim_metadata(handle_t *handle,
1988ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac,
1989ccd979bdSMark Fasheh u32 bits_wanted,
19902b6cb576SJoel Becker u64 *suballoc_loc,
1991ccd979bdSMark Fasheh u16 *suballoc_bit_start,
1992ccd979bdSMark Fasheh unsigned int *num_bits,
1993ccd979bdSMark Fasheh u64 *blkno_start)
1994ccd979bdSMark Fasheh {
1995ccd979bdSMark Fasheh int status;
1996ba206635SJoel Becker struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
1997ccd979bdSMark Fasheh
1998ccd979bdSMark Fasheh BUG_ON(!ac);
1999ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
2000ccd979bdSMark Fasheh BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
2001ccd979bdSMark Fasheh
2002aa8f8e93SJoel Becker status = ocfs2_claim_suballoc_bits(ac,
2003da5cbf2fSMark Fasheh handle,
2004ccd979bdSMark Fasheh bits_wanted,
2005ccd979bdSMark Fasheh 1,
20067d1fe093SJoel Becker &res);
2007ccd979bdSMark Fasheh if (status < 0) {
2008ccd979bdSMark Fasheh mlog_errno(status);
2009ccd979bdSMark Fasheh goto bail;
2010ccd979bdSMark Fasheh }
20111ed9b777SJoel Becker atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
2012ccd979bdSMark Fasheh
20132b6cb576SJoel Becker *suballoc_loc = res.sr_bg_blkno;
20147d1fe093SJoel Becker *suballoc_bit_start = res.sr_bit_offset;
2015ba206635SJoel Becker *blkno_start = res.sr_blkno;
20167d1fe093SJoel Becker ac->ac_bits_given += res.sr_bits;
20177d1fe093SJoel Becker *num_bits = res.sr_bits;
2018ccd979bdSMark Fasheh status = 0;
2019ccd979bdSMark Fasheh bail:
2020c1e8d35eSTao Ma if (status)
2021c1e8d35eSTao Ma mlog_errno(status);
2022ccd979bdSMark Fasheh return status;
2023ccd979bdSMark Fasheh }
2024ccd979bdSMark Fasheh
ocfs2_init_inode_ac_group(struct inode * dir,struct buffer_head * parent_di_bh,struct ocfs2_alloc_context * ac)202513821151STao Ma static void ocfs2_init_inode_ac_group(struct inode *dir,
2026abf1b3cbSTao Ma struct buffer_head *parent_di_bh,
202713821151STao Ma struct ocfs2_alloc_context *ac)
202813821151STao Ma {
2029abf1b3cbSTao Ma struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_di_bh->b_data;
203013821151STao Ma /*
203113821151STao Ma * Try to allocate inodes from some specific group.
203213821151STao Ma *
203313821151STao Ma * If the parent dir has recorded the last group used in allocation,
203413821151STao Ma * cool, use it. Otherwise if we try to allocate new inode from the
203513821151STao Ma * same slot the parent dir belongs to, use the same chunk.
203613821151STao Ma *
203713821151STao Ma * We are very careful here to avoid the mistake of setting
203813821151STao Ma * ac_last_group to a group descriptor from a different (unlocked) slot.
203913821151STao Ma */
204013821151STao Ma if (OCFS2_I(dir)->ip_last_used_group &&
204113821151STao Ma OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
204213821151STao Ma ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
2043abf1b3cbSTao Ma else if (le16_to_cpu(di->i_suballoc_slot) == ac->ac_alloc_slot) {
2044abf1b3cbSTao Ma if (di->i_suballoc_loc)
2045abf1b3cbSTao Ma ac->ac_last_group = le64_to_cpu(di->i_suballoc_loc);
2046abf1b3cbSTao Ma else
204713821151STao Ma ac->ac_last_group = ocfs2_which_suballoc_group(
2048abf1b3cbSTao Ma le64_to_cpu(di->i_blkno),
2049abf1b3cbSTao Ma le16_to_cpu(di->i_suballoc_bit));
2050abf1b3cbSTao Ma }
205113821151STao Ma }
205213821151STao Ma
ocfs2_save_inode_ac_group(struct inode * dir,struct ocfs2_alloc_context * ac)205313821151STao Ma static inline void ocfs2_save_inode_ac_group(struct inode *dir,
205413821151STao Ma struct ocfs2_alloc_context *ac)
205513821151STao Ma {
205613821151STao Ma OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
205713821151STao Ma OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
205813821151STao Ma }
205913821151STao Ma
ocfs2_find_new_inode_loc(struct inode * dir,struct buffer_head * parent_fe_bh,struct ocfs2_alloc_context * ac,u64 * fe_blkno)2060e49e2767SMark Fasheh int ocfs2_find_new_inode_loc(struct inode *dir,
2061e49e2767SMark Fasheh struct buffer_head *parent_fe_bh,
2062e49e2767SMark Fasheh struct ocfs2_alloc_context *ac,
2063e49e2767SMark Fasheh u64 *fe_blkno)
2064e49e2767SMark Fasheh {
2065e49e2767SMark Fasheh int ret;
2066e49e2767SMark Fasheh handle_t *handle = NULL;
2067e49e2767SMark Fasheh struct ocfs2_suballoc_result *res;
2068e49e2767SMark Fasheh
2069e49e2767SMark Fasheh BUG_ON(!ac);
2070e49e2767SMark Fasheh BUG_ON(ac->ac_bits_given != 0);
2071e49e2767SMark Fasheh BUG_ON(ac->ac_bits_wanted != 1);
2072e49e2767SMark Fasheh BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
2073e49e2767SMark Fasheh
2074e49e2767SMark Fasheh res = kzalloc(sizeof(*res), GFP_NOFS);
2075e49e2767SMark Fasheh if (res == NULL) {
2076e49e2767SMark Fasheh ret = -ENOMEM;
2077e49e2767SMark Fasheh mlog_errno(ret);
2078e49e2767SMark Fasheh goto out;
2079e49e2767SMark Fasheh }
2080e49e2767SMark Fasheh
2081e49e2767SMark Fasheh ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
2082e49e2767SMark Fasheh
2083e49e2767SMark Fasheh /*
2084e49e2767SMark Fasheh * The handle started here is for chain relink. Alternatively,
2085e49e2767SMark Fasheh * we could just disable relink for these calls.
2086e49e2767SMark Fasheh */
2087e49e2767SMark Fasheh handle = ocfs2_start_trans(OCFS2_SB(dir->i_sb), OCFS2_SUBALLOC_ALLOC);
2088e49e2767SMark Fasheh if (IS_ERR(handle)) {
2089e49e2767SMark Fasheh ret = PTR_ERR(handle);
2090e49e2767SMark Fasheh handle = NULL;
2091e49e2767SMark Fasheh mlog_errno(ret);
2092e49e2767SMark Fasheh goto out;
2093e49e2767SMark Fasheh }
2094e49e2767SMark Fasheh
2095e49e2767SMark Fasheh /*
2096e49e2767SMark Fasheh * This will instruct ocfs2_claim_suballoc_bits and
2097e49e2767SMark Fasheh * ocfs2_search_one_group to search but save actual allocation
2098e49e2767SMark Fasheh * for later.
2099e49e2767SMark Fasheh */
2100e49e2767SMark Fasheh ac->ac_find_loc_only = 1;
2101e49e2767SMark Fasheh
2102e49e2767SMark Fasheh ret = ocfs2_claim_suballoc_bits(ac, handle, 1, 1, res);
2103e49e2767SMark Fasheh if (ret < 0) {
2104e49e2767SMark Fasheh mlog_errno(ret);
2105e49e2767SMark Fasheh goto out;
2106e49e2767SMark Fasheh }
2107e49e2767SMark Fasheh
2108e49e2767SMark Fasheh ac->ac_find_loc_priv = res;
2109e49e2767SMark Fasheh *fe_blkno = res->sr_blkno;
21106fdb702dSDarrick J. Wong ocfs2_update_inode_fsync_trans(handle, dir, 0);
2111e49e2767SMark Fasheh out:
2112e49e2767SMark Fasheh if (handle)
2113e49e2767SMark Fasheh ocfs2_commit_trans(OCFS2_SB(dir->i_sb), handle);
2114e49e2767SMark Fasheh
2115e49e2767SMark Fasheh if (ret)
2116e49e2767SMark Fasheh kfree(res);
2117e49e2767SMark Fasheh
2118e49e2767SMark Fasheh return ret;
2119e49e2767SMark Fasheh }
2120e49e2767SMark Fasheh
ocfs2_claim_new_inode_at_loc(handle_t * handle,struct inode * dir,struct ocfs2_alloc_context * ac,u64 * suballoc_loc,u16 * suballoc_bit,u64 di_blkno)2121e49e2767SMark Fasheh int ocfs2_claim_new_inode_at_loc(handle_t *handle,
2122e49e2767SMark Fasheh struct inode *dir,
2123e49e2767SMark Fasheh struct ocfs2_alloc_context *ac,
2124e49e2767SMark Fasheh u64 *suballoc_loc,
2125e49e2767SMark Fasheh u16 *suballoc_bit,
2126e49e2767SMark Fasheh u64 di_blkno)
2127e49e2767SMark Fasheh {
2128e49e2767SMark Fasheh int ret;
2129e49e2767SMark Fasheh u16 chain;
2130e49e2767SMark Fasheh struct ocfs2_suballoc_result *res = ac->ac_find_loc_priv;
2131e49e2767SMark Fasheh struct buffer_head *bg_bh = NULL;
2132e49e2767SMark Fasheh struct ocfs2_group_desc *bg;
2133e49e2767SMark Fasheh struct ocfs2_dinode *di = (struct ocfs2_dinode *) ac->ac_bh->b_data;
2134e49e2767SMark Fasheh
2135e49e2767SMark Fasheh /*
2136e49e2767SMark Fasheh * Since di_blkno is being passed back in, we check for any
2137e49e2767SMark Fasheh * inconsistencies which may have happened between
2138e49e2767SMark Fasheh * calls. These are code bugs as di_blkno is not expected to
2139e49e2767SMark Fasheh * change once returned from ocfs2_find_new_inode_loc()
2140e49e2767SMark Fasheh */
2141e49e2767SMark Fasheh BUG_ON(res->sr_blkno != di_blkno);
2142e49e2767SMark Fasheh
2143e49e2767SMark Fasheh ret = ocfs2_read_group_descriptor(ac->ac_inode, di,
2144e49e2767SMark Fasheh res->sr_bg_stable_blkno, &bg_bh);
2145e49e2767SMark Fasheh if (ret) {
2146e49e2767SMark Fasheh mlog_errno(ret);
2147e49e2767SMark Fasheh goto out;
2148e49e2767SMark Fasheh }
2149e49e2767SMark Fasheh
2150e49e2767SMark Fasheh bg = (struct ocfs2_group_desc *) bg_bh->b_data;
2151e49e2767SMark Fasheh chain = le16_to_cpu(bg->bg_chain);
2152e49e2767SMark Fasheh
2153e49e2767SMark Fasheh ret = ocfs2_alloc_dinode_update_counts(ac->ac_inode, handle,
2154e49e2767SMark Fasheh ac->ac_bh, res->sr_bits,
2155e49e2767SMark Fasheh chain);
2156e49e2767SMark Fasheh if (ret) {
2157e49e2767SMark Fasheh mlog_errno(ret);
2158e49e2767SMark Fasheh goto out;
2159e49e2767SMark Fasheh }
2160e49e2767SMark Fasheh
2161e49e2767SMark Fasheh ret = ocfs2_block_group_set_bits(handle,
2162e49e2767SMark Fasheh ac->ac_inode,
2163e49e2767SMark Fasheh bg,
2164e49e2767SMark Fasheh bg_bh,
2165e49e2767SMark Fasheh res->sr_bit_offset,
2166e49e2767SMark Fasheh res->sr_bits);
2167e49e2767SMark Fasheh if (ret < 0) {
2168db66c715SYounger Liu ocfs2_rollback_alloc_dinode_counts(ac->ac_inode,
2169db66c715SYounger Liu ac->ac_bh, res->sr_bits, chain);
2170e49e2767SMark Fasheh mlog_errno(ret);
2171e49e2767SMark Fasheh goto out;
2172e49e2767SMark Fasheh }
2173e49e2767SMark Fasheh
21742f73e135STao Ma trace_ocfs2_claim_new_inode_at_loc((unsigned long long)di_blkno,
21752f73e135STao Ma res->sr_bits);
2176e49e2767SMark Fasheh
2177e49e2767SMark Fasheh atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
2178e49e2767SMark Fasheh
2179e49e2767SMark Fasheh BUG_ON(res->sr_bits != 1);
2180e49e2767SMark Fasheh
2181e49e2767SMark Fasheh *suballoc_loc = res->sr_bg_blkno;
2182e49e2767SMark Fasheh *suballoc_bit = res->sr_bit_offset;
2183e49e2767SMark Fasheh ac->ac_bits_given++;
2184e49e2767SMark Fasheh ocfs2_save_inode_ac_group(dir, ac);
2185e49e2767SMark Fasheh
2186e49e2767SMark Fasheh out:
2187e49e2767SMark Fasheh brelse(bg_bh);
2188e49e2767SMark Fasheh
2189e49e2767SMark Fasheh return ret;
2190e49e2767SMark Fasheh }
2191e49e2767SMark Fasheh
ocfs2_claim_new_inode(handle_t * handle,struct inode * dir,struct buffer_head * parent_fe_bh,struct ocfs2_alloc_context * ac,u64 * suballoc_loc,u16 * suballoc_bit,u64 * fe_blkno)21921ed9b777SJoel Becker int ocfs2_claim_new_inode(handle_t *handle,
219313821151STao Ma struct inode *dir,
219413821151STao Ma struct buffer_head *parent_fe_bh,
2195ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac,
21962b6cb576SJoel Becker u64 *suballoc_loc,
2197ccd979bdSMark Fasheh u16 *suballoc_bit,
2198ccd979bdSMark Fasheh u64 *fe_blkno)
2199ccd979bdSMark Fasheh {
2200ccd979bdSMark Fasheh int status;
22012b6cb576SJoel Becker struct ocfs2_suballoc_result res;
2202ccd979bdSMark Fasheh
2203ccd979bdSMark Fasheh BUG_ON(!ac);
2204ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_given != 0);
2205ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_wanted != 1);
2206ccd979bdSMark Fasheh BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
2207ccd979bdSMark Fasheh
220813821151STao Ma ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
220913821151STao Ma
2210aa8f8e93SJoel Becker status = ocfs2_claim_suballoc_bits(ac,
2211da5cbf2fSMark Fasheh handle,
2212ccd979bdSMark Fasheh 1,
2213ccd979bdSMark Fasheh 1,
22147d1fe093SJoel Becker &res);
2215ccd979bdSMark Fasheh if (status < 0) {
2216ccd979bdSMark Fasheh mlog_errno(status);
2217ccd979bdSMark Fasheh goto bail;
2218ccd979bdSMark Fasheh }
22191ed9b777SJoel Becker atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
2220ccd979bdSMark Fasheh
22217d1fe093SJoel Becker BUG_ON(res.sr_bits != 1);
2222ccd979bdSMark Fasheh
22232b6cb576SJoel Becker *suballoc_loc = res.sr_bg_blkno;
22247d1fe093SJoel Becker *suballoc_bit = res.sr_bit_offset;
2225ba206635SJoel Becker *fe_blkno = res.sr_blkno;
2226ccd979bdSMark Fasheh ac->ac_bits_given++;
222713821151STao Ma ocfs2_save_inode_ac_group(dir, ac);
2228ccd979bdSMark Fasheh status = 0;
2229ccd979bdSMark Fasheh bail:
2230c1e8d35eSTao Ma if (status)
2231c1e8d35eSTao Ma mlog_errno(status);
2232ccd979bdSMark Fasheh return status;
2233ccd979bdSMark Fasheh }
2234ccd979bdSMark Fasheh
2235ccd979bdSMark Fasheh /* translate a group desc. blkno and it's bitmap offset into
2236ccd979bdSMark Fasheh * disk cluster offset. */
ocfs2_desc_bitmap_to_cluster_off(struct inode * inode,u64 bg_blkno,u16 bg_bit_off)2237ccd979bdSMark Fasheh static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
2238ccd979bdSMark Fasheh u64 bg_blkno,
2239ccd979bdSMark Fasheh u16 bg_bit_off)
2240ccd979bdSMark Fasheh {
2241ccd979bdSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2242ccd979bdSMark Fasheh u32 cluster = 0;
2243ccd979bdSMark Fasheh
2244ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2245ccd979bdSMark Fasheh
2246ccd979bdSMark Fasheh if (bg_blkno != osb->first_cluster_group_blkno)
2247ccd979bdSMark Fasheh cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
2248ccd979bdSMark Fasheh cluster += (u32) bg_bit_off;
2249ccd979bdSMark Fasheh return cluster;
2250ccd979bdSMark Fasheh }
2251ccd979bdSMark Fasheh
2252ccd979bdSMark Fasheh /* given a cluster offset, calculate which block group it belongs to
2253ccd979bdSMark Fasheh * and return that block offset. */
ocfs2_which_cluster_group(struct inode * inode,u32 cluster)2254d659072fSTao Ma u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
2255ccd979bdSMark Fasheh {
2256ccd979bdSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2257ccd979bdSMark Fasheh u32 group_no;
2258ccd979bdSMark Fasheh
2259ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2260ccd979bdSMark Fasheh
2261ccd979bdSMark Fasheh group_no = cluster / osb->bitmap_cpg;
2262ccd979bdSMark Fasheh if (!group_no)
2263ccd979bdSMark Fasheh return osb->first_cluster_group_blkno;
2264ccd979bdSMark Fasheh return ocfs2_clusters_to_blocks(inode->i_sb,
2265ccd979bdSMark Fasheh group_no * osb->bitmap_cpg);
2266ccd979bdSMark Fasheh }
2267ccd979bdSMark Fasheh
2268ccd979bdSMark Fasheh /* given the block number of a cluster start, calculate which cluster
2269ccd979bdSMark Fasheh * group and descriptor bitmap offset that corresponds to. */
ocfs2_block_to_cluster_group(struct inode * inode,u64 data_blkno,u64 * bg_blkno,u16 * bg_bit_off)2270ccd979bdSMark Fasheh static inline void ocfs2_block_to_cluster_group(struct inode *inode,
2271ccd979bdSMark Fasheh u64 data_blkno,
2272ccd979bdSMark Fasheh u64 *bg_blkno,
2273ccd979bdSMark Fasheh u16 *bg_bit_off)
2274ccd979bdSMark Fasheh {
2275ccd979bdSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2276ccd979bdSMark Fasheh u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
2277ccd979bdSMark Fasheh
2278ccd979bdSMark Fasheh BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2279ccd979bdSMark Fasheh
2280ccd979bdSMark Fasheh *bg_blkno = ocfs2_which_cluster_group(inode,
2281ccd979bdSMark Fasheh data_cluster);
2282ccd979bdSMark Fasheh
2283ccd979bdSMark Fasheh if (*bg_blkno == osb->first_cluster_group_blkno)
2284ccd979bdSMark Fasheh *bg_bit_off = (u16) data_cluster;
2285ccd979bdSMark Fasheh else
2286ccd979bdSMark Fasheh *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
2287ccd979bdSMark Fasheh data_blkno - *bg_blkno);
2288ccd979bdSMark Fasheh }
2289ccd979bdSMark Fasheh
2290ccd979bdSMark Fasheh /*
2291ccd979bdSMark Fasheh * min_bits - minimum contiguous chunk from this total allocation we
2292ccd979bdSMark Fasheh * can handle. set to what we asked for originally for a full
2293ccd979bdSMark Fasheh * contig. allocation, set to '1' to indicate we can deal with extents
2294ccd979bdSMark Fasheh * of any size.
2295ccd979bdSMark Fasheh */
__ocfs2_claim_clusters(handle_t * handle,struct ocfs2_alloc_context * ac,u32 min_clusters,u32 max_clusters,u32 * cluster_start,u32 * num_clusters)22961ed9b777SJoel Becker int __ocfs2_claim_clusters(handle_t *handle,
2297ccd979bdSMark Fasheh struct ocfs2_alloc_context *ac,
2298ccd979bdSMark Fasheh u32 min_clusters,
2299415cb800SMark Fasheh u32 max_clusters,
2300ccd979bdSMark Fasheh u32 *cluster_start,
2301ccd979bdSMark Fasheh u32 *num_clusters)
2302ccd979bdSMark Fasheh {
2303ccd979bdSMark Fasheh int status;
2304415cb800SMark Fasheh unsigned int bits_wanted = max_clusters;
2305ba206635SJoel Becker struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
23061ed9b777SJoel Becker struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
2307ccd979bdSMark Fasheh
2308ccd979bdSMark Fasheh BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
2309ccd979bdSMark Fasheh
2310ccd979bdSMark Fasheh BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
2311ccd979bdSMark Fasheh && ac->ac_which != OCFS2_AC_USE_MAIN);
2312ccd979bdSMark Fasheh
2313ccd979bdSMark Fasheh if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
231433d5d380SMark Fasheh WARN_ON(min_clusters > 1);
231533d5d380SMark Fasheh
2316ccd979bdSMark Fasheh status = ocfs2_claim_local_alloc_bits(osb,
2317ccd979bdSMark Fasheh handle,
2318ccd979bdSMark Fasheh ac,
2319ccd979bdSMark Fasheh bits_wanted,
2320ccd979bdSMark Fasheh cluster_start,
2321ccd979bdSMark Fasheh num_clusters);
2322ccd979bdSMark Fasheh if (!status)
2323ccd979bdSMark Fasheh atomic_inc(&osb->alloc_stats.local_data);
2324ccd979bdSMark Fasheh } else {
2325ccd979bdSMark Fasheh if (min_clusters > (osb->bitmap_cpg - 1)) {
2326ccd979bdSMark Fasheh /* The only paths asking for contiguousness
2327ccd979bdSMark Fasheh * should know about this already. */
23282fbe8d1eSSunil Mushran mlog(ML_ERROR, "minimum allocation requested %u exceeds "
23292fbe8d1eSSunil Mushran "group bitmap size %u!\n", min_clusters,
23302fbe8d1eSSunil Mushran osb->bitmap_cpg);
2331ccd979bdSMark Fasheh status = -ENOSPC;
2332ccd979bdSMark Fasheh goto bail;
2333ccd979bdSMark Fasheh }
2334ccd979bdSMark Fasheh /* clamp the current request down to a realistic size. */
2335ccd979bdSMark Fasheh if (bits_wanted > (osb->bitmap_cpg - 1))
2336ccd979bdSMark Fasheh bits_wanted = osb->bitmap_cpg - 1;
2337ccd979bdSMark Fasheh
2338aa8f8e93SJoel Becker status = ocfs2_claim_suballoc_bits(ac,
2339da5cbf2fSMark Fasheh handle,
2340ccd979bdSMark Fasheh bits_wanted,
2341ccd979bdSMark Fasheh min_clusters,
23427d1fe093SJoel Becker &res);
2343ccd979bdSMark Fasheh if (!status) {
2344ba206635SJoel Becker BUG_ON(res.sr_blkno); /* cluster alloc can't set */
2345ccd979bdSMark Fasheh *cluster_start =
2346ccd979bdSMark Fasheh ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
23477d1fe093SJoel Becker res.sr_bg_blkno,
23487d1fe093SJoel Becker res.sr_bit_offset);
2349ccd979bdSMark Fasheh atomic_inc(&osb->alloc_stats.bitmap_data);
23504711954eSTao Ma *num_clusters = res.sr_bits;
2351ccd979bdSMark Fasheh }
2352ccd979bdSMark Fasheh }
2353ccd979bdSMark Fasheh if (status < 0) {
2354ccd979bdSMark Fasheh if (status != -ENOSPC)
2355ccd979bdSMark Fasheh mlog_errno(status);
2356ccd979bdSMark Fasheh goto bail;
2357ccd979bdSMark Fasheh }
2358ccd979bdSMark Fasheh
23594711954eSTao Ma ac->ac_bits_given += *num_clusters;
2360ccd979bdSMark Fasheh
2361ccd979bdSMark Fasheh bail:
2362c1e8d35eSTao Ma if (status)
2363c1e8d35eSTao Ma mlog_errno(status);
2364ccd979bdSMark Fasheh return status;
2365ccd979bdSMark Fasheh }
2366ccd979bdSMark Fasheh
ocfs2_claim_clusters(handle_t * handle,struct ocfs2_alloc_context * ac,u32 min_clusters,u32 * cluster_start,u32 * num_clusters)23671ed9b777SJoel Becker int ocfs2_claim_clusters(handle_t *handle,
2368415cb800SMark Fasheh struct ocfs2_alloc_context *ac,
2369415cb800SMark Fasheh u32 min_clusters,
2370415cb800SMark Fasheh u32 *cluster_start,
2371415cb800SMark Fasheh u32 *num_clusters)
2372415cb800SMark Fasheh {
2373415cb800SMark Fasheh unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
2374415cb800SMark Fasheh
23751ed9b777SJoel Becker return __ocfs2_claim_clusters(handle, ac, min_clusters,
2376415cb800SMark Fasheh bits_wanted, cluster_start, num_clusters);
2377415cb800SMark Fasheh }
2378415cb800SMark Fasheh
ocfs2_block_group_clear_bits(handle_t * handle,struct inode * alloc_inode,struct ocfs2_group_desc * bg,struct buffer_head * group_bh,unsigned int bit_off,unsigned int num_bits,void (* undo_fn)(unsigned int bit,unsigned long * bmap))2379b4414eeaSMark Fasheh static int ocfs2_block_group_clear_bits(handle_t *handle,
2380ccd979bdSMark Fasheh struct inode *alloc_inode,
2381ccd979bdSMark Fasheh struct ocfs2_group_desc *bg,
2382ccd979bdSMark Fasheh struct buffer_head *group_bh,
2383ccd979bdSMark Fasheh unsigned int bit_off,
2384b4414eeaSMark Fasheh unsigned int num_bits,
2385b4414eeaSMark Fasheh void (*undo_fn)(unsigned int bit,
2386b4414eeaSMark Fasheh unsigned long *bmap))
2387ccd979bdSMark Fasheh {
2388ccd979bdSMark Fasheh int status;
2389ccd979bdSMark Fasheh unsigned int tmp;
2390ccd979bdSMark Fasheh struct ocfs2_group_desc *undo_bg = NULL;
239146417064SThomas Gleixner struct journal_head *jh;
2392ccd979bdSMark Fasheh
239342035306SJoel Becker /* The caller got this descriptor from
239442035306SJoel Becker * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
239542035306SJoel Becker BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
2396ccd979bdSMark Fasheh
23972f73e135STao Ma trace_ocfs2_block_group_clear_bits(bit_off, num_bits);
2398ccd979bdSMark Fasheh
2399b4414eeaSMark Fasheh BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
24000cf2f763SJoel Becker status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
2401b4414eeaSMark Fasheh group_bh,
2402b4414eeaSMark Fasheh undo_fn ?
2403b4414eeaSMark Fasheh OCFS2_JOURNAL_ACCESS_UNDO :
2404b4414eeaSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE);
2405ccd979bdSMark Fasheh if (status < 0) {
2406ccd979bdSMark Fasheh mlog_errno(status);
2407ccd979bdSMark Fasheh goto bail;
2408ccd979bdSMark Fasheh }
2409ccd979bdSMark Fasheh
241046417064SThomas Gleixner jh = bh2jh(group_bh);
2411b4414eeaSMark Fasheh if (undo_fn) {
241246417064SThomas Gleixner spin_lock(&jh->b_state_lock);
241346417064SThomas Gleixner undo_bg = (struct ocfs2_group_desc *) jh->b_committed_data;
241494e41ecfSSunil Mushran BUG_ON(!undo_bg);
241594e41ecfSSunil Mushran }
2416ccd979bdSMark Fasheh
2417ccd979bdSMark Fasheh tmp = num_bits;
2418ccd979bdSMark Fasheh while(tmp--) {
2419ccd979bdSMark Fasheh ocfs2_clear_bit((bit_off + tmp),
2420ccd979bdSMark Fasheh (unsigned long *) bg->bg_bitmap);
2421b4414eeaSMark Fasheh if (undo_fn)
2422b4414eeaSMark Fasheh undo_fn(bit_off + tmp,
2423ccd979bdSMark Fasheh (unsigned long *) undo_bg->bg_bitmap);
2424ccd979bdSMark Fasheh }
2425ccd979bdSMark Fasheh le16_add_cpu(&bg->bg_free_bits_count, num_bits);
24269b5cd10eSSrinivas Eeda if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) {
2427e75ed71bSChangwei Ge if (undo_fn)
242846417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
24297ecef14aSJoe Perches return ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit count %u but claims %u are freed. num_bits %d\n",
24309b5cd10eSSrinivas Eeda (unsigned long long)le64_to_cpu(bg->bg_blkno),
24319b5cd10eSSrinivas Eeda le16_to_cpu(bg->bg_bits),
24327ecef14aSJoe Perches le16_to_cpu(bg->bg_free_bits_count),
24337ecef14aSJoe Perches num_bits);
24349b5cd10eSSrinivas Eeda }
2435ccd979bdSMark Fasheh
2436b4414eeaSMark Fasheh if (undo_fn)
243746417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
243894e41ecfSSunil Mushran
2439ec20cec7SJoel Becker ocfs2_journal_dirty(handle, group_bh);
2440ccd979bdSMark Fasheh bail:
2441ccd979bdSMark Fasheh return status;
2442ccd979bdSMark Fasheh }
2443ccd979bdSMark Fasheh
2444ccd979bdSMark Fasheh /*
2445ccd979bdSMark Fasheh * expects the suballoc inode to already be locked.
2446ccd979bdSMark Fasheh */
_ocfs2_free_suballoc_bits(handle_t * handle,struct inode * alloc_inode,struct buffer_head * alloc_bh,unsigned int start_bit,u64 bg_blkno,unsigned int count,void (* undo_fn)(unsigned int bit,unsigned long * bitmap))2447b4414eeaSMark Fasheh static int _ocfs2_free_suballoc_bits(handle_t *handle,
2448ccd979bdSMark Fasheh struct inode *alloc_inode,
2449ccd979bdSMark Fasheh struct buffer_head *alloc_bh,
2450ccd979bdSMark Fasheh unsigned int start_bit,
2451ccd979bdSMark Fasheh u64 bg_blkno,
2452b4414eeaSMark Fasheh unsigned int count,
2453b4414eeaSMark Fasheh void (*undo_fn)(unsigned int bit,
2454b4414eeaSMark Fasheh unsigned long *bitmap))
2455ccd979bdSMark Fasheh {
2456ccd979bdSMark Fasheh int status = 0;
2457ccd979bdSMark Fasheh u32 tmp_used;
2458ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
2459ccd979bdSMark Fasheh struct ocfs2_chain_list *cl = &fe->id2.i_chain;
2460ccd979bdSMark Fasheh struct buffer_head *group_bh = NULL;
2461ccd979bdSMark Fasheh struct ocfs2_group_desc *group;
2462ccd979bdSMark Fasheh
246310995aa2SJoel Becker /* The alloc_bh comes from ocfs2_free_dinode() or
246410995aa2SJoel Becker * ocfs2_free_clusters(). The callers have all locked the
246510995aa2SJoel Becker * allocator and gotten alloc_bh from the lock call. This
246625985edcSLucas De Marchi * validates the dinode buffer. Any corruption that has happened
246710995aa2SJoel Becker * is a code bug. */
246810995aa2SJoel Becker BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
2469ccd979bdSMark Fasheh BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2470ccd979bdSMark Fasheh
24712f73e135STao Ma trace_ocfs2_free_suballoc_bits(
24722f73e135STao Ma (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno,
24732f73e135STao Ma (unsigned long long)bg_blkno,
24742f73e135STao Ma start_bit, count);
2475ccd979bdSMark Fasheh
247668f64d47SJoel Becker status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
247768f64d47SJoel Becker &group_bh);
2478ccd979bdSMark Fasheh if (status < 0) {
2479ccd979bdSMark Fasheh mlog_errno(status);
2480ccd979bdSMark Fasheh goto bail;
2481ccd979bdSMark Fasheh }
2482ccd979bdSMark Fasheh group = (struct ocfs2_group_desc *) group_bh->b_data;
248368f64d47SJoel Becker
2484ccd979bdSMark Fasheh BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
2485ccd979bdSMark Fasheh
2486ccd979bdSMark Fasheh status = ocfs2_block_group_clear_bits(handle, alloc_inode,
2487ccd979bdSMark Fasheh group, group_bh,
2488b4414eeaSMark Fasheh start_bit, count, undo_fn);
2489ccd979bdSMark Fasheh if (status < 0) {
2490ccd979bdSMark Fasheh mlog_errno(status);
2491ccd979bdSMark Fasheh goto bail;
2492ccd979bdSMark Fasheh }
2493ccd979bdSMark Fasheh
24940cf2f763SJoel Becker status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
24950cf2f763SJoel Becker alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
2496ccd979bdSMark Fasheh if (status < 0) {
2497ccd979bdSMark Fasheh mlog_errno(status);
2498a47726bcSJoseph Qi ocfs2_block_group_set_bits(handle, alloc_inode, group, group_bh,
2499a47726bcSJoseph Qi start_bit, count);
2500ccd979bdSMark Fasheh goto bail;
2501ccd979bdSMark Fasheh }
2502ccd979bdSMark Fasheh
2503ccd979bdSMark Fasheh le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2504ccd979bdSMark Fasheh count);
2505ccd979bdSMark Fasheh tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2506ccd979bdSMark Fasheh fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
2507ec20cec7SJoel Becker ocfs2_journal_dirty(handle, alloc_bh);
2508ccd979bdSMark Fasheh
2509ccd979bdSMark Fasheh bail:
2510ccd979bdSMark Fasheh brelse(group_bh);
2511ccd979bdSMark Fasheh return status;
2512ccd979bdSMark Fasheh }
2513ccd979bdSMark Fasheh
ocfs2_free_suballoc_bits(handle_t * handle,struct inode * alloc_inode,struct buffer_head * alloc_bh,unsigned int start_bit,u64 bg_blkno,unsigned int count)2514b4414eeaSMark Fasheh int ocfs2_free_suballoc_bits(handle_t *handle,
2515b4414eeaSMark Fasheh struct inode *alloc_inode,
2516b4414eeaSMark Fasheh struct buffer_head *alloc_bh,
2517b4414eeaSMark Fasheh unsigned int start_bit,
2518b4414eeaSMark Fasheh u64 bg_blkno,
2519b4414eeaSMark Fasheh unsigned int count)
2520b4414eeaSMark Fasheh {
2521b4414eeaSMark Fasheh return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
2522b4414eeaSMark Fasheh start_bit, bg_blkno, count, NULL);
2523b4414eeaSMark Fasheh }
2524b4414eeaSMark Fasheh
ocfs2_free_dinode(handle_t * handle,struct inode * inode_alloc_inode,struct buffer_head * inode_alloc_bh,struct ocfs2_dinode * di)25251fabe148SMark Fasheh int ocfs2_free_dinode(handle_t *handle,
2526ccd979bdSMark Fasheh struct inode *inode_alloc_inode,
2527ccd979bdSMark Fasheh struct buffer_head *inode_alloc_bh,
2528ccd979bdSMark Fasheh struct ocfs2_dinode *di)
2529ccd979bdSMark Fasheh {
2530ccd979bdSMark Fasheh u64 blk = le64_to_cpu(di->i_blkno);
2531ccd979bdSMark Fasheh u16 bit = le16_to_cpu(di->i_suballoc_bit);
2532ccd979bdSMark Fasheh u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2533ccd979bdSMark Fasheh
253474380c47STao Ma if (di->i_suballoc_loc)
253574380c47STao Ma bg_blkno = le64_to_cpu(di->i_suballoc_loc);
2536ccd979bdSMark Fasheh return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2537ccd979bdSMark Fasheh inode_alloc_bh, bit, bg_blkno, 1);
2538ccd979bdSMark Fasheh }
2539ccd979bdSMark Fasheh
_ocfs2_free_clusters(handle_t * handle,struct inode * bitmap_inode,struct buffer_head * bitmap_bh,u64 start_blk,unsigned int num_clusters,void (* undo_fn)(unsigned int bit,unsigned long * bitmap))2540b4414eeaSMark Fasheh static int _ocfs2_free_clusters(handle_t *handle,
2541ccd979bdSMark Fasheh struct inode *bitmap_inode,
2542ccd979bdSMark Fasheh struct buffer_head *bitmap_bh,
2543ccd979bdSMark Fasheh u64 start_blk,
2544b4414eeaSMark Fasheh unsigned int num_clusters,
2545b4414eeaSMark Fasheh void (*undo_fn)(unsigned int bit,
2546b4414eeaSMark Fasheh unsigned long *bitmap))
2547ccd979bdSMark Fasheh {
2548ccd979bdSMark Fasheh int status;
2549ccd979bdSMark Fasheh u16 bg_start_bit;
2550ccd979bdSMark Fasheh u64 bg_blkno;
2551ccd979bdSMark Fasheh
2552ccd979bdSMark Fasheh /* You can't ever have a contiguous set of clusters
2553ccd979bdSMark Fasheh * bigger than a block group bitmap so we never have to worry
2554ef6b689bSTao Ma * about looping on them.
2555ef6b689bSTao Ma * This is expensive. We can safely remove once this stuff has
2556ccd979bdSMark Fasheh * gotten tested really well. */
2557dd7b5f9dSChangwei Ge BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb,
2558dd7b5f9dSChangwei Ge ocfs2_blocks_to_clusters(bitmap_inode->i_sb,
2559dd7b5f9dSChangwei Ge start_blk)));
2560ccd979bdSMark Fasheh
2561ccd979bdSMark Fasheh
2562ccd979bdSMark Fasheh ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2563ccd979bdSMark Fasheh &bg_start_bit);
2564ccd979bdSMark Fasheh
25652f73e135STao Ma trace_ocfs2_free_clusters((unsigned long long)bg_blkno,
25662f73e135STao Ma (unsigned long long)start_blk,
25672f73e135STao Ma bg_start_bit, num_clusters);
2568ccd979bdSMark Fasheh
2569b4414eeaSMark Fasheh status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2570ccd979bdSMark Fasheh bg_start_bit, bg_blkno,
2571b4414eeaSMark Fasheh num_clusters, undo_fn);
25729c7af40bSMark Fasheh if (status < 0) {
2573ccd979bdSMark Fasheh mlog_errno(status);
25749c7af40bSMark Fasheh goto out;
25759c7af40bSMark Fasheh }
2576ccd979bdSMark Fasheh
25779c7af40bSMark Fasheh ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
25789c7af40bSMark Fasheh num_clusters);
25799c7af40bSMark Fasheh
25809c7af40bSMark Fasheh out:
2581ccd979bdSMark Fasheh return status;
2582ccd979bdSMark Fasheh }
2583ccd979bdSMark Fasheh
ocfs2_free_clusters(handle_t * handle,struct inode * bitmap_inode,struct buffer_head * bitmap_bh,u64 start_blk,unsigned int num_clusters)2584b4414eeaSMark Fasheh int ocfs2_free_clusters(handle_t *handle,
2585b4414eeaSMark Fasheh struct inode *bitmap_inode,
2586b4414eeaSMark Fasheh struct buffer_head *bitmap_bh,
2587b4414eeaSMark Fasheh u64 start_blk,
2588b4414eeaSMark Fasheh unsigned int num_clusters)
2589b4414eeaSMark Fasheh {
2590b4414eeaSMark Fasheh return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2591b4414eeaSMark Fasheh start_blk, num_clusters,
2592b4414eeaSMark Fasheh _ocfs2_set_bit);
2593b4414eeaSMark Fasheh }
2594b4414eeaSMark Fasheh
2595b4414eeaSMark Fasheh /*
2596b4414eeaSMark Fasheh * Give never-used clusters back to the global bitmap. We don't need
2597b4414eeaSMark Fasheh * to protect these bits in the undo buffer.
2598b4414eeaSMark Fasheh */
ocfs2_release_clusters(handle_t * handle,struct inode * bitmap_inode,struct buffer_head * bitmap_bh,u64 start_blk,unsigned int num_clusters)2599b4414eeaSMark Fasheh int ocfs2_release_clusters(handle_t *handle,
2600b4414eeaSMark Fasheh struct inode *bitmap_inode,
2601b4414eeaSMark Fasheh struct buffer_head *bitmap_bh,
2602b4414eeaSMark Fasheh u64 start_blk,
2603b4414eeaSMark Fasheh unsigned int num_clusters)
2604b4414eeaSMark Fasheh {
2605b4414eeaSMark Fasheh return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2606b4414eeaSMark Fasheh start_blk, num_clusters,
2607b4414eeaSMark Fasheh _ocfs2_clear_bit);
2608b4414eeaSMark Fasheh }
2609b4414eeaSMark Fasheh
2610e7d4cb6bSTao Ma /*
2611e7d4cb6bSTao Ma * For a given allocation, determine which allocators will need to be
2612e7d4cb6bSTao Ma * accessed, and lock them, reserving the appropriate number of bits.
2613e7d4cb6bSTao Ma *
2614e7d4cb6bSTao Ma * Sparse file systems call this from ocfs2_write_begin_nolock()
2615e7d4cb6bSTao Ma * and ocfs2_allocate_unwritten_extents().
2616e7d4cb6bSTao Ma *
2617e7d4cb6bSTao Ma * File systems which don't support holes call this from
2618e7d4cb6bSTao Ma * ocfs2_extend_allocation().
2619e7d4cb6bSTao Ma */
ocfs2_lock_allocators(struct inode * inode,struct ocfs2_extent_tree * et,u32 clusters_to_add,u32 extents_to_split,struct ocfs2_alloc_context ** data_ac,struct ocfs2_alloc_context ** meta_ac)2620f99b9b7cSJoel Becker int ocfs2_lock_allocators(struct inode *inode,
2621f99b9b7cSJoel Becker struct ocfs2_extent_tree *et,
2622e7d4cb6bSTao Ma u32 clusters_to_add, u32 extents_to_split,
2623e7d4cb6bSTao Ma struct ocfs2_alloc_context **data_ac,
2624f99b9b7cSJoel Becker struct ocfs2_alloc_context **meta_ac)
2625e7d4cb6bSTao Ma {
2626e7d4cb6bSTao Ma int ret = 0, num_free_extents;
2627e7d4cb6bSTao Ma unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2628e7d4cb6bSTao Ma struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2629e7d4cb6bSTao Ma
2630e7d4cb6bSTao Ma *meta_ac = NULL;
2631e7d4cb6bSTao Ma if (data_ac)
2632e7d4cb6bSTao Ma *data_ac = NULL;
2633e7d4cb6bSTao Ma
2634e7d4cb6bSTao Ma BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2635e7d4cb6bSTao Ma
2636964f14a0SJun Piao num_free_extents = ocfs2_num_free_extents(et);
2637e7d4cb6bSTao Ma if (num_free_extents < 0) {
2638e7d4cb6bSTao Ma ret = num_free_extents;
2639e7d4cb6bSTao Ma mlog_errno(ret);
2640e7d4cb6bSTao Ma goto out;
2641e7d4cb6bSTao Ma }
2642e7d4cb6bSTao Ma
2643e7d4cb6bSTao Ma /*
2644e7d4cb6bSTao Ma * Sparse allocation file systems need to be more conservative
2645e7d4cb6bSTao Ma * with reserving room for expansion - the actual allocation
2646e7d4cb6bSTao Ma * happens while we've got a journal handle open so re-taking
2647e7d4cb6bSTao Ma * a cluster lock (because we ran out of room for another
2648e7d4cb6bSTao Ma * extent) will violate ordering rules.
2649e7d4cb6bSTao Ma *
2650e7d4cb6bSTao Ma * Most of the time we'll only be seeing this 1 cluster at a time
2651e7d4cb6bSTao Ma * anyway.
2652e7d4cb6bSTao Ma *
2653e7d4cb6bSTao Ma * Always lock for any unwritten extents - we might want to
2654e7d4cb6bSTao Ma * add blocks during a split.
2655e7d4cb6bSTao Ma */
2656e7d4cb6bSTao Ma if (!num_free_extents ||
2657e7d4cb6bSTao Ma (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
2658f99b9b7cSJoel Becker ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
2659e7d4cb6bSTao Ma if (ret < 0) {
2660e7d4cb6bSTao Ma if (ret != -ENOSPC)
2661e7d4cb6bSTao Ma mlog_errno(ret);
2662e7d4cb6bSTao Ma goto out;
2663e7d4cb6bSTao Ma }
2664e7d4cb6bSTao Ma }
2665e7d4cb6bSTao Ma
2666e7d4cb6bSTao Ma if (clusters_to_add == 0)
2667e7d4cb6bSTao Ma goto out;
2668e7d4cb6bSTao Ma
2669e7d4cb6bSTao Ma ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2670e7d4cb6bSTao Ma if (ret < 0) {
2671e7d4cb6bSTao Ma if (ret != -ENOSPC)
2672e7d4cb6bSTao Ma mlog_errno(ret);
2673e7d4cb6bSTao Ma goto out;
2674e7d4cb6bSTao Ma }
2675e7d4cb6bSTao Ma
2676e7d4cb6bSTao Ma out:
2677e7d4cb6bSTao Ma if (ret) {
2678e7d4cb6bSTao Ma if (*meta_ac) {
2679e7d4cb6bSTao Ma ocfs2_free_alloc_context(*meta_ac);
2680e7d4cb6bSTao Ma *meta_ac = NULL;
2681e7d4cb6bSTao Ma }
2682e7d4cb6bSTao Ma
2683e7d4cb6bSTao Ma /*
2684e7d4cb6bSTao Ma * We cannot have an error and a non null *data_ac.
2685e7d4cb6bSTao Ma */
2686e7d4cb6bSTao Ma }
2687e7d4cb6bSTao Ma
2688e7d4cb6bSTao Ma return ret;
2689e7d4cb6bSTao Ma }
26906ca497a8Swengang wang
26916ca497a8Swengang wang /*
26926ca497a8Swengang wang * Read the inode specified by blkno to get suballoc_slot and
26936ca497a8Swengang wang * suballoc_bit.
26946ca497a8Swengang wang */
ocfs2_get_suballoc_slot_bit(struct ocfs2_super * osb,u64 blkno,u16 * suballoc_slot,u64 * group_blkno,u16 * suballoc_bit)26956ca497a8Swengang wang static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
2696889f004aSTao Ma u16 *suballoc_slot, u64 *group_blkno,
2697889f004aSTao Ma u16 *suballoc_bit)
26986ca497a8Swengang wang {
26996ca497a8Swengang wang int status;
27006ca497a8Swengang wang struct buffer_head *inode_bh = NULL;
27016ca497a8Swengang wang struct ocfs2_dinode *inode_fe;
27026ca497a8Swengang wang
27032f73e135STao Ma trace_ocfs2_get_suballoc_slot_bit((unsigned long long)blkno);
27046ca497a8Swengang wang
27056ca497a8Swengang wang /* dirty read disk */
27066ca497a8Swengang wang status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
27076ca497a8Swengang wang if (status < 0) {
27085b09b507SJoel Becker mlog(ML_ERROR, "read block %llu failed %d\n",
27095b09b507SJoel Becker (unsigned long long)blkno, status);
27106ca497a8Swengang wang goto bail;
27116ca497a8Swengang wang }
27126ca497a8Swengang wang
27136ca497a8Swengang wang inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
27146ca497a8Swengang wang if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
27155b09b507SJoel Becker mlog(ML_ERROR, "invalid inode %llu requested\n",
27165b09b507SJoel Becker (unsigned long long)blkno);
27176ca497a8Swengang wang status = -EINVAL;
27186ca497a8Swengang wang goto bail;
27196ca497a8Swengang wang }
27206ca497a8Swengang wang
27210fba8137STao Ma if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
27226ca497a8Swengang wang (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
27236ca497a8Swengang wang mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
27245b09b507SJoel Becker (unsigned long long)blkno,
27255b09b507SJoel Becker (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
27266ca497a8Swengang wang status = -EINVAL;
27276ca497a8Swengang wang goto bail;
27286ca497a8Swengang wang }
27296ca497a8Swengang wang
27306ca497a8Swengang wang if (suballoc_slot)
27316ca497a8Swengang wang *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
27326ca497a8Swengang wang if (suballoc_bit)
27336ca497a8Swengang wang *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
2734889f004aSTao Ma if (group_blkno)
2735889f004aSTao Ma *group_blkno = le64_to_cpu(inode_fe->i_suballoc_loc);
27366ca497a8Swengang wang
27376ca497a8Swengang wang bail:
27386ca497a8Swengang wang brelse(inode_bh);
27396ca497a8Swengang wang
2740c1e8d35eSTao Ma if (status)
2741c1e8d35eSTao Ma mlog_errno(status);
27426ca497a8Swengang wang return status;
27436ca497a8Swengang wang }
27446ca497a8Swengang wang
27456ca497a8Swengang wang /*
27466ca497a8Swengang wang * test whether bit is SET in allocator bitmap or not. on success, 0
27476ca497a8Swengang wang * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
27486ca497a8Swengang wang * is returned and *res is meaningless. Call this after you have
27496ca497a8Swengang wang * cluster locked against suballoc, or you may get a result based on
27506ca497a8Swengang wang * non-up2date contents
27516ca497a8Swengang wang */
ocfs2_test_suballoc_bit(struct ocfs2_super * osb,struct inode * suballoc,struct buffer_head * alloc_bh,u64 group_blkno,u64 blkno,u16 bit,int * res)27526ca497a8Swengang wang static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
27536ca497a8Swengang wang struct inode *suballoc,
2754889f004aSTao Ma struct buffer_head *alloc_bh,
2755889f004aSTao Ma u64 group_blkno, u64 blkno,
27566ca497a8Swengang wang u16 bit, int *res)
27576ca497a8Swengang wang {
2758abf1b3cbSTao Ma struct ocfs2_dinode *alloc_di;
27596ca497a8Swengang wang struct ocfs2_group_desc *group;
27606ca497a8Swengang wang struct buffer_head *group_bh = NULL;
27616ca497a8Swengang wang u64 bg_blkno;
27626ca497a8Swengang wang int status;
27636ca497a8Swengang wang
27642f73e135STao Ma trace_ocfs2_test_suballoc_bit((unsigned long long)blkno,
27655b09b507SJoel Becker (unsigned int)bit);
27666ca497a8Swengang wang
2767abf1b3cbSTao Ma alloc_di = (struct ocfs2_dinode *)alloc_bh->b_data;
2768abf1b3cbSTao Ma if ((bit + 1) > ocfs2_bits_per_group(&alloc_di->id2.i_chain)) {
27696ca497a8Swengang wang mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
27706ca497a8Swengang wang (unsigned int)bit,
2771abf1b3cbSTao Ma ocfs2_bits_per_group(&alloc_di->id2.i_chain));
27726ca497a8Swengang wang status = -EINVAL;
27736ca497a8Swengang wang goto bail;
27746ca497a8Swengang wang }
27756ca497a8Swengang wang
2776889f004aSTao Ma bg_blkno = group_blkno ? group_blkno :
2777889f004aSTao Ma ocfs2_which_suballoc_group(blkno, bit);
2778abf1b3cbSTao Ma status = ocfs2_read_group_descriptor(suballoc, alloc_di, bg_blkno,
27796ca497a8Swengang wang &group_bh);
27806ca497a8Swengang wang if (status < 0) {
27815b09b507SJoel Becker mlog(ML_ERROR, "read group %llu failed %d\n",
27825b09b507SJoel Becker (unsigned long long)bg_blkno, status);
27836ca497a8Swengang wang goto bail;
27846ca497a8Swengang wang }
27856ca497a8Swengang wang
27866ca497a8Swengang wang group = (struct ocfs2_group_desc *) group_bh->b_data;
27876ca497a8Swengang wang *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
27886ca497a8Swengang wang
27896ca497a8Swengang wang bail:
27906ca497a8Swengang wang brelse(group_bh);
27916ca497a8Swengang wang
2792c1e8d35eSTao Ma if (status)
2793c1e8d35eSTao Ma mlog_errno(status);
27946ca497a8Swengang wang return status;
27956ca497a8Swengang wang }
27966ca497a8Swengang wang
27976ca497a8Swengang wang /*
27986ca497a8Swengang wang * Test if the bit representing this inode (blkno) is set in the
27996ca497a8Swengang wang * suballocator.
28006ca497a8Swengang wang *
28016ca497a8Swengang wang * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
28026ca497a8Swengang wang *
28036ca497a8Swengang wang * In the event of failure, a negative value is returned and *res is
28046ca497a8Swengang wang * meaningless.
28056ca497a8Swengang wang *
28066ca497a8Swengang wang * Callers must make sure to hold nfs_sync_lock to prevent
28076ca497a8Swengang wang * ocfs2_delete_inode() on another node from accessing the same
28086ca497a8Swengang wang * suballocator concurrently.
28096ca497a8Swengang wang */
ocfs2_test_inode_bit(struct ocfs2_super * osb,u64 blkno,int * res)28106ca497a8Swengang wang int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
28116ca497a8Swengang wang {
28126ca497a8Swengang wang int status;
2813889f004aSTao Ma u64 group_blkno = 0;
28146ca497a8Swengang wang u16 suballoc_bit = 0, suballoc_slot = 0;
28156ca497a8Swengang wang struct inode *inode_alloc_inode;
28166ca497a8Swengang wang struct buffer_head *alloc_bh = NULL;
28176ca497a8Swengang wang
28182f73e135STao Ma trace_ocfs2_test_inode_bit((unsigned long long)blkno);
28196ca497a8Swengang wang
28206ca497a8Swengang wang status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
2821889f004aSTao Ma &group_blkno, &suballoc_bit);
28226ca497a8Swengang wang if (status < 0) {
28236ca497a8Swengang wang mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
28246ca497a8Swengang wang goto bail;
28256ca497a8Swengang wang }
28266ca497a8Swengang wang
2827e5a15e17SJunxiao Bi if (suballoc_slot == (u16)OCFS2_INVALID_SLOT)
2828e5a15e17SJunxiao Bi inode_alloc_inode = ocfs2_get_system_file_inode(osb,
2829e5a15e17SJunxiao Bi GLOBAL_INODE_ALLOC_SYSTEM_INODE, suballoc_slot);
2830e5a15e17SJunxiao Bi else
2831e5a15e17SJunxiao Bi inode_alloc_inode = ocfs2_get_system_file_inode(osb,
2832e5a15e17SJunxiao Bi INODE_ALLOC_SYSTEM_INODE, suballoc_slot);
28336ca497a8Swengang wang if (!inode_alloc_inode) {
28346ca497a8Swengang wang /* the error code could be inaccurate, but we are not able to
28356ca497a8Swengang wang * get the correct one. */
28366ca497a8Swengang wang status = -EINVAL;
28376ca497a8Swengang wang mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
28386ca497a8Swengang wang (u32)suballoc_slot);
28396ca497a8Swengang wang goto bail;
28406ca497a8Swengang wang }
28416ca497a8Swengang wang
28425955102cSAl Viro inode_lock(inode_alloc_inode);
28436ca497a8Swengang wang status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
28446ca497a8Swengang wang if (status < 0) {
28455955102cSAl Viro inode_unlock(inode_alloc_inode);
28467dc3e839Sjiangyiwen iput(inode_alloc_inode);
28476ca497a8Swengang wang mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
28486ca497a8Swengang wang (u32)suballoc_slot, status);
28496ca497a8Swengang wang goto bail;
28506ca497a8Swengang wang }
28516ca497a8Swengang wang
28526ca497a8Swengang wang status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
2853889f004aSTao Ma group_blkno, blkno, suballoc_bit, res);
28546ca497a8Swengang wang if (status < 0)
28556ca497a8Swengang wang mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
28566ca497a8Swengang wang
28576ca497a8Swengang wang ocfs2_inode_unlock(inode_alloc_inode, 0);
28585955102cSAl Viro inode_unlock(inode_alloc_inode);
28596ca497a8Swengang wang
28606ca497a8Swengang wang iput(inode_alloc_inode);
28616ca497a8Swengang wang brelse(alloc_bh);
28626ca497a8Swengang wang bail:
2863c1e8d35eSTao Ma if (status)
2864c1e8d35eSTao Ma mlog_errno(status);
28656ca497a8Swengang wang return status;
28666ca497a8Swengang wang }
2867