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> | |
9a8ff578 | 31 | #include <linux/debugfs.h> |
ccd979bd MF |
32 | |
33 | #define MLOG_MASK_PREFIX ML_DISK_ALLOC | |
34 | #include <cluster/masklog.h> | |
35 | ||
36 | #include "ocfs2.h" | |
37 | ||
38 | #include "alloc.h" | |
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 | ||
47 | #include "buffer_head_io.h" | |
48 | ||
49 | #define OCFS2_LOCAL_ALLOC(dinode) (&((dinode)->id2.i_lab)) | |
50 | ||
ccd979bd MF |
51 | static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc); |
52 | ||
53 | static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb, | |
54 | struct ocfs2_dinode *alloc, | |
55 | u32 numbits); | |
56 | ||
57 | static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc); | |
58 | ||
59 | static int ocfs2_sync_local_to_main(struct ocfs2_super *osb, | |
1fabe148 | 60 | handle_t *handle, |
ccd979bd MF |
61 | struct ocfs2_dinode *alloc, |
62 | struct inode *main_bm_inode, | |
63 | struct buffer_head *main_bm_bh); | |
64 | ||
65 | static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb, | |
ccd979bd MF |
66 | struct ocfs2_alloc_context **ac, |
67 | struct inode **bitmap_inode, | |
68 | struct buffer_head **bitmap_bh); | |
69 | ||
70 | static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb, | |
1fabe148 | 71 | handle_t *handle, |
ccd979bd MF |
72 | struct ocfs2_alloc_context *ac); |
73 | ||
74 | static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb, | |
75 | struct inode *local_alloc_inode); | |
76 | ||
9a8ff578 MF |
77 | #ifdef CONFIG_OCFS2_FS_STATS |
78 | ||
79 | DEFINE_MUTEX(la_debug_mutex); | |
80 | ||
81 | static int ocfs2_la_debug_open(struct inode *inode, struct file *file) | |
82 | { | |
83 | file->private_data = inode->i_private; | |
84 | return 0; | |
85 | } | |
86 | ||
87 | #define LA_DEBUG_BUF_SZ PAGE_CACHE_SIZE | |
88 | #define LA_DEBUG_VER 1 | |
89 | static ssize_t ocfs2_la_debug_read(struct file *file, char __user *userbuf, | |
90 | size_t count, loff_t *ppos) | |
91 | { | |
92 | struct ocfs2_super *osb = file->private_data; | |
93 | int written, ret; | |
94 | char *buf = osb->local_alloc_debug_buf; | |
95 | ||
96 | mutex_lock(&la_debug_mutex); | |
97 | memset(buf, 0, LA_DEBUG_BUF_SZ); | |
98 | ||
99 | written = snprintf(buf, LA_DEBUG_BUF_SZ, | |
100 | "0x%x\t0x%llx\t%u\t%u\t0x%x\n", | |
101 | LA_DEBUG_VER, | |
102 | (unsigned long long)osb->la_last_gd, | |
103 | osb->local_alloc_default_bits, | |
104 | osb->local_alloc_bits, osb->local_alloc_state); | |
105 | ||
106 | ret = simple_read_from_buffer(userbuf, count, ppos, buf, written); | |
107 | ||
108 | mutex_unlock(&la_debug_mutex); | |
109 | return ret; | |
110 | } | |
111 | ||
112 | static const struct file_operations ocfs2_la_debug_fops = { | |
113 | .open = ocfs2_la_debug_open, | |
114 | .read = ocfs2_la_debug_read, | |
115 | }; | |
116 | ||
117 | static void ocfs2_init_la_debug(struct ocfs2_super *osb) | |
118 | { | |
119 | osb->local_alloc_debug_buf = kmalloc(LA_DEBUG_BUF_SZ, GFP_NOFS); | |
120 | if (!osb->local_alloc_debug_buf) | |
121 | return; | |
122 | ||
123 | osb->local_alloc_debug = debugfs_create_file("local_alloc_stats", | |
124 | S_IFREG|S_IRUSR, | |
125 | osb->osb_debug_root, | |
126 | osb, | |
127 | &ocfs2_la_debug_fops); | |
128 | if (!osb->local_alloc_debug) { | |
129 | kfree(osb->local_alloc_debug_buf); | |
130 | osb->local_alloc_debug_buf = NULL; | |
131 | } | |
132 | } | |
133 | ||
134 | static void ocfs2_shutdown_la_debug(struct ocfs2_super *osb) | |
135 | { | |
136 | if (osb->local_alloc_debug) | |
137 | debugfs_remove(osb->local_alloc_debug); | |
138 | ||
139 | if (osb->local_alloc_debug_buf) | |
140 | kfree(osb->local_alloc_debug_buf); | |
141 | ||
142 | osb->local_alloc_debug_buf = NULL; | |
143 | osb->local_alloc_debug = NULL; | |
144 | } | |
145 | #else /* CONFIG_OCFS2_FS_STATS */ | |
146 | static void ocfs2_init_la_debug(struct ocfs2_super *osb) | |
147 | { | |
148 | return; | |
149 | } | |
150 | static void ocfs2_shutdown_la_debug(struct ocfs2_super *osb) | |
151 | { | |
152 | return; | |
153 | } | |
154 | #endif | |
155 | ||
9c7af40b MF |
156 | static inline int ocfs2_la_state_enabled(struct ocfs2_super *osb) |
157 | { | |
158 | return (osb->local_alloc_state == OCFS2_LA_THROTTLED || | |
159 | osb->local_alloc_state == OCFS2_LA_ENABLED); | |
160 | } | |
161 | ||
162 | void ocfs2_local_alloc_seen_free_bits(struct ocfs2_super *osb, | |
163 | unsigned int num_clusters) | |
164 | { | |
165 | spin_lock(&osb->osb_lock); | |
166 | if (osb->local_alloc_state == OCFS2_LA_DISABLED || | |
167 | osb->local_alloc_state == OCFS2_LA_THROTTLED) | |
168 | if (num_clusters >= osb->local_alloc_default_bits) { | |
169 | cancel_delayed_work(&osb->la_enable_wq); | |
170 | osb->local_alloc_state = OCFS2_LA_ENABLED; | |
171 | } | |
172 | spin_unlock(&osb->osb_lock); | |
173 | } | |
174 | ||
175 | void ocfs2_la_enable_worker(struct work_struct *work) | |
176 | { | |
177 | struct ocfs2_super *osb = | |
178 | container_of(work, struct ocfs2_super, | |
179 | la_enable_wq.work); | |
180 | spin_lock(&osb->osb_lock); | |
181 | osb->local_alloc_state = OCFS2_LA_ENABLED; | |
182 | spin_unlock(&osb->osb_lock); | |
183 | } | |
184 | ||
ccd979bd MF |
185 | /* |
186 | * Tell us whether a given allocation should use the local alloc | |
187 | * file. Otherwise, it has to go to the main bitmap. | |
9c7af40b MF |
188 | * |
189 | * This function does semi-dirty reads of local alloc size and state! | |
190 | * This is ok however, as the values are re-checked once under mutex. | |
ccd979bd MF |
191 | */ |
192 | int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits) | |
193 | { | |
2fbe8d1e | 194 | int ret = 0; |
9c7af40b MF |
195 | int la_bits; |
196 | ||
197 | spin_lock(&osb->osb_lock); | |
198 | la_bits = osb->local_alloc_bits; | |
ccd979bd | 199 | |
9c7af40b | 200 | if (!ocfs2_la_state_enabled(osb)) |
2fbe8d1e | 201 | goto bail; |
ccd979bd MF |
202 | |
203 | /* la_bits should be at least twice the size (in clusters) of | |
204 | * a new block group. We want to be sure block group | |
205 | * allocations go through the local alloc, so allow an | |
206 | * allocation to take up to half the bitmap. */ | |
207 | if (bits > (la_bits / 2)) | |
2fbe8d1e | 208 | goto bail; |
ccd979bd | 209 | |
2fbe8d1e SM |
210 | ret = 1; |
211 | bail: | |
212 | mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n", | |
213 | osb->local_alloc_state, (unsigned long long)bits, la_bits, ret); | |
9c7af40b | 214 | spin_unlock(&osb->osb_lock); |
2fbe8d1e | 215 | return ret; |
ccd979bd MF |
216 | } |
217 | ||
218 | int ocfs2_load_local_alloc(struct ocfs2_super *osb) | |
219 | { | |
220 | int status = 0; | |
221 | struct ocfs2_dinode *alloc = NULL; | |
222 | struct buffer_head *alloc_bh = NULL; | |
223 | u32 num_used; | |
224 | struct inode *inode = NULL; | |
225 | struct ocfs2_local_alloc *la; | |
226 | ||
227 | mlog_entry_void(); | |
228 | ||
9a8ff578 MF |
229 | ocfs2_init_la_debug(osb); |
230 | ||
ebcee4b5 | 231 | if (osb->local_alloc_bits == 0) |
2fbe8d1e SM |
232 | goto bail; |
233 | ||
ebcee4b5 | 234 | if (osb->local_alloc_bits >= osb->bitmap_cpg) { |
2fbe8d1e SM |
235 | mlog(ML_NOTICE, "Requested local alloc window %d is larger " |
236 | "than max possible %u. Using defaults.\n", | |
ebcee4b5 MF |
237 | osb->local_alloc_bits, (osb->bitmap_cpg - 1)); |
238 | osb->local_alloc_bits = | |
239 | ocfs2_megabytes_to_clusters(osb->sb, | |
240 | OCFS2_DEFAULT_LOCAL_ALLOC_SIZE); | |
2fbe8d1e SM |
241 | } |
242 | ||
ccd979bd MF |
243 | /* read the alloc off disk */ |
244 | inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE, | |
245 | osb->slot_num); | |
246 | if (!inode) { | |
247 | status = -EINVAL; | |
248 | mlog_errno(status); | |
249 | goto bail; | |
250 | } | |
251 | ||
252 | status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, | |
253 | &alloc_bh, 0, inode); | |
254 | if (status < 0) { | |
255 | mlog_errno(status); | |
256 | goto bail; | |
257 | } | |
258 | ||
259 | alloc = (struct ocfs2_dinode *) alloc_bh->b_data; | |
260 | la = OCFS2_LOCAL_ALLOC(alloc); | |
261 | ||
262 | if (!(le32_to_cpu(alloc->i_flags) & | |
263 | (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) { | |
b0697053 MF |
264 | mlog(ML_ERROR, "Invalid local alloc inode, %llu\n", |
265 | (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
ccd979bd MF |
266 | status = -EINVAL; |
267 | goto bail; | |
268 | } | |
269 | ||
270 | if ((la->la_size == 0) || | |
271 | (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) { | |
272 | mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n", | |
273 | le16_to_cpu(la->la_size)); | |
274 | status = -EINVAL; | |
275 | goto bail; | |
276 | } | |
277 | ||
278 | /* do a little verification. */ | |
279 | num_used = ocfs2_local_alloc_count_bits(alloc); | |
280 | ||
281 | /* hopefully the local alloc has always been recovered before | |
282 | * we load it. */ | |
283 | if (num_used | |
284 | || alloc->id1.bitmap1.i_used | |
285 | || alloc->id1.bitmap1.i_total | |
286 | || la->la_bm_off) | |
287 | mlog(ML_ERROR, "Local alloc hasn't been recovered!\n" | |
288 | "found = %u, set = %u, taken = %u, off = %u\n", | |
289 | num_used, le32_to_cpu(alloc->id1.bitmap1.i_used), | |
290 | le32_to_cpu(alloc->id1.bitmap1.i_total), | |
291 | OCFS2_LOCAL_ALLOC(alloc)->la_bm_off); | |
292 | ||
293 | osb->local_alloc_bh = alloc_bh; | |
294 | osb->local_alloc_state = OCFS2_LA_ENABLED; | |
295 | ||
296 | bail: | |
297 | if (status < 0) | |
298 | if (alloc_bh) | |
299 | brelse(alloc_bh); | |
300 | if (inode) | |
301 | iput(inode); | |
302 | ||
9a8ff578 MF |
303 | if (status < 0) |
304 | ocfs2_shutdown_la_debug(osb); | |
305 | ||
ebcee4b5 | 306 | mlog(0, "Local alloc window bits = %d\n", osb->local_alloc_bits); |
2fbe8d1e | 307 | |
ccd979bd MF |
308 | mlog_exit(status); |
309 | return status; | |
310 | } | |
311 | ||
312 | /* | |
313 | * return any unused bits to the bitmap and write out a clean | |
314 | * local_alloc. | |
315 | * | |
316 | * local_alloc_bh is optional. If not passed, we will simply use the | |
317 | * one off osb. If you do pass it however, be warned that it *will* be | |
318 | * returned brelse'd and NULL'd out.*/ | |
319 | void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb) | |
320 | { | |
321 | int status; | |
1fabe148 | 322 | handle_t *handle; |
ccd979bd MF |
323 | struct inode *local_alloc_inode = NULL; |
324 | struct buffer_head *bh = NULL; | |
325 | struct buffer_head *main_bm_bh = NULL; | |
326 | struct inode *main_bm_inode = NULL; | |
327 | struct ocfs2_dinode *alloc_copy = NULL; | |
328 | struct ocfs2_dinode *alloc = NULL; | |
329 | ||
330 | mlog_entry_void(); | |
331 | ||
9c7af40b MF |
332 | cancel_delayed_work(&osb->la_enable_wq); |
333 | flush_workqueue(ocfs2_wq); | |
334 | ||
9a8ff578 MF |
335 | ocfs2_shutdown_la_debug(osb); |
336 | ||
ccd979bd | 337 | if (osb->local_alloc_state == OCFS2_LA_UNUSED) |
8898a5a5 | 338 | goto out; |
ccd979bd MF |
339 | |
340 | local_alloc_inode = | |
341 | ocfs2_get_system_file_inode(osb, | |
342 | LOCAL_ALLOC_SYSTEM_INODE, | |
343 | osb->slot_num); | |
344 | if (!local_alloc_inode) { | |
345 | status = -ENOENT; | |
346 | mlog_errno(status); | |
8898a5a5 | 347 | goto out; |
ccd979bd MF |
348 | } |
349 | ||
350 | osb->local_alloc_state = OCFS2_LA_DISABLED; | |
351 | ||
ccd979bd MF |
352 | main_bm_inode = ocfs2_get_system_file_inode(osb, |
353 | GLOBAL_BITMAP_SYSTEM_INODE, | |
354 | OCFS2_INVALID_SLOT); | |
355 | if (!main_bm_inode) { | |
356 | status = -EINVAL; | |
357 | mlog_errno(status); | |
8898a5a5 | 358 | goto out; |
ccd979bd MF |
359 | } |
360 | ||
8898a5a5 MF |
361 | mutex_lock(&main_bm_inode->i_mutex); |
362 | ||
e63aecb6 | 363 | status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1); |
ccd979bd MF |
364 | if (status < 0) { |
365 | mlog_errno(status); | |
8898a5a5 | 366 | goto out_mutex; |
ccd979bd MF |
367 | } |
368 | ||
369 | /* WINDOW_MOVE_CREDITS is a bit heavy... */ | |
65eff9cc | 370 | handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS); |
ccd979bd MF |
371 | if (IS_ERR(handle)) { |
372 | mlog_errno(PTR_ERR(handle)); | |
373 | handle = NULL; | |
8898a5a5 | 374 | goto out_unlock; |
ccd979bd MF |
375 | } |
376 | ||
377 | bh = osb->local_alloc_bh; | |
378 | alloc = (struct ocfs2_dinode *) bh->b_data; | |
379 | ||
4ba1c5bf | 380 | alloc_copy = kmalloc(bh->b_size, GFP_NOFS); |
ccd979bd MF |
381 | if (!alloc_copy) { |
382 | status = -ENOMEM; | |
8898a5a5 | 383 | goto out_commit; |
ccd979bd MF |
384 | } |
385 | memcpy(alloc_copy, alloc, bh->b_size); | |
386 | ||
387 | status = ocfs2_journal_access(handle, local_alloc_inode, bh, | |
388 | OCFS2_JOURNAL_ACCESS_WRITE); | |
389 | if (status < 0) { | |
390 | mlog_errno(status); | |
8898a5a5 | 391 | goto out_commit; |
ccd979bd MF |
392 | } |
393 | ||
394 | ocfs2_clear_local_alloc(alloc); | |
395 | ||
396 | status = ocfs2_journal_dirty(handle, bh); | |
397 | if (status < 0) { | |
398 | mlog_errno(status); | |
8898a5a5 | 399 | goto out_commit; |
ccd979bd MF |
400 | } |
401 | ||
402 | brelse(bh); | |
403 | osb->local_alloc_bh = NULL; | |
404 | osb->local_alloc_state = OCFS2_LA_UNUSED; | |
405 | ||
406 | status = ocfs2_sync_local_to_main(osb, handle, alloc_copy, | |
407 | main_bm_inode, main_bm_bh); | |
408 | if (status < 0) | |
409 | mlog_errno(status); | |
410 | ||
8898a5a5 | 411 | out_commit: |
02dc1af4 | 412 | ocfs2_commit_trans(osb, handle); |
ccd979bd | 413 | |
8898a5a5 | 414 | out_unlock: |
ccd979bd MF |
415 | if (main_bm_bh) |
416 | brelse(main_bm_bh); | |
417 | ||
e63aecb6 | 418 | ocfs2_inode_unlock(main_bm_inode, 1); |
ccd979bd | 419 | |
8898a5a5 MF |
420 | out_mutex: |
421 | mutex_unlock(&main_bm_inode->i_mutex); | |
422 | iput(main_bm_inode); | |
423 | ||
424 | out: | |
ccd979bd MF |
425 | if (local_alloc_inode) |
426 | iput(local_alloc_inode); | |
427 | ||
428 | if (alloc_copy) | |
429 | kfree(alloc_copy); | |
430 | ||
431 | mlog_exit_void(); | |
432 | } | |
433 | ||
434 | /* | |
435 | * We want to free the bitmap bits outside of any recovery context as | |
436 | * we'll need a cluster lock to do so, but we must clear the local | |
437 | * alloc before giving up the recovered nodes journal. To solve this, | |
438 | * we kmalloc a copy of the local alloc before it's change for the | |
439 | * caller to process with ocfs2_complete_local_alloc_recovery | |
440 | */ | |
441 | int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb, | |
442 | int slot_num, | |
443 | struct ocfs2_dinode **alloc_copy) | |
444 | { | |
445 | int status = 0; | |
446 | struct buffer_head *alloc_bh = NULL; | |
447 | struct inode *inode = NULL; | |
448 | struct ocfs2_dinode *alloc; | |
449 | ||
450 | mlog_entry("(slot_num = %d)\n", slot_num); | |
451 | ||
452 | *alloc_copy = NULL; | |
453 | ||
454 | inode = ocfs2_get_system_file_inode(osb, | |
455 | LOCAL_ALLOC_SYSTEM_INODE, | |
456 | slot_num); | |
457 | if (!inode) { | |
458 | status = -EINVAL; | |
459 | mlog_errno(status); | |
460 | goto bail; | |
461 | } | |
462 | ||
1b1dcc1b | 463 | mutex_lock(&inode->i_mutex); |
ccd979bd MF |
464 | |
465 | status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, | |
466 | &alloc_bh, 0, inode); | |
467 | if (status < 0) { | |
468 | mlog_errno(status); | |
469 | goto bail; | |
470 | } | |
471 | ||
472 | *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL); | |
473 | if (!(*alloc_copy)) { | |
474 | status = -ENOMEM; | |
475 | goto bail; | |
476 | } | |
477 | memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size); | |
478 | ||
479 | alloc = (struct ocfs2_dinode *) alloc_bh->b_data; | |
480 | ocfs2_clear_local_alloc(alloc); | |
481 | ||
482 | status = ocfs2_write_block(osb, alloc_bh, inode); | |
483 | if (status < 0) | |
484 | mlog_errno(status); | |
485 | ||
486 | bail: | |
487 | if ((status < 0) && (*alloc_copy)) { | |
488 | kfree(*alloc_copy); | |
489 | *alloc_copy = NULL; | |
490 | } | |
491 | ||
492 | if (alloc_bh) | |
493 | brelse(alloc_bh); | |
494 | ||
495 | if (inode) { | |
1b1dcc1b | 496 | mutex_unlock(&inode->i_mutex); |
ccd979bd MF |
497 | iput(inode); |
498 | } | |
499 | ||
500 | mlog_exit(status); | |
501 | return status; | |
502 | } | |
503 | ||
504 | /* | |
505 | * Step 2: By now, we've completed the journal recovery, we've stamped | |
506 | * a clean local alloc on disk and dropped the node out of the | |
507 | * recovery map. Dlm locks will no longer stall, so lets clear out the | |
508 | * main bitmap. | |
509 | */ | |
510 | int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb, | |
511 | struct ocfs2_dinode *alloc) | |
512 | { | |
513 | int status; | |
1fabe148 | 514 | handle_t *handle; |
ccd979bd | 515 | struct buffer_head *main_bm_bh = NULL; |
8898a5a5 | 516 | struct inode *main_bm_inode; |
ccd979bd MF |
517 | |
518 | mlog_entry_void(); | |
519 | ||
ccd979bd MF |
520 | main_bm_inode = ocfs2_get_system_file_inode(osb, |
521 | GLOBAL_BITMAP_SYSTEM_INODE, | |
522 | OCFS2_INVALID_SLOT); | |
523 | if (!main_bm_inode) { | |
524 | status = -EINVAL; | |
525 | mlog_errno(status); | |
8898a5a5 | 526 | goto out; |
ccd979bd MF |
527 | } |
528 | ||
8898a5a5 MF |
529 | mutex_lock(&main_bm_inode->i_mutex); |
530 | ||
e63aecb6 | 531 | status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1); |
ccd979bd MF |
532 | if (status < 0) { |
533 | mlog_errno(status); | |
8898a5a5 | 534 | goto out_mutex; |
ccd979bd MF |
535 | } |
536 | ||
65eff9cc | 537 | handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS); |
ccd979bd MF |
538 | if (IS_ERR(handle)) { |
539 | status = PTR_ERR(handle); | |
540 | handle = NULL; | |
541 | mlog_errno(status); | |
8898a5a5 | 542 | goto out_unlock; |
ccd979bd MF |
543 | } |
544 | ||
545 | /* we want the bitmap change to be recorded on disk asap */ | |
1fabe148 | 546 | handle->h_sync = 1; |
ccd979bd MF |
547 | |
548 | status = ocfs2_sync_local_to_main(osb, handle, alloc, | |
549 | main_bm_inode, main_bm_bh); | |
550 | if (status < 0) | |
551 | mlog_errno(status); | |
552 | ||
02dc1af4 | 553 | ocfs2_commit_trans(osb, handle); |
8898a5a5 MF |
554 | |
555 | out_unlock: | |
e63aecb6 | 556 | ocfs2_inode_unlock(main_bm_inode, 1); |
8898a5a5 MF |
557 | |
558 | out_mutex: | |
559 | mutex_unlock(&main_bm_inode->i_mutex); | |
ccd979bd MF |
560 | |
561 | if (main_bm_bh) | |
562 | brelse(main_bm_bh); | |
563 | ||
8898a5a5 | 564 | iput(main_bm_inode); |
ccd979bd | 565 | |
8898a5a5 | 566 | out: |
4d0ddb2c TM |
567 | if (!status) |
568 | ocfs2_init_inode_steal_slot(osb); | |
ccd979bd MF |
569 | mlog_exit(status); |
570 | return status; | |
571 | } | |
572 | ||
573 | /* | |
9c7af40b | 574 | * make sure we've got at least bits_wanted contiguous bits in the |
1b1dcc1b | 575 | * local alloc. You lose them when you drop i_mutex. |
ccd979bd MF |
576 | * |
577 | * We will add ourselves to the transaction passed in, but may start | |
578 | * our own in order to shift windows. | |
579 | */ | |
580 | int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb, | |
ccd979bd MF |
581 | u32 bits_wanted, |
582 | struct ocfs2_alloc_context *ac) | |
583 | { | |
584 | int status; | |
585 | struct ocfs2_dinode *alloc; | |
586 | struct inode *local_alloc_inode; | |
587 | unsigned int free_bits; | |
588 | ||
589 | mlog_entry_void(); | |
590 | ||
ccd979bd | 591 | BUG_ON(!ac); |
ccd979bd MF |
592 | |
593 | local_alloc_inode = | |
594 | ocfs2_get_system_file_inode(osb, | |
595 | LOCAL_ALLOC_SYSTEM_INODE, | |
596 | osb->slot_num); | |
597 | if (!local_alloc_inode) { | |
598 | status = -ENOENT; | |
599 | mlog_errno(status); | |
600 | goto bail; | |
601 | } | |
da5cbf2f MF |
602 | |
603 | mutex_lock(&local_alloc_inode->i_mutex); | |
604 | ||
9c7af40b MF |
605 | /* |
606 | * We must double check state and allocator bits because | |
607 | * another process may have changed them while holding i_mutex. | |
608 | */ | |
609 | spin_lock(&osb->osb_lock); | |
610 | if (!ocfs2_la_state_enabled(osb) || | |
611 | (bits_wanted > osb->local_alloc_bits)) { | |
612 | spin_unlock(&osb->osb_lock); | |
ccd979bd MF |
613 | status = -ENOSPC; |
614 | goto bail; | |
615 | } | |
9c7af40b | 616 | spin_unlock(&osb->osb_lock); |
ccd979bd MF |
617 | |
618 | alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; | |
619 | ||
e407e397 | 620 | #ifdef CONFIG_OCFS2_DEBUG_FS |
ccd979bd MF |
621 | if (le32_to_cpu(alloc->id1.bitmap1.i_used) != |
622 | ocfs2_local_alloc_count_bits(alloc)) { | |
b0697053 | 623 | ocfs2_error(osb->sb, "local alloc inode %llu says it has " |
ccd979bd | 624 | "%u free bits, but a count shows %u", |
b0697053 | 625 | (unsigned long long)le64_to_cpu(alloc->i_blkno), |
ccd979bd MF |
626 | le32_to_cpu(alloc->id1.bitmap1.i_used), |
627 | ocfs2_local_alloc_count_bits(alloc)); | |
628 | status = -EIO; | |
629 | goto bail; | |
630 | } | |
5a58c3ef | 631 | #endif |
ccd979bd MF |
632 | |
633 | free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) - | |
634 | le32_to_cpu(alloc->id1.bitmap1.i_used); | |
635 | if (bits_wanted > free_bits) { | |
636 | /* uhoh, window change time. */ | |
637 | status = | |
638 | ocfs2_local_alloc_slide_window(osb, local_alloc_inode); | |
639 | if (status < 0) { | |
640 | if (status != -ENOSPC) | |
641 | mlog_errno(status); | |
642 | goto bail; | |
643 | } | |
9c7af40b MF |
644 | |
645 | /* | |
646 | * Under certain conditions, the window slide code | |
647 | * might have reduced the number of bits available or | |
648 | * disabled the the local alloc entirely. Re-check | |
649 | * here and return -ENOSPC if necessary. | |
650 | */ | |
651 | status = -ENOSPC; | |
652 | if (!ocfs2_la_state_enabled(osb)) | |
653 | goto bail; | |
654 | ||
655 | free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) - | |
656 | le32_to_cpu(alloc->id1.bitmap1.i_used); | |
657 | if (bits_wanted > free_bits) | |
658 | goto bail; | |
ccd979bd MF |
659 | } |
660 | ||
8fccfc82 | 661 | ac->ac_inode = local_alloc_inode; |
a4a48911 TM |
662 | /* We should never use localalloc from another slot */ |
663 | ac->ac_alloc_slot = osb->slot_num; | |
8fccfc82 | 664 | ac->ac_which = OCFS2_AC_USE_LOCAL; |
ccd979bd MF |
665 | get_bh(osb->local_alloc_bh); |
666 | ac->ac_bh = osb->local_alloc_bh; | |
ccd979bd MF |
667 | status = 0; |
668 | bail: | |
bda0233b SM |
669 | if (status < 0 && local_alloc_inode) { |
670 | mutex_unlock(&local_alloc_inode->i_mutex); | |
8fccfc82 | 671 | iput(local_alloc_inode); |
bda0233b | 672 | } |
ccd979bd | 673 | |
2fbe8d1e SM |
674 | mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num, |
675 | status); | |
676 | ||
ccd979bd MF |
677 | mlog_exit(status); |
678 | return status; | |
679 | } | |
680 | ||
681 | int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb, | |
1fabe148 | 682 | handle_t *handle, |
ccd979bd | 683 | struct ocfs2_alloc_context *ac, |
415cb800 | 684 | u32 bits_wanted, |
ccd979bd MF |
685 | u32 *bit_off, |
686 | u32 *num_bits) | |
687 | { | |
688 | int status, start; | |
689 | struct inode *local_alloc_inode; | |
ccd979bd MF |
690 | void *bitmap; |
691 | struct ocfs2_dinode *alloc; | |
692 | struct ocfs2_local_alloc *la; | |
693 | ||
694 | mlog_entry_void(); | |
695 | BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL); | |
696 | ||
ccd979bd MF |
697 | local_alloc_inode = ac->ac_inode; |
698 | alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; | |
699 | la = OCFS2_LOCAL_ALLOC(alloc); | |
700 | ||
701 | start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted); | |
702 | if (start == -1) { | |
703 | /* TODO: Shouldn't we just BUG here? */ | |
704 | status = -ENOSPC; | |
705 | mlog_errno(status); | |
706 | goto bail; | |
707 | } | |
708 | ||
709 | bitmap = la->la_bitmap; | |
710 | *bit_off = le32_to_cpu(la->la_bm_off) + start; | |
711 | /* local alloc is always contiguous by nature -- we never | |
712 | * delete bits from it! */ | |
713 | *num_bits = bits_wanted; | |
714 | ||
715 | status = ocfs2_journal_access(handle, local_alloc_inode, | |
716 | osb->local_alloc_bh, | |
717 | OCFS2_JOURNAL_ACCESS_WRITE); | |
718 | if (status < 0) { | |
719 | mlog_errno(status); | |
720 | goto bail; | |
721 | } | |
722 | ||
723 | while(bits_wanted--) | |
724 | ocfs2_set_bit(start++, bitmap); | |
725 | ||
0dd3256e | 726 | le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits); |
ccd979bd MF |
727 | |
728 | status = ocfs2_journal_dirty(handle, osb->local_alloc_bh); | |
729 | if (status < 0) { | |
730 | mlog_errno(status); | |
731 | goto bail; | |
732 | } | |
733 | ||
734 | status = 0; | |
735 | bail: | |
736 | mlog_exit(status); | |
737 | return status; | |
738 | } | |
739 | ||
740 | static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc) | |
741 | { | |
742 | int i; | |
743 | u8 *buffer; | |
744 | u32 count = 0; | |
745 | struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc); | |
746 | ||
747 | mlog_entry_void(); | |
748 | ||
749 | buffer = la->la_bitmap; | |
750 | for (i = 0; i < le16_to_cpu(la->la_size); i++) | |
751 | count += hweight8(buffer[i]); | |
752 | ||
753 | mlog_exit(count); | |
754 | return count; | |
755 | } | |
756 | ||
757 | static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb, | |
758 | struct ocfs2_dinode *alloc, | |
759 | u32 numbits) | |
760 | { | |
761 | int numfound, bitoff, left, startoff, lastzero; | |
762 | void *bitmap = NULL; | |
763 | ||
764 | mlog_entry("(numbits wanted = %u)\n", numbits); | |
765 | ||
766 | if (!alloc->id1.bitmap1.i_total) { | |
767 | mlog(0, "No bits in my window!\n"); | |
768 | bitoff = -1; | |
769 | goto bail; | |
770 | } | |
771 | ||
772 | bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap; | |
773 | ||
774 | numfound = bitoff = startoff = 0; | |
775 | lastzero = -1; | |
776 | left = le32_to_cpu(alloc->id1.bitmap1.i_total); | |
777 | while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) { | |
778 | if (bitoff == left) { | |
779 | /* mlog(0, "bitoff (%d) == left", bitoff); */ | |
780 | break; | |
781 | } | |
782 | /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, " | |
783 | "numfound = %d\n", bitoff, startoff, numfound);*/ | |
784 | ||
785 | /* Ok, we found a zero bit... is it contig. or do we | |
786 | * start over?*/ | |
787 | if (bitoff == startoff) { | |
788 | /* we found a zero */ | |
789 | numfound++; | |
790 | startoff++; | |
791 | } else { | |
792 | /* got a zero after some ones */ | |
793 | numfound = 1; | |
794 | startoff = bitoff+1; | |
795 | } | |
796 | /* we got everything we needed */ | |
797 | if (numfound == numbits) { | |
798 | /* mlog(0, "Found it all!\n"); */ | |
799 | break; | |
800 | } | |
801 | } | |
802 | ||
803 | mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff, | |
804 | numfound); | |
805 | ||
806 | if (numfound == numbits) | |
807 | bitoff = startoff - numfound; | |
808 | else | |
809 | bitoff = -1; | |
810 | ||
811 | bail: | |
812 | mlog_exit(bitoff); | |
813 | return bitoff; | |
814 | } | |
815 | ||
816 | static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc) | |
817 | { | |
818 | struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc); | |
819 | int i; | |
820 | mlog_entry_void(); | |
821 | ||
822 | alloc->id1.bitmap1.i_total = 0; | |
823 | alloc->id1.bitmap1.i_used = 0; | |
824 | la->la_bm_off = 0; | |
825 | for(i = 0; i < le16_to_cpu(la->la_size); i++) | |
826 | la->la_bitmap[i] = 0; | |
827 | ||
828 | mlog_exit_void(); | |
829 | } | |
830 | ||
831 | #if 0 | |
832 | /* turn this on and uncomment below to aid debugging window shifts. */ | |
833 | static void ocfs2_verify_zero_bits(unsigned long *bitmap, | |
834 | unsigned int start, | |
835 | unsigned int count) | |
836 | { | |
837 | unsigned int tmp = count; | |
838 | while(tmp--) { | |
839 | if (ocfs2_test_bit(start + tmp, bitmap)) { | |
840 | printk("ocfs2_verify_zero_bits: start = %u, count = " | |
841 | "%u\n", start, count); | |
842 | printk("ocfs2_verify_zero_bits: bit %u is set!", | |
843 | start + tmp); | |
844 | BUG(); | |
845 | } | |
846 | } | |
847 | } | |
848 | #endif | |
849 | ||
850 | /* | |
851 | * sync the local alloc to main bitmap. | |
852 | * | |
853 | * assumes you've already locked the main bitmap -- the bitmap inode | |
854 | * passed is used for caching. | |
855 | */ | |
856 | static int ocfs2_sync_local_to_main(struct ocfs2_super *osb, | |
1fabe148 | 857 | handle_t *handle, |
ccd979bd MF |
858 | struct ocfs2_dinode *alloc, |
859 | struct inode *main_bm_inode, | |
860 | struct buffer_head *main_bm_bh) | |
861 | { | |
862 | int status = 0; | |
863 | int bit_off, left, count, start; | |
864 | u64 la_start_blk; | |
865 | u64 blkno; | |
866 | void *bitmap; | |
867 | struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc); | |
868 | ||
5a58c3ef | 869 | mlog_entry("total = %u, used = %u\n", |
ccd979bd | 870 | le32_to_cpu(alloc->id1.bitmap1.i_total), |
ccd979bd MF |
871 | le32_to_cpu(alloc->id1.bitmap1.i_used)); |
872 | ||
873 | if (!alloc->id1.bitmap1.i_total) { | |
874 | mlog(0, "nothing to sync!\n"); | |
875 | goto bail; | |
876 | } | |
877 | ||
878 | if (le32_to_cpu(alloc->id1.bitmap1.i_used) == | |
879 | le32_to_cpu(alloc->id1.bitmap1.i_total)) { | |
880 | mlog(0, "all bits were taken!\n"); | |
881 | goto bail; | |
882 | } | |
883 | ||
884 | la_start_blk = ocfs2_clusters_to_blocks(osb->sb, | |
885 | le32_to_cpu(la->la_bm_off)); | |
886 | bitmap = la->la_bitmap; | |
887 | start = count = bit_off = 0; | |
888 | left = le32_to_cpu(alloc->id1.bitmap1.i_total); | |
889 | ||
890 | while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start)) | |
891 | != -1) { | |
892 | if ((bit_off < left) && (bit_off == start)) { | |
893 | count++; | |
894 | start++; | |
895 | continue; | |
896 | } | |
897 | if (count) { | |
898 | blkno = la_start_blk + | |
899 | ocfs2_clusters_to_blocks(osb->sb, | |
900 | start - count); | |
901 | ||
b0697053 MF |
902 | mlog(0, "freeing %u bits starting at local alloc bit " |
903 | "%u (la_start_blk = %llu, blkno = %llu)\n", | |
904 | count, start - count, | |
905 | (unsigned long long)la_start_blk, | |
906 | (unsigned long long)blkno); | |
ccd979bd MF |
907 | |
908 | status = ocfs2_free_clusters(handle, main_bm_inode, | |
909 | main_bm_bh, blkno, count); | |
910 | if (status < 0) { | |
911 | mlog_errno(status); | |
912 | goto bail; | |
913 | } | |
914 | } | |
915 | if (bit_off >= left) | |
916 | break; | |
917 | count = 1; | |
918 | start = bit_off + 1; | |
919 | } | |
920 | ||
921 | bail: | |
922 | mlog_exit(status); | |
923 | return status; | |
924 | } | |
925 | ||
9c7af40b MF |
926 | enum ocfs2_la_event { |
927 | OCFS2_LA_EVENT_SLIDE, /* Normal window slide. */ | |
928 | OCFS2_LA_EVENT_FRAGMENTED, /* The global bitmap has | |
929 | * enough bits theoretically | |
930 | * free, but a contiguous | |
931 | * allocation could not be | |
932 | * found. */ | |
933 | OCFS2_LA_EVENT_ENOSPC, /* Global bitmap doesn't have | |
934 | * enough bits free to satisfy | |
935 | * our request. */ | |
936 | }; | |
937 | #define OCFS2_LA_ENABLE_INTERVAL (30 * HZ) | |
938 | /* | |
939 | * Given an event, calculate the size of our next local alloc window. | |
940 | * | |
941 | * This should always be called under i_mutex of the local alloc inode | |
942 | * so that local alloc disabling doesn't race with processes trying to | |
943 | * use the allocator. | |
944 | * | |
945 | * Returns the state which the local alloc was left in. This value can | |
946 | * be ignored by some paths. | |
947 | */ | |
948 | static int ocfs2_recalc_la_window(struct ocfs2_super *osb, | |
949 | enum ocfs2_la_event event) | |
950 | { | |
951 | unsigned int bits; | |
952 | int state; | |
953 | ||
954 | spin_lock(&osb->osb_lock); | |
955 | if (osb->local_alloc_state == OCFS2_LA_DISABLED) { | |
956 | WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED); | |
957 | goto out_unlock; | |
958 | } | |
959 | ||
960 | /* | |
961 | * ENOSPC and fragmentation are treated similarly for now. | |
962 | */ | |
963 | if (event == OCFS2_LA_EVENT_ENOSPC || | |
964 | event == OCFS2_LA_EVENT_FRAGMENTED) { | |
965 | /* | |
966 | * We ran out of contiguous space in the primary | |
967 | * bitmap. Drastically reduce the number of bits used | |
968 | * by local alloc until we have to disable it. | |
969 | */ | |
970 | bits = osb->local_alloc_bits >> 1; | |
971 | if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) { | |
972 | /* | |
973 | * By setting state to THROTTLED, we'll keep | |
974 | * the number of local alloc bits used down | |
975 | * until an event occurs which would give us | |
976 | * reason to assume the bitmap situation might | |
977 | * have changed. | |
978 | */ | |
979 | osb->local_alloc_state = OCFS2_LA_THROTTLED; | |
980 | osb->local_alloc_bits = bits; | |
981 | } else { | |
982 | osb->local_alloc_state = OCFS2_LA_DISABLED; | |
983 | } | |
984 | queue_delayed_work(ocfs2_wq, &osb->la_enable_wq, | |
985 | OCFS2_LA_ENABLE_INTERVAL); | |
986 | goto out_unlock; | |
987 | } | |
988 | ||
989 | /* | |
990 | * Don't increase the size of the local alloc window until we | |
991 | * know we might be able to fulfill the request. Otherwise, we | |
992 | * risk bouncing around the global bitmap during periods of | |
993 | * low space. | |
994 | */ | |
995 | if (osb->local_alloc_state != OCFS2_LA_THROTTLED) | |
996 | osb->local_alloc_bits = osb->local_alloc_default_bits; | |
997 | ||
998 | out_unlock: | |
999 | state = osb->local_alloc_state; | |
1000 | spin_unlock(&osb->osb_lock); | |
1001 | ||
1002 | return state; | |
1003 | } | |
1004 | ||
ccd979bd | 1005 | static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb, |
ccd979bd MF |
1006 | struct ocfs2_alloc_context **ac, |
1007 | struct inode **bitmap_inode, | |
1008 | struct buffer_head **bitmap_bh) | |
1009 | { | |
1010 | int status; | |
1011 | ||
cd861280 | 1012 | *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL); |
ccd979bd MF |
1013 | if (!(*ac)) { |
1014 | status = -ENOMEM; | |
1015 | mlog_errno(status); | |
1016 | goto bail; | |
1017 | } | |
1018 | ||
9c7af40b | 1019 | retry_enospc: |
ebcee4b5 | 1020 | (*ac)->ac_bits_wanted = osb->local_alloc_bits; |
ccd979bd MF |
1021 | |
1022 | status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac); | |
9c7af40b MF |
1023 | if (status == -ENOSPC) { |
1024 | if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) == | |
1025 | OCFS2_LA_DISABLED) | |
1026 | goto bail; | |
1027 | ||
1028 | ocfs2_free_ac_resource(*ac); | |
1029 | memset(*ac, 0, sizeof(struct ocfs2_alloc_context)); | |
1030 | goto retry_enospc; | |
1031 | } | |
ccd979bd | 1032 | if (status < 0) { |
9c7af40b | 1033 | mlog_errno(status); |
ccd979bd MF |
1034 | goto bail; |
1035 | } | |
1036 | ||
1037 | *bitmap_inode = (*ac)->ac_inode; | |
1038 | igrab(*bitmap_inode); | |
1039 | *bitmap_bh = (*ac)->ac_bh; | |
1040 | get_bh(*bitmap_bh); | |
1041 | status = 0; | |
1042 | bail: | |
1043 | if ((status < 0) && *ac) { | |
1044 | ocfs2_free_alloc_context(*ac); | |
1045 | *ac = NULL; | |
1046 | } | |
1047 | ||
1048 | mlog_exit(status); | |
1049 | return status; | |
1050 | } | |
1051 | ||
1052 | /* | |
1053 | * pass it the bitmap lock in lock_bh if you have it. | |
1054 | */ | |
1055 | static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb, | |
1fabe148 | 1056 | handle_t *handle, |
ccd979bd MF |
1057 | struct ocfs2_alloc_context *ac) |
1058 | { | |
1059 | int status = 0; | |
1060 | u32 cluster_off, cluster_count; | |
1061 | struct ocfs2_dinode *alloc = NULL; | |
1062 | struct ocfs2_local_alloc *la; | |
1063 | ||
1064 | mlog_entry_void(); | |
1065 | ||
1066 | alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; | |
1067 | la = OCFS2_LOCAL_ALLOC(alloc); | |
1068 | ||
1069 | if (alloc->id1.bitmap1.i_total) | |
1070 | mlog(0, "asking me to alloc a new window over a non-empty " | |
1071 | "one\n"); | |
1072 | ||
1073 | mlog(0, "Allocating %u clusters for a new window.\n", | |
ebcee4b5 | 1074 | osb->local_alloc_bits); |
883d4cae MF |
1075 | |
1076 | /* Instruct the allocation code to try the most recently used | |
1077 | * cluster group. We'll re-record the group used this pass | |
1078 | * below. */ | |
1079 | ac->ac_last_group = osb->la_last_gd; | |
1080 | ||
ccd979bd MF |
1081 | /* we used the generic suballoc reserve function, but we set |
1082 | * everything up nicely, so there's no reason why we can't use | |
1083 | * the more specific cluster api to claim bits. */ | |
ebcee4b5 | 1084 | status = ocfs2_claim_clusters(osb, handle, ac, osb->local_alloc_bits, |
ccd979bd | 1085 | &cluster_off, &cluster_count); |
9c7af40b MF |
1086 | if (status == -ENOSPC) { |
1087 | retry_enospc: | |
1088 | /* | |
1089 | * Note: We could also try syncing the journal here to | |
1090 | * allow use of any free bits which the current | |
1091 | * transaction can't give us access to. --Mark | |
1092 | */ | |
1093 | if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) == | |
1094 | OCFS2_LA_DISABLED) | |
1095 | goto bail; | |
1096 | ||
1097 | status = ocfs2_claim_clusters(osb, handle, ac, | |
1098 | osb->local_alloc_bits, | |
1099 | &cluster_off, | |
1100 | &cluster_count); | |
1101 | if (status == -ENOSPC) | |
1102 | goto retry_enospc; | |
1103 | /* | |
1104 | * We only shrunk the *minimum* number of in our | |
1105 | * request - it's entirely possible that the allocator | |
1106 | * might give us more than we asked for. | |
1107 | */ | |
1108 | if (status == 0) { | |
1109 | spin_lock(&osb->osb_lock); | |
1110 | osb->local_alloc_bits = cluster_count; | |
1111 | spin_unlock(&osb->osb_lock); | |
1112 | } | |
1113 | } | |
ccd979bd MF |
1114 | if (status < 0) { |
1115 | if (status != -ENOSPC) | |
1116 | mlog_errno(status); | |
1117 | goto bail; | |
1118 | } | |
1119 | ||
883d4cae MF |
1120 | osb->la_last_gd = ac->ac_last_group; |
1121 | ||
ccd979bd MF |
1122 | la->la_bm_off = cpu_to_le32(cluster_off); |
1123 | alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count); | |
1124 | /* just in case... In the future when we find space ourselves, | |
1125 | * we don't have to get all contiguous -- but we'll have to | |
1126 | * set all previously used bits in bitmap and update | |
1127 | * la_bits_set before setting the bits in the main bitmap. */ | |
1128 | alloc->id1.bitmap1.i_used = 0; | |
1129 | memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0, | |
1130 | le16_to_cpu(la->la_size)); | |
1131 | ||
1132 | mlog(0, "New window allocated:\n"); | |
1133 | mlog(0, "window la_bm_off = %u\n", | |
1134 | OCFS2_LOCAL_ALLOC(alloc)->la_bm_off); | |
1135 | mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total)); | |
1136 | ||
1137 | bail: | |
1138 | mlog_exit(status); | |
1139 | return status; | |
1140 | } | |
1141 | ||
1142 | /* Note that we do *NOT* lock the local alloc inode here as | |
1143 | * it's been locked already for us. */ | |
1144 | static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb, | |
1145 | struct inode *local_alloc_inode) | |
1146 | { | |
1147 | int status = 0; | |
1148 | struct buffer_head *main_bm_bh = NULL; | |
1149 | struct inode *main_bm_inode = NULL; | |
1fabe148 | 1150 | handle_t *handle = NULL; |
ccd979bd MF |
1151 | struct ocfs2_dinode *alloc; |
1152 | struct ocfs2_dinode *alloc_copy = NULL; | |
1153 | struct ocfs2_alloc_context *ac = NULL; | |
1154 | ||
1155 | mlog_entry_void(); | |
1156 | ||
9c7af40b MF |
1157 | ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE); |
1158 | ||
ccd979bd MF |
1159 | /* This will lock the main bitmap for us. */ |
1160 | status = ocfs2_local_alloc_reserve_for_window(osb, | |
ccd979bd MF |
1161 | &ac, |
1162 | &main_bm_inode, | |
1163 | &main_bm_bh); | |
1164 | if (status < 0) { | |
1165 | if (status != -ENOSPC) | |
1166 | mlog_errno(status); | |
1167 | goto bail; | |
1168 | } | |
1169 | ||
65eff9cc | 1170 | handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS); |
ccd979bd MF |
1171 | if (IS_ERR(handle)) { |
1172 | status = PTR_ERR(handle); | |
1173 | handle = NULL; | |
1174 | mlog_errno(status); | |
1175 | goto bail; | |
1176 | } | |
1177 | ||
1178 | alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data; | |
1179 | ||
1180 | /* We want to clear the local alloc before doing anything | |
1181 | * else, so that if we error later during this operation, | |
1182 | * local alloc shutdown won't try to double free main bitmap | |
1183 | * bits. Make a copy so the sync function knows which bits to | |
1184 | * free. */ | |
4ba1c5bf | 1185 | alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS); |
ccd979bd MF |
1186 | if (!alloc_copy) { |
1187 | status = -ENOMEM; | |
1188 | mlog_errno(status); | |
1189 | goto bail; | |
1190 | } | |
1191 | memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size); | |
1192 | ||
1193 | status = ocfs2_journal_access(handle, local_alloc_inode, | |
1194 | osb->local_alloc_bh, | |
1195 | OCFS2_JOURNAL_ACCESS_WRITE); | |
1196 | if (status < 0) { | |
1197 | mlog_errno(status); | |
1198 | goto bail; | |
1199 | } | |
1200 | ||
1201 | ocfs2_clear_local_alloc(alloc); | |
1202 | ||
1203 | status = ocfs2_journal_dirty(handle, osb->local_alloc_bh); | |
1204 | if (status < 0) { | |
1205 | mlog_errno(status); | |
1206 | goto bail; | |
1207 | } | |
1208 | ||
1209 | status = ocfs2_sync_local_to_main(osb, handle, alloc_copy, | |
1210 | main_bm_inode, main_bm_bh); | |
1211 | if (status < 0) { | |
1212 | mlog_errno(status); | |
1213 | goto bail; | |
1214 | } | |
1215 | ||
1216 | status = ocfs2_local_alloc_new_window(osb, handle, ac); | |
1217 | if (status < 0) { | |
1218 | if (status != -ENOSPC) | |
1219 | mlog_errno(status); | |
1220 | goto bail; | |
1221 | } | |
1222 | ||
1223 | atomic_inc(&osb->alloc_stats.moves); | |
1224 | ||
1225 | status = 0; | |
1226 | bail: | |
1227 | if (handle) | |
02dc1af4 | 1228 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
1229 | |
1230 | if (main_bm_bh) | |
1231 | brelse(main_bm_bh); | |
1232 | ||
1233 | if (main_bm_inode) | |
1234 | iput(main_bm_inode); | |
1235 | ||
1236 | if (alloc_copy) | |
1237 | kfree(alloc_copy); | |
1238 | ||
1239 | if (ac) | |
1240 | ocfs2_free_alloc_context(ac); | |
1241 | ||
1242 | mlog_exit(status); | |
1243 | return status; | |
1244 | } | |
1245 |