blob: b1c634d676a0efaabbc5be918bc1352b8110c054 [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * 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>
Mark Fasheh9a8ff572008-07-29 18:29:18 -070031#include <linux/debugfs.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080032
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
Mark Fashehccd979b2005-12-15 14:31:24 -080051static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc);
52
53static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
54 struct ocfs2_dinode *alloc,
55 u32 numbits);
56
57static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc);
58
59static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -070060 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080061 struct ocfs2_dinode *alloc,
62 struct inode *main_bm_inode,
63 struct buffer_head *main_bm_bh);
64
65static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -080066 struct ocfs2_alloc_context **ac,
67 struct inode **bitmap_inode,
68 struct buffer_head **bitmap_bh);
69
70static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -070071 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080072 struct ocfs2_alloc_context *ac);
73
74static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
75 struct inode *local_alloc_inode);
76
Mark Fasheh9a8ff572008-07-29 18:29:18 -070077#ifdef CONFIG_OCFS2_FS_STATS
78
Mark Fasheh9a8ff572008-07-29 18:29:18 -070079static int ocfs2_la_debug_open(struct inode *inode, struct file *file)
80{
81 file->private_data = inode->i_private;
82 return 0;
83}
84
85#define LA_DEBUG_BUF_SZ PAGE_CACHE_SIZE
86#define LA_DEBUG_VER 1
87static ssize_t ocfs2_la_debug_read(struct file *file, char __user *userbuf,
88 size_t count, loff_t *ppos)
89{
Mark Fasheh4cc81242008-10-07 11:02:04 -070090 static DEFINE_MUTEX(la_debug_mutex);
Mark Fasheh9a8ff572008-07-29 18:29:18 -070091 struct ocfs2_super *osb = file->private_data;
92 int written, ret;
93 char *buf = osb->local_alloc_debug_buf;
94
95 mutex_lock(&la_debug_mutex);
96 memset(buf, 0, LA_DEBUG_BUF_SZ);
97
98 written = snprintf(buf, LA_DEBUG_BUF_SZ,
99 "0x%x\t0x%llx\t%u\t%u\t0x%x\n",
100 LA_DEBUG_VER,
101 (unsigned long long)osb->la_last_gd,
102 osb->local_alloc_default_bits,
103 osb->local_alloc_bits, osb->local_alloc_state);
104
105 ret = simple_read_from_buffer(userbuf, count, ppos, buf, written);
106
107 mutex_unlock(&la_debug_mutex);
108 return ret;
109}
110
111static const struct file_operations ocfs2_la_debug_fops = {
112 .open = ocfs2_la_debug_open,
113 .read = ocfs2_la_debug_read,
114};
115
116static void ocfs2_init_la_debug(struct ocfs2_super *osb)
117{
118 osb->local_alloc_debug_buf = kmalloc(LA_DEBUG_BUF_SZ, GFP_NOFS);
119 if (!osb->local_alloc_debug_buf)
120 return;
121
122 osb->local_alloc_debug = debugfs_create_file("local_alloc_stats",
123 S_IFREG|S_IRUSR,
124 osb->osb_debug_root,
125 osb,
126 &ocfs2_la_debug_fops);
127 if (!osb->local_alloc_debug) {
128 kfree(osb->local_alloc_debug_buf);
129 osb->local_alloc_debug_buf = NULL;
130 }
131}
132
133static void ocfs2_shutdown_la_debug(struct ocfs2_super *osb)
134{
135 if (osb->local_alloc_debug)
136 debugfs_remove(osb->local_alloc_debug);
137
138 if (osb->local_alloc_debug_buf)
139 kfree(osb->local_alloc_debug_buf);
140
141 osb->local_alloc_debug_buf = NULL;
142 osb->local_alloc_debug = NULL;
143}
144#else /* CONFIG_OCFS2_FS_STATS */
145static void ocfs2_init_la_debug(struct ocfs2_super *osb)
146{
147 return;
148}
149static void ocfs2_shutdown_la_debug(struct ocfs2_super *osb)
150{
151 return;
152}
153#endif
154
Mark Fasheh9c7af402008-07-28 18:02:53 -0700155static inline int ocfs2_la_state_enabled(struct ocfs2_super *osb)
156{
157 return (osb->local_alloc_state == OCFS2_LA_THROTTLED ||
158 osb->local_alloc_state == OCFS2_LA_ENABLED);
159}
160
161void ocfs2_local_alloc_seen_free_bits(struct ocfs2_super *osb,
162 unsigned int num_clusters)
163{
164 spin_lock(&osb->osb_lock);
165 if (osb->local_alloc_state == OCFS2_LA_DISABLED ||
166 osb->local_alloc_state == OCFS2_LA_THROTTLED)
167 if (num_clusters >= osb->local_alloc_default_bits) {
168 cancel_delayed_work(&osb->la_enable_wq);
169 osb->local_alloc_state = OCFS2_LA_ENABLED;
170 }
171 spin_unlock(&osb->osb_lock);
172}
173
174void ocfs2_la_enable_worker(struct work_struct *work)
175{
176 struct ocfs2_super *osb =
177 container_of(work, struct ocfs2_super,
178 la_enable_wq.work);
179 spin_lock(&osb->osb_lock);
180 osb->local_alloc_state = OCFS2_LA_ENABLED;
181 spin_unlock(&osb->osb_lock);
182}
183
Mark Fashehccd979b2005-12-15 14:31:24 -0800184/*
185 * Tell us whether a given allocation should use the local alloc
186 * file. Otherwise, it has to go to the main bitmap.
Mark Fasheh9c7af402008-07-28 18:02:53 -0700187 *
188 * This function does semi-dirty reads of local alloc size and state!
189 * This is ok however, as the values are re-checked once under mutex.
Mark Fashehccd979b2005-12-15 14:31:24 -0800190 */
191int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
192{
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800193 int ret = 0;
Mark Fasheh9c7af402008-07-28 18:02:53 -0700194 int la_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800195
Mark Fasheh9c7af402008-07-28 18:02:53 -0700196 spin_lock(&osb->osb_lock);
197 la_bits = osb->local_alloc_bits;
198
199 if (!ocfs2_la_state_enabled(osb))
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800200 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800201
202 /* la_bits should be at least twice the size (in clusters) of
203 * a new block group. We want to be sure block group
204 * allocations go through the local alloc, so allow an
205 * allocation to take up to half the bitmap. */
206 if (bits > (la_bits / 2))
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800207 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800208
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800209 ret = 1;
210bail:
211 mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n",
212 osb->local_alloc_state, (unsigned long long)bits, la_bits, ret);
Mark Fasheh9c7af402008-07-28 18:02:53 -0700213 spin_unlock(&osb->osb_lock);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800214 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -0800215}
216
217int ocfs2_load_local_alloc(struct ocfs2_super *osb)
218{
219 int status = 0;
220 struct ocfs2_dinode *alloc = NULL;
221 struct buffer_head *alloc_bh = NULL;
222 u32 num_used;
223 struct inode *inode = NULL;
224 struct ocfs2_local_alloc *la;
225
226 mlog_entry_void();
227
Mark Fasheh9a8ff572008-07-29 18:29:18 -0700228 ocfs2_init_la_debug(osb);
229
Mark Fashehebcee4b2008-07-28 14:55:20 -0700230 if (osb->local_alloc_bits == 0)
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800231 goto bail;
232
Mark Fashehebcee4b2008-07-28 14:55:20 -0700233 if (osb->local_alloc_bits >= osb->bitmap_cpg) {
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800234 mlog(ML_NOTICE, "Requested local alloc window %d is larger "
235 "than max possible %u. Using defaults.\n",
Mark Fashehebcee4b2008-07-28 14:55:20 -0700236 osb->local_alloc_bits, (osb->bitmap_cpg - 1));
237 osb->local_alloc_bits =
238 ocfs2_megabytes_to_clusters(osb->sb,
239 OCFS2_DEFAULT_LOCAL_ALLOC_SIZE);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800240 }
241
Mark Fashehccd979b2005-12-15 14:31:24 -0800242 /* read the alloc off disk */
243 inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
244 osb->slot_num);
245 if (!inode) {
246 status = -EINVAL;
247 mlog_errno(status);
248 goto bail;
249 }
250
251 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
252 &alloc_bh, 0, inode);
253 if (status < 0) {
254 mlog_errno(status);
255 goto bail;
256 }
257
258 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
259 la = OCFS2_LOCAL_ALLOC(alloc);
260
261 if (!(le32_to_cpu(alloc->i_flags) &
262 (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
Mark Fashehb06970532006-03-03 10:24:33 -0800263 mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
264 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800265 status = -EINVAL;
266 goto bail;
267 }
268
269 if ((la->la_size == 0) ||
270 (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) {
271 mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n",
272 le16_to_cpu(la->la_size));
273 status = -EINVAL;
274 goto bail;
275 }
276
277 /* do a little verification. */
278 num_used = ocfs2_local_alloc_count_bits(alloc);
279
280 /* hopefully the local alloc has always been recovered before
281 * we load it. */
282 if (num_used
283 || alloc->id1.bitmap1.i_used
284 || alloc->id1.bitmap1.i_total
285 || la->la_bm_off)
286 mlog(ML_ERROR, "Local alloc hasn't been recovered!\n"
287 "found = %u, set = %u, taken = %u, off = %u\n",
288 num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
289 le32_to_cpu(alloc->id1.bitmap1.i_total),
290 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
291
292 osb->local_alloc_bh = alloc_bh;
293 osb->local_alloc_state = OCFS2_LA_ENABLED;
294
295bail:
296 if (status < 0)
297 if (alloc_bh)
298 brelse(alloc_bh);
299 if (inode)
300 iput(inode);
301
Mark Fasheh9a8ff572008-07-29 18:29:18 -0700302 if (status < 0)
303 ocfs2_shutdown_la_debug(osb);
304
Mark Fashehebcee4b2008-07-28 14:55:20 -0700305 mlog(0, "Local alloc window bits = %d\n", osb->local_alloc_bits);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800306
Mark Fashehccd979b2005-12-15 14:31:24 -0800307 mlog_exit(status);
308 return status;
309}
310
311/*
312 * return any unused bits to the bitmap and write out a clean
313 * local_alloc.
314 *
315 * local_alloc_bh is optional. If not passed, we will simply use the
316 * one off osb. If you do pass it however, be warned that it *will* be
317 * returned brelse'd and NULL'd out.*/
318void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
319{
320 int status;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700321 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800322 struct inode *local_alloc_inode = NULL;
323 struct buffer_head *bh = NULL;
324 struct buffer_head *main_bm_bh = NULL;
325 struct inode *main_bm_inode = NULL;
326 struct ocfs2_dinode *alloc_copy = NULL;
327 struct ocfs2_dinode *alloc = NULL;
328
329 mlog_entry_void();
330
Mark Fasheh9c7af402008-07-28 18:02:53 -0700331 cancel_delayed_work(&osb->la_enable_wq);
332 flush_workqueue(ocfs2_wq);
333
Mark Fasheh9a8ff572008-07-29 18:29:18 -0700334 ocfs2_shutdown_la_debug(osb);
335
Mark Fashehccd979b2005-12-15 14:31:24 -0800336 if (osb->local_alloc_state == OCFS2_LA_UNUSED)
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700337 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800338
339 local_alloc_inode =
340 ocfs2_get_system_file_inode(osb,
341 LOCAL_ALLOC_SYSTEM_INODE,
342 osb->slot_num);
343 if (!local_alloc_inode) {
344 status = -ENOENT;
345 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700346 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800347 }
348
349 osb->local_alloc_state = OCFS2_LA_DISABLED;
350
Mark Fashehccd979b2005-12-15 14:31:24 -0800351 main_bm_inode = ocfs2_get_system_file_inode(osb,
352 GLOBAL_BITMAP_SYSTEM_INODE,
353 OCFS2_INVALID_SLOT);
354 if (!main_bm_inode) {
355 status = -EINVAL;
356 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700357 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800358 }
359
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700360 mutex_lock(&main_bm_inode->i_mutex);
361
Mark Fashehe63aecb62007-10-18 15:30:42 -0700362 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800363 if (status < 0) {
364 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700365 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -0800366 }
367
368 /* WINDOW_MOVE_CREDITS is a bit heavy... */
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700369 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800370 if (IS_ERR(handle)) {
371 mlog_errno(PTR_ERR(handle));
372 handle = NULL;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700373 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800374 }
375
376 bh = osb->local_alloc_bh;
377 alloc = (struct ocfs2_dinode *) bh->b_data;
378
Sunil Mushran4ba1c5b2008-04-18 15:03:59 -0700379 alloc_copy = kmalloc(bh->b_size, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800380 if (!alloc_copy) {
381 status = -ENOMEM;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700382 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800383 }
384 memcpy(alloc_copy, alloc, bh->b_size);
385
386 status = ocfs2_journal_access(handle, local_alloc_inode, bh,
387 OCFS2_JOURNAL_ACCESS_WRITE);
388 if (status < 0) {
389 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700390 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800391 }
392
393 ocfs2_clear_local_alloc(alloc);
394
395 status = ocfs2_journal_dirty(handle, bh);
396 if (status < 0) {
397 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700398 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800399 }
400
401 brelse(bh);
402 osb->local_alloc_bh = NULL;
403 osb->local_alloc_state = OCFS2_LA_UNUSED;
404
405 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
406 main_bm_inode, main_bm_bh);
407 if (status < 0)
408 mlog_errno(status);
409
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700410out_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700411 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800412
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700413out_unlock:
Mark Fashehccd979b2005-12-15 14:31:24 -0800414 if (main_bm_bh)
415 brelse(main_bm_bh);
416
Mark Fashehe63aecb62007-10-18 15:30:42 -0700417 ocfs2_inode_unlock(main_bm_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800418
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700419out_mutex:
420 mutex_unlock(&main_bm_inode->i_mutex);
421 iput(main_bm_inode);
422
423out:
Mark Fashehccd979b2005-12-15 14:31:24 -0800424 if (local_alloc_inode)
425 iput(local_alloc_inode);
426
427 if (alloc_copy)
428 kfree(alloc_copy);
429
430 mlog_exit_void();
431}
432
433/*
434 * We want to free the bitmap bits outside of any recovery context as
435 * we'll need a cluster lock to do so, but we must clear the local
436 * alloc before giving up the recovered nodes journal. To solve this,
437 * we kmalloc a copy of the local alloc before it's change for the
438 * caller to process with ocfs2_complete_local_alloc_recovery
439 */
440int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
441 int slot_num,
442 struct ocfs2_dinode **alloc_copy)
443{
444 int status = 0;
445 struct buffer_head *alloc_bh = NULL;
446 struct inode *inode = NULL;
447 struct ocfs2_dinode *alloc;
448
449 mlog_entry("(slot_num = %d)\n", slot_num);
450
451 *alloc_copy = NULL;
452
453 inode = ocfs2_get_system_file_inode(osb,
454 LOCAL_ALLOC_SYSTEM_INODE,
455 slot_num);
456 if (!inode) {
457 status = -EINVAL;
458 mlog_errno(status);
459 goto bail;
460 }
461
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800462 mutex_lock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800463
464 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
465 &alloc_bh, 0, inode);
466 if (status < 0) {
467 mlog_errno(status);
468 goto bail;
469 }
470
471 *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL);
472 if (!(*alloc_copy)) {
473 status = -ENOMEM;
474 goto bail;
475 }
476 memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
477
478 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
479 ocfs2_clear_local_alloc(alloc);
480
481 status = ocfs2_write_block(osb, alloc_bh, inode);
482 if (status < 0)
483 mlog_errno(status);
484
485bail:
486 if ((status < 0) && (*alloc_copy)) {
487 kfree(*alloc_copy);
488 *alloc_copy = NULL;
489 }
490
491 if (alloc_bh)
492 brelse(alloc_bh);
493
494 if (inode) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800495 mutex_unlock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800496 iput(inode);
497 }
498
499 mlog_exit(status);
500 return status;
501}
502
503/*
504 * Step 2: By now, we've completed the journal recovery, we've stamped
505 * a clean local alloc on disk and dropped the node out of the
506 * recovery map. Dlm locks will no longer stall, so lets clear out the
507 * main bitmap.
508 */
509int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
510 struct ocfs2_dinode *alloc)
511{
512 int status;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700513 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800514 struct buffer_head *main_bm_bh = NULL;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700515 struct inode *main_bm_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800516
517 mlog_entry_void();
518
Mark Fashehccd979b2005-12-15 14:31:24 -0800519 main_bm_inode = ocfs2_get_system_file_inode(osb,
520 GLOBAL_BITMAP_SYSTEM_INODE,
521 OCFS2_INVALID_SLOT);
522 if (!main_bm_inode) {
523 status = -EINVAL;
524 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700525 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800526 }
527
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700528 mutex_lock(&main_bm_inode->i_mutex);
529
Mark Fashehe63aecb62007-10-18 15:30:42 -0700530 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800531 if (status < 0) {
532 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700533 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -0800534 }
535
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700536 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800537 if (IS_ERR(handle)) {
538 status = PTR_ERR(handle);
539 handle = NULL;
540 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700541 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800542 }
543
544 /* we want the bitmap change to be recorded on disk asap */
Mark Fasheh1fabe142006-10-09 18:11:45 -0700545 handle->h_sync = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800546
547 status = ocfs2_sync_local_to_main(osb, handle, alloc,
548 main_bm_inode, main_bm_bh);
549 if (status < 0)
550 mlog_errno(status);
551
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700552 ocfs2_commit_trans(osb, handle);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700553
554out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700555 ocfs2_inode_unlock(main_bm_inode, 1);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700556
557out_mutex:
558 mutex_unlock(&main_bm_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800559
560 if (main_bm_bh)
561 brelse(main_bm_bh);
562
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700563 iput(main_bm_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800564
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700565out:
Tao Ma4d0ddb22008-03-05 16:11:46 +0800566 if (!status)
567 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800568 mlog_exit(status);
569 return status;
570}
571
Joel Becker1187c962008-09-03 20:03:39 -0700572/* Check to see if the local alloc window is within ac->ac_max_block */
573static int ocfs2_local_alloc_in_range(struct inode *inode,
574 struct ocfs2_alloc_context *ac,
575 u32 bits_wanted)
576{
577 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
578 struct ocfs2_dinode *alloc;
579 struct ocfs2_local_alloc *la;
580 int start;
581 u64 block_off;
582
583 if (!ac->ac_max_block)
584 return 1;
585
586 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
587 la = OCFS2_LOCAL_ALLOC(alloc);
588
589 start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted);
590 if (start == -1) {
591 mlog_errno(-ENOSPC);
592 return 0;
593 }
594
595 /*
596 * Converting (bm_off + start + bits_wanted) to blocks gives us
597 * the blkno just past our actual allocation. This is perfect
598 * to compare with ac_max_block.
599 */
600 block_off = ocfs2_clusters_to_blocks(inode->i_sb,
601 le32_to_cpu(la->la_bm_off) +
602 start + bits_wanted);
603 mlog(0, "Checking %llu against %llu\n",
604 (unsigned long long)block_off,
605 (unsigned long long)ac->ac_max_block);
606 if (block_off > ac->ac_max_block)
607 return 0;
608
609 return 1;
610}
611
Mark Fashehccd979b2005-12-15 14:31:24 -0800612/*
Mark Fasheh9c7af402008-07-28 18:02:53 -0700613 * make sure we've got at least bits_wanted contiguous bits in the
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800614 * local alloc. You lose them when you drop i_mutex.
Mark Fashehccd979b2005-12-15 14:31:24 -0800615 *
616 * We will add ourselves to the transaction passed in, but may start
617 * our own in order to shift windows.
618 */
619int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800620 u32 bits_wanted,
621 struct ocfs2_alloc_context *ac)
622{
623 int status;
624 struct ocfs2_dinode *alloc;
625 struct inode *local_alloc_inode;
626 unsigned int free_bits;
627
628 mlog_entry_void();
629
Mark Fashehccd979b2005-12-15 14:31:24 -0800630 BUG_ON(!ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800631
632 local_alloc_inode =
633 ocfs2_get_system_file_inode(osb,
634 LOCAL_ALLOC_SYSTEM_INODE,
635 osb->slot_num);
636 if (!local_alloc_inode) {
637 status = -ENOENT;
638 mlog_errno(status);
639 goto bail;
640 }
Mark Fashehda5cbf22006-10-06 18:34:35 -0700641
642 mutex_lock(&local_alloc_inode->i_mutex);
643
Mark Fasheh9c7af402008-07-28 18:02:53 -0700644 /*
645 * We must double check state and allocator bits because
646 * another process may have changed them while holding i_mutex.
647 */
648 spin_lock(&osb->osb_lock);
649 if (!ocfs2_la_state_enabled(osb) ||
650 (bits_wanted > osb->local_alloc_bits)) {
651 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800652 status = -ENOSPC;
653 goto bail;
654 }
Mark Fasheh9c7af402008-07-28 18:02:53 -0700655 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800656
657 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
658
Joel Beckere407e392008-06-12 22:35:39 -0700659#ifdef CONFIG_OCFS2_DEBUG_FS
Mark Fashehccd979b2005-12-15 14:31:24 -0800660 if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
661 ocfs2_local_alloc_count_bits(alloc)) {
Mark Fashehb06970532006-03-03 10:24:33 -0800662 ocfs2_error(osb->sb, "local alloc inode %llu says it has "
Mark Fashehccd979b2005-12-15 14:31:24 -0800663 "%u free bits, but a count shows %u",
Mark Fashehb06970532006-03-03 10:24:33 -0800664 (unsigned long long)le64_to_cpu(alloc->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -0800665 le32_to_cpu(alloc->id1.bitmap1.i_used),
666 ocfs2_local_alloc_count_bits(alloc));
667 status = -EIO;
668 goto bail;
669 }
Jan Kara5a58c3e2007-11-13 19:59:33 +0100670#endif
Mark Fashehccd979b2005-12-15 14:31:24 -0800671
672 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
673 le32_to_cpu(alloc->id1.bitmap1.i_used);
674 if (bits_wanted > free_bits) {
675 /* uhoh, window change time. */
676 status =
677 ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
678 if (status < 0) {
679 if (status != -ENOSPC)
680 mlog_errno(status);
681 goto bail;
682 }
Mark Fasheh9c7af402008-07-28 18:02:53 -0700683
684 /*
685 * Under certain conditions, the window slide code
686 * might have reduced the number of bits available or
687 * disabled the the local alloc entirely. Re-check
688 * here and return -ENOSPC if necessary.
689 */
690 status = -ENOSPC;
691 if (!ocfs2_la_state_enabled(osb))
692 goto bail;
693
694 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
695 le32_to_cpu(alloc->id1.bitmap1.i_used);
696 if (bits_wanted > free_bits)
697 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800698 }
699
Joel Becker1187c962008-09-03 20:03:39 -0700700 if (ac->ac_max_block)
701 mlog(0, "Calling in_range for max block %llu\n",
702 (unsigned long long)ac->ac_max_block);
703
704 if (!ocfs2_local_alloc_in_range(local_alloc_inode, ac,
705 bits_wanted)) {
706 /*
707 * The window is outside ac->ac_max_block.
708 * This errno tells the caller to keep localalloc enabled
709 * but to get the allocation from the main bitmap.
710 */
711 status = -EFBIG;
712 goto bail;
713 }
714
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700715 ac->ac_inode = local_alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800716 /* We should never use localalloc from another slot */
717 ac->ac_alloc_slot = osb->slot_num;
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700718 ac->ac_which = OCFS2_AC_USE_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800719 get_bh(osb->local_alloc_bh);
720 ac->ac_bh = osb->local_alloc_bh;
Mark Fashehccd979b2005-12-15 14:31:24 -0800721 status = 0;
722bail:
Sunil Mushranbda02332007-09-21 11:41:43 -0700723 if (status < 0 && local_alloc_inode) {
724 mutex_unlock(&local_alloc_inode->i_mutex);
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700725 iput(local_alloc_inode);
Sunil Mushranbda02332007-09-21 11:41:43 -0700726 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800727
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800728 mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num,
729 status);
730
Mark Fashehccd979b2005-12-15 14:31:24 -0800731 mlog_exit(status);
732 return status;
733}
734
735int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700736 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800737 struct ocfs2_alloc_context *ac,
Mark Fasheh415cb802007-09-16 20:10:16 -0700738 u32 bits_wanted,
Mark Fashehccd979b2005-12-15 14:31:24 -0800739 u32 *bit_off,
740 u32 *num_bits)
741{
742 int status, start;
743 struct inode *local_alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800744 void *bitmap;
745 struct ocfs2_dinode *alloc;
746 struct ocfs2_local_alloc *la;
747
748 mlog_entry_void();
749 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
750
Mark Fashehccd979b2005-12-15 14:31:24 -0800751 local_alloc_inode = ac->ac_inode;
752 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
753 la = OCFS2_LOCAL_ALLOC(alloc);
754
755 start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted);
756 if (start == -1) {
757 /* TODO: Shouldn't we just BUG here? */
758 status = -ENOSPC;
759 mlog_errno(status);
760 goto bail;
761 }
762
763 bitmap = la->la_bitmap;
764 *bit_off = le32_to_cpu(la->la_bm_off) + start;
765 /* local alloc is always contiguous by nature -- we never
766 * delete bits from it! */
767 *num_bits = bits_wanted;
768
769 status = ocfs2_journal_access(handle, local_alloc_inode,
770 osb->local_alloc_bh,
771 OCFS2_JOURNAL_ACCESS_WRITE);
772 if (status < 0) {
773 mlog_errno(status);
774 goto bail;
775 }
776
777 while(bits_wanted--)
778 ocfs2_set_bit(start++, bitmap);
779
Marcin Slusarz0dd32562008-02-13 00:06:18 +0100780 le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800781
782 status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
783 if (status < 0) {
784 mlog_errno(status);
785 goto bail;
786 }
787
788 status = 0;
789bail:
790 mlog_exit(status);
791 return status;
792}
793
794static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
795{
796 int i;
797 u8 *buffer;
798 u32 count = 0;
799 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
800
801 mlog_entry_void();
802
803 buffer = la->la_bitmap;
804 for (i = 0; i < le16_to_cpu(la->la_size); i++)
805 count += hweight8(buffer[i]);
806
807 mlog_exit(count);
808 return count;
809}
810
811static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
812 struct ocfs2_dinode *alloc,
813 u32 numbits)
814{
815 int numfound, bitoff, left, startoff, lastzero;
816 void *bitmap = NULL;
817
818 mlog_entry("(numbits wanted = %u)\n", numbits);
819
820 if (!alloc->id1.bitmap1.i_total) {
821 mlog(0, "No bits in my window!\n");
822 bitoff = -1;
823 goto bail;
824 }
825
826 bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
827
828 numfound = bitoff = startoff = 0;
829 lastzero = -1;
830 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
831 while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
832 if (bitoff == left) {
833 /* mlog(0, "bitoff (%d) == left", bitoff); */
834 break;
835 }
836 /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
837 "numfound = %d\n", bitoff, startoff, numfound);*/
838
839 /* Ok, we found a zero bit... is it contig. or do we
840 * start over?*/
841 if (bitoff == startoff) {
842 /* we found a zero */
843 numfound++;
844 startoff++;
845 } else {
846 /* got a zero after some ones */
847 numfound = 1;
848 startoff = bitoff+1;
849 }
850 /* we got everything we needed */
851 if (numfound == numbits) {
852 /* mlog(0, "Found it all!\n"); */
853 break;
854 }
855 }
856
857 mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff,
858 numfound);
859
860 if (numfound == numbits)
861 bitoff = startoff - numfound;
862 else
863 bitoff = -1;
864
865bail:
866 mlog_exit(bitoff);
867 return bitoff;
868}
869
870static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
871{
872 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
873 int i;
874 mlog_entry_void();
875
876 alloc->id1.bitmap1.i_total = 0;
877 alloc->id1.bitmap1.i_used = 0;
878 la->la_bm_off = 0;
879 for(i = 0; i < le16_to_cpu(la->la_size); i++)
880 la->la_bitmap[i] = 0;
881
882 mlog_exit_void();
883}
884
885#if 0
886/* turn this on and uncomment below to aid debugging window shifts. */
887static void ocfs2_verify_zero_bits(unsigned long *bitmap,
888 unsigned int start,
889 unsigned int count)
890{
891 unsigned int tmp = count;
892 while(tmp--) {
893 if (ocfs2_test_bit(start + tmp, bitmap)) {
894 printk("ocfs2_verify_zero_bits: start = %u, count = "
895 "%u\n", start, count);
896 printk("ocfs2_verify_zero_bits: bit %u is set!",
897 start + tmp);
898 BUG();
899 }
900 }
901}
902#endif
903
904/*
905 * sync the local alloc to main bitmap.
906 *
907 * assumes you've already locked the main bitmap -- the bitmap inode
908 * passed is used for caching.
909 */
910static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700911 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800912 struct ocfs2_dinode *alloc,
913 struct inode *main_bm_inode,
914 struct buffer_head *main_bm_bh)
915{
916 int status = 0;
917 int bit_off, left, count, start;
918 u64 la_start_blk;
919 u64 blkno;
920 void *bitmap;
921 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
922
Jan Kara5a58c3e2007-11-13 19:59:33 +0100923 mlog_entry("total = %u, used = %u\n",
Mark Fashehccd979b2005-12-15 14:31:24 -0800924 le32_to_cpu(alloc->id1.bitmap1.i_total),
Mark Fashehccd979b2005-12-15 14:31:24 -0800925 le32_to_cpu(alloc->id1.bitmap1.i_used));
926
927 if (!alloc->id1.bitmap1.i_total) {
928 mlog(0, "nothing to sync!\n");
929 goto bail;
930 }
931
932 if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
933 le32_to_cpu(alloc->id1.bitmap1.i_total)) {
934 mlog(0, "all bits were taken!\n");
935 goto bail;
936 }
937
938 la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
939 le32_to_cpu(la->la_bm_off));
940 bitmap = la->la_bitmap;
941 start = count = bit_off = 0;
942 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
943
944 while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
945 != -1) {
946 if ((bit_off < left) && (bit_off == start)) {
947 count++;
948 start++;
949 continue;
950 }
951 if (count) {
952 blkno = la_start_blk +
953 ocfs2_clusters_to_blocks(osb->sb,
954 start - count);
955
Mark Fashehb06970532006-03-03 10:24:33 -0800956 mlog(0, "freeing %u bits starting at local alloc bit "
957 "%u (la_start_blk = %llu, blkno = %llu)\n",
958 count, start - count,
959 (unsigned long long)la_start_blk,
960 (unsigned long long)blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800961
962 status = ocfs2_free_clusters(handle, main_bm_inode,
963 main_bm_bh, blkno, count);
964 if (status < 0) {
965 mlog_errno(status);
966 goto bail;
967 }
968 }
969 if (bit_off >= left)
970 break;
971 count = 1;
972 start = bit_off + 1;
973 }
974
975bail:
976 mlog_exit(status);
977 return status;
978}
979
Mark Fasheh9c7af402008-07-28 18:02:53 -0700980enum ocfs2_la_event {
981 OCFS2_LA_EVENT_SLIDE, /* Normal window slide. */
982 OCFS2_LA_EVENT_FRAGMENTED, /* The global bitmap has
983 * enough bits theoretically
984 * free, but a contiguous
985 * allocation could not be
986 * found. */
987 OCFS2_LA_EVENT_ENOSPC, /* Global bitmap doesn't have
988 * enough bits free to satisfy
989 * our request. */
990};
991#define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
992/*
993 * Given an event, calculate the size of our next local alloc window.
994 *
995 * This should always be called under i_mutex of the local alloc inode
996 * so that local alloc disabling doesn't race with processes trying to
997 * use the allocator.
998 *
999 * Returns the state which the local alloc was left in. This value can
1000 * be ignored by some paths.
1001 */
1002static int ocfs2_recalc_la_window(struct ocfs2_super *osb,
1003 enum ocfs2_la_event event)
1004{
1005 unsigned int bits;
1006 int state;
1007
1008 spin_lock(&osb->osb_lock);
1009 if (osb->local_alloc_state == OCFS2_LA_DISABLED) {
1010 WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED);
1011 goto out_unlock;
1012 }
1013
1014 /*
1015 * ENOSPC and fragmentation are treated similarly for now.
1016 */
1017 if (event == OCFS2_LA_EVENT_ENOSPC ||
1018 event == OCFS2_LA_EVENT_FRAGMENTED) {
1019 /*
1020 * We ran out of contiguous space in the primary
1021 * bitmap. Drastically reduce the number of bits used
1022 * by local alloc until we have to disable it.
1023 */
1024 bits = osb->local_alloc_bits >> 1;
1025 if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) {
1026 /*
1027 * By setting state to THROTTLED, we'll keep
1028 * the number of local alloc bits used down
1029 * until an event occurs which would give us
1030 * reason to assume the bitmap situation might
1031 * have changed.
1032 */
1033 osb->local_alloc_state = OCFS2_LA_THROTTLED;
1034 osb->local_alloc_bits = bits;
1035 } else {
1036 osb->local_alloc_state = OCFS2_LA_DISABLED;
1037 }
1038 queue_delayed_work(ocfs2_wq, &osb->la_enable_wq,
1039 OCFS2_LA_ENABLE_INTERVAL);
1040 goto out_unlock;
1041 }
1042
1043 /*
1044 * Don't increase the size of the local alloc window until we
1045 * know we might be able to fulfill the request. Otherwise, we
1046 * risk bouncing around the global bitmap during periods of
1047 * low space.
1048 */
1049 if (osb->local_alloc_state != OCFS2_LA_THROTTLED)
1050 osb->local_alloc_bits = osb->local_alloc_default_bits;
1051
1052out_unlock:
1053 state = osb->local_alloc_state;
1054 spin_unlock(&osb->osb_lock);
1055
1056 return state;
1057}
1058
Mark Fashehccd979b2005-12-15 14:31:24 -08001059static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001060 struct ocfs2_alloc_context **ac,
1061 struct inode **bitmap_inode,
1062 struct buffer_head **bitmap_bh)
1063{
1064 int status;
1065
Robert P. J. Daycd861282006-12-13 00:34:52 -08001066 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001067 if (!(*ac)) {
1068 status = -ENOMEM;
1069 mlog_errno(status);
1070 goto bail;
1071 }
1072
Mark Fasheh9c7af402008-07-28 18:02:53 -07001073retry_enospc:
Mark Fashehebcee4b2008-07-28 14:55:20 -07001074 (*ac)->ac_bits_wanted = osb->local_alloc_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08001075
1076 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
Mark Fasheh9c7af402008-07-28 18:02:53 -07001077 if (status == -ENOSPC) {
1078 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) ==
1079 OCFS2_LA_DISABLED)
1080 goto bail;
1081
1082 ocfs2_free_ac_resource(*ac);
1083 memset(*ac, 0, sizeof(struct ocfs2_alloc_context));
1084 goto retry_enospc;
1085 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001086 if (status < 0) {
Mark Fasheh9c7af402008-07-28 18:02:53 -07001087 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001088 goto bail;
1089 }
1090
1091 *bitmap_inode = (*ac)->ac_inode;
1092 igrab(*bitmap_inode);
1093 *bitmap_bh = (*ac)->ac_bh;
1094 get_bh(*bitmap_bh);
1095 status = 0;
1096bail:
1097 if ((status < 0) && *ac) {
1098 ocfs2_free_alloc_context(*ac);
1099 *ac = NULL;
1100 }
1101
1102 mlog_exit(status);
1103 return status;
1104}
1105
1106/*
1107 * pass it the bitmap lock in lock_bh if you have it.
1108 */
1109static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001110 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001111 struct ocfs2_alloc_context *ac)
1112{
1113 int status = 0;
1114 u32 cluster_off, cluster_count;
1115 struct ocfs2_dinode *alloc = NULL;
1116 struct ocfs2_local_alloc *la;
1117
1118 mlog_entry_void();
1119
1120 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
1121 la = OCFS2_LOCAL_ALLOC(alloc);
1122
1123 if (alloc->id1.bitmap1.i_total)
1124 mlog(0, "asking me to alloc a new window over a non-empty "
1125 "one\n");
1126
1127 mlog(0, "Allocating %u clusters for a new window.\n",
Mark Fashehebcee4b2008-07-28 14:55:20 -07001128 osb->local_alloc_bits);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001129
1130 /* Instruct the allocation code to try the most recently used
1131 * cluster group. We'll re-record the group used this pass
1132 * below. */
1133 ac->ac_last_group = osb->la_last_gd;
1134
Mark Fashehccd979b2005-12-15 14:31:24 -08001135 /* we used the generic suballoc reserve function, but we set
1136 * everything up nicely, so there's no reason why we can't use
1137 * the more specific cluster api to claim bits. */
Mark Fashehebcee4b2008-07-28 14:55:20 -07001138 status = ocfs2_claim_clusters(osb, handle, ac, osb->local_alloc_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -08001139 &cluster_off, &cluster_count);
Mark Fasheh9c7af402008-07-28 18:02:53 -07001140 if (status == -ENOSPC) {
1141retry_enospc:
1142 /*
1143 * Note: We could also try syncing the journal here to
1144 * allow use of any free bits which the current
1145 * transaction can't give us access to. --Mark
1146 */
1147 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) ==
1148 OCFS2_LA_DISABLED)
1149 goto bail;
1150
1151 status = ocfs2_claim_clusters(osb, handle, ac,
1152 osb->local_alloc_bits,
1153 &cluster_off,
1154 &cluster_count);
1155 if (status == -ENOSPC)
1156 goto retry_enospc;
1157 /*
1158 * We only shrunk the *minimum* number of in our
1159 * request - it's entirely possible that the allocator
1160 * might give us more than we asked for.
1161 */
1162 if (status == 0) {
1163 spin_lock(&osb->osb_lock);
1164 osb->local_alloc_bits = cluster_count;
1165 spin_unlock(&osb->osb_lock);
1166 }
1167 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001168 if (status < 0) {
1169 if (status != -ENOSPC)
1170 mlog_errno(status);
1171 goto bail;
1172 }
1173
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001174 osb->la_last_gd = ac->ac_last_group;
1175
Mark Fashehccd979b2005-12-15 14:31:24 -08001176 la->la_bm_off = cpu_to_le32(cluster_off);
1177 alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
1178 /* just in case... In the future when we find space ourselves,
1179 * we don't have to get all contiguous -- but we'll have to
1180 * set all previously used bits in bitmap and update
1181 * la_bits_set before setting the bits in the main bitmap. */
1182 alloc->id1.bitmap1.i_used = 0;
1183 memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
1184 le16_to_cpu(la->la_size));
1185
1186 mlog(0, "New window allocated:\n");
1187 mlog(0, "window la_bm_off = %u\n",
1188 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
1189 mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total));
1190
1191bail:
1192 mlog_exit(status);
1193 return status;
1194}
1195
1196/* Note that we do *NOT* lock the local alloc inode here as
1197 * it's been locked already for us. */
1198static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
1199 struct inode *local_alloc_inode)
1200{
1201 int status = 0;
1202 struct buffer_head *main_bm_bh = NULL;
1203 struct inode *main_bm_inode = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001204 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001205 struct ocfs2_dinode *alloc;
1206 struct ocfs2_dinode *alloc_copy = NULL;
1207 struct ocfs2_alloc_context *ac = NULL;
1208
1209 mlog_entry_void();
1210
Mark Fasheh9c7af402008-07-28 18:02:53 -07001211 ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
1212
Mark Fashehccd979b2005-12-15 14:31:24 -08001213 /* This will lock the main bitmap for us. */
1214 status = ocfs2_local_alloc_reserve_for_window(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001215 &ac,
1216 &main_bm_inode,
1217 &main_bm_bh);
1218 if (status < 0) {
1219 if (status != -ENOSPC)
1220 mlog_errno(status);
1221 goto bail;
1222 }
1223
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001224 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001225 if (IS_ERR(handle)) {
1226 status = PTR_ERR(handle);
1227 handle = NULL;
1228 mlog_errno(status);
1229 goto bail;
1230 }
1231
1232 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
1233
1234 /* We want to clear the local alloc before doing anything
1235 * else, so that if we error later during this operation,
1236 * local alloc shutdown won't try to double free main bitmap
1237 * bits. Make a copy so the sync function knows which bits to
1238 * free. */
Sunil Mushran4ba1c5b2008-04-18 15:03:59 -07001239 alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001240 if (!alloc_copy) {
1241 status = -ENOMEM;
1242 mlog_errno(status);
1243 goto bail;
1244 }
1245 memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
1246
1247 status = ocfs2_journal_access(handle, local_alloc_inode,
1248 osb->local_alloc_bh,
1249 OCFS2_JOURNAL_ACCESS_WRITE);
1250 if (status < 0) {
1251 mlog_errno(status);
1252 goto bail;
1253 }
1254
1255 ocfs2_clear_local_alloc(alloc);
1256
1257 status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
1258 if (status < 0) {
1259 mlog_errno(status);
1260 goto bail;
1261 }
1262
1263 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
1264 main_bm_inode, main_bm_bh);
1265 if (status < 0) {
1266 mlog_errno(status);
1267 goto bail;
1268 }
1269
1270 status = ocfs2_local_alloc_new_window(osb, handle, ac);
1271 if (status < 0) {
1272 if (status != -ENOSPC)
1273 mlog_errno(status);
1274 goto bail;
1275 }
1276
1277 atomic_inc(&osb->alloc_stats.moves);
1278
1279 status = 0;
1280bail:
1281 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001282 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001283
1284 if (main_bm_bh)
1285 brelse(main_bm_bh);
1286
1287 if (main_bm_inode)
1288 iput(main_bm_inode);
1289
1290 if (alloc_copy)
1291 kfree(alloc_copy);
1292
1293 if (ac)
1294 ocfs2_free_alloc_context(ac);
1295
1296 mlog_exit(status);
1297 return status;
1298}
1299