blob: d8f5f6ce99dc880f0bf9422c98baf773ba2fc791 [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * suballoc.c
5 *
6 * metadata alloc and free
7 * Inspired by ext3 block groups.
8 *
9 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
25 */
26
27#include <linux/fs.h>
28#include <linux/types.h>
29#include <linux/slab.h>
30#include <linux/highmem.h>
31
Mark Fashehccd979b2005-12-15 14:31:24 -080032#include <cluster/masklog.h>
33
34#include "ocfs2.h"
35
36#include "alloc.h"
Joel Beckerd6b32bb2008-10-17 14:55:01 -070037#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080038#include "dlmglue.h"
39#include "inode.h"
40#include "journal.h"
41#include "localalloc.h"
42#include "suballoc.h"
43#include "super.h"
44#include "sysfile.h"
45#include "uptodate.h"
Tao Ma2f73e132011-02-22 08:22:33 +080046#include "ocfs2_trace.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080047
48#include "buffer_head_io.h"
49
Tao Maffda89a2008-03-03 17:12:09 +080050#define NOT_ALLOC_NEW_GROUP 0
Tao Ma60ca81e2009-02-25 00:53:24 +080051#define ALLOC_NEW_GROUP 0x1
52#define ALLOC_GROUPS_FROM_GLOBAL 0x2
Tao Maffda89a2008-03-03 17:12:09 +080053
Tiger Yangb89c5422010-01-25 14:11:06 +080054#define OCFS2_MAX_TO_STEAL 1024
Tao Ma4d0ddb22008-03-05 16:11:46 +080055
Joel Becker7d1fe092010-04-13 14:30:19 +080056struct ocfs2_suballoc_result {
Joel Becker2b6cb572010-03-26 10:09:15 +080057 u64 sr_bg_blkno; /* The bg we allocated from. Set
58 to 0 when a block group is
59 contiguous. */
Mark Fashehe49e2762010-08-13 15:15:17 -070060 u64 sr_bg_stable_blkno; /*
61 * Doesn't change, always
62 * set to target block
63 * group descriptor
64 * block.
65 */
Joel Beckerba206632010-03-26 10:08:59 +080066 u64 sr_blkno; /* The first allocated block */
Joel Becker7d1fe092010-04-13 14:30:19 +080067 unsigned int sr_bit_offset; /* The bit in the bg */
68 unsigned int sr_bits; /* How many bits we claimed */
69};
70
Mark Fashehb2b6ebf2010-08-26 13:06:50 -070071static u64 ocfs2_group_from_res(struct ocfs2_suballoc_result *res)
72{
73 if (res->sr_blkno == 0)
74 return 0;
75
76 if (res->sr_bg_blkno)
77 return res->sr_bg_blkno;
78
79 return ocfs2_which_suballoc_group(res->sr_blkno, res->sr_bit_offset);
80}
81
Mark Fashehccd979b2005-12-15 14:31:24 -080082static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
83static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
84static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
Mark Fasheh1fabe142006-10-09 18:11:45 -070085static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080086 struct inode *alloc_inode,
87 struct buffer_head *bg_bh,
88 u64 group_blkno,
Joel Becker798db352010-04-13 14:26:32 +080089 unsigned int group_clusters,
Mark Fashehccd979b2005-12-15 14:31:24 -080090 u16 my_chain,
91 struct ocfs2_chain_list *cl);
92static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
93 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -070094 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +080095 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +080096 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +080097 int flags);
Mark Fashehccd979b2005-12-15 14:31:24 -080098
Mark Fashehccd979b2005-12-15 14:31:24 -080099static int ocfs2_cluster_group_search(struct inode *inode,
100 struct buffer_head *group_bh,
101 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -0700102 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +0800103 struct ocfs2_suballoc_result *res);
Mark Fashehccd979b2005-12-15 14:31:24 -0800104static int ocfs2_block_group_search(struct inode *inode,
105 struct buffer_head *group_bh,
106 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -0700107 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +0800108 struct ocfs2_suballoc_result *res);
Joel Beckeraa8f8e92010-03-26 10:08:07 +0800109static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700110 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800111 u32 bits_wanted,
112 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +0800113 struct ocfs2_suballoc_result *res);
Mark Fashehccd979b2005-12-15 14:31:24 -0800114static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
115 int nr);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700116static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800117 struct inode *alloc_inode,
118 struct buffer_head *fe_bh,
119 struct buffer_head *bg_bh,
120 struct buffer_head *prev_bg_bh,
121 u16 chain);
122static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
123 u32 wanted);
Mark Fashehccd979b2005-12-15 14:31:24 -0800124static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
125 u64 bg_blkno,
126 u16 bg_bit_off);
Mark Fashehccd979b2005-12-15 14:31:24 -0800127static inline void ocfs2_block_to_cluster_group(struct inode *inode,
128 u64 data_blkno,
129 u64 *bg_blkno,
130 u16 *bg_bit_off);
Joel Becker1187c962008-09-03 20:03:39 -0700131static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
132 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +0800133 int flags,
Joel Becker1187c962008-09-03 20:03:39 -0700134 struct ocfs2_alloc_context **ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800135
Mark Fasheh9c7af402008-07-28 18:02:53 -0700136void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800137{
Mark Fashehda5cbf22006-10-06 18:34:35 -0700138 struct inode *inode = ac->ac_inode;
139
140 if (inode) {
141 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700142 ocfs2_inode_unlock(inode, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700143
Al Viro59551022016-01-22 15:40:57 -0500144 inode_unlock(inode);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700145
146 iput(inode);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800147 ac->ac_inode = NULL;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700148 }
Mark Fasheha81cb882008-10-07 14:25:16 -0700149 brelse(ac->ac_bh);
150 ac->ac_bh = NULL;
Mark Fashehe3b4a972009-12-07 13:16:07 -0800151 ac->ac_resv = NULL;
Joseph Qi46359292015-09-04 15:44:54 -0700152 kfree(ac->ac_find_loc_priv);
153 ac->ac_find_loc_priv = NULL;
Tao Ma4d0ddb22008-03-05 16:11:46 +0800154}
155
156void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
157{
158 ocfs2_free_ac_resource(ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800159 kfree(ac);
160}
161
162static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
163{
164 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
165}
166
Joel Becker57e3e792008-11-13 14:49:13 -0800167#define do_error(fmt, ...) \
Joe Perches7ecef142015-09-04 15:44:51 -0700168do { \
169 if (resize) \
170 mlog(ML_ERROR, fmt, ##__VA_ARGS__); \
171 else \
172 return ocfs2_error(sb, fmt, ##__VA_ARGS__); \
173} while (0)
Joel Becker57e3e792008-11-13 14:49:13 -0800174
Joel Becker970e4932008-11-13 14:49:19 -0800175static int ocfs2_validate_gd_self(struct super_block *sb,
176 struct buffer_head *bh,
Tao Ma78c37eb2010-03-03 11:26:27 +0800177 int resize)
Joel Becker970e4932008-11-13 14:49:19 -0800178{
179 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
180
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700181 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
Joe Perches7ecef142015-09-04 15:44:51 -0700182 do_error("Group descriptor #%llu has bad signature %.*s\n",
Joel Becker68f64d42008-11-13 14:49:14 -0800183 (unsigned long long)bh->b_blocknr, 7,
Joel Becker57e3e792008-11-13 14:49:13 -0800184 gd->bg_signature);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700185 }
186
Joel Becker68f64d42008-11-13 14:49:14 -0800187 if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
Joe Perches7ecef142015-09-04 15:44:51 -0700188 do_error("Group descriptor #%llu has an invalid bg_blkno of %llu\n",
Joel Becker68f64d42008-11-13 14:49:14 -0800189 (unsigned long long)bh->b_blocknr,
190 (unsigned long long)le64_to_cpu(gd->bg_blkno));
Joel Becker68f64d42008-11-13 14:49:14 -0800191 }
192
193 if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
Joe Perches7ecef142015-09-04 15:44:51 -0700194 do_error("Group descriptor #%llu has an invalid fs_generation of #%u\n",
Joel Becker68f64d42008-11-13 14:49:14 -0800195 (unsigned long long)bh->b_blocknr,
196 le32_to_cpu(gd->bg_generation));
Joel Becker68f64d42008-11-13 14:49:14 -0800197 }
198
Joel Becker970e4932008-11-13 14:49:19 -0800199 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
Joe Perches7ecef142015-09-04 15:44:51 -0700200 do_error("Group descriptor #%llu has bit count %u but claims that %u are free\n",
Joel Becker970e4932008-11-13 14:49:19 -0800201 (unsigned long long)bh->b_blocknr,
202 le16_to_cpu(gd->bg_bits),
203 le16_to_cpu(gd->bg_free_bits_count));
Joel Becker970e4932008-11-13 14:49:19 -0800204 }
205
206 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
Joe Perches7ecef142015-09-04 15:44:51 -0700207 do_error("Group descriptor #%llu has bit count %u but max bitmap bits of %u\n",
Joel Becker970e4932008-11-13 14:49:19 -0800208 (unsigned long long)bh->b_blocknr,
209 le16_to_cpu(gd->bg_bits),
210 8 * le16_to_cpu(gd->bg_size));
Joel Becker970e4932008-11-13 14:49:19 -0800211 }
212
213 return 0;
214}
215
216static int ocfs2_validate_gd_parent(struct super_block *sb,
217 struct ocfs2_dinode *di,
218 struct buffer_head *bh,
Tao Ma78c37eb2010-03-03 11:26:27 +0800219 int resize)
Joel Becker970e4932008-11-13 14:49:19 -0800220{
221 unsigned int max_bits;
222 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
223
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700224 if (di->i_blkno != gd->bg_parent_dinode) {
Joe Perches7ecef142015-09-04 15:44:51 -0700225 do_error("Group descriptor #%llu has bad parent pointer (%llu, expected %llu)\n",
Joel Becker68f64d42008-11-13 14:49:14 -0800226 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800227 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
228 (unsigned long long)le64_to_cpu(di->i_blkno));
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700229 }
230
231 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
232 if (le16_to_cpu(gd->bg_bits) > max_bits) {
Joe Perches7ecef142015-09-04 15:44:51 -0700233 do_error("Group descriptor #%llu has bit count of %u\n",
Joel Becker68f64d42008-11-13 14:49:14 -0800234 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800235 le16_to_cpu(gd->bg_bits));
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700236 }
237
Tao Ma78c37eb2010-03-03 11:26:27 +0800238 /* In resize, we may meet the case bg_chain == cl_next_free_rec. */
239 if ((le16_to_cpu(gd->bg_chain) >
240 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) ||
241 ((le16_to_cpu(gd->bg_chain) ==
242 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) {
Joe Perches7ecef142015-09-04 15:44:51 -0700243 do_error("Group descriptor #%llu has bad chain %u\n",
Joel Becker68f64d42008-11-13 14:49:14 -0800244 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800245 le16_to_cpu(gd->bg_chain));
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700246 }
247
Joel Becker970e4932008-11-13 14:49:19 -0800248 return 0;
249}
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700250
Joel Becker57e3e792008-11-13 14:49:13 -0800251#undef do_error
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700252
Joel Becker970e4932008-11-13 14:49:19 -0800253/*
254 * This version only prints errors. It does not fail the filesystem, and
255 * exists only for resize.
256 */
257int ocfs2_check_group_descriptor(struct super_block *sb,
258 struct ocfs2_dinode *di,
259 struct buffer_head *bh)
260{
261 int rc;
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700262 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
Joel Becker970e4932008-11-13 14:49:19 -0800263
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700264 BUG_ON(!buffer_uptodate(bh));
265
266 /*
267 * If the ecc fails, we return the error but otherwise
268 * leave the filesystem running. We know any error is
269 * local to this block.
270 */
271 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
Joel Becker13723d02008-10-17 19:25:01 -0700272 if (rc) {
273 mlog(ML_ERROR,
274 "Checksum failed for group descriptor %llu\n",
275 (unsigned long long)bh->b_blocknr);
276 } else
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700277 rc = ocfs2_validate_gd_self(sb, bh, 1);
Joel Becker970e4932008-11-13 14:49:19 -0800278 if (!rc)
279 rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
280
281 return rc;
282}
283
284static int ocfs2_validate_group_descriptor(struct super_block *sb,
285 struct buffer_head *bh)
286{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700287 int rc;
288 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
289
Tao Ma2f73e132011-02-22 08:22:33 +0800290 trace_ocfs2_validate_group_descriptor(
291 (unsigned long long)bh->b_blocknr);
Joel Becker970e4932008-11-13 14:49:19 -0800292
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700293 BUG_ON(!buffer_uptodate(bh));
294
295 /*
296 * If the ecc fails, we return the error but otherwise
297 * leave the filesystem running. We know any error is
298 * local to this block.
299 */
300 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
301 if (rc)
302 return rc;
303
304 /*
305 * Errors after here are fatal.
306 */
307
Joel Becker970e4932008-11-13 14:49:19 -0800308 return ocfs2_validate_gd_self(sb, bh, 0);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700309}
310
Joel Becker68f64d42008-11-13 14:49:14 -0800311int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
312 u64 gd_blkno, struct buffer_head **bh)
313{
314 int rc;
315 struct buffer_head *tmp = *bh;
316
Joel Becker8cb471e2009-02-10 20:00:41 -0800317 rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800318 ocfs2_validate_group_descriptor);
Joel Becker68f64d42008-11-13 14:49:14 -0800319 if (rc)
320 goto out;
321
Joel Becker970e4932008-11-13 14:49:19 -0800322 rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
Joel Becker68f64d42008-11-13 14:49:14 -0800323 if (rc) {
324 brelse(tmp);
325 goto out;
326 }
327
328 /* If ocfs2_read_block() got us a new bh, pass it up. */
329 if (!*bh)
330 *bh = tmp;
331
332out:
333 return rc;
334}
335
Joel Becker798db352010-04-13 14:26:32 +0800336static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb,
337 struct ocfs2_group_desc *bg,
338 struct ocfs2_chain_list *cl,
Tao Ma47dea422010-09-13 15:13:50 +0800339 u64 p_blkno, unsigned int clusters)
Joel Becker798db352010-04-13 14:26:32 +0800340{
341 struct ocfs2_extent_list *el = &bg->bg_list;
342 struct ocfs2_extent_rec *rec;
343
Tao Ma47119542010-04-22 14:09:15 +0800344 BUG_ON(!ocfs2_supports_discontig_bg(osb));
Joel Becker798db352010-04-13 14:26:32 +0800345 if (!el->l_next_free_rec)
346 el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb));
347 rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)];
Tao Ma47119542010-04-22 14:09:15 +0800348 rec->e_blkno = cpu_to_le64(p_blkno);
Joel Becker798db352010-04-13 14:26:32 +0800349 rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) /
350 le16_to_cpu(cl->cl_bpc));
Tao Ma47dea422010-09-13 15:13:50 +0800351 rec->e_leaf_clusters = cpu_to_le16(clusters);
Joel Becker798db352010-04-13 14:26:32 +0800352 le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc));
Tao Ma47119542010-04-22 14:09:15 +0800353 le16_add_cpu(&bg->bg_free_bits_count,
354 clusters * le16_to_cpu(cl->cl_bpc));
Joel Becker798db352010-04-13 14:26:32 +0800355 le16_add_cpu(&el->l_next_free_rec, 1);
356}
357
Mark Fasheh1fabe142006-10-09 18:11:45 -0700358static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800359 struct inode *alloc_inode,
360 struct buffer_head *bg_bh,
361 u64 group_blkno,
Joel Becker798db352010-04-13 14:26:32 +0800362 unsigned int group_clusters,
Mark Fashehccd979b2005-12-15 14:31:24 -0800363 u16 my_chain,
364 struct ocfs2_chain_list *cl)
365{
366 int status = 0;
Joel Becker798db352010-04-13 14:26:32 +0800367 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800368 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
369 struct super_block * sb = alloc_inode->i_sb;
370
Mark Fashehccd979b2005-12-15 14:31:24 -0800371 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
Joe Perches7ecef142015-09-04 15:44:51 -0700372 status = ocfs2_error(alloc_inode->i_sb,
373 "group block (%llu) != b_blocknr (%llu)\n",
374 (unsigned long long)group_blkno,
375 (unsigned long long) bg_bh->b_blocknr);
Mark Fashehccd979b2005-12-15 14:31:24 -0800376 goto bail;
377 }
378
Joel Becker13723d02008-10-17 19:25:01 -0700379 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -0800380 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700381 bg_bh,
382 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800383 if (status < 0) {
384 mlog_errno(status);
385 goto bail;
386 }
387
388 memset(bg, 0, sb->s_blocksize);
389 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
390 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
Tao Ma85718822010-04-13 14:38:06 +0800391 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1,
392 osb->s_feature_incompat));
Mark Fashehccd979b2005-12-15 14:31:24 -0800393 bg->bg_chain = cpu_to_le16(my_chain);
394 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
395 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
396 bg->bg_blkno = cpu_to_le64(group_blkno);
Joel Becker798db352010-04-13 14:26:32 +0800397 if (group_clusters == le16_to_cpu(cl->cl_cpg))
398 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
399 else
Tao Ma47119542010-04-22 14:09:15 +0800400 ocfs2_bg_discontig_add_extent(osb, bg, cl, group_blkno,
Joel Becker798db352010-04-13 14:26:32 +0800401 group_clusters);
402
Mark Fashehccd979b2005-12-15 14:31:24 -0800403 /* set the 1st bit in the bitmap to account for the descriptor block */
404 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
405 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
406
Joel Beckerec20cec2010-03-19 14:13:52 -0700407 ocfs2_journal_dirty(handle, bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800408
409 /* There is no need to zero out or otherwise initialize the
410 * other blocks in a group - All valid FS metadata in a block
411 * group stores the superblock fs_generation value at
412 * allocation time. */
413
414bail:
Tao Mac1e8d352011-03-07 16:43:21 +0800415 if (status)
416 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800417 return status;
418}
419
420static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
421{
422 u16 curr, best;
423
424 best = curr = 0;
425 while (curr < le16_to_cpu(cl->cl_count)) {
426 if (le32_to_cpu(cl->cl_recs[best].c_total) >
427 le32_to_cpu(cl->cl_recs[curr].c_total))
428 best = curr;
429 curr++;
430 }
431 return best;
432}
433
Joel Becker798db352010-04-13 14:26:32 +0800434static struct buffer_head *
435ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle,
436 struct inode *alloc_inode,
437 struct ocfs2_alloc_context *ac,
438 struct ocfs2_chain_list *cl)
439{
440 int status;
441 u32 bit_off, num_bits;
442 u64 bg_blkno;
443 struct buffer_head *bg_bh;
444 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
445
Joel Becker1ed9b772010-05-06 13:59:06 +0800446 status = ocfs2_claim_clusters(handle, ac,
Joel Becker798db352010-04-13 14:26:32 +0800447 le16_to_cpu(cl->cl_cpg), &bit_off,
448 &num_bits);
449 if (status < 0) {
450 if (status != -ENOSPC)
451 mlog_errno(status);
452 goto bail;
453 }
454
455 /* setup the group */
456 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Tao Ma2f73e132011-02-22 08:22:33 +0800457 trace_ocfs2_block_group_alloc_contig(
458 (unsigned long long)bg_blkno, alloc_rec);
Joel Becker798db352010-04-13 14:26:32 +0800459
460 bg_bh = sb_getblk(osb->sb, bg_blkno);
461 if (!bg_bh) {
Rui Xiang7391a292013-11-12 15:06:54 -0800462 status = -ENOMEM;
Joel Becker798db352010-04-13 14:26:32 +0800463 mlog_errno(status);
464 goto bail;
465 }
466 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
467
468 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
469 bg_blkno, num_bits, alloc_rec, cl);
470 if (status < 0) {
471 brelse(bg_bh);
472 mlog_errno(status);
473 }
474
475bail:
476 return status ? ERR_PTR(status) : bg_bh;
477}
478
479static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb,
480 handle_t *handle,
481 struct ocfs2_alloc_context *ac,
482 unsigned int min_bits,
483 u32 *bit_off, u32 *num_bits)
484{
Joel Becker18d3a982010-05-18 16:47:55 -0700485 int status = 0;
Joel Becker798db352010-04-13 14:26:32 +0800486
487 while (min_bits) {
Joel Becker1ed9b772010-05-06 13:59:06 +0800488 status = ocfs2_claim_clusters(handle, ac, min_bits,
Joel Becker798db352010-04-13 14:26:32 +0800489 bit_off, num_bits);
490 if (status != -ENOSPC)
491 break;
492
493 min_bits >>= 1;
494 }
495
496 return status;
497}
498
499static int ocfs2_block_group_grow_discontig(handle_t *handle,
500 struct inode *alloc_inode,
501 struct buffer_head *bg_bh,
502 struct ocfs2_alloc_context *ac,
503 struct ocfs2_chain_list *cl,
504 unsigned int min_bits)
505{
506 int status;
507 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
508 struct ocfs2_group_desc *bg =
509 (struct ocfs2_group_desc *)bg_bh->b_data;
Tao Ma47119542010-04-22 14:09:15 +0800510 unsigned int needed = le16_to_cpu(cl->cl_cpg) -
511 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
Joel Becker798db352010-04-13 14:26:32 +0800512 u32 p_cpos, clusters;
513 u64 p_blkno;
514 struct ocfs2_extent_list *el = &bg->bg_list;
515
516 status = ocfs2_journal_access_gd(handle,
517 INODE_CACHE(alloc_inode),
518 bg_bh,
519 OCFS2_JOURNAL_ACCESS_CREATE);
520 if (status < 0) {
521 mlog_errno(status);
522 goto bail;
523 }
524
525 while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) <
526 le16_to_cpu(el->l_count))) {
Joel Becker798db352010-04-13 14:26:32 +0800527 if (min_bits > needed)
528 min_bits = needed;
529 status = ocfs2_block_group_claim_bits(osb, handle, ac,
530 min_bits, &p_cpos,
531 &clusters);
532 if (status < 0) {
533 if (status != -ENOSPC)
534 mlog_errno(status);
535 goto bail;
536 }
537 p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos);
538 ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno,
539 clusters);
540
541 min_bits = clusters;
Tao Ma47119542010-04-22 14:09:15 +0800542 needed = le16_to_cpu(cl->cl_cpg) -
543 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
Joel Becker798db352010-04-13 14:26:32 +0800544 }
545
546 if (needed > 0) {
Tao Ma47119542010-04-22 14:09:15 +0800547 /*
548 * We have used up all the extent rec but can't fill up
549 * the cpg. So bail out.
550 */
551 status = -ENOSPC;
552 goto bail;
Joel Becker798db352010-04-13 14:26:32 +0800553 }
554
555 ocfs2_journal_dirty(handle, bg_bh);
556
557bail:
558 return status;
559}
560
Joel Becker8b06bc52010-03-26 10:09:29 +0800561static void ocfs2_bg_alloc_cleanup(handle_t *handle,
562 struct ocfs2_alloc_context *cluster_ac,
563 struct inode *alloc_inode,
564 struct buffer_head *bg_bh)
Joel Becker798db352010-04-13 14:26:32 +0800565{
Joel Becker8b06bc52010-03-26 10:09:29 +0800566 int i, ret;
Joel Becker798db352010-04-13 14:26:32 +0800567 struct ocfs2_group_desc *bg;
568 struct ocfs2_extent_list *el;
569 struct ocfs2_extent_rec *rec;
570
571 if (!bg_bh)
572 return;
573
574 bg = (struct ocfs2_group_desc *)bg_bh->b_data;
575 el = &bg->bg_list;
576 for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
577 rec = &el->l_recs[i];
Joel Becker8b06bc52010-03-26 10:09:29 +0800578 ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode,
579 cluster_ac->ac_bh,
580 le64_to_cpu(rec->e_blkno),
Al Viro72094e42012-04-13 12:30:02 -0400581 le16_to_cpu(rec->e_leaf_clusters));
Joel Becker8b06bc52010-03-26 10:09:29 +0800582 if (ret)
583 mlog_errno(ret);
584 /* Try all the clusters to free */
Joel Becker798db352010-04-13 14:26:32 +0800585 }
586
587 ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh);
588 brelse(bg_bh);
589}
590
591static struct buffer_head *
592ocfs2_block_group_alloc_discontig(handle_t *handle,
593 struct inode *alloc_inode,
594 struct ocfs2_alloc_context *ac,
Joel Becker8b06bc52010-03-26 10:09:29 +0800595 struct ocfs2_chain_list *cl)
Joel Becker798db352010-04-13 14:26:32 +0800596{
597 int status;
598 u32 bit_off, num_bits;
599 u64 bg_blkno;
600 unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1;
601 struct buffer_head *bg_bh = NULL;
602 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
603 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
604
Tao Ma47119542010-04-22 14:09:15 +0800605 if (!ocfs2_supports_discontig_bg(osb)) {
Joel Becker798db352010-04-13 14:26:32 +0800606 status = -ENOSPC;
607 goto bail;
608 }
609
Joel Becker8b06bc52010-03-26 10:09:29 +0800610 status = ocfs2_extend_trans(handle,
611 ocfs2_calc_bg_discontig_credits(osb->sb));
612 if (status) {
613 mlog_errno(status);
614 goto bail;
615 }
616
Joel Becker95ec0ad2010-03-26 10:10:08 +0800617 /*
618 * We're going to be grabbing from multiple cluster groups.
619 * We don't have enough credits to relink them all, and the
620 * cluster groups will be staying in cache for the duration of
621 * this operation.
622 */
Xiaowei.Hu309a85b2013-02-27 17:02:49 -0800623 ac->ac_disable_chain_relink = 1;
Joel Becker95ec0ad2010-03-26 10:10:08 +0800624
Joel Becker798db352010-04-13 14:26:32 +0800625 /* Claim the first region */
626 status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits,
627 &bit_off, &num_bits);
628 if (status < 0) {
629 if (status != -ENOSPC)
630 mlog_errno(status);
631 goto bail;
632 }
633 min_bits = num_bits;
634
635 /* setup the group */
636 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Tao Ma2f73e132011-02-22 08:22:33 +0800637 trace_ocfs2_block_group_alloc_discontig(
638 (unsigned long long)bg_blkno, alloc_rec);
Joel Becker798db352010-04-13 14:26:32 +0800639
640 bg_bh = sb_getblk(osb->sb, bg_blkno);
641 if (!bg_bh) {
Rui Xiang7391a292013-11-12 15:06:54 -0800642 status = -ENOMEM;
Joel Becker798db352010-04-13 14:26:32 +0800643 mlog_errno(status);
644 goto bail;
645 }
646 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
647
648 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
649 bg_blkno, num_bits, alloc_rec, cl);
650 if (status < 0) {
651 mlog_errno(status);
652 goto bail;
653 }
654
655 status = ocfs2_block_group_grow_discontig(handle, alloc_inode,
656 bg_bh, ac, cl, min_bits);
657 if (status)
658 mlog_errno(status);
659
660bail:
661 if (status)
Joel Becker8b06bc52010-03-26 10:09:29 +0800662 ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh);
Joel Becker798db352010-04-13 14:26:32 +0800663 return status ? ERR_PTR(status) : bg_bh;
664}
665
Mark Fashehccd979b2005-12-15 14:31:24 -0800666/*
667 * We expect the block group allocator to already be locked.
668 */
669static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
670 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -0700671 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +0800672 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +0800673 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800674 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800675{
676 int status, credits;
677 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
678 struct ocfs2_chain_list *cl;
679 struct ocfs2_alloc_context *ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700680 handle_t *handle = NULL;
Tao Ma47119542010-04-22 14:09:15 +0800681 u16 alloc_rec;
Mark Fashehccd979b2005-12-15 14:31:24 -0800682 struct buffer_head *bg_bh = NULL;
683 struct ocfs2_group_desc *bg;
684
685 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
686
Mark Fashehccd979b2005-12-15 14:31:24 -0800687 cl = &fe->id2.i_chain;
Joel Becker1187c962008-09-03 20:03:39 -0700688 status = ocfs2_reserve_clusters_with_limit(osb,
689 le16_to_cpu(cl->cl_cpg),
Tao Ma60ca81e2009-02-25 00:53:24 +0800690 max_block, flags, &ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800691 if (status < 0) {
692 if (status != -ENOSPC)
693 mlog_errno(status);
694 goto bail;
695 }
696
697 credits = ocfs2_calc_group_alloc_credits(osb->sb,
698 le16_to_cpu(cl->cl_cpg));
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700699 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800700 if (IS_ERR(handle)) {
701 status = PTR_ERR(handle);
702 handle = NULL;
703 mlog_errno(status);
704 goto bail;
705 }
706
Tao Mafeb473a2009-02-25 00:53:25 +0800707 if (last_alloc_group && *last_alloc_group != 0) {
Tao Ma2f73e132011-02-22 08:22:33 +0800708 trace_ocfs2_block_group_alloc(
709 (unsigned long long)*last_alloc_group);
Tao Mafeb473a2009-02-25 00:53:25 +0800710 ac->ac_last_group = *last_alloc_group;
711 }
Joel Becker798db352010-04-13 14:26:32 +0800712
713 bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode,
714 ac, cl);
715 if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC))
716 bg_bh = ocfs2_block_group_alloc_discontig(handle,
717 alloc_inode,
Joel Becker8b06bc52010-03-26 10:09:29 +0800718 ac, cl);
Joel Becker798db352010-04-13 14:26:32 +0800719 if (IS_ERR(bg_bh)) {
720 status = PTR_ERR(bg_bh);
721 bg_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800722 if (status != -ENOSPC)
723 mlog_errno(status);
724 goto bail;
725 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800726 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
727
Joel Becker0cf2f762009-02-12 16:41:25 -0800728 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700729 bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800730 if (status < 0) {
731 mlog_errno(status);
732 goto bail;
733 }
734
Tao Ma47119542010-04-22 14:09:15 +0800735 alloc_rec = le16_to_cpu(bg->bg_chain);
736 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
Mark Fashehccd979b2005-12-15 14:31:24 -0800737 le16_to_cpu(bg->bg_free_bits_count));
Tao Ma47119542010-04-22 14:09:15 +0800738 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total,
Joel Becker798db352010-04-13 14:26:32 +0800739 le16_to_cpu(bg->bg_bits));
Tao Ma0a463b72010-07-08 11:11:11 +0800740 cl->cl_recs[alloc_rec].c_blkno = bg->bg_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -0800741 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
742 le16_add_cpu(&cl->cl_next_free_rec, 1);
743
744 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
745 le16_to_cpu(bg->bg_free_bits_count));
746 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
747 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
748
Joel Beckerec20cec2010-03-19 14:13:52 -0700749 ocfs2_journal_dirty(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800750
751 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
752 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
753 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
754 le32_to_cpu(fe->i_clusters)));
755 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
756 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
Mark Fasheh8110b072007-03-22 16:53:23 -0700757 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
Darrick J. Wong6fdb7022014-04-03 14:47:08 -0700758 ocfs2_update_inode_fsync_trans(handle, alloc_inode, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800759
760 status = 0;
Tao Mafeb473a2009-02-25 00:53:25 +0800761
762 /* save the new last alloc group so that the caller can cache it. */
763 if (last_alloc_group)
764 *last_alloc_group = ac->ac_last_group;
765
Mark Fashehccd979b2005-12-15 14:31:24 -0800766bail:
767 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700768 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800769
770 if (ac)
771 ocfs2_free_alloc_context(ac);
772
Mark Fasheha81cb882008-10-07 14:25:16 -0700773 brelse(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800774
Tao Mac1e8d352011-03-07 16:43:21 +0800775 if (status)
776 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800777 return status;
778}
779
780static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
Mark Fashehda5cbf22006-10-06 18:34:35 -0700781 struct ocfs2_alloc_context *ac,
782 int type,
Tao Maffda89a2008-03-03 17:12:09 +0800783 u32 slot,
Tao Mafeb473a2009-02-25 00:53:25 +0800784 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800785 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800786{
787 int status;
788 u32 bits_wanted = ac->ac_bits_wanted;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700789 struct inode *alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800790 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800791 struct ocfs2_dinode *fe;
792 u32 free_bits;
793
Mark Fashehda5cbf22006-10-06 18:34:35 -0700794 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
795 if (!alloc_inode) {
796 mlog_errno(-EINVAL);
797 return -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800798 }
799
Al Viro59551022016-01-22 15:40:57 -0500800 inode_lock(alloc_inode);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700801
Mark Fashehe63aecb62007-10-18 15:30:42 -0700802 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700803 if (status < 0) {
Al Viro59551022016-01-22 15:40:57 -0500804 inode_unlock(alloc_inode);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700805 iput(alloc_inode);
806
807 mlog_errno(status);
808 return status;
809 }
810
811 ac->ac_inode = alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800812 ac->ac_alloc_slot = slot;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700813
Mark Fashehccd979b2005-12-15 14:31:24 -0800814 fe = (struct ocfs2_dinode *) bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -0800815
816 /* The bh was validated by the inode read inside
817 * ocfs2_inode_lock(). Any corruption is a code bug. */
818 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
819
Mark Fashehccd979b2005-12-15 14:31:24 -0800820 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
Joe Perches7ecef142015-09-04 15:44:51 -0700821 status = ocfs2_error(alloc_inode->i_sb,
822 "Invalid chain allocator %llu\n",
823 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800824 goto bail;
825 }
826
827 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
828 le32_to_cpu(fe->id1.bitmap1.i_used);
829
830 if (bits_wanted > free_bits) {
831 /* cluster bitmap never grows */
832 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
Tao Ma2f73e132011-02-22 08:22:33 +0800833 trace_ocfs2_reserve_suballoc_bits_nospc(bits_wanted,
834 free_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800835 status = -ENOSPC;
836 goto bail;
837 }
838
Tao Ma60ca81e2009-02-25 00:53:24 +0800839 if (!(flags & ALLOC_NEW_GROUP)) {
Tao Ma2f73e132011-02-22 08:22:33 +0800840 trace_ocfs2_reserve_suballoc_bits_no_new_group(
841 slot, bits_wanted, free_bits);
Tao Maffda89a2008-03-03 17:12:09 +0800842 status = -ENOSPC;
843 goto bail;
844 }
845
Joel Becker1187c962008-09-03 20:03:39 -0700846 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
Tao Mafeb473a2009-02-25 00:53:25 +0800847 ac->ac_max_block,
848 last_alloc_group, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800849 if (status < 0) {
850 if (status != -ENOSPC)
851 mlog_errno(status);
852 goto bail;
853 }
854 atomic_inc(&osb->alloc_stats.bg_extends);
855
856 /* You should never ask for this much metadata */
857 BUG_ON(bits_wanted >
858 (le32_to_cpu(fe->id1.bitmap1.i_total)
859 - le32_to_cpu(fe->id1.bitmap1.i_used)));
860 }
861
862 get_bh(bh);
863 ac->ac_bh = bh;
864bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700865 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800866
Tao Mac1e8d352011-03-07 16:43:21 +0800867 if (status)
868 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800869 return status;
870}
871
Tiger Yangb89c5422010-01-25 14:11:06 +0800872static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
873{
874 spin_lock(&osb->osb_lock);
875 osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
876 spin_unlock(&osb->osb_lock);
877 atomic_set(&osb->s_num_inodes_stolen, 0);
878}
879
880static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
881{
882 spin_lock(&osb->osb_lock);
883 osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
884 spin_unlock(&osb->osb_lock);
885 atomic_set(&osb->s_num_meta_stolen, 0);
886}
887
888void ocfs2_init_steal_slots(struct ocfs2_super *osb)
889{
890 ocfs2_init_inode_steal_slot(osb);
891 ocfs2_init_meta_steal_slot(osb);
892}
893
894static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
895{
896 spin_lock(&osb->osb_lock);
897 if (type == INODE_ALLOC_SYSTEM_INODE)
898 osb->s_inode_steal_slot = slot;
899 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
900 osb->s_meta_steal_slot = slot;
901 spin_unlock(&osb->osb_lock);
902}
903
904static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
905{
906 int slot = OCFS2_INVALID_SLOT;
907
908 spin_lock(&osb->osb_lock);
909 if (type == INODE_ALLOC_SYSTEM_INODE)
910 slot = osb->s_inode_steal_slot;
911 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
912 slot = osb->s_meta_steal_slot;
913 spin_unlock(&osb->osb_lock);
914
915 return slot;
916}
917
918static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
919{
920 return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
921}
922
923static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
924{
925 return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
926}
927
928static int ocfs2_steal_resource(struct ocfs2_super *osb,
929 struct ocfs2_alloc_context *ac,
930 int type)
931{
932 int i, status = -ENOSPC;
933 int slot = __ocfs2_get_steal_slot(osb, type);
934
935 /* Start to steal resource from the first slot after ours. */
936 if (slot == OCFS2_INVALID_SLOT)
937 slot = osb->slot_num + 1;
938
939 for (i = 0; i < osb->max_slots; i++, slot++) {
940 if (slot == osb->max_slots)
941 slot = 0;
942
943 if (slot == osb->slot_num)
944 continue;
945
946 status = ocfs2_reserve_suballoc_bits(osb, ac,
947 type,
948 (u32)slot, NULL,
949 NOT_ALLOC_NEW_GROUP);
950 if (status >= 0) {
951 __ocfs2_set_steal_slot(osb, slot, type);
952 break;
953 }
954
955 ocfs2_free_ac_resource(ac);
956 }
957
958 return status;
959}
960
961static int ocfs2_steal_inode(struct ocfs2_super *osb,
962 struct ocfs2_alloc_context *ac)
963{
964 return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
965}
966
967static int ocfs2_steal_meta(struct ocfs2_super *osb,
968 struct ocfs2_alloc_context *ac)
969{
970 return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
971}
972
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800973int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
974 int blocks,
975 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800976{
977 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +0800978 int slot = ocfs2_get_meta_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800979
Robert P. J. Daycd861282006-12-13 00:34:52 -0800980 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800981 if (!(*ac)) {
982 status = -ENOMEM;
983 mlog_errno(status);
984 goto bail;
985 }
986
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800987 (*ac)->ac_bits_wanted = blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -0800988 (*ac)->ac_which = OCFS2_AC_USE_META;
Mark Fashehccd979b2005-12-15 14:31:24 -0800989 (*ac)->ac_group_search = ocfs2_block_group_search;
990
Tiger Yangb89c5422010-01-25 14:11:06 +0800991 if (slot != OCFS2_INVALID_SLOT &&
992 atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
993 goto extent_steal;
994
995 atomic_set(&osb->s_num_meta_stolen, 0);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700996 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
Tao Maffda89a2008-03-03 17:12:09 +0800997 EXTENT_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +0800998 (u32)osb->slot_num, NULL,
Mark Fasheh33d5d382010-02-24 13:34:09 -0800999 ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP);
Tiger Yangb89c5422010-01-25 14:11:06 +08001000
1001
1002 if (status >= 0) {
1003 status = 0;
1004 if (slot != OCFS2_INVALID_SLOT)
1005 ocfs2_init_meta_steal_slot(osb);
1006 goto bail;
1007 } else if (status < 0 && status != -ENOSPC) {
1008 mlog_errno(status);
1009 goto bail;
1010 }
1011
1012 ocfs2_free_ac_resource(*ac);
1013
1014extent_steal:
1015 status = ocfs2_steal_meta(osb, *ac);
1016 atomic_inc(&osb->s_num_meta_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -08001017 if (status < 0) {
1018 if (status != -ENOSPC)
1019 mlog_errno(status);
1020 goto bail;
1021 }
1022
1023 status = 0;
1024bail:
1025 if ((status < 0) && *ac) {
1026 ocfs2_free_alloc_context(*ac);
1027 *ac = NULL;
1028 }
1029
Tao Mac1e8d352011-03-07 16:43:21 +08001030 if (status)
1031 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001032 return status;
1033}
1034
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001035int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
1036 struct ocfs2_extent_list *root_el,
1037 struct ocfs2_alloc_context **ac)
1038{
1039 return ocfs2_reserve_new_metadata_blocks(osb,
1040 ocfs2_extend_meta_needed(root_el),
1041 ac);
1042}
1043
Mark Fashehccd979b2005-12-15 14:31:24 -08001044int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001045 struct ocfs2_alloc_context **ac)
1046{
1047 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +08001048 int slot = ocfs2_get_inode_steal_slot(osb);
Tao Mafeb473a2009-02-25 00:53:25 +08001049 u64 alloc_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001050
Robert P. J. Daycd861282006-12-13 00:34:52 -08001051 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001052 if (!(*ac)) {
1053 status = -ENOMEM;
1054 mlog_errno(status);
1055 goto bail;
1056 }
1057
1058 (*ac)->ac_bits_wanted = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001059 (*ac)->ac_which = OCFS2_AC_USE_INODE;
1060
Mark Fashehccd979b2005-12-15 14:31:24 -08001061 (*ac)->ac_group_search = ocfs2_block_group_search;
1062
Tao Ma4d0ddb22008-03-05 16:11:46 +08001063 /*
Joel Becker1187c962008-09-03 20:03:39 -07001064 * stat(2) can't handle i_ino > 32bits, so we tell the
1065 * lower levels not to allocate us a block group past that
Joel Becker12462f12008-09-03 20:03:40 -07001066 * limit. The 'inode64' mount option avoids this behavior.
Joel Becker1187c962008-09-03 20:03:39 -07001067 */
Joel Becker12462f12008-09-03 20:03:40 -07001068 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
1069 (*ac)->ac_max_block = (u32)~0U;
Joel Becker1187c962008-09-03 20:03:39 -07001070
1071 /*
Tao Ma4d0ddb22008-03-05 16:11:46 +08001072 * slot is set when we successfully steal inode from other nodes.
1073 * It is reset in 3 places:
1074 * 1. when we flush the truncate log
1075 * 2. when we complete local alloc recovery.
1076 * 3. when we successfully allocate from our own slot.
1077 * After it is set, we will go on stealing inodes until we find the
1078 * need to check our slots to see whether there is some space for us.
1079 */
1080 if (slot != OCFS2_INVALID_SLOT &&
Tiger Yangb89c5422010-01-25 14:11:06 +08001081 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL)
Tao Ma4d0ddb22008-03-05 16:11:46 +08001082 goto inode_steal;
1083
1084 atomic_set(&osb->s_num_inodes_stolen, 0);
Tao Mafeb473a2009-02-25 00:53:25 +08001085 alloc_group = osb->osb_inode_alloc_group;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001086 status = ocfs2_reserve_suballoc_bits(osb, *ac,
1087 INODE_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +08001088 (u32)osb->slot_num,
Tao Mafeb473a2009-02-25 00:53:25 +08001089 &alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +08001090 ALLOC_NEW_GROUP |
1091 ALLOC_GROUPS_FROM_GLOBAL);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001092 if (status >= 0) {
1093 status = 0;
1094
Tao Mafeb473a2009-02-25 00:53:25 +08001095 spin_lock(&osb->osb_lock);
1096 osb->osb_inode_alloc_group = alloc_group;
1097 spin_unlock(&osb->osb_lock);
Tao Ma2f73e132011-02-22 08:22:33 +08001098 trace_ocfs2_reserve_new_inode_new_group(
1099 (unsigned long long)alloc_group);
Tao Mafeb473a2009-02-25 00:53:25 +08001100
Tao Ma4d0ddb22008-03-05 16:11:46 +08001101 /*
1102 * Some inodes must be freed by us, so try to allocate
1103 * from our own next time.
1104 */
1105 if (slot != OCFS2_INVALID_SLOT)
1106 ocfs2_init_inode_steal_slot(osb);
1107 goto bail;
1108 } else if (status < 0 && status != -ENOSPC) {
1109 mlog_errno(status);
1110 goto bail;
1111 }
1112
1113 ocfs2_free_ac_resource(*ac);
1114
1115inode_steal:
Tiger Yangb89c5422010-01-25 14:11:06 +08001116 status = ocfs2_steal_inode(osb, *ac);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001117 atomic_inc(&osb->s_num_inodes_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -08001118 if (status < 0) {
1119 if (status != -ENOSPC)
1120 mlog_errno(status);
1121 goto bail;
1122 }
1123
1124 status = 0;
1125bail:
1126 if ((status < 0) && *ac) {
1127 ocfs2_free_alloc_context(*ac);
1128 *ac = NULL;
1129 }
1130
Tao Mac1e8d352011-03-07 16:43:21 +08001131 if (status)
1132 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001133 return status;
1134}
1135
1136/* local alloc code has to do the same thing, so rather than do this
1137 * twice.. */
1138int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
1139 struct ocfs2_alloc_context *ac)
1140{
1141 int status;
1142
Mark Fashehccd979b2005-12-15 14:31:24 -08001143 ac->ac_which = OCFS2_AC_USE_MAIN;
1144 ac->ac_group_search = ocfs2_cluster_group_search;
1145
Mark Fashehda5cbf22006-10-06 18:34:35 -07001146 status = ocfs2_reserve_suballoc_bits(osb, ac,
1147 GLOBAL_BITMAP_SYSTEM_INODE,
Tao Mafeb473a2009-02-25 00:53:25 +08001148 OCFS2_INVALID_SLOT, NULL,
Tao Maffda89a2008-03-03 17:12:09 +08001149 ALLOC_NEW_GROUP);
Guozhonghua47ee9d82017-11-15 17:31:55 -08001150 if (status < 0 && status != -ENOSPC)
Mark Fashehccd979b2005-12-15 14:31:24 -08001151 mlog_errno(status);
Mark Fashehda5cbf22006-10-06 18:34:35 -07001152
Mark Fashehccd979b2005-12-15 14:31:24 -08001153 return status;
1154}
1155
1156/* Callers don't need to care which bitmap (local alloc or main) to
1157 * use so we figure it out for them, but unfortunately this clutters
1158 * things a bit. */
Joel Becker1187c962008-09-03 20:03:39 -07001159static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
1160 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +08001161 int flags,
Joel Becker1187c962008-09-03 20:03:39 -07001162 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08001163{
Eric Ren2070ad12016-08-02 14:02:10 -07001164 int status, ret = 0;
1165 int retried = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001166
Robert P. J. Daycd861282006-12-13 00:34:52 -08001167 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001168 if (!(*ac)) {
1169 status = -ENOMEM;
1170 mlog_errno(status);
1171 goto bail;
1172 }
1173
1174 (*ac)->ac_bits_wanted = bits_wanted;
Joel Becker1187c962008-09-03 20:03:39 -07001175 (*ac)->ac_max_block = max_block;
Mark Fashehccd979b2005-12-15 14:31:24 -08001176
1177 status = -ENOSPC;
Tao Ma60ca81e2009-02-25 00:53:24 +08001178 if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
1179 ocfs2_alloc_should_use_local(osb, bits_wanted)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001180 status = ocfs2_reserve_local_alloc_bits(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001181 bits_wanted,
1182 *ac);
Mark Fasheha57c8fd2010-03-16 21:01:00 -07001183 if ((status < 0) && (status != -ENOSPC)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001184 mlog_errno(status);
1185 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08001186 }
1187 }
1188
1189 if (status == -ENOSPC) {
Eric Ren2070ad12016-08-02 14:02:10 -07001190retry:
Mark Fashehccd979b2005-12-15 14:31:24 -08001191 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
Eric Ren2070ad12016-08-02 14:02:10 -07001192 /* Retry if there is sufficient space cached in truncate log */
1193 if (status == -ENOSPC && !retried) {
1194 retried = 1;
1195 ocfs2_inode_unlock((*ac)->ac_inode, 1);
1196 inode_unlock((*ac)->ac_inode);
1197
1198 ret = ocfs2_try_to_free_truncate_log(osb, bits_wanted);
Joseph Qi3bb8b6532016-09-19 14:44:33 -07001199 if (ret == 1) {
1200 iput((*ac)->ac_inode);
1201 (*ac)->ac_inode = NULL;
Eric Ren2070ad12016-08-02 14:02:10 -07001202 goto retry;
Joseph Qi3bb8b6532016-09-19 14:44:33 -07001203 }
Eric Ren2070ad12016-08-02 14:02:10 -07001204
1205 if (ret < 0)
1206 mlog_errno(ret);
1207
1208 inode_lock((*ac)->ac_inode);
Joseph Qi3bb8b6532016-09-19 14:44:33 -07001209 ret = ocfs2_inode_lock((*ac)->ac_inode, NULL, 1);
1210 if (ret < 0) {
1211 mlog_errno(ret);
1212 inode_unlock((*ac)->ac_inode);
1213 iput((*ac)->ac_inode);
1214 (*ac)->ac_inode = NULL;
1215 goto bail;
1216 }
Eric Ren2070ad12016-08-02 14:02:10 -07001217 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001218 if (status < 0) {
1219 if (status != -ENOSPC)
1220 mlog_errno(status);
1221 goto bail;
1222 }
1223 }
1224
1225 status = 0;
1226bail:
1227 if ((status < 0) && *ac) {
1228 ocfs2_free_alloc_context(*ac);
1229 *ac = NULL;
1230 }
1231
Tao Mac1e8d352011-03-07 16:43:21 +08001232 if (status)
1233 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001234 return status;
1235}
1236
Joel Becker1187c962008-09-03 20:03:39 -07001237int ocfs2_reserve_clusters(struct ocfs2_super *osb,
1238 u32 bits_wanted,
1239 struct ocfs2_alloc_context **ac)
1240{
Tao Ma60ca81e2009-02-25 00:53:24 +08001241 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
1242 ALLOC_NEW_GROUP, ac);
Joel Becker1187c962008-09-03 20:03:39 -07001243}
1244
Mark Fashehccd979b2005-12-15 14:31:24 -08001245/*
1246 * More or less lifted from ext3. I'll leave their description below:
1247 *
1248 * "For ext3 allocations, we must not reuse any blocks which are
1249 * allocated in the bitmap buffer's "last committed data" copy. This
1250 * prevents deletes from freeing up the page for reuse until we have
1251 * committed the delete transaction.
1252 *
1253 * If we didn't do this, then deleting something and reallocating it as
1254 * data would allow the old block to be overwritten before the
1255 * transaction committed (because we force data to disk before commit).
1256 * This would lead to corruption if we crashed between overwriting the
1257 * data and committing the delete.
1258 *
1259 * @@@ We may want to make this allocation behaviour conditional on
1260 * data-writes at some point, and disable it for metadata allocations or
1261 * sync-data inodes."
1262 *
1263 * Note: OCFS2 already does this differently for metadata vs data
Joe Perchesc78bad12008-02-03 17:33:42 +02001264 * allocations, as those bitmaps are separate and undo access is never
Mark Fashehccd979b2005-12-15 14:31:24 -08001265 * called on a metadata group descriptor.
1266 */
1267static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
1268 int nr)
1269{
1270 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001271 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001272
1273 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
1274 return 0;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001275
1276 if (!buffer_jbd(bg_bh))
Mark Fashehccd979b2005-12-15 14:31:24 -08001277 return 1;
1278
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001279 jbd_lock_bh_state(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001280 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001281 if (bg)
1282 ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
1283 else
1284 ret = 1;
1285 jbd_unlock_bh_state(bg_bh);
1286
1287 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001288}
1289
1290static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
1291 struct buffer_head *bg_bh,
1292 unsigned int bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001293 unsigned int total_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001294 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001295{
1296 void *bitmap;
1297 u16 best_offset, best_size;
1298 int offset, start, found, status = 0;
1299 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1300
Joel Becker42035302008-11-13 14:49:15 -08001301 /* Callers got this descriptor from
1302 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1303 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001304
1305 found = start = best_offset = best_size = 0;
1306 bitmap = bg->bg_bitmap;
1307
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001308 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
1309 if (offset == total_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -08001310 break;
1311
1312 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
1313 /* We found a zero, but we can't use it as it
1314 * hasn't been put to disk yet! */
1315 found = 0;
1316 start = offset + 1;
1317 } else if (offset == start) {
1318 /* we found a zero */
1319 found++;
1320 /* move start to the next bit to test */
1321 start++;
1322 } else {
1323 /* got a zero after some ones */
1324 found = 1;
1325 start = offset + 1;
1326 }
1327 if (found > best_size) {
1328 best_size = found;
1329 best_offset = start - found;
1330 }
1331 /* we got everything we needed */
1332 if (found == bits_wanted) {
1333 /* mlog(0, "Found it all!\n"); */
1334 break;
1335 }
1336 }
1337
Joel Becker7d1fe092010-04-13 14:30:19 +08001338 if (best_size) {
1339 res->sr_bit_offset = best_offset;
1340 res->sr_bits = best_size;
Mark Fashehccd979b2005-12-15 14:31:24 -08001341 } else {
1342 status = -ENOSPC;
1343 /* No error log here -- see the comment above
1344 * ocfs2_test_bg_bit_allocatable */
1345 }
1346
1347 return status;
1348}
1349
Younger Liu0a2fcd892014-01-21 15:48:33 -08001350int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001351 struct inode *alloc_inode,
1352 struct ocfs2_group_desc *bg,
1353 struct buffer_head *group_bh,
1354 unsigned int bit_off,
1355 unsigned int num_bits)
1356{
1357 int status;
1358 void *bitmap = bg->bg_bitmap;
1359 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1360
Joel Becker42035302008-11-13 14:49:15 -08001361 /* All callers get the descriptor via
1362 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1363 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001364 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
1365
Tao Ma2f73e132011-02-22 08:22:33 +08001366 trace_ocfs2_block_group_set_bits(bit_off, num_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001367
1368 if (ocfs2_is_cluster_bitmap(alloc_inode))
1369 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1370
Joel Becker13723d02008-10-17 19:25:01 -07001371 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001372 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001373 group_bh,
1374 journal_type);
Mark Fashehccd979b2005-12-15 14:31:24 -08001375 if (status < 0) {
1376 mlog_errno(status);
1377 goto bail;
1378 }
1379
1380 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
Srinivas Eeda9b5cd102010-10-05 15:53:06 -07001381 if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) {
Joe Perches7ecef142015-09-04 15:44:51 -07001382 return ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit count %u but claims %u are freed. num_bits %d\n",
1383 (unsigned long long)le64_to_cpu(bg->bg_blkno),
1384 le16_to_cpu(bg->bg_bits),
1385 le16_to_cpu(bg->bg_free_bits_count),
1386 num_bits);
Srinivas Eeda9b5cd102010-10-05 15:53:06 -07001387 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001388 while(num_bits--)
1389 ocfs2_set_bit(bit_off++, bitmap);
1390
Joel Beckerec20cec2010-03-19 14:13:52 -07001391 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001392
1393bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001394 return status;
1395}
1396
1397/* find the one with the most empty bits */
1398static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
1399{
1400 u16 curr, best;
1401
1402 BUG_ON(!cl->cl_next_free_rec);
1403
1404 best = curr = 0;
1405 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
1406 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
1407 le32_to_cpu(cl->cl_recs[best].c_free))
1408 best = curr;
1409 curr++;
1410 }
1411
1412 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
1413 return best;
1414}
1415
Mark Fasheh1fabe142006-10-09 18:11:45 -07001416static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001417 struct inode *alloc_inode,
1418 struct buffer_head *fe_bh,
1419 struct buffer_head *bg_bh,
1420 struct buffer_head *prev_bg_bh,
1421 u16 chain)
1422{
1423 int status;
1424 /* there is a really tiny chance the journal calls could fail,
1425 * but we wouldn't want inconsistent blocks in *any* case. */
Jie Liu49309842013-07-03 15:00:59 -07001426 u64 bg_ptr, prev_bg_ptr;
Mark Fashehccd979b2005-12-15 14:31:24 -08001427 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
1428 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1429 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
1430
Joel Becker42035302008-11-13 14:49:15 -08001431 /* The caller got these descriptors from
1432 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1433 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1434 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001435
Tao Ma2f73e132011-02-22 08:22:33 +08001436 trace_ocfs2_relink_block_group(
1437 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
1438 (unsigned long long)le64_to_cpu(bg->bg_blkno),
1439 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001440
Mark Fashehccd979b2005-12-15 14:31:24 -08001441 bg_ptr = le64_to_cpu(bg->bg_next_group);
1442 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
1443
Joel Becker0cf2f762009-02-12 16:41:25 -08001444 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1445 prev_bg_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001446 OCFS2_JOURNAL_ACCESS_WRITE);
Andrew Morton25e28922013-07-03 15:01:00 -07001447 if (status < 0)
Jie Liu49309842013-07-03 15:00:59 -07001448 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08001449
1450 prev_bg->bg_next_group = bg->bg_next_group;
Joel Beckerec20cec2010-03-19 14:13:52 -07001451 ocfs2_journal_dirty(handle, prev_bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001452
Joel Becker0cf2f762009-02-12 16:41:25 -08001453 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1454 bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Andrew Morton25e28922013-07-03 15:01:00 -07001455 if (status < 0)
Jie Liu49309842013-07-03 15:00:59 -07001456 goto out_rollback_prev_bg;
Mark Fashehccd979b2005-12-15 14:31:24 -08001457
1458 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -07001459 ocfs2_journal_dirty(handle, bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001460
Joel Becker0cf2f762009-02-12 16:41:25 -08001461 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
1462 fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Andrew Morton25e28922013-07-03 15:01:00 -07001463 if (status < 0)
Jie Liu49309842013-07-03 15:00:59 -07001464 goto out_rollback_bg;
Mark Fashehccd979b2005-12-15 14:31:24 -08001465
1466 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -07001467 ocfs2_journal_dirty(handle, fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001468
Jie Liu49309842013-07-03 15:00:59 -07001469out:
Andrew Morton25e28922013-07-03 15:01:00 -07001470 if (status < 0)
1471 mlog_errno(status);
Jie Liu49309842013-07-03 15:00:59 -07001472 return status;
Joel Becker42035302008-11-13 14:49:15 -08001473
Jie Liu49309842013-07-03 15:00:59 -07001474out_rollback_bg:
1475 bg->bg_next_group = cpu_to_le64(bg_ptr);
1476out_rollback_prev_bg:
1477 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
Andrew Morton25e28922013-07-03 15:01:00 -07001478 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08001479}
1480
1481static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1482 u32 wanted)
1483{
1484 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1485}
1486
1487/* return 0 on success, -ENOSPC to keep searching and any other < 0
1488 * value on error. */
1489static int ocfs2_cluster_group_search(struct inode *inode,
1490 struct buffer_head *group_bh,
1491 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001492 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +08001493 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001494{
1495 int search = -ENOSPC;
1496 int ret;
Joel Becker1187c962008-09-03 20:03:39 -07001497 u64 blkoff;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001498 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001499 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001500 unsigned int max_bits, gd_cluster_off;
Mark Fashehccd979b2005-12-15 14:31:24 -08001501
1502 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1503
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001504 if (gd->bg_free_bits_count) {
1505 max_bits = le16_to_cpu(gd->bg_bits);
1506
1507 /* Tail groups in cluster bitmaps which aren't cpg
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001508 * aligned are prone to partial extension by a failed
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001509 * fs resize. If the file system resize never got to
1510 * update the dinode cluster count, then we don't want
1511 * to trust any clusters past it, regardless of what
1512 * the group descriptor says. */
1513 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1514 le64_to_cpu(gd->bg_blkno));
1515 if ((gd_cluster_off + max_bits) >
1516 OCFS2_I(inode)->ip_clusters) {
1517 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
Tao Ma2f73e132011-02-22 08:22:33 +08001518 trace_ocfs2_cluster_group_search_wrong_max_bits(
1519 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1520 le16_to_cpu(gd->bg_bits),
1521 OCFS2_I(inode)->ip_clusters, max_bits);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001522 }
1523
Mark Fashehccd979b2005-12-15 14:31:24 -08001524 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1525 group_bh, bits_wanted,
Joel Becker7d1fe092010-04-13 14:30:19 +08001526 max_bits, res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001527 if (ret)
1528 return ret;
1529
Joel Becker1187c962008-09-03 20:03:39 -07001530 if (max_block) {
1531 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1532 gd_cluster_off +
Joel Becker7d1fe092010-04-13 14:30:19 +08001533 res->sr_bit_offset +
1534 res->sr_bits);
Tao Ma2f73e132011-02-22 08:22:33 +08001535 trace_ocfs2_cluster_group_search_max_block(
1536 (unsigned long long)blkoff,
1537 (unsigned long long)max_block);
Joel Becker1187c962008-09-03 20:03:39 -07001538 if (blkoff > max_block)
1539 return -ENOSPC;
1540 }
1541
Mark Fashehccd979b2005-12-15 14:31:24 -08001542 /* ocfs2_block_group_find_clear_bits() might
1543 * return success, but we still want to return
1544 * -ENOSPC unless it found the minimum number
1545 * of bits. */
Joel Becker7d1fe092010-04-13 14:30:19 +08001546 if (min_bits <= res->sr_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -08001547 search = 0; /* success */
Joel Becker7d1fe092010-04-13 14:30:19 +08001548 else if (res->sr_bits) {
Mark Fasheh9c7af402008-07-28 18:02:53 -07001549 /*
1550 * Don't show bits which we'll be returning
1551 * for allocation to the local alloc bitmap.
1552 */
Joel Becker7d1fe092010-04-13 14:30:19 +08001553 ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001554 }
1555 }
1556
1557 return search;
1558}
1559
1560static int ocfs2_block_group_search(struct inode *inode,
1561 struct buffer_head *group_bh,
1562 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001563 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +08001564 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001565{
1566 int ret = -ENOSPC;
Joel Becker1187c962008-09-03 20:03:39 -07001567 u64 blkoff;
Mark Fashehccd979b2005-12-15 14:31:24 -08001568 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1569
1570 BUG_ON(min_bits != 1);
1571 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1572
Joel Becker1187c962008-09-03 20:03:39 -07001573 if (bg->bg_free_bits_count) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001574 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1575 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001576 le16_to_cpu(bg->bg_bits),
Joel Becker7d1fe092010-04-13 14:30:19 +08001577 res);
Joel Becker1187c962008-09-03 20:03:39 -07001578 if (!ret && max_block) {
Joel Becker7d1fe092010-04-13 14:30:19 +08001579 blkoff = le64_to_cpu(bg->bg_blkno) +
1580 res->sr_bit_offset + res->sr_bits;
Tao Ma2f73e132011-02-22 08:22:33 +08001581 trace_ocfs2_block_group_search_max_block(
1582 (unsigned long long)blkoff,
1583 (unsigned long long)max_block);
Joel Becker1187c962008-09-03 20:03:39 -07001584 if (blkoff > max_block)
1585 ret = -ENOSPC;
1586 }
1587 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001588
1589 return ret;
1590}
1591
Younger Liu0a2fcd892014-01-21 15:48:33 -08001592int ocfs2_alloc_dinode_update_counts(struct inode *inode,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001593 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001594 struct buffer_head *di_bh,
1595 u32 num_bits,
1596 u16 chain)
1597{
1598 int ret;
1599 u32 tmp_used;
1600 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1601 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1602
Joel Becker0cf2f762009-02-12 16:41:25 -08001603 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001604 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001605 if (ret < 0) {
1606 mlog_errno(ret);
1607 goto out;
1608 }
1609
1610 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1611 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1612 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
Joel Beckerec20cec2010-03-19 14:13:52 -07001613 ocfs2_journal_dirty(handle, di_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001614
1615out:
1616 return ret;
1617}
1618
Younger Liudb66c712014-04-03 14:47:10 -07001619void ocfs2_rollback_alloc_dinode_counts(struct inode *inode,
1620 struct buffer_head *di_bh,
1621 u32 num_bits,
1622 u16 chain)
1623{
1624 u32 tmp_used;
1625 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1626 struct ocfs2_chain_list *cl;
1627
1628 cl = (struct ocfs2_chain_list *)&di->id2.i_chain;
1629 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1630 di->id1.bitmap1.i_used = cpu_to_le32(tmp_used - num_bits);
1631 le32_add_cpu(&cl->cl_recs[chain].c_free, num_bits);
1632}
1633
Joel Beckerba206632010-03-26 10:08:59 +08001634static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res,
1635 struct ocfs2_extent_rec *rec,
1636 struct ocfs2_chain_list *cl)
Joel Becker13e434c2010-03-26 10:08:27 +08001637{
1638 unsigned int bpc = le16_to_cpu(cl->cl_bpc);
1639 unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc;
Al Viro72094e42012-04-13 12:30:02 -04001640 unsigned int bitcount = le16_to_cpu(rec->e_leaf_clusters) * bpc;
Joel Becker13e434c2010-03-26 10:08:27 +08001641
1642 if (res->sr_bit_offset < bitoff)
1643 return 0;
1644 if (res->sr_bit_offset >= (bitoff + bitcount))
1645 return 0;
Joel Beckerba206632010-03-26 10:08:59 +08001646 res->sr_blkno = le64_to_cpu(rec->e_blkno) +
1647 (res->sr_bit_offset - bitoff);
Joel Becker13e434c2010-03-26 10:08:27 +08001648 if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount))
1649 res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset;
1650 return 1;
1651}
1652
Joel Beckerba206632010-03-26 10:08:59 +08001653static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac,
1654 struct ocfs2_group_desc *bg,
1655 struct ocfs2_suballoc_result *res)
Joel Becker13e434c2010-03-26 10:08:27 +08001656{
1657 int i;
Joel Becker2b6cb572010-03-26 10:09:15 +08001658 u64 bg_blkno = res->sr_bg_blkno; /* Save off */
Joel Becker13e434c2010-03-26 10:08:27 +08001659 struct ocfs2_extent_rec *rec;
1660 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
1661 struct ocfs2_chain_list *cl = &di->id2.i_chain;
1662
Joel Beckerba206632010-03-26 10:08:59 +08001663 if (ocfs2_is_cluster_bitmap(ac->ac_inode)) {
1664 res->sr_blkno = 0;
Joel Becker13e434c2010-03-26 10:08:27 +08001665 return;
Joel Beckerba206632010-03-26 10:08:59 +08001666 }
Joel Becker13e434c2010-03-26 10:08:27 +08001667
Joel Beckerba206632010-03-26 10:08:59 +08001668 res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset;
Joel Becker2b6cb572010-03-26 10:09:15 +08001669 res->sr_bg_blkno = 0; /* Clear it for contig block groups */
Tao Ma47119542010-04-22 14:09:15 +08001670 if (!ocfs2_supports_discontig_bg(OCFS2_SB(ac->ac_inode->i_sb)) ||
Joel Beckerba206632010-03-26 10:08:59 +08001671 !bg->bg_list.l_next_free_rec)
Joel Becker13e434c2010-03-26 10:08:27 +08001672 return;
1673
1674 for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) {
1675 rec = &bg->bg_list.l_recs[i];
Joel Becker2b6cb572010-03-26 10:09:15 +08001676 if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) {
1677 res->sr_bg_blkno = bg_blkno; /* Restore */
Joel Becker13e434c2010-03-26 10:08:27 +08001678 break;
Joel Becker2b6cb572010-03-26 10:09:15 +08001679 }
Joel Becker13e434c2010-03-26 10:08:27 +08001680 }
1681}
1682
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001683static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001684 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001685 u32 bits_wanted,
1686 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001687 struct ocfs2_suballoc_result *res,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001688 u16 *bits_left)
1689{
1690 int ret;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001691 struct buffer_head *group_bh = NULL;
1692 struct ocfs2_group_desc *gd;
Joel Becker68f64d42008-11-13 14:49:14 -08001693 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001694 struct inode *alloc_inode = ac->ac_inode;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001695
Joel Becker7d1fe092010-04-13 14:30:19 +08001696 ret = ocfs2_read_group_descriptor(alloc_inode, di,
1697 res->sr_bg_blkno, &group_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001698 if (ret < 0) {
1699 mlog_errno(ret);
1700 return ret;
1701 }
1702
1703 gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001704 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001705 ac->ac_max_block, res);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001706 if (ret < 0) {
1707 if (ret != -ENOSPC)
1708 mlog_errno(ret);
1709 goto out;
1710 }
1711
Joel Becker13e434c2010-03-26 10:08:27 +08001712 if (!ret)
Joel Beckerba206632010-03-26 10:08:59 +08001713 ocfs2_bg_discontig_fix_result(ac, gd, res);
Joel Becker13e434c2010-03-26 10:08:27 +08001714
Mark Fashehe49e2762010-08-13 15:15:17 -07001715 /*
1716 * sr_bg_blkno might have been changed by
1717 * ocfs2_bg_discontig_fix_result
1718 */
1719 res->sr_bg_stable_blkno = group_bh->b_blocknr;
1720
1721 if (ac->ac_find_loc_only)
1722 goto out_loc_only;
1723
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001724 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001725 res->sr_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001726 le16_to_cpu(gd->bg_chain));
1727 if (ret < 0) {
1728 mlog_errno(ret);
1729 goto out;
1730 }
1731
1732 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001733 res->sr_bit_offset, res->sr_bits);
Younger Liudb66c712014-04-03 14:47:10 -07001734 if (ret < 0) {
1735 ocfs2_rollback_alloc_dinode_counts(alloc_inode, ac->ac_bh,
1736 res->sr_bits,
1737 le16_to_cpu(gd->bg_chain));
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001738 mlog_errno(ret);
Younger Liudb66c712014-04-03 14:47:10 -07001739 }
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001740
Mark Fashehe49e2762010-08-13 15:15:17 -07001741out_loc_only:
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001742 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1743
1744out:
1745 brelse(group_bh);
1746
1747 return ret;
1748}
1749
Mark Fashehccd979b2005-12-15 14:31:24 -08001750static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001751 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001752 u32 bits_wanted,
1753 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001754 struct ocfs2_suballoc_result *res,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001755 u16 *bits_left)
Mark Fashehccd979b2005-12-15 14:31:24 -08001756{
1757 int status;
Joel Becker7d1fe092010-04-13 14:30:19 +08001758 u16 chain;
Mark Fashehccd979b2005-12-15 14:31:24 -08001759 u64 next_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001760 struct inode *alloc_inode = ac->ac_inode;
1761 struct buffer_head *group_bh = NULL;
1762 struct buffer_head *prev_group_bh = NULL;
1763 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1764 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1765 struct ocfs2_group_desc *bg;
1766
1767 chain = ac->ac_chain;
Tao Ma2f73e132011-02-22 08:22:33 +08001768 trace_ocfs2_search_chain_begin(
1769 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno,
1770 bits_wanted, chain);
Mark Fashehccd979b2005-12-15 14:31:24 -08001771
Joel Becker68f64d42008-11-13 14:49:14 -08001772 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1773 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1774 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001775 if (status < 0) {
1776 mlog_errno(status);
1777 goto bail;
1778 }
1779 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001780
1781 status = -ENOSPC;
1782 /* for now, the chain search is a bit simplistic. We just use
1783 * the 1st group with any empty bits. */
Joel Becker1187c962008-09-03 20:03:39 -07001784 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1785 bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001786 ac->ac_max_block,
1787 res)) == -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001788 if (!bg->bg_next_group)
1789 break;
Mark Fasheha81cb882008-10-07 14:25:16 -07001790
1791 brelse(prev_group_bh);
1792 prev_group_bh = NULL;
1793
Mark Fashehccd979b2005-12-15 14:31:24 -08001794 next_group = le64_to_cpu(bg->bg_next_group);
1795 prev_group_bh = group_bh;
1796 group_bh = NULL;
Joel Becker68f64d42008-11-13 14:49:14 -08001797 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1798 next_group, &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001799 if (status < 0) {
1800 mlog_errno(status);
1801 goto bail;
1802 }
1803 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001804 }
1805 if (status < 0) {
1806 if (status != -ENOSPC)
1807 mlog_errno(status);
1808 goto bail;
1809 }
1810
Tao Ma2f73e132011-02-22 08:22:33 +08001811 trace_ocfs2_search_chain_succ(
1812 (unsigned long long)le64_to_cpu(bg->bg_blkno), res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001813
Joel Becker7d1fe092010-04-13 14:30:19 +08001814 res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001815
Joel Becker7d1fe092010-04-13 14:30:19 +08001816 BUG_ON(res->sr_bits == 0);
Joel Becker13e434c2010-03-26 10:08:27 +08001817 if (!status)
Joel Beckerba206632010-03-26 10:08:59 +08001818 ocfs2_bg_discontig_fix_result(ac, bg, res);
Joel Becker13e434c2010-03-26 10:08:27 +08001819
Mark Fashehe49e2762010-08-13 15:15:17 -07001820 /*
1821 * sr_bg_blkno might have been changed by
1822 * ocfs2_bg_discontig_fix_result
1823 */
1824 res->sr_bg_stable_blkno = group_bh->b_blocknr;
Mark Fashehccd979b2005-12-15 14:31:24 -08001825
1826 /*
1827 * Keep track of previous block descriptor read. When
1828 * we find a target, if we have read more than X
1829 * number of descriptors, and the target is reasonably
1830 * empty, relink him to top of his chain.
1831 *
1832 * We've read 0 extra blocks and only send one more to
1833 * the transaction, yet the next guy to search has a
1834 * much easier time.
1835 *
1836 * Do this *after* figuring out how many bits we're taking out
1837 * of our target group.
1838 */
Xiaowei.Hu309a85b2013-02-27 17:02:49 -08001839 if (!ac->ac_disable_chain_relink &&
Mark Fashehccd979b2005-12-15 14:31:24 -08001840 (prev_group_bh) &&
Joel Becker7d1fe092010-04-13 14:30:19 +08001841 (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001842 status = ocfs2_relink_block_group(handle, alloc_inode,
1843 ac->ac_bh, group_bh,
1844 prev_group_bh, chain);
1845 if (status < 0) {
1846 mlog_errno(status);
1847 goto bail;
1848 }
1849 }
1850
Mark Fashehe49e2762010-08-13 15:15:17 -07001851 if (ac->ac_find_loc_only)
1852 goto out_loc_only;
1853
Mark Fashehd5134982010-08-13 15:15:16 -07001854 status = ocfs2_alloc_dinode_update_counts(alloc_inode, handle,
1855 ac->ac_bh, res->sr_bits,
1856 chain);
1857 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001858 mlog_errno(status);
1859 goto bail;
1860 }
1861
Mark Fashehccd979b2005-12-15 14:31:24 -08001862 status = ocfs2_block_group_set_bits(handle,
1863 alloc_inode,
1864 bg,
1865 group_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001866 res->sr_bit_offset,
1867 res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001868 if (status < 0) {
Younger Liudb66c712014-04-03 14:47:10 -07001869 ocfs2_rollback_alloc_dinode_counts(alloc_inode,
1870 ac->ac_bh, res->sr_bits, chain);
Mark Fashehccd979b2005-12-15 14:31:24 -08001871 mlog_errno(status);
1872 goto bail;
1873 }
1874
Tao Ma2f73e132011-02-22 08:22:33 +08001875 trace_ocfs2_search_chain_end(
1876 (unsigned long long)le64_to_cpu(fe->i_blkno),
1877 res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001878
Mark Fashehe49e2762010-08-13 15:15:17 -07001879out_loc_only:
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001880 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001881bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001882 brelse(group_bh);
1883 brelse(prev_group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001884
Tao Mac1e8d352011-03-07 16:43:21 +08001885 if (status)
1886 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001887 return status;
1888}
1889
1890/* will give out up to bits_wanted contiguous bits. */
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001891static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001892 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001893 u32 bits_wanted,
1894 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001895 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001896{
1897 int status;
1898 u16 victim, i;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001899 u16 bits_left = 0;
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001900 u64 hint = ac->ac_last_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001901 struct ocfs2_chain_list *cl;
1902 struct ocfs2_dinode *fe;
1903
Mark Fashehccd979b2005-12-15 14:31:24 -08001904 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1905 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1906 BUG_ON(!ac->ac_bh);
1907
1908 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -08001909
1910 /* The bh was validated by the inode read during
1911 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1912 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1913
Mark Fashehccd979b2005-12-15 14:31:24 -08001914 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1915 le32_to_cpu(fe->id1.bitmap1.i_total)) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -07001916 status = ocfs2_error(ac->ac_inode->i_sb,
Joe Perches7ecef142015-09-04 15:44:51 -07001917 "Chain allocator dinode %llu has %u used bits but only %u total\n",
1918 (unsigned long long)le64_to_cpu(fe->i_blkno),
1919 le32_to_cpu(fe->id1.bitmap1.i_used),
1920 le32_to_cpu(fe->id1.bitmap1.i_total));
Mark Fashehccd979b2005-12-15 14:31:24 -08001921 goto bail;
1922 }
1923
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001924 res->sr_bg_blkno = hint;
Joel Becker7d1fe092010-04-13 14:30:19 +08001925 if (res->sr_bg_blkno) {
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001926 /* Attempt to short-circuit the usual search mechanism
1927 * by jumping straight to the most recently used
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001928 * allocation group. This helps us maintain some
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001929 * contiguousness across allocations. */
Mark Fashehda5cbf22006-10-06 18:34:35 -07001930 status = ocfs2_search_one_group(ac, handle, bits_wanted,
Joel Becker7d1fe092010-04-13 14:30:19 +08001931 min_bits, res, &bits_left);
1932 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001933 goto set_hint;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001934 if (status < 0 && status != -ENOSPC) {
1935 mlog_errno(status);
1936 goto bail;
1937 }
1938 }
1939
Mark Fashehccd979b2005-12-15 14:31:24 -08001940 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1941
1942 victim = ocfs2_find_victim_chain(cl);
1943 ac->ac_chain = victim;
Mark Fashehccd979b2005-12-15 14:31:24 -08001944
Joel Becker7d1fe092010-04-13 14:30:19 +08001945 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1946 res, &bits_left);
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001947 if (!status) {
Joseph Qi1d1aff82015-11-05 18:43:58 -08001948 if (ocfs2_is_cluster_bitmap(ac->ac_inode))
1949 hint = res->sr_bg_blkno;
1950 else
1951 hint = ocfs2_group_from_res(res);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001952 goto set_hint;
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001953 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001954 if (status < 0 && status != -ENOSPC) {
1955 mlog_errno(status);
1956 goto bail;
1957 }
1958
Tao Ma2f73e132011-02-22 08:22:33 +08001959 trace_ocfs2_claim_suballoc_bits(victim);
Mark Fashehccd979b2005-12-15 14:31:24 -08001960
1961 /* If we didn't pick a good victim, then just default to
1962 * searching each chain in order. Don't allow chain relinking
1963 * because we only calculate enough journal credits for one
1964 * relink per alloc. */
Xiaowei.Hu309a85b2013-02-27 17:02:49 -08001965 ac->ac_disable_chain_relink = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001966 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1967 if (i == victim)
1968 continue;
1969 if (!cl->cl_recs[i].c_free)
1970 continue;
1971
1972 ac->ac_chain = i;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001973 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001974 res, &bits_left);
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001975 if (!status) {
1976 hint = ocfs2_group_from_res(res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001977 break;
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001978 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001979 if (status < 0 && status != -ENOSPC) {
1980 mlog_errno(status);
1981 goto bail;
1982 }
1983 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001984
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001985set_hint:
1986 if (status != -ENOSPC) {
1987 /* If the next search of this group is not likely to
1988 * yield a suitable extent, then we reset the last
1989 * group hint so as to not waste a disk read */
1990 if (bits_left < min_bits)
1991 ac->ac_last_group = 0;
1992 else
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001993 ac->ac_last_group = hint;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001994 }
1995
1996bail:
Tao Mac1e8d352011-03-07 16:43:21 +08001997 if (status)
1998 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001999 return status;
2000}
2001
Joel Becker1ed9b772010-05-06 13:59:06 +08002002int ocfs2_claim_metadata(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002003 struct ocfs2_alloc_context *ac,
2004 u32 bits_wanted,
Joel Becker2b6cb572010-03-26 10:09:15 +08002005 u64 *suballoc_loc,
Mark Fashehccd979b2005-12-15 14:31:24 -08002006 u16 *suballoc_bit_start,
2007 unsigned int *num_bits,
2008 u64 *blkno_start)
2009{
2010 int status;
Joel Beckerba206632010-03-26 10:08:59 +08002011 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
Mark Fashehccd979b2005-12-15 14:31:24 -08002012
2013 BUG_ON(!ac);
2014 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
2015 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
Mark Fashehccd979b2005-12-15 14:31:24 -08002016
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002017 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002018 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002019 bits_wanted,
2020 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08002021 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002022 if (status < 0) {
2023 mlog_errno(status);
2024 goto bail;
2025 }
Joel Becker1ed9b772010-05-06 13:59:06 +08002026 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
Mark Fashehccd979b2005-12-15 14:31:24 -08002027
Joel Becker2b6cb572010-03-26 10:09:15 +08002028 *suballoc_loc = res.sr_bg_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08002029 *suballoc_bit_start = res.sr_bit_offset;
Joel Beckerba206632010-03-26 10:08:59 +08002030 *blkno_start = res.sr_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08002031 ac->ac_bits_given += res.sr_bits;
2032 *num_bits = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08002033 status = 0;
2034bail:
Tao Mac1e8d352011-03-07 16:43:21 +08002035 if (status)
2036 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002037 return status;
2038}
2039
Tao Ma13821152009-02-25 00:53:23 +08002040static void ocfs2_init_inode_ac_group(struct inode *dir,
Tao Maabf1b3c2010-04-27 08:30:36 +08002041 struct buffer_head *parent_di_bh,
Tao Ma13821152009-02-25 00:53:23 +08002042 struct ocfs2_alloc_context *ac)
2043{
Tao Maabf1b3c2010-04-27 08:30:36 +08002044 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_di_bh->b_data;
Tao Ma13821152009-02-25 00:53:23 +08002045 /*
2046 * Try to allocate inodes from some specific group.
2047 *
2048 * If the parent dir has recorded the last group used in allocation,
2049 * cool, use it. Otherwise if we try to allocate new inode from the
2050 * same slot the parent dir belongs to, use the same chunk.
2051 *
2052 * We are very careful here to avoid the mistake of setting
2053 * ac_last_group to a group descriptor from a different (unlocked) slot.
2054 */
2055 if (OCFS2_I(dir)->ip_last_used_group &&
2056 OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
2057 ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
Tao Maabf1b3c2010-04-27 08:30:36 +08002058 else if (le16_to_cpu(di->i_suballoc_slot) == ac->ac_alloc_slot) {
2059 if (di->i_suballoc_loc)
2060 ac->ac_last_group = le64_to_cpu(di->i_suballoc_loc);
2061 else
2062 ac->ac_last_group = ocfs2_which_suballoc_group(
2063 le64_to_cpu(di->i_blkno),
2064 le16_to_cpu(di->i_suballoc_bit));
2065 }
Tao Ma13821152009-02-25 00:53:23 +08002066}
2067
2068static inline void ocfs2_save_inode_ac_group(struct inode *dir,
2069 struct ocfs2_alloc_context *ac)
2070{
2071 OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
2072 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
2073}
2074
Mark Fashehe49e2762010-08-13 15:15:17 -07002075int ocfs2_find_new_inode_loc(struct inode *dir,
2076 struct buffer_head *parent_fe_bh,
2077 struct ocfs2_alloc_context *ac,
2078 u64 *fe_blkno)
2079{
2080 int ret;
2081 handle_t *handle = NULL;
2082 struct ocfs2_suballoc_result *res;
2083
2084 BUG_ON(!ac);
2085 BUG_ON(ac->ac_bits_given != 0);
2086 BUG_ON(ac->ac_bits_wanted != 1);
2087 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
2088
2089 res = kzalloc(sizeof(*res), GFP_NOFS);
2090 if (res == NULL) {
2091 ret = -ENOMEM;
2092 mlog_errno(ret);
2093 goto out;
2094 }
2095
2096 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
2097
2098 /*
2099 * The handle started here is for chain relink. Alternatively,
2100 * we could just disable relink for these calls.
2101 */
2102 handle = ocfs2_start_trans(OCFS2_SB(dir->i_sb), OCFS2_SUBALLOC_ALLOC);
2103 if (IS_ERR(handle)) {
2104 ret = PTR_ERR(handle);
2105 handle = NULL;
2106 mlog_errno(ret);
2107 goto out;
2108 }
2109
2110 /*
2111 * This will instruct ocfs2_claim_suballoc_bits and
2112 * ocfs2_search_one_group to search but save actual allocation
2113 * for later.
2114 */
2115 ac->ac_find_loc_only = 1;
2116
2117 ret = ocfs2_claim_suballoc_bits(ac, handle, 1, 1, res);
2118 if (ret < 0) {
2119 mlog_errno(ret);
2120 goto out;
2121 }
2122
2123 ac->ac_find_loc_priv = res;
2124 *fe_blkno = res->sr_blkno;
Darrick J. Wong6fdb7022014-04-03 14:47:08 -07002125 ocfs2_update_inode_fsync_trans(handle, dir, 0);
Mark Fashehe49e2762010-08-13 15:15:17 -07002126out:
2127 if (handle)
2128 ocfs2_commit_trans(OCFS2_SB(dir->i_sb), handle);
2129
2130 if (ret)
2131 kfree(res);
2132
2133 return ret;
2134}
2135
2136int ocfs2_claim_new_inode_at_loc(handle_t *handle,
2137 struct inode *dir,
2138 struct ocfs2_alloc_context *ac,
2139 u64 *suballoc_loc,
2140 u16 *suballoc_bit,
2141 u64 di_blkno)
2142{
2143 int ret;
2144 u16 chain;
2145 struct ocfs2_suballoc_result *res = ac->ac_find_loc_priv;
2146 struct buffer_head *bg_bh = NULL;
2147 struct ocfs2_group_desc *bg;
2148 struct ocfs2_dinode *di = (struct ocfs2_dinode *) ac->ac_bh->b_data;
2149
2150 /*
2151 * Since di_blkno is being passed back in, we check for any
2152 * inconsistencies which may have happened between
2153 * calls. These are code bugs as di_blkno is not expected to
2154 * change once returned from ocfs2_find_new_inode_loc()
2155 */
2156 BUG_ON(res->sr_blkno != di_blkno);
2157
2158 ret = ocfs2_read_group_descriptor(ac->ac_inode, di,
2159 res->sr_bg_stable_blkno, &bg_bh);
2160 if (ret) {
2161 mlog_errno(ret);
2162 goto out;
2163 }
2164
2165 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
2166 chain = le16_to_cpu(bg->bg_chain);
2167
2168 ret = ocfs2_alloc_dinode_update_counts(ac->ac_inode, handle,
2169 ac->ac_bh, res->sr_bits,
2170 chain);
2171 if (ret) {
2172 mlog_errno(ret);
2173 goto out;
2174 }
2175
2176 ret = ocfs2_block_group_set_bits(handle,
2177 ac->ac_inode,
2178 bg,
2179 bg_bh,
2180 res->sr_bit_offset,
2181 res->sr_bits);
2182 if (ret < 0) {
Younger Liudb66c712014-04-03 14:47:10 -07002183 ocfs2_rollback_alloc_dinode_counts(ac->ac_inode,
2184 ac->ac_bh, res->sr_bits, chain);
Mark Fashehe49e2762010-08-13 15:15:17 -07002185 mlog_errno(ret);
2186 goto out;
2187 }
2188
Tao Ma2f73e132011-02-22 08:22:33 +08002189 trace_ocfs2_claim_new_inode_at_loc((unsigned long long)di_blkno,
2190 res->sr_bits);
Mark Fashehe49e2762010-08-13 15:15:17 -07002191
2192 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
2193
2194 BUG_ON(res->sr_bits != 1);
2195
2196 *suballoc_loc = res->sr_bg_blkno;
2197 *suballoc_bit = res->sr_bit_offset;
2198 ac->ac_bits_given++;
2199 ocfs2_save_inode_ac_group(dir, ac);
2200
2201out:
2202 brelse(bg_bh);
2203
2204 return ret;
2205}
2206
Joel Becker1ed9b772010-05-06 13:59:06 +08002207int ocfs2_claim_new_inode(handle_t *handle,
Tao Ma13821152009-02-25 00:53:23 +08002208 struct inode *dir,
2209 struct buffer_head *parent_fe_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08002210 struct ocfs2_alloc_context *ac,
Joel Becker2b6cb572010-03-26 10:09:15 +08002211 u64 *suballoc_loc,
Mark Fashehccd979b2005-12-15 14:31:24 -08002212 u16 *suballoc_bit,
2213 u64 *fe_blkno)
2214{
2215 int status;
Joel Becker2b6cb572010-03-26 10:09:15 +08002216 struct ocfs2_suballoc_result res;
Mark Fashehccd979b2005-12-15 14:31:24 -08002217
Mark Fashehccd979b2005-12-15 14:31:24 -08002218 BUG_ON(!ac);
2219 BUG_ON(ac->ac_bits_given != 0);
2220 BUG_ON(ac->ac_bits_wanted != 1);
2221 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002222
Tao Ma13821152009-02-25 00:53:23 +08002223 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
2224
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002225 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002226 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002227 1,
2228 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08002229 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002230 if (status < 0) {
2231 mlog_errno(status);
2232 goto bail;
2233 }
Joel Becker1ed9b772010-05-06 13:59:06 +08002234 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
Mark Fashehccd979b2005-12-15 14:31:24 -08002235
Joel Becker7d1fe092010-04-13 14:30:19 +08002236 BUG_ON(res.sr_bits != 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08002237
Joel Becker2b6cb572010-03-26 10:09:15 +08002238 *suballoc_loc = res.sr_bg_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08002239 *suballoc_bit = res.sr_bit_offset;
Joel Beckerba206632010-03-26 10:08:59 +08002240 *fe_blkno = res.sr_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08002241 ac->ac_bits_given++;
Tao Ma13821152009-02-25 00:53:23 +08002242 ocfs2_save_inode_ac_group(dir, ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08002243 status = 0;
2244bail:
Tao Mac1e8d352011-03-07 16:43:21 +08002245 if (status)
2246 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002247 return status;
2248}
2249
2250/* translate a group desc. blkno and it's bitmap offset into
2251 * disk cluster offset. */
2252static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
2253 u64 bg_blkno,
2254 u16 bg_bit_off)
2255{
2256 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2257 u32 cluster = 0;
2258
2259 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2260
2261 if (bg_blkno != osb->first_cluster_group_blkno)
2262 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
2263 cluster += (u32) bg_bit_off;
2264 return cluster;
2265}
2266
2267/* given a cluster offset, calculate which block group it belongs to
2268 * and return that block offset. */
Tao Mad6590722007-12-18 15:47:03 +08002269u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
Mark Fashehccd979b2005-12-15 14:31:24 -08002270{
2271 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2272 u32 group_no;
2273
2274 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2275
2276 group_no = cluster / osb->bitmap_cpg;
2277 if (!group_no)
2278 return osb->first_cluster_group_blkno;
2279 return ocfs2_clusters_to_blocks(inode->i_sb,
2280 group_no * osb->bitmap_cpg);
2281}
2282
2283/* given the block number of a cluster start, calculate which cluster
2284 * group and descriptor bitmap offset that corresponds to. */
2285static inline void ocfs2_block_to_cluster_group(struct inode *inode,
2286 u64 data_blkno,
2287 u64 *bg_blkno,
2288 u16 *bg_bit_off)
2289{
2290 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2291 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
2292
2293 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2294
2295 *bg_blkno = ocfs2_which_cluster_group(inode,
2296 data_cluster);
2297
2298 if (*bg_blkno == osb->first_cluster_group_blkno)
2299 *bg_bit_off = (u16) data_cluster;
2300 else
2301 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
2302 data_blkno - *bg_blkno);
2303}
2304
2305/*
2306 * min_bits - minimum contiguous chunk from this total allocation we
2307 * can handle. set to what we asked for originally for a full
2308 * contig. allocation, set to '1' to indicate we can deal with extents
2309 * of any size.
2310 */
Joel Becker1ed9b772010-05-06 13:59:06 +08002311int __ocfs2_claim_clusters(handle_t *handle,
Mark Fasheh415cb802007-09-16 20:10:16 -07002312 struct ocfs2_alloc_context *ac,
2313 u32 min_clusters,
2314 u32 max_clusters,
2315 u32 *cluster_start,
2316 u32 *num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08002317{
2318 int status;
Mark Fasheh415cb802007-09-16 20:10:16 -07002319 unsigned int bits_wanted = max_clusters;
Joel Beckerba206632010-03-26 10:08:59 +08002320 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
Joel Becker1ed9b772010-05-06 13:59:06 +08002321 struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002322
Mark Fashehccd979b2005-12-15 14:31:24 -08002323 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
2324
2325 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
2326 && ac->ac_which != OCFS2_AC_USE_MAIN);
Mark Fashehccd979b2005-12-15 14:31:24 -08002327
2328 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
Mark Fasheh33d5d382010-02-24 13:34:09 -08002329 WARN_ON(min_clusters > 1);
2330
Mark Fashehccd979b2005-12-15 14:31:24 -08002331 status = ocfs2_claim_local_alloc_bits(osb,
2332 handle,
2333 ac,
2334 bits_wanted,
2335 cluster_start,
2336 num_clusters);
2337 if (!status)
2338 atomic_inc(&osb->alloc_stats.local_data);
2339 } else {
2340 if (min_clusters > (osb->bitmap_cpg - 1)) {
2341 /* The only paths asking for contiguousness
2342 * should know about this already. */
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08002343 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
2344 "group bitmap size %u!\n", min_clusters,
2345 osb->bitmap_cpg);
Mark Fashehccd979b2005-12-15 14:31:24 -08002346 status = -ENOSPC;
2347 goto bail;
2348 }
2349 /* clamp the current request down to a realistic size. */
2350 if (bits_wanted > (osb->bitmap_cpg - 1))
2351 bits_wanted = osb->bitmap_cpg - 1;
2352
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002353 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002354 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002355 bits_wanted,
2356 min_clusters,
Joel Becker7d1fe092010-04-13 14:30:19 +08002357 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002358 if (!status) {
Joel Beckerba206632010-03-26 10:08:59 +08002359 BUG_ON(res.sr_blkno); /* cluster alloc can't set */
Mark Fashehccd979b2005-12-15 14:31:24 -08002360 *cluster_start =
2361 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
Joel Becker7d1fe092010-04-13 14:30:19 +08002362 res.sr_bg_blkno,
2363 res.sr_bit_offset);
Mark Fashehccd979b2005-12-15 14:31:24 -08002364 atomic_inc(&osb->alloc_stats.bitmap_data);
Tao Ma47119542010-04-22 14:09:15 +08002365 *num_clusters = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08002366 }
2367 }
2368 if (status < 0) {
2369 if (status != -ENOSPC)
2370 mlog_errno(status);
2371 goto bail;
2372 }
2373
Tao Ma47119542010-04-22 14:09:15 +08002374 ac->ac_bits_given += *num_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08002375
2376bail:
Tao Mac1e8d352011-03-07 16:43:21 +08002377 if (status)
2378 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002379 return status;
2380}
2381
Joel Becker1ed9b772010-05-06 13:59:06 +08002382int ocfs2_claim_clusters(handle_t *handle,
Mark Fasheh415cb802007-09-16 20:10:16 -07002383 struct ocfs2_alloc_context *ac,
2384 u32 min_clusters,
2385 u32 *cluster_start,
2386 u32 *num_clusters)
2387{
2388 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
2389
Joel Becker1ed9b772010-05-06 13:59:06 +08002390 return __ocfs2_claim_clusters(handle, ac, min_clusters,
Mark Fasheh415cb802007-09-16 20:10:16 -07002391 bits_wanted, cluster_start, num_clusters);
2392}
2393
Mark Fashehb4414ee2010-03-11 18:31:09 -08002394static int ocfs2_block_group_clear_bits(handle_t *handle,
2395 struct inode *alloc_inode,
2396 struct ocfs2_group_desc *bg,
2397 struct buffer_head *group_bh,
2398 unsigned int bit_off,
2399 unsigned int num_bits,
2400 void (*undo_fn)(unsigned int bit,
2401 unsigned long *bmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002402{
2403 int status;
2404 unsigned int tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -08002405 struct ocfs2_group_desc *undo_bg = NULL;
2406
Joel Becker42035302008-11-13 14:49:15 -08002407 /* The caller got this descriptor from
2408 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
2409 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08002410
Tao Ma2f73e132011-02-22 08:22:33 +08002411 trace_ocfs2_block_group_clear_bits(bit_off, num_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08002412
Mark Fashehb4414ee2010-03-11 18:31:09 -08002413 BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
Joel Becker0cf2f762009-02-12 16:41:25 -08002414 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
Mark Fashehb4414ee2010-03-11 18:31:09 -08002415 group_bh,
2416 undo_fn ?
2417 OCFS2_JOURNAL_ACCESS_UNDO :
2418 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002419 if (status < 0) {
2420 mlog_errno(status);
2421 goto bail;
2422 }
2423
Mark Fashehb4414ee2010-03-11 18:31:09 -08002424 if (undo_fn) {
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002425 jbd_lock_bh_state(group_bh);
2426 undo_bg = (struct ocfs2_group_desc *)
2427 bh2jh(group_bh)->b_committed_data;
2428 BUG_ON(!undo_bg);
2429 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002430
2431 tmp = num_bits;
2432 while(tmp--) {
2433 ocfs2_clear_bit((bit_off + tmp),
2434 (unsigned long *) bg->bg_bitmap);
Mark Fashehb4414ee2010-03-11 18:31:09 -08002435 if (undo_fn)
2436 undo_fn(bit_off + tmp,
2437 (unsigned long *) undo_bg->bg_bitmap);
Mark Fashehccd979b2005-12-15 14:31:24 -08002438 }
2439 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
Srinivas Eeda9b5cd102010-10-05 15:53:06 -07002440 if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) {
Changwei Gee75ed712018-01-31 16:15:29 -08002441 if (undo_fn)
2442 jbd_unlock_bh_state(group_bh);
Joe Perches7ecef142015-09-04 15:44:51 -07002443 return ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit count %u but claims %u are freed. num_bits %d\n",
2444 (unsigned long long)le64_to_cpu(bg->bg_blkno),
2445 le16_to_cpu(bg->bg_bits),
2446 le16_to_cpu(bg->bg_free_bits_count),
2447 num_bits);
Srinivas Eeda9b5cd102010-10-05 15:53:06 -07002448 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002449
Mark Fashehb4414ee2010-03-11 18:31:09 -08002450 if (undo_fn)
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002451 jbd_unlock_bh_state(group_bh);
2452
Joel Beckerec20cec2010-03-19 14:13:52 -07002453 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002454bail:
2455 return status;
2456}
2457
2458/*
2459 * expects the suballoc inode to already be locked.
2460 */
Mark Fashehb4414ee2010-03-11 18:31:09 -08002461static int _ocfs2_free_suballoc_bits(handle_t *handle,
2462 struct inode *alloc_inode,
2463 struct buffer_head *alloc_bh,
2464 unsigned int start_bit,
2465 u64 bg_blkno,
2466 unsigned int count,
2467 void (*undo_fn)(unsigned int bit,
2468 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002469{
2470 int status = 0;
2471 u32 tmp_used;
Mark Fashehccd979b2005-12-15 14:31:24 -08002472 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
2473 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
2474 struct buffer_head *group_bh = NULL;
2475 struct ocfs2_group_desc *group;
2476
Joel Becker10995aa2008-11-13 14:49:12 -08002477 /* The alloc_bh comes from ocfs2_free_dinode() or
2478 * ocfs2_free_clusters(). The callers have all locked the
2479 * allocator and gotten alloc_bh from the lock call. This
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002480 * validates the dinode buffer. Any corruption that has happened
Joel Becker10995aa2008-11-13 14:49:12 -08002481 * is a code bug. */
2482 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -08002483 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2484
Tao Ma2f73e132011-02-22 08:22:33 +08002485 trace_ocfs2_free_suballoc_bits(
2486 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno,
2487 (unsigned long long)bg_blkno,
2488 start_bit, count);
Mark Fashehccd979b2005-12-15 14:31:24 -08002489
Joel Becker68f64d42008-11-13 14:49:14 -08002490 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
2491 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002492 if (status < 0) {
2493 mlog_errno(status);
2494 goto bail;
2495 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002496 group = (struct ocfs2_group_desc *) group_bh->b_data;
Joel Becker68f64d42008-11-13 14:49:14 -08002497
Mark Fashehccd979b2005-12-15 14:31:24 -08002498 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
2499
2500 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
2501 group, group_bh,
Mark Fashehb4414ee2010-03-11 18:31:09 -08002502 start_bit, count, undo_fn);
Mark Fashehccd979b2005-12-15 14:31:24 -08002503 if (status < 0) {
2504 mlog_errno(status);
2505 goto bail;
2506 }
2507
Joel Becker0cf2f762009-02-12 16:41:25 -08002508 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
2509 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002510 if (status < 0) {
2511 mlog_errno(status);
Joseph Qia47726b2015-04-14 15:43:27 -07002512 ocfs2_block_group_set_bits(handle, alloc_inode, group, group_bh,
2513 start_bit, count);
Mark Fashehccd979b2005-12-15 14:31:24 -08002514 goto bail;
2515 }
2516
2517 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2518 count);
2519 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2520 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
Joel Beckerec20cec2010-03-19 14:13:52 -07002521 ocfs2_journal_dirty(handle, alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002522
2523bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07002524 brelse(group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002525
Tao Mac1e8d352011-03-07 16:43:21 +08002526 if (status)
2527 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002528 return status;
2529}
2530
Mark Fashehb4414ee2010-03-11 18:31:09 -08002531int ocfs2_free_suballoc_bits(handle_t *handle,
2532 struct inode *alloc_inode,
2533 struct buffer_head *alloc_bh,
2534 unsigned int start_bit,
2535 u64 bg_blkno,
2536 unsigned int count)
2537{
2538 return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
2539 start_bit, bg_blkno, count, NULL);
2540}
2541
Mark Fasheh1fabe142006-10-09 18:11:45 -07002542int ocfs2_free_dinode(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002543 struct inode *inode_alloc_inode,
2544 struct buffer_head *inode_alloc_bh,
2545 struct ocfs2_dinode *di)
2546{
2547 u64 blk = le64_to_cpu(di->i_blkno);
2548 u16 bit = le16_to_cpu(di->i_suballoc_bit);
2549 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2550
Tao Ma74380c42010-03-22 14:20:18 +08002551 if (di->i_suballoc_loc)
2552 bg_blkno = le64_to_cpu(di->i_suballoc_loc);
Mark Fashehccd979b2005-12-15 14:31:24 -08002553 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2554 inode_alloc_bh, bit, bg_blkno, 1);
2555}
2556
Mark Fashehb4414ee2010-03-11 18:31:09 -08002557static int _ocfs2_free_clusters(handle_t *handle,
2558 struct inode *bitmap_inode,
2559 struct buffer_head *bitmap_bh,
2560 u64 start_blk,
2561 unsigned int num_clusters,
2562 void (*undo_fn)(unsigned int bit,
2563 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002564{
2565 int status;
2566 u16 bg_start_bit;
2567 u64 bg_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08002568
2569 /* You can't ever have a contiguous set of clusters
2570 * bigger than a block group bitmap so we never have to worry
Tao Maef6b6892011-02-21 11:10:44 +08002571 * about looping on them.
2572 * This is expensive. We can safely remove once this stuff has
Mark Fashehccd979b2005-12-15 14:31:24 -08002573 * gotten tested really well. */
Changwei Gedd7b5f92018-01-31 16:14:40 -08002574 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb,
2575 ocfs2_blocks_to_clusters(bitmap_inode->i_sb,
2576 start_blk)));
Mark Fashehccd979b2005-12-15 14:31:24 -08002577
Mark Fashehccd979b2005-12-15 14:31:24 -08002578
2579 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2580 &bg_start_bit);
2581
Tao Ma2f73e132011-02-22 08:22:33 +08002582 trace_ocfs2_free_clusters((unsigned long long)bg_blkno,
2583 (unsigned long long)start_blk,
2584 bg_start_bit, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08002585
Mark Fashehb4414ee2010-03-11 18:31:09 -08002586 status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2587 bg_start_bit, bg_blkno,
2588 num_clusters, undo_fn);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002589 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002590 mlog_errno(status);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002591 goto out;
2592 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002593
Mark Fasheh9c7af402008-07-28 18:02:53 -07002594 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
2595 num_clusters);
2596
2597out:
Tao Mac1e8d352011-03-07 16:43:21 +08002598 if (status)
2599 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002600 return status;
2601}
2602
Mark Fashehb4414ee2010-03-11 18:31:09 -08002603int ocfs2_free_clusters(handle_t *handle,
2604 struct inode *bitmap_inode,
2605 struct buffer_head *bitmap_bh,
2606 u64 start_blk,
2607 unsigned int num_clusters)
2608{
2609 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2610 start_blk, num_clusters,
2611 _ocfs2_set_bit);
2612}
2613
2614/*
2615 * Give never-used clusters back to the global bitmap. We don't need
2616 * to protect these bits in the undo buffer.
2617 */
2618int ocfs2_release_clusters(handle_t *handle,
2619 struct inode *bitmap_inode,
2620 struct buffer_head *bitmap_bh,
2621 u64 start_blk,
2622 unsigned int num_clusters)
2623{
2624 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2625 start_blk, num_clusters,
2626 _ocfs2_clear_bit);
2627}
2628
Mark Fashehccd979b2005-12-15 14:31:24 -08002629static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2630{
2631 printk("Block Group:\n");
2632 printk("bg_signature: %s\n", bg->bg_signature);
2633 printk("bg_size: %u\n", bg->bg_size);
2634 printk("bg_bits: %u\n", bg->bg_bits);
2635 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2636 printk("bg_chain: %u\n", bg->bg_chain);
2637 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
Mark Fashehb06970532006-03-03 10:24:33 -08002638 printk("bg_next_group: %llu\n",
2639 (unsigned long long)bg->bg_next_group);
2640 printk("bg_parent_dinode: %llu\n",
2641 (unsigned long long)bg->bg_parent_dinode);
2642 printk("bg_blkno: %llu\n",
2643 (unsigned long long)bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002644}
2645
2646static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2647{
2648 int i;
2649
Mark Fashehb06970532006-03-03 10:24:33 -08002650 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002651 printk("i_signature: %s\n", fe->i_signature);
Mark Fashehb06970532006-03-03 10:24:33 -08002652 printk("i_size: %llu\n",
2653 (unsigned long long)fe->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08002654 printk("i_clusters: %u\n", fe->i_clusters);
2655 printk("i_generation: %u\n",
2656 le32_to_cpu(fe->i_generation));
2657 printk("id1.bitmap1.i_used: %u\n",
2658 le32_to_cpu(fe->id1.bitmap1.i_used));
2659 printk("id1.bitmap1.i_total: %u\n",
2660 le32_to_cpu(fe->id1.bitmap1.i_total));
2661 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2662 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2663 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2664 printk("id2.i_chain.cl_next_free_rec: %u\n",
2665 fe->id2.i_chain.cl_next_free_rec);
2666 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2667 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2668 fe->id2.i_chain.cl_recs[i].c_free);
2669 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2670 fe->id2.i_chain.cl_recs[i].c_total);
Mark Fashehb06970532006-03-03 10:24:33 -08002671 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2672 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002673 }
2674}
Tao Mae7d4cb62008-08-18 17:38:44 +08002675
2676/*
2677 * For a given allocation, determine which allocators will need to be
2678 * accessed, and lock them, reserving the appropriate number of bits.
2679 *
2680 * Sparse file systems call this from ocfs2_write_begin_nolock()
2681 * and ocfs2_allocate_unwritten_extents().
2682 *
2683 * File systems which don't support holes call this from
2684 * ocfs2_extend_allocation().
2685 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07002686int ocfs2_lock_allocators(struct inode *inode,
2687 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08002688 u32 clusters_to_add, u32 extents_to_split,
2689 struct ocfs2_alloc_context **data_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07002690 struct ocfs2_alloc_context **meta_ac)
Tao Mae7d4cb62008-08-18 17:38:44 +08002691{
2692 int ret = 0, num_free_extents;
2693 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2694 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2695
2696 *meta_ac = NULL;
2697 if (data_ac)
2698 *data_ac = NULL;
2699
2700 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2701
Jun Piao964f14a2017-09-06 16:19:11 -07002702 num_free_extents = ocfs2_num_free_extents(et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002703 if (num_free_extents < 0) {
2704 ret = num_free_extents;
2705 mlog_errno(ret);
2706 goto out;
2707 }
2708
2709 /*
2710 * Sparse allocation file systems need to be more conservative
2711 * with reserving room for expansion - the actual allocation
2712 * happens while we've got a journal handle open so re-taking
2713 * a cluster lock (because we ran out of room for another
2714 * extent) will violate ordering rules.
2715 *
2716 * Most of the time we'll only be seeing this 1 cluster at a time
2717 * anyway.
2718 *
2719 * Always lock for any unwritten extents - we might want to
2720 * add blocks during a split.
2721 */
2722 if (!num_free_extents ||
2723 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
Joel Beckerf99b9b72008-08-20 19:36:33 -07002724 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
Tao Mae7d4cb62008-08-18 17:38:44 +08002725 if (ret < 0) {
2726 if (ret != -ENOSPC)
2727 mlog_errno(ret);
2728 goto out;
2729 }
2730 }
2731
2732 if (clusters_to_add == 0)
2733 goto out;
2734
2735 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2736 if (ret < 0) {
2737 if (ret != -ENOSPC)
2738 mlog_errno(ret);
2739 goto out;
2740 }
2741
2742out:
2743 if (ret) {
2744 if (*meta_ac) {
2745 ocfs2_free_alloc_context(*meta_ac);
2746 *meta_ac = NULL;
2747 }
2748
2749 /*
2750 * We cannot have an error and a non null *data_ac.
2751 */
2752 }
2753
2754 return ret;
2755}
wengang wang6ca497a2009-03-06 21:29:10 +08002756
2757/*
2758 * Read the inode specified by blkno to get suballoc_slot and
2759 * suballoc_bit.
2760 */
2761static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
Tao Ma889f0042010-09-02 13:10:10 +08002762 u16 *suballoc_slot, u64 *group_blkno,
2763 u16 *suballoc_bit)
wengang wang6ca497a2009-03-06 21:29:10 +08002764{
2765 int status;
2766 struct buffer_head *inode_bh = NULL;
2767 struct ocfs2_dinode *inode_fe;
2768
Tao Ma2f73e132011-02-22 08:22:33 +08002769 trace_ocfs2_get_suballoc_slot_bit((unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002770
2771 /* dirty read disk */
2772 status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
2773 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002774 mlog(ML_ERROR, "read block %llu failed %d\n",
2775 (unsigned long long)blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002776 goto bail;
2777 }
2778
2779 inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
2780 if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
Joel Becker5b09b502009-04-21 16:31:20 -07002781 mlog(ML_ERROR, "invalid inode %llu requested\n",
2782 (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002783 status = -EINVAL;
2784 goto bail;
2785 }
2786
Tao Ma0fba8132009-03-19 05:08:43 +08002787 if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
wengang wang6ca497a2009-03-06 21:29:10 +08002788 (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
2789 mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
Joel Becker5b09b502009-04-21 16:31:20 -07002790 (unsigned long long)blkno,
2791 (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
wengang wang6ca497a2009-03-06 21:29:10 +08002792 status = -EINVAL;
2793 goto bail;
2794 }
2795
2796 if (suballoc_slot)
2797 *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
2798 if (suballoc_bit)
2799 *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
Tao Ma889f0042010-09-02 13:10:10 +08002800 if (group_blkno)
2801 *group_blkno = le64_to_cpu(inode_fe->i_suballoc_loc);
wengang wang6ca497a2009-03-06 21:29:10 +08002802
2803bail:
2804 brelse(inode_bh);
2805
Tao Mac1e8d352011-03-07 16:43:21 +08002806 if (status)
2807 mlog_errno(status);
wengang wang6ca497a2009-03-06 21:29:10 +08002808 return status;
2809}
2810
2811/*
2812 * test whether bit is SET in allocator bitmap or not. on success, 0
2813 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2814 * is returned and *res is meaningless. Call this after you have
2815 * cluster locked against suballoc, or you may get a result based on
2816 * non-up2date contents
2817 */
2818static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2819 struct inode *suballoc,
Tao Ma889f0042010-09-02 13:10:10 +08002820 struct buffer_head *alloc_bh,
2821 u64 group_blkno, u64 blkno,
wengang wang6ca497a2009-03-06 21:29:10 +08002822 u16 bit, int *res)
2823{
Tao Maabf1b3c2010-04-27 08:30:36 +08002824 struct ocfs2_dinode *alloc_di;
wengang wang6ca497a2009-03-06 21:29:10 +08002825 struct ocfs2_group_desc *group;
2826 struct buffer_head *group_bh = NULL;
2827 u64 bg_blkno;
2828 int status;
2829
Tao Ma2f73e132011-02-22 08:22:33 +08002830 trace_ocfs2_test_suballoc_bit((unsigned long long)blkno,
2831 (unsigned int)bit);
wengang wang6ca497a2009-03-06 21:29:10 +08002832
Tao Maabf1b3c2010-04-27 08:30:36 +08002833 alloc_di = (struct ocfs2_dinode *)alloc_bh->b_data;
2834 if ((bit + 1) > ocfs2_bits_per_group(&alloc_di->id2.i_chain)) {
wengang wang6ca497a2009-03-06 21:29:10 +08002835 mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
2836 (unsigned int)bit,
Tao Maabf1b3c2010-04-27 08:30:36 +08002837 ocfs2_bits_per_group(&alloc_di->id2.i_chain));
wengang wang6ca497a2009-03-06 21:29:10 +08002838 status = -EINVAL;
2839 goto bail;
2840 }
2841
Tao Ma889f0042010-09-02 13:10:10 +08002842 bg_blkno = group_blkno ? group_blkno :
2843 ocfs2_which_suballoc_group(blkno, bit);
Tao Maabf1b3c2010-04-27 08:30:36 +08002844 status = ocfs2_read_group_descriptor(suballoc, alloc_di, bg_blkno,
wengang wang6ca497a2009-03-06 21:29:10 +08002845 &group_bh);
2846 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002847 mlog(ML_ERROR, "read group %llu failed %d\n",
2848 (unsigned long long)bg_blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002849 goto bail;
2850 }
2851
2852 group = (struct ocfs2_group_desc *) group_bh->b_data;
2853 *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
2854
2855bail:
2856 brelse(group_bh);
2857
Tao Mac1e8d352011-03-07 16:43:21 +08002858 if (status)
2859 mlog_errno(status);
wengang wang6ca497a2009-03-06 21:29:10 +08002860 return status;
2861}
2862
2863/*
2864 * Test if the bit representing this inode (blkno) is set in the
2865 * suballocator.
2866 *
2867 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2868 *
2869 * In the event of failure, a negative value is returned and *res is
2870 * meaningless.
2871 *
2872 * Callers must make sure to hold nfs_sync_lock to prevent
2873 * ocfs2_delete_inode() on another node from accessing the same
2874 * suballocator concurrently.
2875 */
2876int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
2877{
2878 int status;
Tao Ma889f0042010-09-02 13:10:10 +08002879 u64 group_blkno = 0;
wengang wang6ca497a2009-03-06 21:29:10 +08002880 u16 suballoc_bit = 0, suballoc_slot = 0;
2881 struct inode *inode_alloc_inode;
2882 struct buffer_head *alloc_bh = NULL;
2883
Tao Ma2f73e132011-02-22 08:22:33 +08002884 trace_ocfs2_test_inode_bit((unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002885
2886 status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
Tao Ma889f0042010-09-02 13:10:10 +08002887 &group_blkno, &suballoc_bit);
wengang wang6ca497a2009-03-06 21:29:10 +08002888 if (status < 0) {
2889 mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
2890 goto bail;
2891 }
2892
2893 inode_alloc_inode =
2894 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
2895 suballoc_slot);
2896 if (!inode_alloc_inode) {
2897 /* the error code could be inaccurate, but we are not able to
2898 * get the correct one. */
2899 status = -EINVAL;
2900 mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
2901 (u32)suballoc_slot);
2902 goto bail;
2903 }
2904
Al Viro59551022016-01-22 15:40:57 -05002905 inode_lock(inode_alloc_inode);
wengang wang6ca497a2009-03-06 21:29:10 +08002906 status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
2907 if (status < 0) {
Al Viro59551022016-01-22 15:40:57 -05002908 inode_unlock(inode_alloc_inode);
jiangyiwen7dc3e832014-04-03 14:47:12 -07002909 iput(inode_alloc_inode);
wengang wang6ca497a2009-03-06 21:29:10 +08002910 mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
2911 (u32)suballoc_slot, status);
2912 goto bail;
2913 }
2914
2915 status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
Tao Ma889f0042010-09-02 13:10:10 +08002916 group_blkno, blkno, suballoc_bit, res);
wengang wang6ca497a2009-03-06 21:29:10 +08002917 if (status < 0)
2918 mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
2919
2920 ocfs2_inode_unlock(inode_alloc_inode, 0);
Al Viro59551022016-01-22 15:40:57 -05002921 inode_unlock(inode_alloc_inode);
wengang wang6ca497a2009-03-06 21:29:10 +08002922
2923 iput(inode_alloc_inode);
2924 brelse(alloc_bh);
2925bail:
Tao Mac1e8d352011-03-07 16:43:21 +08002926 if (status)
2927 mlog_errno(status);
wengang wang6ca497a2009-03-06 21:29:10 +08002928 return status;
2929}