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