blob: 02227c392510e9d52cb13a86ba18add9cec8459a [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
79DEFINE_MUTEX(la_debug_mutex);
80
81static 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
89static 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
112static const struct file_operations ocfs2_la_debug_fops = {
113 .open = ocfs2_la_debug_open,
114 .read = ocfs2_la_debug_read,
115};
116
117static 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
134static 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 */
146static void ocfs2_init_la_debug(struct ocfs2_super *osb)
147{
148 return;
149}
150static void ocfs2_shutdown_la_debug(struct ocfs2_super *osb)
151{
152 return;
153}
154#endif
155
Mark Fasheh9c7af402008-07-28 18:02:53 -0700156static 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
162void 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
175void 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
Mark Fashehccd979b2005-12-15 14:31:24 -0800185/*
186 * Tell us whether a given allocation should use the local alloc
187 * file. Otherwise, it has to go to the main bitmap.
Mark Fasheh9c7af402008-07-28 18:02:53 -0700188 *
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.
Mark Fashehccd979b2005-12-15 14:31:24 -0800191 */
192int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
193{
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800194 int ret = 0;
Mark Fasheh9c7af402008-07-28 18:02:53 -0700195 int la_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800196
Mark Fasheh9c7af402008-07-28 18:02:53 -0700197 spin_lock(&osb->osb_lock);
198 la_bits = osb->local_alloc_bits;
199
200 if (!ocfs2_la_state_enabled(osb))
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800201 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800202
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))
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800208 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800209
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800210 ret = 1;
211bail:
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);
Mark Fasheh9c7af402008-07-28 18:02:53 -0700214 spin_unlock(&osb->osb_lock);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800215 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -0800216}
217
218int 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
Mark Fasheh9a8ff572008-07-29 18:29:18 -0700229 ocfs2_init_la_debug(osb);
230
Mark Fashehebcee4b2008-07-28 14:55:20 -0700231 if (osb->local_alloc_bits == 0)
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800232 goto bail;
233
Mark Fashehebcee4b2008-07-28 14:55:20 -0700234 if (osb->local_alloc_bits >= osb->bitmap_cpg) {
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800235 mlog(ML_NOTICE, "Requested local alloc window %d is larger "
236 "than max possible %u. Using defaults.\n",
Mark Fashehebcee4b2008-07-28 14:55:20 -0700237 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);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800241 }
242
Mark Fashehccd979b2005-12-15 14:31:24 -0800243 /* 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))) {
Mark Fashehb0697052006-03-03 10:24:33 -0800264 mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
265 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800266 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
296bail:
297 if (status < 0)
298 if (alloc_bh)
299 brelse(alloc_bh);
300 if (inode)
301 iput(inode);
302
Mark Fasheh9a8ff572008-07-29 18:29:18 -0700303 if (status < 0)
304 ocfs2_shutdown_la_debug(osb);
305
Mark Fashehebcee4b2008-07-28 14:55:20 -0700306 mlog(0, "Local alloc window bits = %d\n", osb->local_alloc_bits);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800307
Mark Fashehccd979b2005-12-15 14:31:24 -0800308 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.*/
319void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
320{
321 int status;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700322 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800323 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
Mark Fasheh9c7af402008-07-28 18:02:53 -0700332 cancel_delayed_work(&osb->la_enable_wq);
333 flush_workqueue(ocfs2_wq);
334
Mark Fasheh9a8ff572008-07-29 18:29:18 -0700335 ocfs2_shutdown_la_debug(osb);
336
Mark Fashehccd979b2005-12-15 14:31:24 -0800337 if (osb->local_alloc_state == OCFS2_LA_UNUSED)
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700338 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800339
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);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700347 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800348 }
349
350 osb->local_alloc_state = OCFS2_LA_DISABLED;
351
Mark Fashehccd979b2005-12-15 14:31:24 -0800352 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);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700358 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800359 }
360
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700361 mutex_lock(&main_bm_inode->i_mutex);
362
Mark Fashehe63aecb62007-10-18 15:30:42 -0700363 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800364 if (status < 0) {
365 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700366 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -0800367 }
368
369 /* WINDOW_MOVE_CREDITS is a bit heavy... */
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700370 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800371 if (IS_ERR(handle)) {
372 mlog_errno(PTR_ERR(handle));
373 handle = NULL;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700374 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800375 }
376
377 bh = osb->local_alloc_bh;
378 alloc = (struct ocfs2_dinode *) bh->b_data;
379
Sunil Mushran4ba1c5b2008-04-18 15:03:59 -0700380 alloc_copy = kmalloc(bh->b_size, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800381 if (!alloc_copy) {
382 status = -ENOMEM;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700383 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800384 }
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);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700391 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800392 }
393
394 ocfs2_clear_local_alloc(alloc);
395
396 status = ocfs2_journal_dirty(handle, bh);
397 if (status < 0) {
398 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700399 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800400 }
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
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700411out_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700412 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800413
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700414out_unlock:
Mark Fashehccd979b2005-12-15 14:31:24 -0800415 if (main_bm_bh)
416 brelse(main_bm_bh);
417
Mark Fashehe63aecb62007-10-18 15:30:42 -0700418 ocfs2_inode_unlock(main_bm_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800419
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700420out_mutex:
421 mutex_unlock(&main_bm_inode->i_mutex);
422 iput(main_bm_inode);
423
424out:
Mark Fashehccd979b2005-12-15 14:31:24 -0800425 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 */
441int 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
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800463 mutex_lock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800464
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
486bail:
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) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800496 mutex_unlock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800497 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 */
510int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
511 struct ocfs2_dinode *alloc)
512{
513 int status;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700514 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800515 struct buffer_head *main_bm_bh = NULL;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700516 struct inode *main_bm_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800517
518 mlog_entry_void();
519
Mark Fashehccd979b2005-12-15 14:31:24 -0800520 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);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700526 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800527 }
528
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700529 mutex_lock(&main_bm_inode->i_mutex);
530
Mark Fashehe63aecb62007-10-18 15:30:42 -0700531 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800532 if (status < 0) {
533 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700534 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -0800535 }
536
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700537 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800538 if (IS_ERR(handle)) {
539 status = PTR_ERR(handle);
540 handle = NULL;
541 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700542 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800543 }
544
545 /* we want the bitmap change to be recorded on disk asap */
Mark Fasheh1fabe142006-10-09 18:11:45 -0700546 handle->h_sync = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800547
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
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700553 ocfs2_commit_trans(osb, handle);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700554
555out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700556 ocfs2_inode_unlock(main_bm_inode, 1);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700557
558out_mutex:
559 mutex_unlock(&main_bm_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800560
561 if (main_bm_bh)
562 brelse(main_bm_bh);
563
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700564 iput(main_bm_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800565
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700566out:
Tao Ma4d0ddb22008-03-05 16:11:46 +0800567 if (!status)
568 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800569 mlog_exit(status);
570 return status;
571}
572
Joel Becker1187c962008-09-03 20:03:39 -0700573/* Check to see if the local alloc window is within ac->ac_max_block */
574static int ocfs2_local_alloc_in_range(struct inode *inode,
575 struct ocfs2_alloc_context *ac,
576 u32 bits_wanted)
577{
578 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
579 struct ocfs2_dinode *alloc;
580 struct ocfs2_local_alloc *la;
581 int start;
582 u64 block_off;
583
584 if (!ac->ac_max_block)
585 return 1;
586
587 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
588 la = OCFS2_LOCAL_ALLOC(alloc);
589
590 start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted);
591 if (start == -1) {
592 mlog_errno(-ENOSPC);
593 return 0;
594 }
595
596 /*
597 * Converting (bm_off + start + bits_wanted) to blocks gives us
598 * the blkno just past our actual allocation. This is perfect
599 * to compare with ac_max_block.
600 */
601 block_off = ocfs2_clusters_to_blocks(inode->i_sb,
602 le32_to_cpu(la->la_bm_off) +
603 start + bits_wanted);
604 mlog(0, "Checking %llu against %llu\n",
605 (unsigned long long)block_off,
606 (unsigned long long)ac->ac_max_block);
607 if (block_off > ac->ac_max_block)
608 return 0;
609
610 return 1;
611}
612
Mark Fashehccd979b2005-12-15 14:31:24 -0800613/*
Mark Fasheh9c7af402008-07-28 18:02:53 -0700614 * make sure we've got at least bits_wanted contiguous bits in the
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800615 * local alloc. You lose them when you drop i_mutex.
Mark Fashehccd979b2005-12-15 14:31:24 -0800616 *
617 * We will add ourselves to the transaction passed in, but may start
618 * our own in order to shift windows.
619 */
620int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800621 u32 bits_wanted,
622 struct ocfs2_alloc_context *ac)
623{
624 int status;
625 struct ocfs2_dinode *alloc;
626 struct inode *local_alloc_inode;
627 unsigned int free_bits;
628
629 mlog_entry_void();
630
Mark Fashehccd979b2005-12-15 14:31:24 -0800631 BUG_ON(!ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800632
633 local_alloc_inode =
634 ocfs2_get_system_file_inode(osb,
635 LOCAL_ALLOC_SYSTEM_INODE,
636 osb->slot_num);
637 if (!local_alloc_inode) {
638 status = -ENOENT;
639 mlog_errno(status);
640 goto bail;
641 }
Mark Fashehda5cbf22006-10-06 18:34:35 -0700642
643 mutex_lock(&local_alloc_inode->i_mutex);
644
Mark Fasheh9c7af402008-07-28 18:02:53 -0700645 /*
646 * We must double check state and allocator bits because
647 * another process may have changed them while holding i_mutex.
648 */
649 spin_lock(&osb->osb_lock);
650 if (!ocfs2_la_state_enabled(osb) ||
651 (bits_wanted > osb->local_alloc_bits)) {
652 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800653 status = -ENOSPC;
654 goto bail;
655 }
Mark Fasheh9c7af402008-07-28 18:02:53 -0700656 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800657
658 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
659
Joel Beckere407e392008-06-12 22:35:39 -0700660#ifdef CONFIG_OCFS2_DEBUG_FS
Mark Fashehccd979b2005-12-15 14:31:24 -0800661 if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
662 ocfs2_local_alloc_count_bits(alloc)) {
Mark Fashehb0697052006-03-03 10:24:33 -0800663 ocfs2_error(osb->sb, "local alloc inode %llu says it has "
Mark Fashehccd979b2005-12-15 14:31:24 -0800664 "%u free bits, but a count shows %u",
Mark Fashehb0697052006-03-03 10:24:33 -0800665 (unsigned long long)le64_to_cpu(alloc->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -0800666 le32_to_cpu(alloc->id1.bitmap1.i_used),
667 ocfs2_local_alloc_count_bits(alloc));
668 status = -EIO;
669 goto bail;
670 }
Jan Kara5a58c3e2007-11-13 19:59:33 +0100671#endif
Mark Fashehccd979b2005-12-15 14:31:24 -0800672
673 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
674 le32_to_cpu(alloc->id1.bitmap1.i_used);
675 if (bits_wanted > free_bits) {
676 /* uhoh, window change time. */
677 status =
678 ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
679 if (status < 0) {
680 if (status != -ENOSPC)
681 mlog_errno(status);
682 goto bail;
683 }
Mark Fasheh9c7af402008-07-28 18:02:53 -0700684
685 /*
686 * Under certain conditions, the window slide code
687 * might have reduced the number of bits available or
688 * disabled the the local alloc entirely. Re-check
689 * here and return -ENOSPC if necessary.
690 */
691 status = -ENOSPC;
692 if (!ocfs2_la_state_enabled(osb))
693 goto bail;
694
695 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
696 le32_to_cpu(alloc->id1.bitmap1.i_used);
697 if (bits_wanted > free_bits)
698 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800699 }
700
Joel Becker1187c962008-09-03 20:03:39 -0700701 if (ac->ac_max_block)
702 mlog(0, "Calling in_range for max block %llu\n",
703 (unsigned long long)ac->ac_max_block);
704
705 if (!ocfs2_local_alloc_in_range(local_alloc_inode, ac,
706 bits_wanted)) {
707 /*
708 * The window is outside ac->ac_max_block.
709 * This errno tells the caller to keep localalloc enabled
710 * but to get the allocation from the main bitmap.
711 */
712 status = -EFBIG;
713 goto bail;
714 }
715
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700716 ac->ac_inode = local_alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800717 /* We should never use localalloc from another slot */
718 ac->ac_alloc_slot = osb->slot_num;
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700719 ac->ac_which = OCFS2_AC_USE_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800720 get_bh(osb->local_alloc_bh);
721 ac->ac_bh = osb->local_alloc_bh;
Mark Fashehccd979b2005-12-15 14:31:24 -0800722 status = 0;
723bail:
Sunil Mushranbda02332007-09-21 11:41:43 -0700724 if (status < 0 && local_alloc_inode) {
725 mutex_unlock(&local_alloc_inode->i_mutex);
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700726 iput(local_alloc_inode);
Sunil Mushranbda02332007-09-21 11:41:43 -0700727 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800728
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800729 mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num,
730 status);
731
Mark Fashehccd979b2005-12-15 14:31:24 -0800732 mlog_exit(status);
733 return status;
734}
735
736int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700737 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800738 struct ocfs2_alloc_context *ac,
Mark Fasheh415cb802007-09-16 20:10:16 -0700739 u32 bits_wanted,
Mark Fashehccd979b2005-12-15 14:31:24 -0800740 u32 *bit_off,
741 u32 *num_bits)
742{
743 int status, start;
744 struct inode *local_alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800745 void *bitmap;
746 struct ocfs2_dinode *alloc;
747 struct ocfs2_local_alloc *la;
748
749 mlog_entry_void();
750 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
751
Mark Fashehccd979b2005-12-15 14:31:24 -0800752 local_alloc_inode = ac->ac_inode;
753 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
754 la = OCFS2_LOCAL_ALLOC(alloc);
755
756 start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted);
757 if (start == -1) {
758 /* TODO: Shouldn't we just BUG here? */
759 status = -ENOSPC;
760 mlog_errno(status);
761 goto bail;
762 }
763
764 bitmap = la->la_bitmap;
765 *bit_off = le32_to_cpu(la->la_bm_off) + start;
766 /* local alloc is always contiguous by nature -- we never
767 * delete bits from it! */
768 *num_bits = bits_wanted;
769
770 status = ocfs2_journal_access(handle, local_alloc_inode,
771 osb->local_alloc_bh,
772 OCFS2_JOURNAL_ACCESS_WRITE);
773 if (status < 0) {
774 mlog_errno(status);
775 goto bail;
776 }
777
778 while(bits_wanted--)
779 ocfs2_set_bit(start++, bitmap);
780
Marcin Slusarz0dd32562008-02-13 00:06:18 +0100781 le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800782
783 status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
784 if (status < 0) {
785 mlog_errno(status);
786 goto bail;
787 }
788
789 status = 0;
790bail:
791 mlog_exit(status);
792 return status;
793}
794
795static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
796{
797 int i;
798 u8 *buffer;
799 u32 count = 0;
800 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
801
802 mlog_entry_void();
803
804 buffer = la->la_bitmap;
805 for (i = 0; i < le16_to_cpu(la->la_size); i++)
806 count += hweight8(buffer[i]);
807
808 mlog_exit(count);
809 return count;
810}
811
812static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
813 struct ocfs2_dinode *alloc,
814 u32 numbits)
815{
816 int numfound, bitoff, left, startoff, lastzero;
817 void *bitmap = NULL;
818
819 mlog_entry("(numbits wanted = %u)\n", numbits);
820
821 if (!alloc->id1.bitmap1.i_total) {
822 mlog(0, "No bits in my window!\n");
823 bitoff = -1;
824 goto bail;
825 }
826
827 bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
828
829 numfound = bitoff = startoff = 0;
830 lastzero = -1;
831 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
832 while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
833 if (bitoff == left) {
834 /* mlog(0, "bitoff (%d) == left", bitoff); */
835 break;
836 }
837 /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
838 "numfound = %d\n", bitoff, startoff, numfound);*/
839
840 /* Ok, we found a zero bit... is it contig. or do we
841 * start over?*/
842 if (bitoff == startoff) {
843 /* we found a zero */
844 numfound++;
845 startoff++;
846 } else {
847 /* got a zero after some ones */
848 numfound = 1;
849 startoff = bitoff+1;
850 }
851 /* we got everything we needed */
852 if (numfound == numbits) {
853 /* mlog(0, "Found it all!\n"); */
854 break;
855 }
856 }
857
858 mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff,
859 numfound);
860
861 if (numfound == numbits)
862 bitoff = startoff - numfound;
863 else
864 bitoff = -1;
865
866bail:
867 mlog_exit(bitoff);
868 return bitoff;
869}
870
871static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
872{
873 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
874 int i;
875 mlog_entry_void();
876
877 alloc->id1.bitmap1.i_total = 0;
878 alloc->id1.bitmap1.i_used = 0;
879 la->la_bm_off = 0;
880 for(i = 0; i < le16_to_cpu(la->la_size); i++)
881 la->la_bitmap[i] = 0;
882
883 mlog_exit_void();
884}
885
886#if 0
887/* turn this on and uncomment below to aid debugging window shifts. */
888static void ocfs2_verify_zero_bits(unsigned long *bitmap,
889 unsigned int start,
890 unsigned int count)
891{
892 unsigned int tmp = count;
893 while(tmp--) {
894 if (ocfs2_test_bit(start + tmp, bitmap)) {
895 printk("ocfs2_verify_zero_bits: start = %u, count = "
896 "%u\n", start, count);
897 printk("ocfs2_verify_zero_bits: bit %u is set!",
898 start + tmp);
899 BUG();
900 }
901 }
902}
903#endif
904
905/*
906 * sync the local alloc to main bitmap.
907 *
908 * assumes you've already locked the main bitmap -- the bitmap inode
909 * passed is used for caching.
910 */
911static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700912 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800913 struct ocfs2_dinode *alloc,
914 struct inode *main_bm_inode,
915 struct buffer_head *main_bm_bh)
916{
917 int status = 0;
918 int bit_off, left, count, start;
919 u64 la_start_blk;
920 u64 blkno;
921 void *bitmap;
922 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
923
Jan Kara5a58c3e2007-11-13 19:59:33 +0100924 mlog_entry("total = %u, used = %u\n",
Mark Fashehccd979b2005-12-15 14:31:24 -0800925 le32_to_cpu(alloc->id1.bitmap1.i_total),
Mark Fashehccd979b2005-12-15 14:31:24 -0800926 le32_to_cpu(alloc->id1.bitmap1.i_used));
927
928 if (!alloc->id1.bitmap1.i_total) {
929 mlog(0, "nothing to sync!\n");
930 goto bail;
931 }
932
933 if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
934 le32_to_cpu(alloc->id1.bitmap1.i_total)) {
935 mlog(0, "all bits were taken!\n");
936 goto bail;
937 }
938
939 la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
940 le32_to_cpu(la->la_bm_off));
941 bitmap = la->la_bitmap;
942 start = count = bit_off = 0;
943 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
944
945 while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
946 != -1) {
947 if ((bit_off < left) && (bit_off == start)) {
948 count++;
949 start++;
950 continue;
951 }
952 if (count) {
953 blkno = la_start_blk +
954 ocfs2_clusters_to_blocks(osb->sb,
955 start - count);
956
Mark Fashehb0697052006-03-03 10:24:33 -0800957 mlog(0, "freeing %u bits starting at local alloc bit "
958 "%u (la_start_blk = %llu, blkno = %llu)\n",
959 count, start - count,
960 (unsigned long long)la_start_blk,
961 (unsigned long long)blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800962
963 status = ocfs2_free_clusters(handle, main_bm_inode,
964 main_bm_bh, blkno, count);
965 if (status < 0) {
966 mlog_errno(status);
967 goto bail;
968 }
969 }
970 if (bit_off >= left)
971 break;
972 count = 1;
973 start = bit_off + 1;
974 }
975
976bail:
977 mlog_exit(status);
978 return status;
979}
980
Mark Fasheh9c7af402008-07-28 18:02:53 -0700981enum ocfs2_la_event {
982 OCFS2_LA_EVENT_SLIDE, /* Normal window slide. */
983 OCFS2_LA_EVENT_FRAGMENTED, /* The global bitmap has
984 * enough bits theoretically
985 * free, but a contiguous
986 * allocation could not be
987 * found. */
988 OCFS2_LA_EVENT_ENOSPC, /* Global bitmap doesn't have
989 * enough bits free to satisfy
990 * our request. */
991};
992#define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
993/*
994 * Given an event, calculate the size of our next local alloc window.
995 *
996 * This should always be called under i_mutex of the local alloc inode
997 * so that local alloc disabling doesn't race with processes trying to
998 * use the allocator.
999 *
1000 * Returns the state which the local alloc was left in. This value can
1001 * be ignored by some paths.
1002 */
1003static int ocfs2_recalc_la_window(struct ocfs2_super *osb,
1004 enum ocfs2_la_event event)
1005{
1006 unsigned int bits;
1007 int state;
1008
1009 spin_lock(&osb->osb_lock);
1010 if (osb->local_alloc_state == OCFS2_LA_DISABLED) {
1011 WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED);
1012 goto out_unlock;
1013 }
1014
1015 /*
1016 * ENOSPC and fragmentation are treated similarly for now.
1017 */
1018 if (event == OCFS2_LA_EVENT_ENOSPC ||
1019 event == OCFS2_LA_EVENT_FRAGMENTED) {
1020 /*
1021 * We ran out of contiguous space in the primary
1022 * bitmap. Drastically reduce the number of bits used
1023 * by local alloc until we have to disable it.
1024 */
1025 bits = osb->local_alloc_bits >> 1;
1026 if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) {
1027 /*
1028 * By setting state to THROTTLED, we'll keep
1029 * the number of local alloc bits used down
1030 * until an event occurs which would give us
1031 * reason to assume the bitmap situation might
1032 * have changed.
1033 */
1034 osb->local_alloc_state = OCFS2_LA_THROTTLED;
1035 osb->local_alloc_bits = bits;
1036 } else {
1037 osb->local_alloc_state = OCFS2_LA_DISABLED;
1038 }
1039 queue_delayed_work(ocfs2_wq, &osb->la_enable_wq,
1040 OCFS2_LA_ENABLE_INTERVAL);
1041 goto out_unlock;
1042 }
1043
1044 /*
1045 * Don't increase the size of the local alloc window until we
1046 * know we might be able to fulfill the request. Otherwise, we
1047 * risk bouncing around the global bitmap during periods of
1048 * low space.
1049 */
1050 if (osb->local_alloc_state != OCFS2_LA_THROTTLED)
1051 osb->local_alloc_bits = osb->local_alloc_default_bits;
1052
1053out_unlock:
1054 state = osb->local_alloc_state;
1055 spin_unlock(&osb->osb_lock);
1056
1057 return state;
1058}
1059
Mark Fashehccd979b2005-12-15 14:31:24 -08001060static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001061 struct ocfs2_alloc_context **ac,
1062 struct inode **bitmap_inode,
1063 struct buffer_head **bitmap_bh)
1064{
1065 int status;
1066
Robert P. J. Daycd861282006-12-13 00:34:52 -08001067 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001068 if (!(*ac)) {
1069 status = -ENOMEM;
1070 mlog_errno(status);
1071 goto bail;
1072 }
1073
Mark Fasheh9c7af402008-07-28 18:02:53 -07001074retry_enospc:
Mark Fashehebcee4b2008-07-28 14:55:20 -07001075 (*ac)->ac_bits_wanted = osb->local_alloc_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08001076
1077 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
Mark Fasheh9c7af402008-07-28 18:02:53 -07001078 if (status == -ENOSPC) {
1079 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) ==
1080 OCFS2_LA_DISABLED)
1081 goto bail;
1082
1083 ocfs2_free_ac_resource(*ac);
1084 memset(*ac, 0, sizeof(struct ocfs2_alloc_context));
1085 goto retry_enospc;
1086 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001087 if (status < 0) {
Mark Fasheh9c7af402008-07-28 18:02:53 -07001088 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001089 goto bail;
1090 }
1091
1092 *bitmap_inode = (*ac)->ac_inode;
1093 igrab(*bitmap_inode);
1094 *bitmap_bh = (*ac)->ac_bh;
1095 get_bh(*bitmap_bh);
1096 status = 0;
1097bail:
1098 if ((status < 0) && *ac) {
1099 ocfs2_free_alloc_context(*ac);
1100 *ac = NULL;
1101 }
1102
1103 mlog_exit(status);
1104 return status;
1105}
1106
1107/*
1108 * pass it the bitmap lock in lock_bh if you have it.
1109 */
1110static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001111 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001112 struct ocfs2_alloc_context *ac)
1113{
1114 int status = 0;
1115 u32 cluster_off, cluster_count;
1116 struct ocfs2_dinode *alloc = NULL;
1117 struct ocfs2_local_alloc *la;
1118
1119 mlog_entry_void();
1120
1121 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
1122 la = OCFS2_LOCAL_ALLOC(alloc);
1123
1124 if (alloc->id1.bitmap1.i_total)
1125 mlog(0, "asking me to alloc a new window over a non-empty "
1126 "one\n");
1127
1128 mlog(0, "Allocating %u clusters for a new window.\n",
Mark Fashehebcee4b2008-07-28 14:55:20 -07001129 osb->local_alloc_bits);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001130
1131 /* Instruct the allocation code to try the most recently used
1132 * cluster group. We'll re-record the group used this pass
1133 * below. */
1134 ac->ac_last_group = osb->la_last_gd;
1135
Mark Fashehccd979b2005-12-15 14:31:24 -08001136 /* we used the generic suballoc reserve function, but we set
1137 * everything up nicely, so there's no reason why we can't use
1138 * the more specific cluster api to claim bits. */
Mark Fashehebcee4b2008-07-28 14:55:20 -07001139 status = ocfs2_claim_clusters(osb, handle, ac, osb->local_alloc_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -08001140 &cluster_off, &cluster_count);
Mark Fasheh9c7af402008-07-28 18:02:53 -07001141 if (status == -ENOSPC) {
1142retry_enospc:
1143 /*
1144 * Note: We could also try syncing the journal here to
1145 * allow use of any free bits which the current
1146 * transaction can't give us access to. --Mark
1147 */
1148 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) ==
1149 OCFS2_LA_DISABLED)
1150 goto bail;
1151
1152 status = ocfs2_claim_clusters(osb, handle, ac,
1153 osb->local_alloc_bits,
1154 &cluster_off,
1155 &cluster_count);
1156 if (status == -ENOSPC)
1157 goto retry_enospc;
1158 /*
1159 * We only shrunk the *minimum* number of in our
1160 * request - it's entirely possible that the allocator
1161 * might give us more than we asked for.
1162 */
1163 if (status == 0) {
1164 spin_lock(&osb->osb_lock);
1165 osb->local_alloc_bits = cluster_count;
1166 spin_unlock(&osb->osb_lock);
1167 }
1168 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001169 if (status < 0) {
1170 if (status != -ENOSPC)
1171 mlog_errno(status);
1172 goto bail;
1173 }
1174
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001175 osb->la_last_gd = ac->ac_last_group;
1176
Mark Fashehccd979b2005-12-15 14:31:24 -08001177 la->la_bm_off = cpu_to_le32(cluster_off);
1178 alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
1179 /* just in case... In the future when we find space ourselves,
1180 * we don't have to get all contiguous -- but we'll have to
1181 * set all previously used bits in bitmap and update
1182 * la_bits_set before setting the bits in the main bitmap. */
1183 alloc->id1.bitmap1.i_used = 0;
1184 memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
1185 le16_to_cpu(la->la_size));
1186
1187 mlog(0, "New window allocated:\n");
1188 mlog(0, "window la_bm_off = %u\n",
1189 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
1190 mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total));
1191
1192bail:
1193 mlog_exit(status);
1194 return status;
1195}
1196
1197/* Note that we do *NOT* lock the local alloc inode here as
1198 * it's been locked already for us. */
1199static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
1200 struct inode *local_alloc_inode)
1201{
1202 int status = 0;
1203 struct buffer_head *main_bm_bh = NULL;
1204 struct inode *main_bm_inode = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001205 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001206 struct ocfs2_dinode *alloc;
1207 struct ocfs2_dinode *alloc_copy = NULL;
1208 struct ocfs2_alloc_context *ac = NULL;
1209
1210 mlog_entry_void();
1211
Mark Fasheh9c7af402008-07-28 18:02:53 -07001212 ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
1213
Mark Fashehccd979b2005-12-15 14:31:24 -08001214 /* This will lock the main bitmap for us. */
1215 status = ocfs2_local_alloc_reserve_for_window(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001216 &ac,
1217 &main_bm_inode,
1218 &main_bm_bh);
1219 if (status < 0) {
1220 if (status != -ENOSPC)
1221 mlog_errno(status);
1222 goto bail;
1223 }
1224
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001225 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001226 if (IS_ERR(handle)) {
1227 status = PTR_ERR(handle);
1228 handle = NULL;
1229 mlog_errno(status);
1230 goto bail;
1231 }
1232
1233 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
1234
1235 /* We want to clear the local alloc before doing anything
1236 * else, so that if we error later during this operation,
1237 * local alloc shutdown won't try to double free main bitmap
1238 * bits. Make a copy so the sync function knows which bits to
1239 * free. */
Sunil Mushran4ba1c5b2008-04-18 15:03:59 -07001240 alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001241 if (!alloc_copy) {
1242 status = -ENOMEM;
1243 mlog_errno(status);
1244 goto bail;
1245 }
1246 memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
1247
1248 status = ocfs2_journal_access(handle, local_alloc_inode,
1249 osb->local_alloc_bh,
1250 OCFS2_JOURNAL_ACCESS_WRITE);
1251 if (status < 0) {
1252 mlog_errno(status);
1253 goto bail;
1254 }
1255
1256 ocfs2_clear_local_alloc(alloc);
1257
1258 status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
1259 if (status < 0) {
1260 mlog_errno(status);
1261 goto bail;
1262 }
1263
1264 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
1265 main_bm_inode, main_bm_bh);
1266 if (status < 0) {
1267 mlog_errno(status);
1268 goto bail;
1269 }
1270
1271 status = ocfs2_local_alloc_new_window(osb, handle, ac);
1272 if (status < 0) {
1273 if (status != -ENOSPC)
1274 mlog_errno(status);
1275 goto bail;
1276 }
1277
1278 atomic_inc(&osb->alloc_stats.moves);
1279
1280 status = 0;
1281bail:
1282 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001283 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001284
1285 if (main_bm_bh)
1286 brelse(main_bm_bh);
1287
1288 if (main_bm_inode)
1289 iput(main_bm_inode);
1290
1291 if (alloc_copy)
1292 kfree(alloc_copy);
1293
1294 if (ac)
1295 ocfs2_free_alloc_context(ac);
1296
1297 mlog_exit(status);
1298 return status;
1299}
1300