blob: be774bdc8b3658bdec41332f49a3d8d1e4df5a58 [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>
31
32#define MLOG_MASK_PREFIX ML_DISK_ALLOC
33#include <cluster/masklog.h>
34
35#include "ocfs2.h"
36
37#include "alloc.h"
38#include "dlmglue.h"
39#include "inode.h"
40#include "journal.h"
41#include "localalloc.h"
42#include "suballoc.h"
43#include "super.h"
44#include "sysfile.h"
45
46#include "buffer_head_io.h"
47
48#define OCFS2_LOCAL_ALLOC(dinode) (&((dinode)->id2.i_lab))
49
50static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb);
51
52static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc);
53
54static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
55 struct ocfs2_dinode *alloc,
56 u32 numbits);
57
58static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc);
59
60static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -070061 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080062 struct ocfs2_dinode *alloc,
63 struct inode *main_bm_inode,
64 struct buffer_head *main_bm_bh);
65
66static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -080067 struct ocfs2_alloc_context **ac,
68 struct inode **bitmap_inode,
69 struct buffer_head **bitmap_bh);
70
71static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -070072 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080073 struct ocfs2_alloc_context *ac);
74
75static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
76 struct inode *local_alloc_inode);
77
Mark Fashehccd979b2005-12-15 14:31:24 -080078static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb)
79{
Sunil Mushran2fbe8d12007-12-20 14:58:11 -080080 BUG_ON(osb->s_clustersize_bits > 20);
Mark Fashehccd979b2005-12-15 14:31:24 -080081
Sunil Mushran2fbe8d12007-12-20 14:58:11 -080082 /* Size local alloc windows by the megabyte */
83 return osb->local_alloc_size << (20 - osb->s_clustersize_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -080084}
85
86/*
87 * Tell us whether a given allocation should use the local alloc
88 * file. Otherwise, it has to go to the main bitmap.
89 */
90int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
91{
92 int la_bits = ocfs2_local_alloc_window_bits(osb);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -080093 int ret = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -080094
95 if (osb->local_alloc_state != OCFS2_LA_ENABLED)
Sunil Mushran2fbe8d12007-12-20 14:58:11 -080096 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -080097
98 /* la_bits should be at least twice the size (in clusters) of
99 * a new block group. We want to be sure block group
100 * allocations go through the local alloc, so allow an
101 * allocation to take up to half the bitmap. */
102 if (bits > (la_bits / 2))
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800103 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800104
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800105 ret = 1;
106bail:
107 mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n",
108 osb->local_alloc_state, (unsigned long long)bits, la_bits, ret);
109 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -0800110}
111
112int ocfs2_load_local_alloc(struct ocfs2_super *osb)
113{
114 int status = 0;
115 struct ocfs2_dinode *alloc = NULL;
116 struct buffer_head *alloc_bh = NULL;
117 u32 num_used;
118 struct inode *inode = NULL;
119 struct ocfs2_local_alloc *la;
120
121 mlog_entry_void();
122
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800123 if (osb->local_alloc_size == 0)
124 goto bail;
125
126 if (ocfs2_local_alloc_window_bits(osb) >= osb->bitmap_cpg) {
127 mlog(ML_NOTICE, "Requested local alloc window %d is larger "
128 "than max possible %u. Using defaults.\n",
129 ocfs2_local_alloc_window_bits(osb), (osb->bitmap_cpg - 1));
130 osb->local_alloc_size = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE;
131 }
132
Mark Fashehccd979b2005-12-15 14:31:24 -0800133 /* read the alloc off disk */
134 inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
135 osb->slot_num);
136 if (!inode) {
137 status = -EINVAL;
138 mlog_errno(status);
139 goto bail;
140 }
141
142 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
143 &alloc_bh, 0, inode);
144 if (status < 0) {
145 mlog_errno(status);
146 goto bail;
147 }
148
149 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
150 la = OCFS2_LOCAL_ALLOC(alloc);
151
152 if (!(le32_to_cpu(alloc->i_flags) &
153 (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
Mark Fashehb0697052006-03-03 10:24:33 -0800154 mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
155 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800156 status = -EINVAL;
157 goto bail;
158 }
159
160 if ((la->la_size == 0) ||
161 (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) {
162 mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n",
163 le16_to_cpu(la->la_size));
164 status = -EINVAL;
165 goto bail;
166 }
167
168 /* do a little verification. */
169 num_used = ocfs2_local_alloc_count_bits(alloc);
170
171 /* hopefully the local alloc has always been recovered before
172 * we load it. */
173 if (num_used
174 || alloc->id1.bitmap1.i_used
175 || alloc->id1.bitmap1.i_total
176 || la->la_bm_off)
177 mlog(ML_ERROR, "Local alloc hasn't been recovered!\n"
178 "found = %u, set = %u, taken = %u, off = %u\n",
179 num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
180 le32_to_cpu(alloc->id1.bitmap1.i_total),
181 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
182
183 osb->local_alloc_bh = alloc_bh;
184 osb->local_alloc_state = OCFS2_LA_ENABLED;
185
186bail:
187 if (status < 0)
188 if (alloc_bh)
189 brelse(alloc_bh);
190 if (inode)
191 iput(inode);
192
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800193 mlog(0, "Local alloc window bits = %d\n",
194 ocfs2_local_alloc_window_bits(osb));
195
Mark Fashehccd979b2005-12-15 14:31:24 -0800196 mlog_exit(status);
197 return status;
198}
199
200/*
201 * return any unused bits to the bitmap and write out a clean
202 * local_alloc.
203 *
204 * local_alloc_bh is optional. If not passed, we will simply use the
205 * one off osb. If you do pass it however, be warned that it *will* be
206 * returned brelse'd and NULL'd out.*/
207void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
208{
209 int status;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700210 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800211 struct inode *local_alloc_inode = NULL;
212 struct buffer_head *bh = NULL;
213 struct buffer_head *main_bm_bh = NULL;
214 struct inode *main_bm_inode = NULL;
215 struct ocfs2_dinode *alloc_copy = NULL;
216 struct ocfs2_dinode *alloc = NULL;
217
218 mlog_entry_void();
219
220 if (osb->local_alloc_state == OCFS2_LA_UNUSED)
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700221 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800222
223 local_alloc_inode =
224 ocfs2_get_system_file_inode(osb,
225 LOCAL_ALLOC_SYSTEM_INODE,
226 osb->slot_num);
227 if (!local_alloc_inode) {
228 status = -ENOENT;
229 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700230 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800231 }
232
233 osb->local_alloc_state = OCFS2_LA_DISABLED;
234
Mark Fashehccd979b2005-12-15 14:31:24 -0800235 main_bm_inode = ocfs2_get_system_file_inode(osb,
236 GLOBAL_BITMAP_SYSTEM_INODE,
237 OCFS2_INVALID_SLOT);
238 if (!main_bm_inode) {
239 status = -EINVAL;
240 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700241 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800242 }
243
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700244 mutex_lock(&main_bm_inode->i_mutex);
245
Mark Fashehe63aecb62007-10-18 15:30:42 -0700246 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800247 if (status < 0) {
248 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700249 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -0800250 }
251
252 /* WINDOW_MOVE_CREDITS is a bit heavy... */
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700253 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800254 if (IS_ERR(handle)) {
255 mlog_errno(PTR_ERR(handle));
256 handle = NULL;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700257 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800258 }
259
260 bh = osb->local_alloc_bh;
261 alloc = (struct ocfs2_dinode *) bh->b_data;
262
Sunil Mushran4ba1c5b2008-04-18 15:03:59 -0700263 alloc_copy = kmalloc(bh->b_size, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800264 if (!alloc_copy) {
265 status = -ENOMEM;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700266 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800267 }
268 memcpy(alloc_copy, alloc, bh->b_size);
269
270 status = ocfs2_journal_access(handle, local_alloc_inode, bh,
271 OCFS2_JOURNAL_ACCESS_WRITE);
272 if (status < 0) {
273 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700274 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800275 }
276
277 ocfs2_clear_local_alloc(alloc);
278
279 status = ocfs2_journal_dirty(handle, bh);
280 if (status < 0) {
281 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700282 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800283 }
284
285 brelse(bh);
286 osb->local_alloc_bh = NULL;
287 osb->local_alloc_state = OCFS2_LA_UNUSED;
288
289 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
290 main_bm_inode, main_bm_bh);
291 if (status < 0)
292 mlog_errno(status);
293
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700294out_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700295 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800296
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700297out_unlock:
Mark Fashehccd979b2005-12-15 14:31:24 -0800298 if (main_bm_bh)
299 brelse(main_bm_bh);
300
Mark Fashehe63aecb62007-10-18 15:30:42 -0700301 ocfs2_inode_unlock(main_bm_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800302
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700303out_mutex:
304 mutex_unlock(&main_bm_inode->i_mutex);
305 iput(main_bm_inode);
306
307out:
Mark Fashehccd979b2005-12-15 14:31:24 -0800308 if (local_alloc_inode)
309 iput(local_alloc_inode);
310
311 if (alloc_copy)
312 kfree(alloc_copy);
313
314 mlog_exit_void();
315}
316
317/*
318 * We want to free the bitmap bits outside of any recovery context as
319 * we'll need a cluster lock to do so, but we must clear the local
320 * alloc before giving up the recovered nodes journal. To solve this,
321 * we kmalloc a copy of the local alloc before it's change for the
322 * caller to process with ocfs2_complete_local_alloc_recovery
323 */
324int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
325 int slot_num,
326 struct ocfs2_dinode **alloc_copy)
327{
328 int status = 0;
329 struct buffer_head *alloc_bh = NULL;
330 struct inode *inode = NULL;
331 struct ocfs2_dinode *alloc;
332
333 mlog_entry("(slot_num = %d)\n", slot_num);
334
335 *alloc_copy = NULL;
336
337 inode = ocfs2_get_system_file_inode(osb,
338 LOCAL_ALLOC_SYSTEM_INODE,
339 slot_num);
340 if (!inode) {
341 status = -EINVAL;
342 mlog_errno(status);
343 goto bail;
344 }
345
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800346 mutex_lock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800347
348 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
349 &alloc_bh, 0, inode);
350 if (status < 0) {
351 mlog_errno(status);
352 goto bail;
353 }
354
355 *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL);
356 if (!(*alloc_copy)) {
357 status = -ENOMEM;
358 goto bail;
359 }
360 memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
361
362 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
363 ocfs2_clear_local_alloc(alloc);
364
365 status = ocfs2_write_block(osb, alloc_bh, inode);
366 if (status < 0)
367 mlog_errno(status);
368
369bail:
370 if ((status < 0) && (*alloc_copy)) {
371 kfree(*alloc_copy);
372 *alloc_copy = NULL;
373 }
374
375 if (alloc_bh)
376 brelse(alloc_bh);
377
378 if (inode) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800379 mutex_unlock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800380 iput(inode);
381 }
382
383 mlog_exit(status);
384 return status;
385}
386
387/*
388 * Step 2: By now, we've completed the journal recovery, we've stamped
389 * a clean local alloc on disk and dropped the node out of the
390 * recovery map. Dlm locks will no longer stall, so lets clear out the
391 * main bitmap.
392 */
393int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
394 struct ocfs2_dinode *alloc)
395{
396 int status;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700397 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800398 struct buffer_head *main_bm_bh = NULL;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700399 struct inode *main_bm_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800400
401 mlog_entry_void();
402
Mark Fashehccd979b2005-12-15 14:31:24 -0800403 main_bm_inode = ocfs2_get_system_file_inode(osb,
404 GLOBAL_BITMAP_SYSTEM_INODE,
405 OCFS2_INVALID_SLOT);
406 if (!main_bm_inode) {
407 status = -EINVAL;
408 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700409 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800410 }
411
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700412 mutex_lock(&main_bm_inode->i_mutex);
413
Mark Fashehe63aecb62007-10-18 15:30:42 -0700414 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800415 if (status < 0) {
416 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700417 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -0800418 }
419
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700420 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800421 if (IS_ERR(handle)) {
422 status = PTR_ERR(handle);
423 handle = NULL;
424 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700425 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800426 }
427
428 /* we want the bitmap change to be recorded on disk asap */
Mark Fasheh1fabe142006-10-09 18:11:45 -0700429 handle->h_sync = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800430
431 status = ocfs2_sync_local_to_main(osb, handle, alloc,
432 main_bm_inode, main_bm_bh);
433 if (status < 0)
434 mlog_errno(status);
435
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700436 ocfs2_commit_trans(osb, handle);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700437
438out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700439 ocfs2_inode_unlock(main_bm_inode, 1);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700440
441out_mutex:
442 mutex_unlock(&main_bm_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800443
444 if (main_bm_bh)
445 brelse(main_bm_bh);
446
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700447 iput(main_bm_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800448
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700449out:
Tao Ma4d0ddb22008-03-05 16:11:46 +0800450 if (!status)
451 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800452 mlog_exit(status);
453 return status;
454}
455
456/*
457 * make sure we've got at least bitswanted contiguous bits in the
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800458 * local alloc. You lose them when you drop i_mutex.
Mark Fashehccd979b2005-12-15 14:31:24 -0800459 *
460 * We will add ourselves to the transaction passed in, but may start
461 * our own in order to shift windows.
462 */
463int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800464 u32 bits_wanted,
465 struct ocfs2_alloc_context *ac)
466{
467 int status;
468 struct ocfs2_dinode *alloc;
469 struct inode *local_alloc_inode;
470 unsigned int free_bits;
471
472 mlog_entry_void();
473
Mark Fashehccd979b2005-12-15 14:31:24 -0800474 BUG_ON(!ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800475
476 local_alloc_inode =
477 ocfs2_get_system_file_inode(osb,
478 LOCAL_ALLOC_SYSTEM_INODE,
479 osb->slot_num);
480 if (!local_alloc_inode) {
481 status = -ENOENT;
482 mlog_errno(status);
483 goto bail;
484 }
Mark Fashehda5cbf22006-10-06 18:34:35 -0700485
486 mutex_lock(&local_alloc_inode->i_mutex);
487
Mark Fashehccd979b2005-12-15 14:31:24 -0800488 if (osb->local_alloc_state != OCFS2_LA_ENABLED) {
489 status = -ENOSPC;
490 goto bail;
491 }
492
493 if (bits_wanted > ocfs2_local_alloc_window_bits(osb)) {
494 mlog(0, "Asking for more than my max window size!\n");
495 status = -ENOSPC;
496 goto bail;
497 }
498
499 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
500
Jan Kara5a58c3e2007-11-13 19:59:33 +0100501#ifdef OCFS2_DEBUG_FS
Mark Fashehccd979b2005-12-15 14:31:24 -0800502 if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
503 ocfs2_local_alloc_count_bits(alloc)) {
Mark Fashehb0697052006-03-03 10:24:33 -0800504 ocfs2_error(osb->sb, "local alloc inode %llu says it has "
Mark Fashehccd979b2005-12-15 14:31:24 -0800505 "%u free bits, but a count shows %u",
Mark Fashehb0697052006-03-03 10:24:33 -0800506 (unsigned long long)le64_to_cpu(alloc->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -0800507 le32_to_cpu(alloc->id1.bitmap1.i_used),
508 ocfs2_local_alloc_count_bits(alloc));
509 status = -EIO;
510 goto bail;
511 }
Jan Kara5a58c3e2007-11-13 19:59:33 +0100512#endif
Mark Fashehccd979b2005-12-15 14:31:24 -0800513
514 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
515 le32_to_cpu(alloc->id1.bitmap1.i_used);
516 if (bits_wanted > free_bits) {
517 /* uhoh, window change time. */
518 status =
519 ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
520 if (status < 0) {
521 if (status != -ENOSPC)
522 mlog_errno(status);
523 goto bail;
524 }
525 }
526
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700527 ac->ac_inode = local_alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800528 /* We should never use localalloc from another slot */
529 ac->ac_alloc_slot = osb->slot_num;
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700530 ac->ac_which = OCFS2_AC_USE_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800531 get_bh(osb->local_alloc_bh);
532 ac->ac_bh = osb->local_alloc_bh;
Mark Fashehccd979b2005-12-15 14:31:24 -0800533 status = 0;
534bail:
Sunil Mushranbda02332007-09-21 11:41:43 -0700535 if (status < 0 && local_alloc_inode) {
536 mutex_unlock(&local_alloc_inode->i_mutex);
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700537 iput(local_alloc_inode);
Sunil Mushranbda02332007-09-21 11:41:43 -0700538 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800539
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800540 mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num,
541 status);
542
Mark Fashehccd979b2005-12-15 14:31:24 -0800543 mlog_exit(status);
544 return status;
545}
546
547int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700548 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800549 struct ocfs2_alloc_context *ac,
Mark Fasheh415cb802007-09-16 20:10:16 -0700550 u32 bits_wanted,
Mark Fashehccd979b2005-12-15 14:31:24 -0800551 u32 *bit_off,
552 u32 *num_bits)
553{
554 int status, start;
555 struct inode *local_alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800556 void *bitmap;
557 struct ocfs2_dinode *alloc;
558 struct ocfs2_local_alloc *la;
559
560 mlog_entry_void();
561 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
562
Mark Fashehccd979b2005-12-15 14:31:24 -0800563 local_alloc_inode = ac->ac_inode;
564 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
565 la = OCFS2_LOCAL_ALLOC(alloc);
566
567 start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted);
568 if (start == -1) {
569 /* TODO: Shouldn't we just BUG here? */
570 status = -ENOSPC;
571 mlog_errno(status);
572 goto bail;
573 }
574
575 bitmap = la->la_bitmap;
576 *bit_off = le32_to_cpu(la->la_bm_off) + start;
577 /* local alloc is always contiguous by nature -- we never
578 * delete bits from it! */
579 *num_bits = bits_wanted;
580
581 status = ocfs2_journal_access(handle, local_alloc_inode,
582 osb->local_alloc_bh,
583 OCFS2_JOURNAL_ACCESS_WRITE);
584 if (status < 0) {
585 mlog_errno(status);
586 goto bail;
587 }
588
589 while(bits_wanted--)
590 ocfs2_set_bit(start++, bitmap);
591
Marcin Slusarz0dd32562008-02-13 00:06:18 +0100592 le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800593
594 status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
595 if (status < 0) {
596 mlog_errno(status);
597 goto bail;
598 }
599
600 status = 0;
601bail:
602 mlog_exit(status);
603 return status;
604}
605
606static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
607{
608 int i;
609 u8 *buffer;
610 u32 count = 0;
611 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
612
613 mlog_entry_void();
614
615 buffer = la->la_bitmap;
616 for (i = 0; i < le16_to_cpu(la->la_size); i++)
617 count += hweight8(buffer[i]);
618
619 mlog_exit(count);
620 return count;
621}
622
623static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
624 struct ocfs2_dinode *alloc,
625 u32 numbits)
626{
627 int numfound, bitoff, left, startoff, lastzero;
628 void *bitmap = NULL;
629
630 mlog_entry("(numbits wanted = %u)\n", numbits);
631
632 if (!alloc->id1.bitmap1.i_total) {
633 mlog(0, "No bits in my window!\n");
634 bitoff = -1;
635 goto bail;
636 }
637
638 bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
639
640 numfound = bitoff = startoff = 0;
641 lastzero = -1;
642 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
643 while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
644 if (bitoff == left) {
645 /* mlog(0, "bitoff (%d) == left", bitoff); */
646 break;
647 }
648 /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
649 "numfound = %d\n", bitoff, startoff, numfound);*/
650
651 /* Ok, we found a zero bit... is it contig. or do we
652 * start over?*/
653 if (bitoff == startoff) {
654 /* we found a zero */
655 numfound++;
656 startoff++;
657 } else {
658 /* got a zero after some ones */
659 numfound = 1;
660 startoff = bitoff+1;
661 }
662 /* we got everything we needed */
663 if (numfound == numbits) {
664 /* mlog(0, "Found it all!\n"); */
665 break;
666 }
667 }
668
669 mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff,
670 numfound);
671
672 if (numfound == numbits)
673 bitoff = startoff - numfound;
674 else
675 bitoff = -1;
676
677bail:
678 mlog_exit(bitoff);
679 return bitoff;
680}
681
682static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
683{
684 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
685 int i;
686 mlog_entry_void();
687
688 alloc->id1.bitmap1.i_total = 0;
689 alloc->id1.bitmap1.i_used = 0;
690 la->la_bm_off = 0;
691 for(i = 0; i < le16_to_cpu(la->la_size); i++)
692 la->la_bitmap[i] = 0;
693
694 mlog_exit_void();
695}
696
697#if 0
698/* turn this on and uncomment below to aid debugging window shifts. */
699static void ocfs2_verify_zero_bits(unsigned long *bitmap,
700 unsigned int start,
701 unsigned int count)
702{
703 unsigned int tmp = count;
704 while(tmp--) {
705 if (ocfs2_test_bit(start + tmp, bitmap)) {
706 printk("ocfs2_verify_zero_bits: start = %u, count = "
707 "%u\n", start, count);
708 printk("ocfs2_verify_zero_bits: bit %u is set!",
709 start + tmp);
710 BUG();
711 }
712 }
713}
714#endif
715
716/*
717 * sync the local alloc to main bitmap.
718 *
719 * assumes you've already locked the main bitmap -- the bitmap inode
720 * passed is used for caching.
721 */
722static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700723 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800724 struct ocfs2_dinode *alloc,
725 struct inode *main_bm_inode,
726 struct buffer_head *main_bm_bh)
727{
728 int status = 0;
729 int bit_off, left, count, start;
730 u64 la_start_blk;
731 u64 blkno;
732 void *bitmap;
733 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
734
Jan Kara5a58c3e2007-11-13 19:59:33 +0100735 mlog_entry("total = %u, used = %u\n",
Mark Fashehccd979b2005-12-15 14:31:24 -0800736 le32_to_cpu(alloc->id1.bitmap1.i_total),
Mark Fashehccd979b2005-12-15 14:31:24 -0800737 le32_to_cpu(alloc->id1.bitmap1.i_used));
738
739 if (!alloc->id1.bitmap1.i_total) {
740 mlog(0, "nothing to sync!\n");
741 goto bail;
742 }
743
744 if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
745 le32_to_cpu(alloc->id1.bitmap1.i_total)) {
746 mlog(0, "all bits were taken!\n");
747 goto bail;
748 }
749
750 la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
751 le32_to_cpu(la->la_bm_off));
752 bitmap = la->la_bitmap;
753 start = count = bit_off = 0;
754 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
755
756 while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
757 != -1) {
758 if ((bit_off < left) && (bit_off == start)) {
759 count++;
760 start++;
761 continue;
762 }
763 if (count) {
764 blkno = la_start_blk +
765 ocfs2_clusters_to_blocks(osb->sb,
766 start - count);
767
Mark Fashehb0697052006-03-03 10:24:33 -0800768 mlog(0, "freeing %u bits starting at local alloc bit "
769 "%u (la_start_blk = %llu, blkno = %llu)\n",
770 count, start - count,
771 (unsigned long long)la_start_blk,
772 (unsigned long long)blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800773
774 status = ocfs2_free_clusters(handle, main_bm_inode,
775 main_bm_bh, blkno, count);
776 if (status < 0) {
777 mlog_errno(status);
778 goto bail;
779 }
780 }
781 if (bit_off >= left)
782 break;
783 count = 1;
784 start = bit_off + 1;
785 }
786
787bail:
788 mlog_exit(status);
789 return status;
790}
791
792static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800793 struct ocfs2_alloc_context **ac,
794 struct inode **bitmap_inode,
795 struct buffer_head **bitmap_bh)
796{
797 int status;
798
Robert P. J. Daycd861282006-12-13 00:34:52 -0800799 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800800 if (!(*ac)) {
801 status = -ENOMEM;
802 mlog_errno(status);
803 goto bail;
804 }
805
Mark Fashehccd979b2005-12-15 14:31:24 -0800806 (*ac)->ac_bits_wanted = ocfs2_local_alloc_window_bits(osb);
807
808 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
809 if (status < 0) {
810 if (status != -ENOSPC)
811 mlog_errno(status);
812 goto bail;
813 }
814
815 *bitmap_inode = (*ac)->ac_inode;
816 igrab(*bitmap_inode);
817 *bitmap_bh = (*ac)->ac_bh;
818 get_bh(*bitmap_bh);
819 status = 0;
820bail:
821 if ((status < 0) && *ac) {
822 ocfs2_free_alloc_context(*ac);
823 *ac = NULL;
824 }
825
826 mlog_exit(status);
827 return status;
828}
829
830/*
831 * pass it the bitmap lock in lock_bh if you have it.
832 */
833static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700834 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800835 struct ocfs2_alloc_context *ac)
836{
837 int status = 0;
838 u32 cluster_off, cluster_count;
839 struct ocfs2_dinode *alloc = NULL;
840 struct ocfs2_local_alloc *la;
841
842 mlog_entry_void();
843
844 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
845 la = OCFS2_LOCAL_ALLOC(alloc);
846
847 if (alloc->id1.bitmap1.i_total)
848 mlog(0, "asking me to alloc a new window over a non-empty "
849 "one\n");
850
851 mlog(0, "Allocating %u clusters for a new window.\n",
852 ocfs2_local_alloc_window_bits(osb));
Mark Fasheh883d4ca2006-06-05 16:41:00 -0400853
854 /* Instruct the allocation code to try the most recently used
855 * cluster group. We'll re-record the group used this pass
856 * below. */
857 ac->ac_last_group = osb->la_last_gd;
858
Mark Fashehccd979b2005-12-15 14:31:24 -0800859 /* we used the generic suballoc reserve function, but we set
860 * everything up nicely, so there's no reason why we can't use
861 * the more specific cluster api to claim bits. */
862 status = ocfs2_claim_clusters(osb, handle, ac,
863 ocfs2_local_alloc_window_bits(osb),
864 &cluster_off, &cluster_count);
865 if (status < 0) {
866 if (status != -ENOSPC)
867 mlog_errno(status);
868 goto bail;
869 }
870
Mark Fasheh883d4ca2006-06-05 16:41:00 -0400871 osb->la_last_gd = ac->ac_last_group;
872
Mark Fashehccd979b2005-12-15 14:31:24 -0800873 la->la_bm_off = cpu_to_le32(cluster_off);
874 alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
875 /* just in case... In the future when we find space ourselves,
876 * we don't have to get all contiguous -- but we'll have to
877 * set all previously used bits in bitmap and update
878 * la_bits_set before setting the bits in the main bitmap. */
879 alloc->id1.bitmap1.i_used = 0;
880 memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
881 le16_to_cpu(la->la_size));
882
883 mlog(0, "New window allocated:\n");
884 mlog(0, "window la_bm_off = %u\n",
885 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
886 mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total));
887
888bail:
889 mlog_exit(status);
890 return status;
891}
892
893/* Note that we do *NOT* lock the local alloc inode here as
894 * it's been locked already for us. */
895static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
896 struct inode *local_alloc_inode)
897{
898 int status = 0;
899 struct buffer_head *main_bm_bh = NULL;
900 struct inode *main_bm_inode = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700901 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800902 struct ocfs2_dinode *alloc;
903 struct ocfs2_dinode *alloc_copy = NULL;
904 struct ocfs2_alloc_context *ac = NULL;
905
906 mlog_entry_void();
907
Mark Fashehccd979b2005-12-15 14:31:24 -0800908 /* This will lock the main bitmap for us. */
909 status = ocfs2_local_alloc_reserve_for_window(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800910 &ac,
911 &main_bm_inode,
912 &main_bm_bh);
913 if (status < 0) {
914 if (status != -ENOSPC)
915 mlog_errno(status);
916 goto bail;
917 }
918
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700919 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800920 if (IS_ERR(handle)) {
921 status = PTR_ERR(handle);
922 handle = NULL;
923 mlog_errno(status);
924 goto bail;
925 }
926
927 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
928
929 /* We want to clear the local alloc before doing anything
930 * else, so that if we error later during this operation,
931 * local alloc shutdown won't try to double free main bitmap
932 * bits. Make a copy so the sync function knows which bits to
933 * free. */
Sunil Mushran4ba1c5b2008-04-18 15:03:59 -0700934 alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800935 if (!alloc_copy) {
936 status = -ENOMEM;
937 mlog_errno(status);
938 goto bail;
939 }
940 memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
941
942 status = ocfs2_journal_access(handle, local_alloc_inode,
943 osb->local_alloc_bh,
944 OCFS2_JOURNAL_ACCESS_WRITE);
945 if (status < 0) {
946 mlog_errno(status);
947 goto bail;
948 }
949
950 ocfs2_clear_local_alloc(alloc);
951
952 status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
953 if (status < 0) {
954 mlog_errno(status);
955 goto bail;
956 }
957
958 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
959 main_bm_inode, main_bm_bh);
960 if (status < 0) {
961 mlog_errno(status);
962 goto bail;
963 }
964
965 status = ocfs2_local_alloc_new_window(osb, handle, ac);
966 if (status < 0) {
967 if (status != -ENOSPC)
968 mlog_errno(status);
969 goto bail;
970 }
971
972 atomic_inc(&osb->alloc_stats.moves);
973
974 status = 0;
975bail:
976 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700977 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800978
979 if (main_bm_bh)
980 brelse(main_bm_bh);
981
982 if (main_bm_inode)
983 iput(main_bm_inode);
984
985 if (alloc_copy)
986 kfree(alloc_copy);
987
988 if (ac)
989 ocfs2_free_alloc_context(ac);
990
991 mlog_exit(status);
992 return status;
993}
994