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