Commit | Line | Data |
---|---|---|
ccd979bd MF |
1 | /* -*- mode: c; c-basic-offset: 8; -*- |
2 | * vim: noexpandtab sw=8 ts=8 sts=0: | |
3 | * | |
4 | * localalloc.c | |
5 | * | |
6 | * Node local data allocation | |
7 | * | |
8 | * Copyright (C) 2002, 2004 Oracle. All rights reserved. | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU General Public | |
12 | * License as published by the Free Software Foundation; either | |
13 | * version 2 of the License, or (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | * General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public | |
21 | * License along with this program; if not, write to the | |
22 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | * Boston, MA 021110-1307, USA. | |
24 | */ | |
25 | ||
26 | #include <linux/fs.h> | |
27 | #include <linux/types.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/highmem.h> | |
30 | #include <linux/bitops.h> | |
31 | ||
32 | #define MLOG_MASK_PREFIX ML_DISK_ALLOC | |
33 | #include <cluster/masklog.h> | |
34 | ||
35 | #include "ocfs2.h" | |
36 | ||
37 | #include "alloc.h" | |
38 | #include "dlmglue.h" | |
39 | #include "inode.h" | |
40 | #include "journal.h" | |
41 | #include "localalloc.h" | |
42 | #include "suballoc.h" | |
43 | #include "super.h" | |
44 | #include "sysfile.h" | |
45 | ||
46 | #include "buffer_head_io.h" | |
47 | ||
48 | #define OCFS2_LOCAL_ALLOC(dinode) (&((dinode)->id2.i_lab)) | |
49 | ||
ccd979bd MF |
50 | static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc); |
51 | ||
52 | static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb, | |
53 | struct ocfs2_dinode *alloc, | |
54 | u32 numbits); | |
55 | ||
56 | static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc); | |
57 | ||
58 | static int ocfs2_sync_local_to_main(struct ocfs2_super *osb, | |
1fabe148 | 59 | handle_t *handle, |
ccd979bd MF |
60 | struct ocfs2_dinode *alloc, |
61 | struct inode *main_bm_inode, | |
62 | struct buffer_head *main_bm_bh); | |
63 | ||
64 | static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb, | |
ccd979bd MF |
65 | struct ocfs2_alloc_context **ac, |
66 | struct inode **bitmap_inode, | |
67 | struct buffer_head **bitmap_bh); | |
68 | ||
69 | static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb, | |
1fabe148 | 70 | handle_t *handle, |
ccd979bd MF |
71 | struct ocfs2_alloc_context *ac); |
72 | ||
73 | static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb, | |
74 | struct inode *local_alloc_inode); | |
75 | ||
ccd979bd MF |
76 | /* |
77 | * Tell us whether a given allocation should use the local alloc | |
78 | * file. Otherwise, it has to go to the main bitmap. | |
79 | */ | |
80 | int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits) | |
81 | { | |
ebcee4b5 | 82 | int la_bits = osb->local_alloc_bits; |
2fbe8d1e | 83 | int ret = 0; |
ccd979bd MF |
84 | |
85 | if (osb->local_alloc_state != OCFS2_LA_ENABLED) | |
2fbe8d1e | 86 | goto bail; |
ccd979bd MF |
87 | |
88 | /* la_bits should be at least twice the size (in clusters) of | |
89 | * a new block group. We want to be sure block group | |
90 | * allocations go through the local alloc, so allow an | |
91 | * allocation to take up to half the bitmap. */ | |
92 | if (bits > (la_bits / 2)) | |
2fbe8d1e | 93 | goto bail; |
ccd979bd | 94 | |
2fbe8d1e SM |
95 | ret = 1; |
96 | bail: | |
97 | mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n", | |
98 | osb->local_alloc_state, (unsigned long long)bits, la_bits, ret); | |
99 | return ret; | |
ccd979bd MF |
100 | } |
101 | ||
102 | int ocfs2_load_local_alloc(struct ocfs2_super *osb) | |
103 | { | |
104 | int status = 0; | |
105 | struct ocfs2_dinode *alloc = NULL; | |
106 | struct buffer_head *alloc_bh = NULL; | |
107 | u32 num_used; | |
108 | struct inode *inode = NULL; | |
109 | struct ocfs2_local_alloc *la; | |
110 | ||
111 | mlog_entry_void(); | |
112 | ||
ebcee4b5 | 113 | if (osb->local_alloc_bits == 0) |
2fbe8d1e SM |
114 | goto bail; |
115 | ||
ebcee4b5 | 116 | if (osb->local_alloc_bits >= osb->bitmap_cpg) { |
2fbe8d1e SM |
117 | mlog(ML_NOTICE, "Requested local alloc window %d is larger " |
118 | "than max possible %u. Using defaults.\n", | |
ebcee4b5 MF |
119 | osb->local_alloc_bits, (osb->bitmap_cpg - 1)); |
120 | osb->local_alloc_bits = | |
121 | ocfs2_megabytes_to_clusters(osb->sb, | |
122 | OCFS2_DEFAULT_LOCAL_ALLOC_SIZE); | |
2fbe8d1e SM |
123 | } |
124 | ||
ccd979bd MF |
125 | /* read the alloc off disk */ |
126 | inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE, | |
127 | osb->slot_num); | |
128 | if (!inode) { | |
129 | status = -EINVAL; | |
130 | mlog_errno(status); | |
131 | goto bail; | |
132 | } | |
133 | ||
134 | status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, | |
135 | &alloc_bh, 0, inode); | |
136 | if (status < 0) { | |
137 | mlog_errno(status); | |
138 | goto bail; | |
139 | } | |
140 | ||
141 | alloc = (struct ocfs2_dinode *) alloc_bh->b_data; | |
142 | la = OCFS2_LOCAL_ALLOC(alloc); | |
143 | ||
144 | if (!(le32_to_cpu(alloc->i_flags) & | |
145 | (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) { | |
b0697053 MF |
146 | mlog(ML_ERROR, "Invalid local alloc inode, %llu\n", |
147 | (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
ccd979bd MF |
148 | status = -EINVAL; |
149 | goto bail; | |
150 | } | |
151 | ||
152 | if ((la->la_size == 0) || | |
153 | (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) { | |
154 | mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n", | |
155 | le16_to_cpu(la->la_size)); | |
156 | status = -EINVAL; | |
157 | goto bail; | |
158 | } | |
159 | ||
160 | /* do a little verification. */ | |
161 | num_used = ocfs2_local_alloc_count_bits(alloc); | |
162 | ||
163 | /* hopefully the local alloc has always been recovered before | |
164 | * we load it. */ | |
165 | if (num_used | |
166 | || alloc->id1.bitmap1.i_used | |
167 | || alloc->id1.bitmap1.i_total | |
168 | || la->la_bm_off) | |
169 | mlog(ML_ERROR, "Local alloc hasn't been recovered!\n" | |
170 | "found = %u, set = %u, taken = %u, off = %u\n", | |
171 | num_used, le32_to_cpu(alloc->id1.bitmap1.i_used), | |
172 | le32_to_cpu(alloc->id1.bitmap1.i_total), | |
173 | OCFS2_LOCAL_ALLOC(alloc)->la_bm_off); | |
174 | ||
175 | osb->local_alloc_bh = alloc_bh; | |
176 | osb->local_alloc_state = OCFS2_LA_ENABLED; | |
177 | ||
178 | bail: | |
179 | if (status < 0) | |
180 | if (alloc_bh) | |
181 | brelse(alloc_bh); | |
182 | if (inode) | |
183 | iput(inode); | |
184 | ||
ebcee4b5 | 185 | mlog(0, "Local alloc window bits = %d\n", osb->local_alloc_bits); |
2fbe8d1e | 186 | |
ccd979bd MF |
187 | mlog_exit(status); |
188 | return status; | |
189 | } | |
190 | ||
191 | /* | |
192 | * return any unused bits to the bitmap and write out a clean | |
193 | * local_alloc. | |
194 | * | |
195 | * local_alloc_bh is optional. If not passed, we will simply use the | |
196 | * one off osb. If you do pass it however, be warned that it *will* be | |
197 | * returned brelse'd and NULL'd out.*/ | |
198 | void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb) | |
199 | { | |
200 | int status; | |
1fabe148 | 201 | handle_t *handle; |
ccd979bd MF |
202 | struct inode *local_alloc_inode = NULL; |
203 | struct buffer_head *bh = NULL; | |
204 | struct buffer_head *main_bm_bh = NULL; | |
205 | struct inode *main_bm_inode = NULL; | |
206 | struct ocfs2_dinode *alloc_copy = NULL; | |
207 | struct ocfs2_dinode *alloc = NULL; | |
208 | ||
209 | mlog_entry_void(); | |
210 | ||
211 | if (osb->local_alloc_state == OCFS2_LA_UNUSED) | |
8898a5a5 | 212 | goto out; |
ccd979bd MF |
213 | |
214 | local_alloc_inode = | |
215 | ocfs2_get_system_file_inode(osb, | |
216 | LOCAL_ALLOC_SYSTEM_INODE, | |
217 | osb->slot_num); | |
218 | if (!local_alloc_inode) { | |
219 | status = -ENOENT; | |
220 | mlog_errno(status); | |
8898a5a5 | 221 | goto out; |
ccd979bd MF |
222 | } |
223 | ||
224 | osb->local_alloc_state = OCFS2_LA_DISABLED; | |
225 | ||
ccd979bd MF |
226 | main_bm_inode = ocfs2_get_system_file_inode(osb, |
227 | GLOBAL_BITMAP_SYSTEM_INODE, | |
228 | OCFS2_INVALID_SLOT); | |
229 | if (!main_bm_inode) { | |
230 | status = -EINVAL; | |
231 | mlog_errno(status); | |
8898a5a5 | 232 | goto out; |
ccd979bd MF |
233 | } |
234 | ||
8898a5a5 MF |
235 | mutex_lock(&main_bm_inode->i_mutex); |
236 | ||
e63aecb6 | 237 | status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1); |
ccd979bd MF |
238 | if (status < 0) { |
239 | mlog_errno(status); | |
8898a5a5 | 240 | goto out_mutex; |
ccd979bd MF |
241 | } |
242 | ||
243 | /* WINDOW_MOVE_CREDITS is a bit heavy... */ | |
65eff9cc | 244 | handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS); |
ccd979bd MF |
245 | if (IS_ERR(handle)) { |
246 | mlog_errno(PTR_ERR(handle)); | |
247 | handle = NULL; | |
8898a5a5 | 248 | goto out_unlock; |
ccd979bd MF |
249 | } |
250 | ||
251 | bh = osb->local_alloc_bh; | |
252 | alloc = (struct ocfs2_dinode *) bh->b_data; | |
253 | ||
4ba1c5bf | 254 | alloc_copy = kmalloc(bh->b_size, GFP_NOFS); |
ccd979bd MF |
255 | if (!alloc_copy) { |
256 | status = -ENOMEM; | |
8898a5a5 | 257 | goto out_commit; |
ccd979bd MF |
258 | } |
259 | memcpy(alloc_copy, alloc, bh->b_size); | |
260 | ||
261 | status = ocfs2_journal_access(handle, local_alloc_inode, bh, | |
262 | OCFS2_JOURNAL_ACCESS_WRITE); | |
263 | if (status < 0) { | |
264 | mlog_errno(status); | |
8898a5a5 | 265 | goto out_commit; |
ccd979bd MF |
266 | } |
267 | ||
268 | ocfs2_clear_local_alloc(alloc); | |
269 | ||
270 | status = ocfs2_journal_dirty(handle, bh); | |
271 | if (status < 0) { | |
272 | mlog_errno(status); | |
8898a5a5 | 273 | goto out_commit; |
ccd979bd MF |
274 | } |
275 | ||
276 | brelse(bh); | |
277 | osb->local_alloc_bh = NULL; | |
278 | osb->local_alloc_state = OCFS2_LA_UNUSED; | |
279 | ||
280 | status = ocfs2_sync_local_to_main(osb, handle, alloc_copy, | |
281 | main_bm_inode, main_bm_bh); | |
282 | if (status < 0) | |
283 | mlog_errno(status); | |
284 | ||
8898a5a5 | 285 | out_commit: |
02dc1af4 | 286 | ocfs2_commit_trans(osb, handle); |
ccd979bd | 287 | |
8898a5a5 | 288 | out_unlock: |
ccd979bd MF |
289 | if (main_bm_bh) |
290 | brelse(main_bm_bh); | |
291 | ||
e63aecb6 | 292 | ocfs2_inode_unlock(main_bm_inode, 1); |
ccd979bd | 293 | |
8898a5a5 MF |
294 | out_mutex: |
295 | mutex_unlock(&main_bm_inode->i_mutex); | |
296 | iput(main_bm_inode); | |
297 | ||
298 | out: | |
ccd979bd MF |
299 | if (local_alloc_inode) |
300 | iput(local_alloc_inode); | |
301 | ||
302 | if (alloc_copy) | |
303 | kfree(alloc_copy); | |
304 | ||
305 | mlog_exit_void(); | |
306 | } | |
307 | ||
308 | /* | |
309 | * We want to free the bitmap bits outside of any recovery context as | |
310 | * we'll need a cluster lock to do so, but we must clear the local | |
311 | * alloc before giving up the recovered nodes journal. To solve this, | |
312 | * we kmalloc a copy of the local alloc before it's change for the | |
313 | * caller to process with ocfs2_complete_local_alloc_recovery | |
314 | */ | |
315 | int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb, | |
316 | int slot_num, | |
317 | struct ocfs2_dinode **alloc_copy) | |
318 | { | |
319 | int status = 0; | |
320 | struct buffer_head *alloc_bh = NULL; | |
321 | struct inode *inode = NULL; | |
322 | struct ocfs2_dinode *alloc; | |
323 | ||
324 | mlog_entry("(slot_num = %d)\n", slot_num); | |
325 | ||
326 | *alloc_copy = NULL; | |
327 | ||
328 | inode = ocfs2_get_system_file_inode(osb, | |
329 | LOCAL_ALLOC_SYSTEM_INODE, | |
330 | slot_num); | |
331 | if (!inode) { | |
332 | status = -EINVAL; | |
333 | mlog_errno(status); | |
334 | goto bail; | |
335 | } | |
336 | ||
1b1dcc1b | 337 | mutex_lock(&inode->i_mutex); |
ccd979bd MF |
338 | |
339 | status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, | |
340 | &alloc_bh, 0, inode); | |
341 | if (status < 0) { | |
342 | mlog_errno(status); | |
343 | goto bail; | |
344 | } | |
345 | ||
346 | *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL); | |
347 | if (!(*alloc_copy)) { | |
348 | status = -ENOMEM; | |
349 | goto bail; | |
350 | } | |
351 | memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size); | |
352 | ||
353 | alloc = (struct ocfs2_dinode *) alloc_bh->b_data; | |
354 | ocfs2_clear_local_alloc(alloc); | |
355 | ||
356 | status = ocfs2_write_block(osb, alloc_bh, inode); | |
357 | if (status < 0) | |
358 | mlog_errno(status); | |
359 | ||
360 | bail: | |
361 | if ((status < 0) && (*alloc_copy)) { | |
362 | kfree(*alloc_copy); | |
363 | *alloc_copy = NULL; | |
364 | } | |
365 | ||
366 | if (alloc_bh) | |
367 | brelse(alloc_bh); | |
368 | ||
369 | if (inode) { | |
1b1dcc1b | 370 | mutex_unlock(&inode->i_mutex); |
ccd979bd MF |
371 | iput(inode); |
372 | } | |
373 | ||
374 | mlog_exit(status); | |
375 | return status; | |
376 | } | |
377 | ||
378 | /* | |
379 | * Step 2: By now, we've completed the journal recovery, we've stamped | |
380 | * a clean local alloc on disk and dropped the node out of the | |
381 | * recovery map. Dlm locks will no longer stall, so lets clear out the | |
382 | * main bitmap. | |
383 | */ | |
384 | int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb, | |
385 | struct ocfs2_dinode *alloc) | |
386 | { | |
387 | int status; | |
1fabe148 | 388 | handle_t *handle; |
ccd979bd | 389 | struct buffer_head *main_bm_bh = NULL; |
8898a5a5 | 390 | struct inode *main_bm_inode; |
ccd979bd MF |
391 | |
392 | mlog_entry_void(); | |
393 | ||
ccd979bd MF |
394 | main_bm_inode = ocfs2_get_system_file_inode(osb, |
395 | GLOBAL_BITMAP_SYSTEM_INODE, | |
396 | OCFS2_INVALID_SLOT); | |
397 | if (!main_bm_inode) { | |
398 | status = -EINVAL; | |
399 | mlog_errno(status); | |
8898a5a5 | 400 | goto out; |
ccd979bd MF |
401 | } |
402 | ||
8898a5a5 MF |
403 | mutex_lock(&main_bm_inode->i_mutex); |
404 | ||
e63aecb6 | 405 | status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1); |
ccd979bd MF |
406 | if (status < 0) { |
407 | mlog_errno(status); | |
8898a5a5 | 408 | goto out_mutex; |
ccd979bd MF |
409 | } |
410 | ||
65eff9cc | 411 | handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS); |
ccd979bd MF |
412 | if (IS_ERR(handle)) { |
413 | status = PTR_ERR(handle); | |
414 | handle = NULL; | |
415 | mlog_errno(status); | |
8898a5a5 | 416 | goto out_unlock; |
ccd979bd MF |
417 | } |
418 | ||
419 | /* we want the bitmap change to be recorded on disk asap */ | |
1fabe148 | 420 | handle->h_sync = 1; |
ccd979bd MF |
421 | |
422 | status = ocfs2_sync_local_to_main(osb, handle, alloc, | |
423 | main_bm_inode, main_bm_bh); | |
424 | if (status < 0) | |
425 | mlog_errno(status); | |
426 | ||
02dc1af4 | 427 | ocfs2_commit_trans(osb, handle); |
8898a5a5 MF |
428 | |
429 | out_unlock: | |
e63aecb6 | 430 | ocfs2_inode_unlock(main_bm_inode, 1); |
8898a5a5 MF |
431 | |
432 | out_mutex: | |
433 | mutex_unlock(&main_bm_inode->i_mutex); | |
ccd979bd MF |
434 | |
435 | if (main_bm_bh) | |
436 | brelse(main_bm_bh); | |
437 | ||
8898a5a5 | 438 | iput(main_bm_inode); |
ccd979bd | 439 | |
8898a5a5 | 440 | out: |
4d0ddb2c TM |
441 | if (!status) |
442 | ocfs2_init_inode_steal_slot(osb); | |
ccd979bd MF |
443 | mlog_exit(status); |
444 | return status; | |
445 | } | |
446 | ||
447 | /* | |
448 | * make sure we've got at least bitswanted contiguous bits in the | |
1b1dcc1b | 449 | * local alloc. You lose them when you drop i_mutex. |
ccd979bd MF |
450 | * |
451 | * We will add ourselves to the transaction passed in, but may start | |
452 | * our own in order to shift windows. | |
453 | */ | |
454 | int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb, | |
ccd979bd MF |
455 | u32 bits_wanted, |
456 | struct ocfs2_alloc_context *ac) | |
457 | { | |
458 | int status; | |
459 | struct ocfs2_dinode *alloc; | |
460 | struct inode *local_alloc_inode; | |
461 | unsigned int free_bits; | |
462 | ||
463 | mlog_entry_void(); | |
464 | ||
ccd979bd | 465 | BUG_ON(!ac); |
ccd979bd MF |
466 | |
467 | local_alloc_inode = | |
468 | ocfs2_get_system_file_inode(osb, | |
469 | LOCAL_ALLOC_SYSTEM_INODE, | |
470 | osb->slot_num); | |
471 | if (!local_alloc_inode) { | |
472 | status = -ENOENT; | |
473 | mlog_errno(status); | |
474 | goto bail; | |
475 | } | |
da5cbf2f MF |
476 | |
477 | mutex_lock(&local_alloc_inode->i_mutex); | |
478 | ||
ccd979bd MF |
479 | if (osb->local_alloc_state != OCFS2_LA_ENABLED) { |
480 | status = -ENOSPC; | |
481 | goto bail; | |
482 | } | |
483 | ||
ebcee4b5 | 484 | if (bits_wanted > osb->local_alloc_bits) { |
ccd979bd MF |
485 | mlog(0, "Asking for more than my max window size!\n"); |
486 | status = -ENOSPC; | |
487 | goto bail; | |
488 | } | |
489 | ||
490 | alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; | |
491 | ||
e407e397 | 492 | #ifdef CONFIG_OCFS2_DEBUG_FS |
ccd979bd MF |
493 | if (le32_to_cpu(alloc->id1.bitmap1.i_used) != |
494 | ocfs2_local_alloc_count_bits(alloc)) { | |
b0697053 | 495 | ocfs2_error(osb->sb, "local alloc inode %llu says it has " |
ccd979bd | 496 | "%u free bits, but a count shows %u", |
b0697053 | 497 | (unsigned long long)le64_to_cpu(alloc->i_blkno), |
ccd979bd MF |
498 | le32_to_cpu(alloc->id1.bitmap1.i_used), |
499 | ocfs2_local_alloc_count_bits(alloc)); | |
500 | status = -EIO; | |
501 | goto bail; | |
502 | } | |
5a58c3ef | 503 | #endif |
ccd979bd MF |
504 | |
505 | free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) - | |
506 | le32_to_cpu(alloc->id1.bitmap1.i_used); | |
507 | if (bits_wanted > free_bits) { | |
508 | /* uhoh, window change time. */ | |
509 | status = | |
510 | ocfs2_local_alloc_slide_window(osb, local_alloc_inode); | |
511 | if (status < 0) { | |
512 | if (status != -ENOSPC) | |
513 | mlog_errno(status); | |
514 | goto bail; | |
515 | } | |
516 | } | |
517 | ||
8fccfc82 | 518 | ac->ac_inode = local_alloc_inode; |
a4a48911 TM |
519 | /* We should never use localalloc from another slot */ |
520 | ac->ac_alloc_slot = osb->slot_num; | |
8fccfc82 | 521 | ac->ac_which = OCFS2_AC_USE_LOCAL; |
ccd979bd MF |
522 | get_bh(osb->local_alloc_bh); |
523 | ac->ac_bh = osb->local_alloc_bh; | |
ccd979bd MF |
524 | status = 0; |
525 | bail: | |
bda0233b SM |
526 | if (status < 0 && local_alloc_inode) { |
527 | mutex_unlock(&local_alloc_inode->i_mutex); | |
8fccfc82 | 528 | iput(local_alloc_inode); |
bda0233b | 529 | } |
ccd979bd | 530 | |
2fbe8d1e SM |
531 | mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num, |
532 | status); | |
533 | ||
ccd979bd MF |
534 | mlog_exit(status); |
535 | return status; | |
536 | } | |
537 | ||
538 | int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb, | |
1fabe148 | 539 | handle_t *handle, |
ccd979bd | 540 | struct ocfs2_alloc_context *ac, |
415cb800 | 541 | u32 bits_wanted, |
ccd979bd MF |
542 | u32 *bit_off, |
543 | u32 *num_bits) | |
544 | { | |
545 | int status, start; | |
546 | struct inode *local_alloc_inode; | |
ccd979bd MF |
547 | void *bitmap; |
548 | struct ocfs2_dinode *alloc; | |
549 | struct ocfs2_local_alloc *la; | |
550 | ||
551 | mlog_entry_void(); | |
552 | BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL); | |
553 | ||
ccd979bd MF |
554 | local_alloc_inode = ac->ac_inode; |
555 | alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; | |
556 | la = OCFS2_LOCAL_ALLOC(alloc); | |
557 | ||
558 | start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted); | |
559 | if (start == -1) { | |
560 | /* TODO: Shouldn't we just BUG here? */ | |
561 | status = -ENOSPC; | |
562 | mlog_errno(status); | |
563 | goto bail; | |
564 | } | |
565 | ||
566 | bitmap = la->la_bitmap; | |
567 | *bit_off = le32_to_cpu(la->la_bm_off) + start; | |
568 | /* local alloc is always contiguous by nature -- we never | |
569 | * delete bits from it! */ | |
570 | *num_bits = bits_wanted; | |
571 | ||
572 | status = ocfs2_journal_access(handle, local_alloc_inode, | |
573 | osb->local_alloc_bh, | |
574 | OCFS2_JOURNAL_ACCESS_WRITE); | |
575 | if (status < 0) { | |
576 | mlog_errno(status); | |
577 | goto bail; | |
578 | } | |
579 | ||
580 | while(bits_wanted--) | |
581 | ocfs2_set_bit(start++, bitmap); | |
582 | ||
0dd3256e | 583 | le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits); |
ccd979bd MF |
584 | |
585 | status = ocfs2_journal_dirty(handle, osb->local_alloc_bh); | |
586 | if (status < 0) { | |
587 | mlog_errno(status); | |
588 | goto bail; | |
589 | } | |
590 | ||
591 | status = 0; | |
592 | bail: | |
593 | mlog_exit(status); | |
594 | return status; | |
595 | } | |
596 | ||
597 | static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc) | |
598 | { | |
599 | int i; | |
600 | u8 *buffer; | |
601 | u32 count = 0; | |
602 | struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc); | |
603 | ||
604 | mlog_entry_void(); | |
605 | ||
606 | buffer = la->la_bitmap; | |
607 | for (i = 0; i < le16_to_cpu(la->la_size); i++) | |
608 | count += hweight8(buffer[i]); | |
609 | ||
610 | mlog_exit(count); | |
611 | return count; | |
612 | } | |
613 | ||
614 | static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb, | |
615 | struct ocfs2_dinode *alloc, | |
616 | u32 numbits) | |
617 | { | |
618 | int numfound, bitoff, left, startoff, lastzero; | |
619 | void *bitmap = NULL; | |
620 | ||
621 | mlog_entry("(numbits wanted = %u)\n", numbits); | |
622 | ||
623 | if (!alloc->id1.bitmap1.i_total) { | |
624 | mlog(0, "No bits in my window!\n"); | |
625 | bitoff = -1; | |
626 | goto bail; | |
627 | } | |
628 | ||
629 | bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap; | |
630 | ||
631 | numfound = bitoff = startoff = 0; | |
632 | lastzero = -1; | |
633 | left = le32_to_cpu(alloc->id1.bitmap1.i_total); | |
634 | while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) { | |
635 | if (bitoff == left) { | |
636 | /* mlog(0, "bitoff (%d) == left", bitoff); */ | |
637 | break; | |
638 | } | |
639 | /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, " | |
640 | "numfound = %d\n", bitoff, startoff, numfound);*/ | |
641 | ||
642 | /* Ok, we found a zero bit... is it contig. or do we | |
643 | * start over?*/ | |
644 | if (bitoff == startoff) { | |
645 | /* we found a zero */ | |
646 | numfound++; | |
647 | startoff++; | |
648 | } else { | |
649 | /* got a zero after some ones */ | |
650 | numfound = 1; | |
651 | startoff = bitoff+1; | |
652 | } | |
653 | /* we got everything we needed */ | |
654 | if (numfound == numbits) { | |
655 | /* mlog(0, "Found it all!\n"); */ | |
656 | break; | |
657 | } | |
658 | } | |
659 | ||
660 | mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff, | |
661 | numfound); | |
662 | ||
663 | if (numfound == numbits) | |
664 | bitoff = startoff - numfound; | |
665 | else | |
666 | bitoff = -1; | |
667 | ||
668 | bail: | |
669 | mlog_exit(bitoff); | |
670 | return bitoff; | |
671 | } | |
672 | ||
673 | static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc) | |
674 | { | |
675 | struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc); | |
676 | int i; | |
677 | mlog_entry_void(); | |
678 | ||
679 | alloc->id1.bitmap1.i_total = 0; | |
680 | alloc->id1.bitmap1.i_used = 0; | |
681 | la->la_bm_off = 0; | |
682 | for(i = 0; i < le16_to_cpu(la->la_size); i++) | |
683 | la->la_bitmap[i] = 0; | |
684 | ||
685 | mlog_exit_void(); | |
686 | } | |
687 | ||
688 | #if 0 | |
689 | /* turn this on and uncomment below to aid debugging window shifts. */ | |
690 | static void ocfs2_verify_zero_bits(unsigned long *bitmap, | |
691 | unsigned int start, | |
692 | unsigned int count) | |
693 | { | |
694 | unsigned int tmp = count; | |
695 | while(tmp--) { | |
696 | if (ocfs2_test_bit(start + tmp, bitmap)) { | |
697 | printk("ocfs2_verify_zero_bits: start = %u, count = " | |
698 | "%u\n", start, count); | |
699 | printk("ocfs2_verify_zero_bits: bit %u is set!", | |
700 | start + tmp); | |
701 | BUG(); | |
702 | } | |
703 | } | |
704 | } | |
705 | #endif | |
706 | ||
707 | /* | |
708 | * sync the local alloc to main bitmap. | |
709 | * | |
710 | * assumes you've already locked the main bitmap -- the bitmap inode | |
711 | * passed is used for caching. | |
712 | */ | |
713 | static int ocfs2_sync_local_to_main(struct ocfs2_super *osb, | |
1fabe148 | 714 | handle_t *handle, |
ccd979bd MF |
715 | struct ocfs2_dinode *alloc, |
716 | struct inode *main_bm_inode, | |
717 | struct buffer_head *main_bm_bh) | |
718 | { | |
719 | int status = 0; | |
720 | int bit_off, left, count, start; | |
721 | u64 la_start_blk; | |
722 | u64 blkno; | |
723 | void *bitmap; | |
724 | struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc); | |
725 | ||
5a58c3ef | 726 | mlog_entry("total = %u, used = %u\n", |
ccd979bd | 727 | le32_to_cpu(alloc->id1.bitmap1.i_total), |
ccd979bd MF |
728 | le32_to_cpu(alloc->id1.bitmap1.i_used)); |
729 | ||
730 | if (!alloc->id1.bitmap1.i_total) { | |
731 | mlog(0, "nothing to sync!\n"); | |
732 | goto bail; | |
733 | } | |
734 | ||
735 | if (le32_to_cpu(alloc->id1.bitmap1.i_used) == | |
736 | le32_to_cpu(alloc->id1.bitmap1.i_total)) { | |
737 | mlog(0, "all bits were taken!\n"); | |
738 | goto bail; | |
739 | } | |
740 | ||
741 | la_start_blk = ocfs2_clusters_to_blocks(osb->sb, | |
742 | le32_to_cpu(la->la_bm_off)); | |
743 | bitmap = la->la_bitmap; | |
744 | start = count = bit_off = 0; | |
745 | left = le32_to_cpu(alloc->id1.bitmap1.i_total); | |
746 | ||
747 | while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start)) | |
748 | != -1) { | |
749 | if ((bit_off < left) && (bit_off == start)) { | |
750 | count++; | |
751 | start++; | |
752 | continue; | |
753 | } | |
754 | if (count) { | |
755 | blkno = la_start_blk + | |
756 | ocfs2_clusters_to_blocks(osb->sb, | |
757 | start - count); | |
758 | ||
b0697053 MF |
759 | mlog(0, "freeing %u bits starting at local alloc bit " |
760 | "%u (la_start_blk = %llu, blkno = %llu)\n", | |
761 | count, start - count, | |
762 | (unsigned long long)la_start_blk, | |
763 | (unsigned long long)blkno); | |
ccd979bd MF |
764 | |
765 | status = ocfs2_free_clusters(handle, main_bm_inode, | |
766 | main_bm_bh, blkno, count); | |
767 | if (status < 0) { | |
768 | mlog_errno(status); | |
769 | goto bail; | |
770 | } | |
771 | } | |
772 | if (bit_off >= left) | |
773 | break; | |
774 | count = 1; | |
775 | start = bit_off + 1; | |
776 | } | |
777 | ||
778 | bail: | |
779 | mlog_exit(status); | |
780 | return status; | |
781 | } | |
782 | ||
783 | static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb, | |
ccd979bd MF |
784 | struct ocfs2_alloc_context **ac, |
785 | struct inode **bitmap_inode, | |
786 | struct buffer_head **bitmap_bh) | |
787 | { | |
788 | int status; | |
789 | ||
cd861280 | 790 | *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); |
ccd979bd MF |
791 | if (!(*ac)) { |
792 | status = -ENOMEM; | |
793 | mlog_errno(status); | |
794 | goto bail; | |
795 | } | |
796 | ||
ebcee4b5 | 797 | (*ac)->ac_bits_wanted = osb->local_alloc_bits; |
ccd979bd MF |
798 | |
799 | status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac); | |
800 | if (status < 0) { | |
801 | if (status != -ENOSPC) | |
802 | mlog_errno(status); | |
803 | goto bail; | |
804 | } | |
805 | ||
806 | *bitmap_inode = (*ac)->ac_inode; | |
807 | igrab(*bitmap_inode); | |
808 | *bitmap_bh = (*ac)->ac_bh; | |
809 | get_bh(*bitmap_bh); | |
810 | status = 0; | |
811 | bail: | |
812 | if ((status < 0) && *ac) { | |
813 | ocfs2_free_alloc_context(*ac); | |
814 | *ac = NULL; | |
815 | } | |
816 | ||
817 | mlog_exit(status); | |
818 | return status; | |
819 | } | |
820 | ||
821 | /* | |
822 | * pass it the bitmap lock in lock_bh if you have it. | |
823 | */ | |
824 | static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb, | |
1fabe148 | 825 | handle_t *handle, |
ccd979bd MF |
826 | struct ocfs2_alloc_context *ac) |
827 | { | |
828 | int status = 0; | |
829 | u32 cluster_off, cluster_count; | |
830 | struct ocfs2_dinode *alloc = NULL; | |
831 | struct ocfs2_local_alloc *la; | |
832 | ||
833 | mlog_entry_void(); | |
834 | ||
835 | alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; | |
836 | la = OCFS2_LOCAL_ALLOC(alloc); | |
837 | ||
838 | if (alloc->id1.bitmap1.i_total) | |
839 | mlog(0, "asking me to alloc a new window over a non-empty " | |
840 | "one\n"); | |
841 | ||
842 | mlog(0, "Allocating %u clusters for a new window.\n", | |
ebcee4b5 | 843 | osb->local_alloc_bits); |
883d4cae MF |
844 | |
845 | /* Instruct the allocation code to try the most recently used | |
846 | * cluster group. We'll re-record the group used this pass | |
847 | * below. */ | |
848 | ac->ac_last_group = osb->la_last_gd; | |
849 | ||
ccd979bd MF |
850 | /* we used the generic suballoc reserve function, but we set |
851 | * everything up nicely, so there's no reason why we can't use | |
852 | * the more specific cluster api to claim bits. */ | |
ebcee4b5 | 853 | status = ocfs2_claim_clusters(osb, handle, ac, osb->local_alloc_bits, |
ccd979bd MF |
854 | &cluster_off, &cluster_count); |
855 | if (status < 0) { | |
856 | if (status != -ENOSPC) | |
857 | mlog_errno(status); | |
858 | goto bail; | |
859 | } | |
860 | ||
883d4cae MF |
861 | osb->la_last_gd = ac->ac_last_group; |
862 | ||
ccd979bd MF |
863 | la->la_bm_off = cpu_to_le32(cluster_off); |
864 | alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count); | |
865 | /* just in case... In the future when we find space ourselves, | |
866 | * we don't have to get all contiguous -- but we'll have to | |
867 | * set all previously used bits in bitmap and update | |
868 | * la_bits_set before setting the bits in the main bitmap. */ | |
869 | alloc->id1.bitmap1.i_used = 0; | |
870 | memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0, | |
871 | le16_to_cpu(la->la_size)); | |
872 | ||
873 | mlog(0, "New window allocated:\n"); | |
874 | mlog(0, "window la_bm_off = %u\n", | |
875 | OCFS2_LOCAL_ALLOC(alloc)->la_bm_off); | |
876 | mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total)); | |
877 | ||
878 | bail: | |
879 | mlog_exit(status); | |
880 | return status; | |
881 | } | |
882 | ||
883 | /* Note that we do *NOT* lock the local alloc inode here as | |
884 | * it's been locked already for us. */ | |
885 | static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb, | |
886 | struct inode *local_alloc_inode) | |
887 | { | |
888 | int status = 0; | |
889 | struct buffer_head *main_bm_bh = NULL; | |
890 | struct inode *main_bm_inode = NULL; | |
1fabe148 | 891 | handle_t *handle = NULL; |
ccd979bd MF |
892 | struct ocfs2_dinode *alloc; |
893 | struct ocfs2_dinode *alloc_copy = NULL; | |
894 | struct ocfs2_alloc_context *ac = NULL; | |
895 | ||
896 | mlog_entry_void(); | |
897 | ||
ccd979bd MF |
898 | /* This will lock the main bitmap for us. */ |
899 | status = ocfs2_local_alloc_reserve_for_window(osb, | |
ccd979bd MF |
900 | &ac, |
901 | &main_bm_inode, | |
902 | &main_bm_bh); | |
903 | if (status < 0) { | |
904 | if (status != -ENOSPC) | |
905 | mlog_errno(status); | |
906 | goto bail; | |
907 | } | |
908 | ||
65eff9cc | 909 | handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS); |
ccd979bd MF |
910 | if (IS_ERR(handle)) { |
911 | status = PTR_ERR(handle); | |
912 | handle = NULL; | |
913 | mlog_errno(status); | |
914 | goto bail; | |
915 | } | |
916 | ||
917 | alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; | |
918 | ||
919 | /* We want to clear the local alloc before doing anything | |
920 | * else, so that if we error later during this operation, | |
921 | * local alloc shutdown won't try to double free main bitmap | |
922 | * bits. Make a copy so the sync function knows which bits to | |
923 | * free. */ | |
4ba1c5bf | 924 | alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS); |
ccd979bd MF |
925 | if (!alloc_copy) { |
926 | status = -ENOMEM; | |
927 | mlog_errno(status); | |
928 | goto bail; | |
929 | } | |
930 | memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size); | |
931 | ||
932 | status = ocfs2_journal_access(handle, local_alloc_inode, | |
933 | osb->local_alloc_bh, | |
934 | OCFS2_JOURNAL_ACCESS_WRITE); | |
935 | if (status < 0) { | |
936 | mlog_errno(status); | |
937 | goto bail; | |
938 | } | |
939 | ||
940 | ocfs2_clear_local_alloc(alloc); | |
941 | ||
942 | status = ocfs2_journal_dirty(handle, osb->local_alloc_bh); | |
943 | if (status < 0) { | |
944 | mlog_errno(status); | |
945 | goto bail; | |
946 | } | |
947 | ||
948 | status = ocfs2_sync_local_to_main(osb, handle, alloc_copy, | |
949 | main_bm_inode, main_bm_bh); | |
950 | if (status < 0) { | |
951 | mlog_errno(status); | |
952 | goto bail; | |
953 | } | |
954 | ||
955 | status = ocfs2_local_alloc_new_window(osb, handle, ac); | |
956 | if (status < 0) { | |
957 | if (status != -ENOSPC) | |
958 | mlog_errno(status); | |
959 | goto bail; | |
960 | } | |
961 | ||
962 | atomic_inc(&osb->alloc_stats.moves); | |
963 | ||
964 | status = 0; | |
965 | bail: | |
966 | if (handle) | |
02dc1af4 | 967 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
968 | |
969 | if (main_bm_bh) | |
970 | brelse(main_bm_bh); | |
971 | ||
972 | if (main_bm_inode) | |
973 | iput(main_bm_inode); | |
974 | ||
975 | if (alloc_copy) | |
976 | kfree(alloc_copy); | |
977 | ||
978 | if (ac) | |
979 | ocfs2_free_alloc_context(ac); | |
980 | ||
981 | mlog_exit(status); | |
982 | return status; | |
983 | } | |
984 |