blob: 880e4bc827bedae1b9dbeaa3cef67ac66c3ec4e7 [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"
Joel Becker13723d02008-10-17 19:25:01 -070038#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080039#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,
Mark Fashehd02f00c2009-12-07 13:10:48 -080055 u32 *numbits,
56 struct ocfs2_alloc_reservation *resv);
Mark Fashehccd979b2005-12-15 14:31:24 -080057
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 Fasheh9c7af402008-07-28 18:02:53 -070078static inline int ocfs2_la_state_enabled(struct ocfs2_super *osb)
79{
80 return (osb->local_alloc_state == OCFS2_LA_THROTTLED ||
81 osb->local_alloc_state == OCFS2_LA_ENABLED);
82}
83
84void ocfs2_local_alloc_seen_free_bits(struct ocfs2_super *osb,
85 unsigned int num_clusters)
86{
87 spin_lock(&osb->osb_lock);
88 if (osb->local_alloc_state == OCFS2_LA_DISABLED ||
89 osb->local_alloc_state == OCFS2_LA_THROTTLED)
90 if (num_clusters >= osb->local_alloc_default_bits) {
91 cancel_delayed_work(&osb->la_enable_wq);
92 osb->local_alloc_state = OCFS2_LA_ENABLED;
93 }
94 spin_unlock(&osb->osb_lock);
95}
96
97void ocfs2_la_enable_worker(struct work_struct *work)
98{
99 struct ocfs2_super *osb =
100 container_of(work, struct ocfs2_super,
101 la_enable_wq.work);
102 spin_lock(&osb->osb_lock);
103 osb->local_alloc_state = OCFS2_LA_ENABLED;
104 spin_unlock(&osb->osb_lock);
105}
106
Mark Fashehccd979b2005-12-15 14:31:24 -0800107/*
108 * Tell us whether a given allocation should use the local alloc
109 * file. Otherwise, it has to go to the main bitmap.
Mark Fasheh9c7af402008-07-28 18:02:53 -0700110 *
111 * This function does semi-dirty reads of local alloc size and state!
112 * This is ok however, as the values are re-checked once under mutex.
Mark Fashehccd979b2005-12-15 14:31:24 -0800113 */
114int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
115{
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800116 int ret = 0;
Mark Fasheh9c7af402008-07-28 18:02:53 -0700117 int la_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800118
Mark Fasheh9c7af402008-07-28 18:02:53 -0700119 spin_lock(&osb->osb_lock);
120 la_bits = osb->local_alloc_bits;
121
122 if (!ocfs2_la_state_enabled(osb))
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800123 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800124
125 /* la_bits should be at least twice the size (in clusters) of
126 * a new block group. We want to be sure block group
127 * allocations go through the local alloc, so allow an
128 * allocation to take up to half the bitmap. */
129 if (bits > (la_bits / 2))
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800130 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800131
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800132 ret = 1;
133bail:
134 mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n",
135 osb->local_alloc_state, (unsigned long long)bits, la_bits, ret);
Mark Fasheh9c7af402008-07-28 18:02:53 -0700136 spin_unlock(&osb->osb_lock);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800137 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -0800138}
139
140int ocfs2_load_local_alloc(struct ocfs2_super *osb)
141{
142 int status = 0;
143 struct ocfs2_dinode *alloc = NULL;
144 struct buffer_head *alloc_bh = NULL;
145 u32 num_used;
146 struct inode *inode = NULL;
147 struct ocfs2_local_alloc *la;
148
149 mlog_entry_void();
150
Mark Fashehebcee4b2008-07-28 14:55:20 -0700151 if (osb->local_alloc_bits == 0)
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800152 goto bail;
153
Mark Fashehebcee4b2008-07-28 14:55:20 -0700154 if (osb->local_alloc_bits >= osb->bitmap_cpg) {
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800155 mlog(ML_NOTICE, "Requested local alloc window %d is larger "
156 "than max possible %u. Using defaults.\n",
Mark Fashehebcee4b2008-07-28 14:55:20 -0700157 osb->local_alloc_bits, (osb->bitmap_cpg - 1));
158 osb->local_alloc_bits =
159 ocfs2_megabytes_to_clusters(osb->sb,
160 OCFS2_DEFAULT_LOCAL_ALLOC_SIZE);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800161 }
162
Mark Fashehccd979b2005-12-15 14:31:24 -0800163 /* read the alloc off disk */
164 inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
165 osb->slot_num);
166 if (!inode) {
167 status = -EINVAL;
168 mlog_errno(status);
169 goto bail;
170 }
171
Joel Beckerb657c952008-11-13 14:49:11 -0800172 status = ocfs2_read_inode_block_full(inode, &alloc_bh,
173 OCFS2_BH_IGNORE_CACHE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800174 if (status < 0) {
175 mlog_errno(status);
176 goto bail;
177 }
178
179 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
180 la = OCFS2_LOCAL_ALLOC(alloc);
181
182 if (!(le32_to_cpu(alloc->i_flags) &
183 (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
Mark Fashehb06970532006-03-03 10:24:33 -0800184 mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
185 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800186 status = -EINVAL;
187 goto bail;
188 }
189
190 if ((la->la_size == 0) ||
191 (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) {
192 mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n",
193 le16_to_cpu(la->la_size));
194 status = -EINVAL;
195 goto bail;
196 }
197
198 /* do a little verification. */
199 num_used = ocfs2_local_alloc_count_bits(alloc);
200
201 /* hopefully the local alloc has always been recovered before
202 * we load it. */
203 if (num_used
204 || alloc->id1.bitmap1.i_used
205 || alloc->id1.bitmap1.i_total
206 || la->la_bm_off)
207 mlog(ML_ERROR, "Local alloc hasn't been recovered!\n"
208 "found = %u, set = %u, taken = %u, off = %u\n",
209 num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
210 le32_to_cpu(alloc->id1.bitmap1.i_total),
211 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
212
213 osb->local_alloc_bh = alloc_bh;
214 osb->local_alloc_state = OCFS2_LA_ENABLED;
215
216bail:
217 if (status < 0)
Mark Fasheha81cb882008-10-07 14:25:16 -0700218 brelse(alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800219 if (inode)
220 iput(inode);
221
Mark Fashehebcee4b2008-07-28 14:55:20 -0700222 mlog(0, "Local alloc window bits = %d\n", osb->local_alloc_bits);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800223
Mark Fashehccd979b2005-12-15 14:31:24 -0800224 mlog_exit(status);
225 return status;
226}
227
228/*
229 * return any unused bits to the bitmap and write out a clean
230 * local_alloc.
231 *
232 * local_alloc_bh is optional. If not passed, we will simply use the
233 * one off osb. If you do pass it however, be warned that it *will* be
234 * returned brelse'd and NULL'd out.*/
235void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
236{
237 int status;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700238 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800239 struct inode *local_alloc_inode = NULL;
240 struct buffer_head *bh = NULL;
241 struct buffer_head *main_bm_bh = NULL;
242 struct inode *main_bm_inode = NULL;
243 struct ocfs2_dinode *alloc_copy = NULL;
244 struct ocfs2_dinode *alloc = NULL;
245
246 mlog_entry_void();
247
Mark Fasheh9c7af402008-07-28 18:02:53 -0700248 cancel_delayed_work(&osb->la_enable_wq);
249 flush_workqueue(ocfs2_wq);
250
Mark Fashehccd979b2005-12-15 14:31:24 -0800251 if (osb->local_alloc_state == OCFS2_LA_UNUSED)
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700252 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800253
254 local_alloc_inode =
255 ocfs2_get_system_file_inode(osb,
256 LOCAL_ALLOC_SYSTEM_INODE,
257 osb->slot_num);
258 if (!local_alloc_inode) {
259 status = -ENOENT;
260 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700261 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800262 }
263
264 osb->local_alloc_state = OCFS2_LA_DISABLED;
265
Mark Fashehd02f00c2009-12-07 13:10:48 -0800266 ocfs2_resmap_uninit(&osb->osb_la_resmap);
267
Mark Fashehccd979b2005-12-15 14:31:24 -0800268 main_bm_inode = ocfs2_get_system_file_inode(osb,
269 GLOBAL_BITMAP_SYSTEM_INODE,
270 OCFS2_INVALID_SLOT);
271 if (!main_bm_inode) {
272 status = -EINVAL;
273 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700274 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800275 }
276
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700277 mutex_lock(&main_bm_inode->i_mutex);
278
Mark Fashehe63aecb62007-10-18 15:30:42 -0700279 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800280 if (status < 0) {
281 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700282 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -0800283 }
284
285 /* WINDOW_MOVE_CREDITS is a bit heavy... */
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700286 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800287 if (IS_ERR(handle)) {
288 mlog_errno(PTR_ERR(handle));
289 handle = NULL;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700290 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800291 }
292
293 bh = osb->local_alloc_bh;
294 alloc = (struct ocfs2_dinode *) bh->b_data;
295
Sunil Mushran4ba1c5b2008-04-18 15:03:59 -0700296 alloc_copy = kmalloc(bh->b_size, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800297 if (!alloc_copy) {
298 status = -ENOMEM;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700299 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800300 }
301 memcpy(alloc_copy, alloc, bh->b_size);
302
Joel Becker0cf2f762009-02-12 16:41:25 -0800303 status = ocfs2_journal_access_di(handle, INODE_CACHE(local_alloc_inode),
304 bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800305 if (status < 0) {
306 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700307 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800308 }
309
310 ocfs2_clear_local_alloc(alloc);
Joel Beckerec20cec2010-03-19 14:13:52 -0700311 ocfs2_journal_dirty(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800312
313 brelse(bh);
314 osb->local_alloc_bh = NULL;
315 osb->local_alloc_state = OCFS2_LA_UNUSED;
316
317 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
318 main_bm_inode, main_bm_bh);
319 if (status < 0)
320 mlog_errno(status);
321
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700322out_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700323 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800324
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700325out_unlock:
Mark Fasheha81cb882008-10-07 14:25:16 -0700326 brelse(main_bm_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800327
Mark Fashehe63aecb62007-10-18 15:30:42 -0700328 ocfs2_inode_unlock(main_bm_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800329
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700330out_mutex:
331 mutex_unlock(&main_bm_inode->i_mutex);
332 iput(main_bm_inode);
333
334out:
Mark Fashehccd979b2005-12-15 14:31:24 -0800335 if (local_alloc_inode)
336 iput(local_alloc_inode);
337
338 if (alloc_copy)
339 kfree(alloc_copy);
340
341 mlog_exit_void();
342}
343
344/*
345 * We want to free the bitmap bits outside of any recovery context as
346 * we'll need a cluster lock to do so, but we must clear the local
347 * alloc before giving up the recovered nodes journal. To solve this,
348 * we kmalloc a copy of the local alloc before it's change for the
349 * caller to process with ocfs2_complete_local_alloc_recovery
350 */
351int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
352 int slot_num,
353 struct ocfs2_dinode **alloc_copy)
354{
355 int status = 0;
356 struct buffer_head *alloc_bh = NULL;
357 struct inode *inode = NULL;
358 struct ocfs2_dinode *alloc;
359
360 mlog_entry("(slot_num = %d)\n", slot_num);
361
362 *alloc_copy = NULL;
363
364 inode = ocfs2_get_system_file_inode(osb,
365 LOCAL_ALLOC_SYSTEM_INODE,
366 slot_num);
367 if (!inode) {
368 status = -EINVAL;
369 mlog_errno(status);
370 goto bail;
371 }
372
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800373 mutex_lock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800374
Joel Beckerb657c952008-11-13 14:49:11 -0800375 status = ocfs2_read_inode_block_full(inode, &alloc_bh,
376 OCFS2_BH_IGNORE_CACHE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800377 if (status < 0) {
378 mlog_errno(status);
379 goto bail;
380 }
381
382 *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL);
383 if (!(*alloc_copy)) {
384 status = -ENOMEM;
385 goto bail;
386 }
387 memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
388
389 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
390 ocfs2_clear_local_alloc(alloc);
391
Joel Becker13723d02008-10-17 19:25:01 -0700392 ocfs2_compute_meta_ecc(osb->sb, alloc_bh->b_data, &alloc->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -0800393 status = ocfs2_write_block(osb, alloc_bh, INODE_CACHE(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -0800394 if (status < 0)
395 mlog_errno(status);
396
397bail:
398 if ((status < 0) && (*alloc_copy)) {
399 kfree(*alloc_copy);
400 *alloc_copy = NULL;
401 }
402
Mark Fasheha81cb882008-10-07 14:25:16 -0700403 brelse(alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800404
405 if (inode) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800406 mutex_unlock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800407 iput(inode);
408 }
409
410 mlog_exit(status);
411 return status;
412}
413
414/*
415 * Step 2: By now, we've completed the journal recovery, we've stamped
416 * a clean local alloc on disk and dropped the node out of the
417 * recovery map. Dlm locks will no longer stall, so lets clear out the
418 * main bitmap.
419 */
420int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
421 struct ocfs2_dinode *alloc)
422{
423 int status;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700424 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800425 struct buffer_head *main_bm_bh = NULL;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700426 struct inode *main_bm_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800427
428 mlog_entry_void();
429
Mark Fashehccd979b2005-12-15 14:31:24 -0800430 main_bm_inode = ocfs2_get_system_file_inode(osb,
431 GLOBAL_BITMAP_SYSTEM_INODE,
432 OCFS2_INVALID_SLOT);
433 if (!main_bm_inode) {
434 status = -EINVAL;
435 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700436 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800437 }
438
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700439 mutex_lock(&main_bm_inode->i_mutex);
440
Mark Fashehe63aecb62007-10-18 15:30:42 -0700441 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800442 if (status < 0) {
443 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700444 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -0800445 }
446
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700447 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800448 if (IS_ERR(handle)) {
449 status = PTR_ERR(handle);
450 handle = NULL;
451 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700452 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800453 }
454
455 /* we want the bitmap change to be recorded on disk asap */
Mark Fasheh1fabe142006-10-09 18:11:45 -0700456 handle->h_sync = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800457
458 status = ocfs2_sync_local_to_main(osb, handle, alloc,
459 main_bm_inode, main_bm_bh);
460 if (status < 0)
461 mlog_errno(status);
462
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700463 ocfs2_commit_trans(osb, handle);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700464
465out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700466 ocfs2_inode_unlock(main_bm_inode, 1);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700467
468out_mutex:
469 mutex_unlock(&main_bm_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800470
Mark Fasheha81cb882008-10-07 14:25:16 -0700471 brelse(main_bm_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800472
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700473 iput(main_bm_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800474
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700475out:
Tao Ma4d0ddb22008-03-05 16:11:46 +0800476 if (!status)
Tiger Yangb89c5422010-01-25 14:11:06 +0800477 ocfs2_init_steal_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800478 mlog_exit(status);
479 return status;
480}
481
482/*
Mark Fasheh9c7af402008-07-28 18:02:53 -0700483 * make sure we've got at least bits_wanted contiguous bits in the
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800484 * local alloc. You lose them when you drop i_mutex.
Mark Fashehccd979b2005-12-15 14:31:24 -0800485 *
486 * We will add ourselves to the transaction passed in, but may start
487 * our own in order to shift windows.
488 */
489int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800490 u32 bits_wanted,
491 struct ocfs2_alloc_context *ac)
492{
493 int status;
494 struct ocfs2_dinode *alloc;
495 struct inode *local_alloc_inode;
496 unsigned int free_bits;
497
498 mlog_entry_void();
499
Mark Fashehccd979b2005-12-15 14:31:24 -0800500 BUG_ON(!ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800501
502 local_alloc_inode =
503 ocfs2_get_system_file_inode(osb,
504 LOCAL_ALLOC_SYSTEM_INODE,
505 osb->slot_num);
506 if (!local_alloc_inode) {
507 status = -ENOENT;
508 mlog_errno(status);
509 goto bail;
510 }
Mark Fashehda5cbf22006-10-06 18:34:35 -0700511
512 mutex_lock(&local_alloc_inode->i_mutex);
513
Mark Fasheh9c7af402008-07-28 18:02:53 -0700514 /*
515 * We must double check state and allocator bits because
516 * another process may have changed them while holding i_mutex.
517 */
518 spin_lock(&osb->osb_lock);
519 if (!ocfs2_la_state_enabled(osb) ||
520 (bits_wanted > osb->local_alloc_bits)) {
521 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800522 status = -ENOSPC;
523 goto bail;
524 }
Mark Fasheh9c7af402008-07-28 18:02:53 -0700525 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800526
527 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
528
Joel Beckere407e392008-06-12 22:35:39 -0700529#ifdef CONFIG_OCFS2_DEBUG_FS
Mark Fashehccd979b2005-12-15 14:31:24 -0800530 if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
531 ocfs2_local_alloc_count_bits(alloc)) {
Mark Fashehb06970532006-03-03 10:24:33 -0800532 ocfs2_error(osb->sb, "local alloc inode %llu says it has "
Mark Fashehccd979b2005-12-15 14:31:24 -0800533 "%u free bits, but a count shows %u",
Mark Fashehb06970532006-03-03 10:24:33 -0800534 (unsigned long long)le64_to_cpu(alloc->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -0800535 le32_to_cpu(alloc->id1.bitmap1.i_used),
536 ocfs2_local_alloc_count_bits(alloc));
537 status = -EIO;
538 goto bail;
539 }
Jan Kara5a58c3e2007-11-13 19:59:33 +0100540#endif
Mark Fashehccd979b2005-12-15 14:31:24 -0800541
542 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
543 le32_to_cpu(alloc->id1.bitmap1.i_used);
544 if (bits_wanted > free_bits) {
545 /* uhoh, window change time. */
546 status =
547 ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
548 if (status < 0) {
549 if (status != -ENOSPC)
550 mlog_errno(status);
551 goto bail;
552 }
Mark Fasheh9c7af402008-07-28 18:02:53 -0700553
554 /*
555 * Under certain conditions, the window slide code
556 * might have reduced the number of bits available or
557 * disabled the the local alloc entirely. Re-check
558 * here and return -ENOSPC if necessary.
559 */
560 status = -ENOSPC;
561 if (!ocfs2_la_state_enabled(osb))
562 goto bail;
563
564 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
565 le32_to_cpu(alloc->id1.bitmap1.i_used);
566 if (bits_wanted > free_bits)
567 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800568 }
569
Joel Becker1187c962008-09-03 20:03:39 -0700570 if (ac->ac_max_block)
571 mlog(0, "Calling in_range for max block %llu\n",
572 (unsigned long long)ac->ac_max_block);
573
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700574 ac->ac_inode = local_alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800575 /* We should never use localalloc from another slot */
576 ac->ac_alloc_slot = osb->slot_num;
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700577 ac->ac_which = OCFS2_AC_USE_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800578 get_bh(osb->local_alloc_bh);
579 ac->ac_bh = osb->local_alloc_bh;
Mark Fashehccd979b2005-12-15 14:31:24 -0800580 status = 0;
581bail:
Sunil Mushranbda02332007-09-21 11:41:43 -0700582 if (status < 0 && local_alloc_inode) {
583 mutex_unlock(&local_alloc_inode->i_mutex);
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700584 iput(local_alloc_inode);
Sunil Mushranbda02332007-09-21 11:41:43 -0700585 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800586
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800587 mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num,
588 status);
589
Mark Fashehccd979b2005-12-15 14:31:24 -0800590 mlog_exit(status);
591 return status;
592}
593
594int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700595 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800596 struct ocfs2_alloc_context *ac,
Mark Fasheh415cb802007-09-16 20:10:16 -0700597 u32 bits_wanted,
Mark Fashehccd979b2005-12-15 14:31:24 -0800598 u32 *bit_off,
599 u32 *num_bits)
600{
601 int status, start;
602 struct inode *local_alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800603 void *bitmap;
604 struct ocfs2_dinode *alloc;
605 struct ocfs2_local_alloc *la;
606
607 mlog_entry_void();
608 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
609
Mark Fashehccd979b2005-12-15 14:31:24 -0800610 local_alloc_inode = ac->ac_inode;
611 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
612 la = OCFS2_LOCAL_ALLOC(alloc);
613
Mark Fashehd02f00c2009-12-07 13:10:48 -0800614 start = ocfs2_local_alloc_find_clear_bits(osb, alloc, &bits_wanted,
615 ac->ac_resv);
Mark Fashehccd979b2005-12-15 14:31:24 -0800616 if (start == -1) {
617 /* TODO: Shouldn't we just BUG here? */
618 status = -ENOSPC;
619 mlog_errno(status);
620 goto bail;
621 }
622
623 bitmap = la->la_bitmap;
624 *bit_off = le32_to_cpu(la->la_bm_off) + start;
Mark Fashehccd979b2005-12-15 14:31:24 -0800625 *num_bits = bits_wanted;
626
Joel Becker0cf2f762009-02-12 16:41:25 -0800627 status = ocfs2_journal_access_di(handle,
628 INODE_CACHE(local_alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700629 osb->local_alloc_bh,
630 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800631 if (status < 0) {
632 mlog_errno(status);
633 goto bail;
634 }
635
Mark Fashehd02f00c2009-12-07 13:10:48 -0800636 ocfs2_resmap_claimed_bits(&osb->osb_la_resmap, ac->ac_resv, start,
637 bits_wanted);
638
Mark Fashehccd979b2005-12-15 14:31:24 -0800639 while(bits_wanted--)
640 ocfs2_set_bit(start++, bitmap);
641
Marcin Slusarz0dd32562008-02-13 00:06:18 +0100642 le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits);
Joel Beckerec20cec2010-03-19 14:13:52 -0700643 ocfs2_journal_dirty(handle, osb->local_alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800644
Mark Fashehccd979b2005-12-15 14:31:24 -0800645bail:
646 mlog_exit(status);
647 return status;
648}
649
650static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
651{
652 int i;
653 u8 *buffer;
654 u32 count = 0;
655 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
656
657 mlog_entry_void();
658
659 buffer = la->la_bitmap;
660 for (i = 0; i < le16_to_cpu(la->la_size); i++)
661 count += hweight8(buffer[i]);
662
663 mlog_exit(count);
664 return count;
665}
666
667static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
Mark Fashehd02f00c2009-12-07 13:10:48 -0800668 struct ocfs2_dinode *alloc,
669 u32 *numbits,
670 struct ocfs2_alloc_reservation *resv)
Mark Fashehccd979b2005-12-15 14:31:24 -0800671{
672 int numfound, bitoff, left, startoff, lastzero;
Mark Fashehd02f00c2009-12-07 13:10:48 -0800673 int local_resv = 0;
674 struct ocfs2_alloc_reservation r;
Mark Fashehccd979b2005-12-15 14:31:24 -0800675 void *bitmap = NULL;
Mark Fashehd02f00c2009-12-07 13:10:48 -0800676 struct ocfs2_reservation_map *resmap = &osb->osb_la_resmap;
Mark Fashehccd979b2005-12-15 14:31:24 -0800677
Mark Fashehd02f00c2009-12-07 13:10:48 -0800678 mlog_entry("(numbits wanted = %u)\n", *numbits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800679
680 if (!alloc->id1.bitmap1.i_total) {
681 mlog(0, "No bits in my window!\n");
682 bitoff = -1;
683 goto bail;
684 }
685
Mark Fashehd02f00c2009-12-07 13:10:48 -0800686 if (!resv) {
687 local_resv = 1;
688 ocfs2_resv_init_once(&r);
689 ocfs2_resv_set_type(&r, OCFS2_RESV_FLAG_TMP);
690 resv = &r;
691 }
692
693 numfound = *numbits;
694 if (ocfs2_resmap_resv_bits(resmap, resv, &bitoff, &numfound) == 0) {
695 if (numfound < *numbits)
696 *numbits = numfound;
697 goto bail;
698 }
699
700 /*
701 * Code error. While reservations are enabled, local
702 * allocation should _always_ go through them.
703 */
704 BUG_ON(osb->osb_resv_level != 0);
705
706 /*
707 * Reservations are disabled. Handle this the old way.
708 */
709
Mark Fashehccd979b2005-12-15 14:31:24 -0800710 bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
711
712 numfound = bitoff = startoff = 0;
713 lastzero = -1;
714 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
715 while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
716 if (bitoff == left) {
717 /* mlog(0, "bitoff (%d) == left", bitoff); */
718 break;
719 }
720 /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
721 "numfound = %d\n", bitoff, startoff, numfound);*/
722
723 /* Ok, we found a zero bit... is it contig. or do we
724 * start over?*/
725 if (bitoff == startoff) {
726 /* we found a zero */
727 numfound++;
728 startoff++;
729 } else {
730 /* got a zero after some ones */
731 numfound = 1;
732 startoff = bitoff+1;
733 }
734 /* we got everything we needed */
Mark Fashehd02f00c2009-12-07 13:10:48 -0800735 if (numfound == *numbits) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800736 /* mlog(0, "Found it all!\n"); */
737 break;
738 }
739 }
740
741 mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff,
742 numfound);
743
Mark Fashehd02f00c2009-12-07 13:10:48 -0800744 if (numfound == *numbits) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800745 bitoff = startoff - numfound;
Mark Fashehd02f00c2009-12-07 13:10:48 -0800746 *numbits = numfound;
747 } else {
748 numfound = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800749 bitoff = -1;
Mark Fashehd02f00c2009-12-07 13:10:48 -0800750 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800751
752bail:
Mark Fashehd02f00c2009-12-07 13:10:48 -0800753 if (local_resv)
754 ocfs2_resv_discard(resmap, resv);
755
Mark Fashehccd979b2005-12-15 14:31:24 -0800756 mlog_exit(bitoff);
757 return bitoff;
758}
759
760static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
761{
762 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
763 int i;
764 mlog_entry_void();
765
766 alloc->id1.bitmap1.i_total = 0;
767 alloc->id1.bitmap1.i_used = 0;
768 la->la_bm_off = 0;
769 for(i = 0; i < le16_to_cpu(la->la_size); i++)
770 la->la_bitmap[i] = 0;
771
772 mlog_exit_void();
773}
774
775#if 0
776/* turn this on and uncomment below to aid debugging window shifts. */
777static void ocfs2_verify_zero_bits(unsigned long *bitmap,
778 unsigned int start,
779 unsigned int count)
780{
781 unsigned int tmp = count;
782 while(tmp--) {
783 if (ocfs2_test_bit(start + tmp, bitmap)) {
784 printk("ocfs2_verify_zero_bits: start = %u, count = "
785 "%u\n", start, count);
786 printk("ocfs2_verify_zero_bits: bit %u is set!",
787 start + tmp);
788 BUG();
789 }
790 }
791}
792#endif
793
794/*
795 * sync the local alloc to main bitmap.
796 *
797 * assumes you've already locked the main bitmap -- the bitmap inode
798 * passed is used for caching.
799 */
800static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700801 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800802 struct ocfs2_dinode *alloc,
803 struct inode *main_bm_inode,
804 struct buffer_head *main_bm_bh)
805{
806 int status = 0;
807 int bit_off, left, count, start;
808 u64 la_start_blk;
809 u64 blkno;
810 void *bitmap;
811 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
812
Jan Kara5a58c3e2007-11-13 19:59:33 +0100813 mlog_entry("total = %u, used = %u\n",
Mark Fashehccd979b2005-12-15 14:31:24 -0800814 le32_to_cpu(alloc->id1.bitmap1.i_total),
Mark Fashehccd979b2005-12-15 14:31:24 -0800815 le32_to_cpu(alloc->id1.bitmap1.i_used));
816
817 if (!alloc->id1.bitmap1.i_total) {
818 mlog(0, "nothing to sync!\n");
819 goto bail;
820 }
821
822 if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
823 le32_to_cpu(alloc->id1.bitmap1.i_total)) {
824 mlog(0, "all bits were taken!\n");
825 goto bail;
826 }
827
828 la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
829 le32_to_cpu(la->la_bm_off));
830 bitmap = la->la_bitmap;
831 start = count = bit_off = 0;
832 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
833
834 while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
835 != -1) {
836 if ((bit_off < left) && (bit_off == start)) {
837 count++;
838 start++;
839 continue;
840 }
841 if (count) {
842 blkno = la_start_blk +
843 ocfs2_clusters_to_blocks(osb->sb,
844 start - count);
845
Mark Fashehb06970532006-03-03 10:24:33 -0800846 mlog(0, "freeing %u bits starting at local alloc bit "
847 "%u (la_start_blk = %llu, blkno = %llu)\n",
848 count, start - count,
849 (unsigned long long)la_start_blk,
850 (unsigned long long)blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800851
Mark Fashehb4414ee2010-03-11 18:31:09 -0800852 status = ocfs2_release_clusters(handle,
853 main_bm_inode,
854 main_bm_bh, blkno,
855 count);
Mark Fashehccd979b2005-12-15 14:31:24 -0800856 if (status < 0) {
857 mlog_errno(status);
858 goto bail;
859 }
860 }
861 if (bit_off >= left)
862 break;
863 count = 1;
864 start = bit_off + 1;
865 }
866
867bail:
868 mlog_exit(status);
869 return status;
870}
871
Mark Fasheh9c7af402008-07-28 18:02:53 -0700872enum ocfs2_la_event {
873 OCFS2_LA_EVENT_SLIDE, /* Normal window slide. */
874 OCFS2_LA_EVENT_FRAGMENTED, /* The global bitmap has
875 * enough bits theoretically
876 * free, but a contiguous
877 * allocation could not be
878 * found. */
879 OCFS2_LA_EVENT_ENOSPC, /* Global bitmap doesn't have
880 * enough bits free to satisfy
881 * our request. */
882};
883#define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
884/*
885 * Given an event, calculate the size of our next local alloc window.
886 *
887 * This should always be called under i_mutex of the local alloc inode
888 * so that local alloc disabling doesn't race with processes trying to
889 * use the allocator.
890 *
891 * Returns the state which the local alloc was left in. This value can
892 * be ignored by some paths.
893 */
894static int ocfs2_recalc_la_window(struct ocfs2_super *osb,
895 enum ocfs2_la_event event)
896{
897 unsigned int bits;
898 int state;
899
900 spin_lock(&osb->osb_lock);
901 if (osb->local_alloc_state == OCFS2_LA_DISABLED) {
902 WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED);
903 goto out_unlock;
904 }
905
906 /*
907 * ENOSPC and fragmentation are treated similarly for now.
908 */
909 if (event == OCFS2_LA_EVENT_ENOSPC ||
910 event == OCFS2_LA_EVENT_FRAGMENTED) {
911 /*
912 * We ran out of contiguous space in the primary
913 * bitmap. Drastically reduce the number of bits used
914 * by local alloc until we have to disable it.
915 */
916 bits = osb->local_alloc_bits >> 1;
917 if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) {
918 /*
919 * By setting state to THROTTLED, we'll keep
920 * the number of local alloc bits used down
921 * until an event occurs which would give us
922 * reason to assume the bitmap situation might
923 * have changed.
924 */
925 osb->local_alloc_state = OCFS2_LA_THROTTLED;
926 osb->local_alloc_bits = bits;
927 } else {
928 osb->local_alloc_state = OCFS2_LA_DISABLED;
929 }
930 queue_delayed_work(ocfs2_wq, &osb->la_enable_wq,
931 OCFS2_LA_ENABLE_INTERVAL);
932 goto out_unlock;
933 }
934
935 /*
936 * Don't increase the size of the local alloc window until we
937 * know we might be able to fulfill the request. Otherwise, we
938 * risk bouncing around the global bitmap during periods of
939 * low space.
940 */
941 if (osb->local_alloc_state != OCFS2_LA_THROTTLED)
942 osb->local_alloc_bits = osb->local_alloc_default_bits;
943
944out_unlock:
945 state = osb->local_alloc_state;
946 spin_unlock(&osb->osb_lock);
947
948 return state;
949}
950
Mark Fashehccd979b2005-12-15 14:31:24 -0800951static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800952 struct ocfs2_alloc_context **ac,
953 struct inode **bitmap_inode,
954 struct buffer_head **bitmap_bh)
955{
956 int status;
957
Robert P. J. Daycd861282006-12-13 00:34:52 -0800958 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800959 if (!(*ac)) {
960 status = -ENOMEM;
961 mlog_errno(status);
962 goto bail;
963 }
964
Mark Fasheh9c7af402008-07-28 18:02:53 -0700965retry_enospc:
Mark Fashehb22b63e2010-03-11 18:43:46 -0800966 (*ac)->ac_bits_wanted = osb->local_alloc_default_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800967 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
Mark Fasheh9c7af402008-07-28 18:02:53 -0700968 if (status == -ENOSPC) {
969 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) ==
970 OCFS2_LA_DISABLED)
971 goto bail;
972
973 ocfs2_free_ac_resource(*ac);
974 memset(*ac, 0, sizeof(struct ocfs2_alloc_context));
975 goto retry_enospc;
976 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800977 if (status < 0) {
Mark Fasheh9c7af402008-07-28 18:02:53 -0700978 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800979 goto bail;
980 }
981
982 *bitmap_inode = (*ac)->ac_inode;
983 igrab(*bitmap_inode);
984 *bitmap_bh = (*ac)->ac_bh;
985 get_bh(*bitmap_bh);
986 status = 0;
987bail:
988 if ((status < 0) && *ac) {
989 ocfs2_free_alloc_context(*ac);
990 *ac = NULL;
991 }
992
993 mlog_exit(status);
994 return status;
995}
996
997/*
998 * pass it the bitmap lock in lock_bh if you have it.
999 */
1000static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001001 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001002 struct ocfs2_alloc_context *ac)
1003{
1004 int status = 0;
1005 u32 cluster_off, cluster_count;
1006 struct ocfs2_dinode *alloc = NULL;
1007 struct ocfs2_local_alloc *la;
1008
1009 mlog_entry_void();
1010
1011 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
1012 la = OCFS2_LOCAL_ALLOC(alloc);
1013
1014 if (alloc->id1.bitmap1.i_total)
1015 mlog(0, "asking me to alloc a new window over a non-empty "
1016 "one\n");
1017
1018 mlog(0, "Allocating %u clusters for a new window.\n",
Mark Fashehebcee4b2008-07-28 14:55:20 -07001019 osb->local_alloc_bits);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001020
1021 /* Instruct the allocation code to try the most recently used
1022 * cluster group. We'll re-record the group used this pass
1023 * below. */
1024 ac->ac_last_group = osb->la_last_gd;
1025
Mark Fashehccd979b2005-12-15 14:31:24 -08001026 /* we used the generic suballoc reserve function, but we set
1027 * everything up nicely, so there's no reason why we can't use
1028 * the more specific cluster api to claim bits. */
Mark Fashehebcee4b2008-07-28 14:55:20 -07001029 status = ocfs2_claim_clusters(osb, handle, ac, osb->local_alloc_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -08001030 &cluster_off, &cluster_count);
Mark Fasheh9c7af402008-07-28 18:02:53 -07001031 if (status == -ENOSPC) {
1032retry_enospc:
1033 /*
1034 * Note: We could also try syncing the journal here to
1035 * allow use of any free bits which the current
1036 * transaction can't give us access to. --Mark
1037 */
1038 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) ==
1039 OCFS2_LA_DISABLED)
1040 goto bail;
1041
Mark Fashehb22b63e2010-03-11 18:43:46 -08001042 ac->ac_bits_wanted = osb->local_alloc_default_bits;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001043 status = ocfs2_claim_clusters(osb, handle, ac,
1044 osb->local_alloc_bits,
1045 &cluster_off,
1046 &cluster_count);
1047 if (status == -ENOSPC)
1048 goto retry_enospc;
1049 /*
1050 * We only shrunk the *minimum* number of in our
1051 * request - it's entirely possible that the allocator
1052 * might give us more than we asked for.
1053 */
1054 if (status == 0) {
1055 spin_lock(&osb->osb_lock);
1056 osb->local_alloc_bits = cluster_count;
1057 spin_unlock(&osb->osb_lock);
1058 }
1059 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001060 if (status < 0) {
1061 if (status != -ENOSPC)
1062 mlog_errno(status);
1063 goto bail;
1064 }
1065
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001066 osb->la_last_gd = ac->ac_last_group;
1067
Mark Fashehccd979b2005-12-15 14:31:24 -08001068 la->la_bm_off = cpu_to_le32(cluster_off);
1069 alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
1070 /* just in case... In the future when we find space ourselves,
1071 * we don't have to get all contiguous -- but we'll have to
1072 * set all previously used bits in bitmap and update
1073 * la_bits_set before setting the bits in the main bitmap. */
1074 alloc->id1.bitmap1.i_used = 0;
1075 memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
1076 le16_to_cpu(la->la_size));
1077
Mark Fashehd02f00c2009-12-07 13:10:48 -08001078 ocfs2_resmap_restart(&osb->osb_la_resmap, cluster_count,
1079 OCFS2_LOCAL_ALLOC(alloc)->la_bitmap);
1080
Mark Fashehccd979b2005-12-15 14:31:24 -08001081 mlog(0, "New window allocated:\n");
1082 mlog(0, "window la_bm_off = %u\n",
1083 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
1084 mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total));
1085
1086bail:
1087 mlog_exit(status);
1088 return status;
1089}
1090
1091/* Note that we do *NOT* lock the local alloc inode here as
1092 * it's been locked already for us. */
1093static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
1094 struct inode *local_alloc_inode)
1095{
1096 int status = 0;
1097 struct buffer_head *main_bm_bh = NULL;
1098 struct inode *main_bm_inode = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001099 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001100 struct ocfs2_dinode *alloc;
1101 struct ocfs2_dinode *alloc_copy = NULL;
1102 struct ocfs2_alloc_context *ac = NULL;
1103
1104 mlog_entry_void();
1105
Mark Fasheh9c7af402008-07-28 18:02:53 -07001106 ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
1107
Mark Fashehccd979b2005-12-15 14:31:24 -08001108 /* This will lock the main bitmap for us. */
1109 status = ocfs2_local_alloc_reserve_for_window(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001110 &ac,
1111 &main_bm_inode,
1112 &main_bm_bh);
1113 if (status < 0) {
1114 if (status != -ENOSPC)
1115 mlog_errno(status);
1116 goto bail;
1117 }
1118
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001119 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001120 if (IS_ERR(handle)) {
1121 status = PTR_ERR(handle);
1122 handle = NULL;
1123 mlog_errno(status);
1124 goto bail;
1125 }
1126
1127 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
1128
1129 /* We want to clear the local alloc before doing anything
1130 * else, so that if we error later during this operation,
1131 * local alloc shutdown won't try to double free main bitmap
1132 * bits. Make a copy so the sync function knows which bits to
1133 * free. */
Sunil Mushran4ba1c5b2008-04-18 15:03:59 -07001134 alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001135 if (!alloc_copy) {
1136 status = -ENOMEM;
1137 mlog_errno(status);
1138 goto bail;
1139 }
1140 memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
1141
Joel Becker0cf2f762009-02-12 16:41:25 -08001142 status = ocfs2_journal_access_di(handle,
1143 INODE_CACHE(local_alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001144 osb->local_alloc_bh,
1145 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001146 if (status < 0) {
1147 mlog_errno(status);
1148 goto bail;
1149 }
1150
1151 ocfs2_clear_local_alloc(alloc);
Joel Beckerec20cec2010-03-19 14:13:52 -07001152 ocfs2_journal_dirty(handle, osb->local_alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001153
1154 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
1155 main_bm_inode, main_bm_bh);
1156 if (status < 0) {
1157 mlog_errno(status);
1158 goto bail;
1159 }
1160
1161 status = ocfs2_local_alloc_new_window(osb, handle, ac);
1162 if (status < 0) {
1163 if (status != -ENOSPC)
1164 mlog_errno(status);
1165 goto bail;
1166 }
1167
1168 atomic_inc(&osb->alloc_stats.moves);
1169
Mark Fashehccd979b2005-12-15 14:31:24 -08001170bail:
1171 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001172 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001173
Mark Fasheha81cb882008-10-07 14:25:16 -07001174 brelse(main_bm_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001175
1176 if (main_bm_inode)
1177 iput(main_bm_inode);
1178
1179 if (alloc_copy)
1180 kfree(alloc_copy);
1181
1182 if (ac)
1183 ocfs2_free_alloc_context(ac);
1184
1185 mlog_exit(status);
1186 return status;
1187}
1188