blob: 7e7dd65d97ef2508400cfcae103c45c5afc39656 [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,
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 Fasheh9c7af402008-07-28 18:02:53 -070077static inline int ocfs2_la_state_enabled(struct ocfs2_super *osb)
78{
79 return (osb->local_alloc_state == OCFS2_LA_THROTTLED ||
80 osb->local_alloc_state == OCFS2_LA_ENABLED);
81}
82
83void ocfs2_local_alloc_seen_free_bits(struct ocfs2_super *osb,
84 unsigned int num_clusters)
85{
86 spin_lock(&osb->osb_lock);
87 if (osb->local_alloc_state == OCFS2_LA_DISABLED ||
88 osb->local_alloc_state == OCFS2_LA_THROTTLED)
89 if (num_clusters >= osb->local_alloc_default_bits) {
90 cancel_delayed_work(&osb->la_enable_wq);
91 osb->local_alloc_state = OCFS2_LA_ENABLED;
92 }
93 spin_unlock(&osb->osb_lock);
94}
95
96void ocfs2_la_enable_worker(struct work_struct *work)
97{
98 struct ocfs2_super *osb =
99 container_of(work, struct ocfs2_super,
100 la_enable_wq.work);
101 spin_lock(&osb->osb_lock);
102 osb->local_alloc_state = OCFS2_LA_ENABLED;
103 spin_unlock(&osb->osb_lock);
104}
105
Mark Fashehccd979b2005-12-15 14:31:24 -0800106/*
107 * Tell us whether a given allocation should use the local alloc
108 * file. Otherwise, it has to go to the main bitmap.
Mark Fasheh9c7af402008-07-28 18:02:53 -0700109 *
110 * This function does semi-dirty reads of local alloc size and state!
111 * This is ok however, as the values are re-checked once under mutex.
Mark Fashehccd979b2005-12-15 14:31:24 -0800112 */
113int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
114{
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800115 int ret = 0;
Mark Fasheh9c7af402008-07-28 18:02:53 -0700116 int la_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800117
Mark Fasheh9c7af402008-07-28 18:02:53 -0700118 spin_lock(&osb->osb_lock);
119 la_bits = osb->local_alloc_bits;
120
121 if (!ocfs2_la_state_enabled(osb))
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800122 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800123
124 /* la_bits should be at least twice the size (in clusters) of
125 * a new block group. We want to be sure block group
126 * allocations go through the local alloc, so allow an
127 * allocation to take up to half the bitmap. */
128 if (bits > (la_bits / 2))
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800129 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800130
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800131 ret = 1;
132bail:
133 mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n",
134 osb->local_alloc_state, (unsigned long long)bits, la_bits, ret);
Mark Fasheh9c7af402008-07-28 18:02:53 -0700135 spin_unlock(&osb->osb_lock);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800136 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -0800137}
138
139int ocfs2_load_local_alloc(struct ocfs2_super *osb)
140{
141 int status = 0;
142 struct ocfs2_dinode *alloc = NULL;
143 struct buffer_head *alloc_bh = NULL;
144 u32 num_used;
145 struct inode *inode = NULL;
146 struct ocfs2_local_alloc *la;
147
148 mlog_entry_void();
149
Mark Fashehebcee4b2008-07-28 14:55:20 -0700150 if (osb->local_alloc_bits == 0)
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800151 goto bail;
152
Mark Fashehebcee4b2008-07-28 14:55:20 -0700153 if (osb->local_alloc_bits >= osb->bitmap_cpg) {
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800154 mlog(ML_NOTICE, "Requested local alloc window %d is larger "
155 "than max possible %u. Using defaults.\n",
Mark Fashehebcee4b2008-07-28 14:55:20 -0700156 osb->local_alloc_bits, (osb->bitmap_cpg - 1));
157 osb->local_alloc_bits =
158 ocfs2_megabytes_to_clusters(osb->sb,
159 OCFS2_DEFAULT_LOCAL_ALLOC_SIZE);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800160 }
161
Mark Fashehccd979b2005-12-15 14:31:24 -0800162 /* read the alloc off disk */
163 inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
164 osb->slot_num);
165 if (!inode) {
166 status = -EINVAL;
167 mlog_errno(status);
168 goto bail;
169 }
170
Joel Beckerb657c952008-11-13 14:49:11 -0800171 status = ocfs2_read_inode_block_full(inode, &alloc_bh,
172 OCFS2_BH_IGNORE_CACHE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800173 if (status < 0) {
174 mlog_errno(status);
175 goto bail;
176 }
177
178 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
179 la = OCFS2_LOCAL_ALLOC(alloc);
180
181 if (!(le32_to_cpu(alloc->i_flags) &
182 (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
Mark Fashehb06970532006-03-03 10:24:33 -0800183 mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
184 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800185 status = -EINVAL;
186 goto bail;
187 }
188
189 if ((la->la_size == 0) ||
190 (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) {
191 mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n",
192 le16_to_cpu(la->la_size));
193 status = -EINVAL;
194 goto bail;
195 }
196
197 /* do a little verification. */
198 num_used = ocfs2_local_alloc_count_bits(alloc);
199
200 /* hopefully the local alloc has always been recovered before
201 * we load it. */
202 if (num_used
203 || alloc->id1.bitmap1.i_used
204 || alloc->id1.bitmap1.i_total
205 || la->la_bm_off)
206 mlog(ML_ERROR, "Local alloc hasn't been recovered!\n"
207 "found = %u, set = %u, taken = %u, off = %u\n",
208 num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
209 le32_to_cpu(alloc->id1.bitmap1.i_total),
210 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
211
212 osb->local_alloc_bh = alloc_bh;
213 osb->local_alloc_state = OCFS2_LA_ENABLED;
214
215bail:
216 if (status < 0)
Mark Fasheha81cb882008-10-07 14:25:16 -0700217 brelse(alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800218 if (inode)
219 iput(inode);
220
Mark Fashehebcee4b2008-07-28 14:55:20 -0700221 mlog(0, "Local alloc window bits = %d\n", osb->local_alloc_bits);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800222
Mark Fashehccd979b2005-12-15 14:31:24 -0800223 mlog_exit(status);
224 return status;
225}
226
227/*
228 * return any unused bits to the bitmap and write out a clean
229 * local_alloc.
230 *
231 * local_alloc_bh is optional. If not passed, we will simply use the
232 * one off osb. If you do pass it however, be warned that it *will* be
233 * returned brelse'd and NULL'd out.*/
234void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
235{
236 int status;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700237 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800238 struct inode *local_alloc_inode = NULL;
239 struct buffer_head *bh = NULL;
240 struct buffer_head *main_bm_bh = NULL;
241 struct inode *main_bm_inode = NULL;
242 struct ocfs2_dinode *alloc_copy = NULL;
243 struct ocfs2_dinode *alloc = NULL;
244
245 mlog_entry_void();
246
Mark Fasheh9c7af402008-07-28 18:02:53 -0700247 cancel_delayed_work(&osb->la_enable_wq);
248 flush_workqueue(ocfs2_wq);
249
Mark Fashehccd979b2005-12-15 14:31:24 -0800250 if (osb->local_alloc_state == OCFS2_LA_UNUSED)
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700251 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800252
253 local_alloc_inode =
254 ocfs2_get_system_file_inode(osb,
255 LOCAL_ALLOC_SYSTEM_INODE,
256 osb->slot_num);
257 if (!local_alloc_inode) {
258 status = -ENOENT;
259 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700260 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800261 }
262
263 osb->local_alloc_state = OCFS2_LA_DISABLED;
264
Mark Fashehccd979b2005-12-15 14:31:24 -0800265 main_bm_inode = ocfs2_get_system_file_inode(osb,
266 GLOBAL_BITMAP_SYSTEM_INODE,
267 OCFS2_INVALID_SLOT);
268 if (!main_bm_inode) {
269 status = -EINVAL;
270 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700271 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800272 }
273
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700274 mutex_lock(&main_bm_inode->i_mutex);
275
Mark Fashehe63aecb62007-10-18 15:30:42 -0700276 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800277 if (status < 0) {
278 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700279 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -0800280 }
281
282 /* WINDOW_MOVE_CREDITS is a bit heavy... */
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700283 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800284 if (IS_ERR(handle)) {
285 mlog_errno(PTR_ERR(handle));
286 handle = NULL;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700287 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800288 }
289
290 bh = osb->local_alloc_bh;
291 alloc = (struct ocfs2_dinode *) bh->b_data;
292
Sunil Mushran4ba1c5b2008-04-18 15:03:59 -0700293 alloc_copy = kmalloc(bh->b_size, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800294 if (!alloc_copy) {
295 status = -ENOMEM;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700296 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800297 }
298 memcpy(alloc_copy, alloc, bh->b_size);
299
Joel Becker0cf2f762009-02-12 16:41:25 -0800300 status = ocfs2_journal_access_di(handle, INODE_CACHE(local_alloc_inode),
301 bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800302 if (status < 0) {
303 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700304 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800305 }
306
307 ocfs2_clear_local_alloc(alloc);
Joel Beckerec20cec2010-03-19 14:13:52 -0700308 ocfs2_journal_dirty(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800309
310 brelse(bh);
311 osb->local_alloc_bh = NULL;
312 osb->local_alloc_state = OCFS2_LA_UNUSED;
313
314 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
315 main_bm_inode, main_bm_bh);
316 if (status < 0)
317 mlog_errno(status);
318
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700319out_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700320 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800321
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700322out_unlock:
Mark Fasheha81cb882008-10-07 14:25:16 -0700323 brelse(main_bm_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800324
Mark Fashehe63aecb62007-10-18 15:30:42 -0700325 ocfs2_inode_unlock(main_bm_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800326
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700327out_mutex:
328 mutex_unlock(&main_bm_inode->i_mutex);
329 iput(main_bm_inode);
330
331out:
Mark Fashehccd979b2005-12-15 14:31:24 -0800332 if (local_alloc_inode)
333 iput(local_alloc_inode);
334
335 if (alloc_copy)
336 kfree(alloc_copy);
337
338 mlog_exit_void();
339}
340
341/*
342 * We want to free the bitmap bits outside of any recovery context as
343 * we'll need a cluster lock to do so, but we must clear the local
344 * alloc before giving up the recovered nodes journal. To solve this,
345 * we kmalloc a copy of the local alloc before it's change for the
346 * caller to process with ocfs2_complete_local_alloc_recovery
347 */
348int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
349 int slot_num,
350 struct ocfs2_dinode **alloc_copy)
351{
352 int status = 0;
353 struct buffer_head *alloc_bh = NULL;
354 struct inode *inode = NULL;
355 struct ocfs2_dinode *alloc;
356
357 mlog_entry("(slot_num = %d)\n", slot_num);
358
359 *alloc_copy = NULL;
360
361 inode = ocfs2_get_system_file_inode(osb,
362 LOCAL_ALLOC_SYSTEM_INODE,
363 slot_num);
364 if (!inode) {
365 status = -EINVAL;
366 mlog_errno(status);
367 goto bail;
368 }
369
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800370 mutex_lock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800371
Joel Beckerb657c952008-11-13 14:49:11 -0800372 status = ocfs2_read_inode_block_full(inode, &alloc_bh,
373 OCFS2_BH_IGNORE_CACHE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800374 if (status < 0) {
375 mlog_errno(status);
376 goto bail;
377 }
378
379 *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL);
380 if (!(*alloc_copy)) {
381 status = -ENOMEM;
382 goto bail;
383 }
384 memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
385
386 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
387 ocfs2_clear_local_alloc(alloc);
388
Joel Becker13723d02008-10-17 19:25:01 -0700389 ocfs2_compute_meta_ecc(osb->sb, alloc_bh->b_data, &alloc->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -0800390 status = ocfs2_write_block(osb, alloc_bh, INODE_CACHE(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -0800391 if (status < 0)
392 mlog_errno(status);
393
394bail:
395 if ((status < 0) && (*alloc_copy)) {
396 kfree(*alloc_copy);
397 *alloc_copy = NULL;
398 }
399
Mark Fasheha81cb882008-10-07 14:25:16 -0700400 brelse(alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800401
402 if (inode) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800403 mutex_unlock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800404 iput(inode);
405 }
406
407 mlog_exit(status);
408 return status;
409}
410
411/*
412 * Step 2: By now, we've completed the journal recovery, we've stamped
413 * a clean local alloc on disk and dropped the node out of the
414 * recovery map. Dlm locks will no longer stall, so lets clear out the
415 * main bitmap.
416 */
417int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
418 struct ocfs2_dinode *alloc)
419{
420 int status;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700421 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800422 struct buffer_head *main_bm_bh = NULL;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700423 struct inode *main_bm_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800424
425 mlog_entry_void();
426
Mark Fashehccd979b2005-12-15 14:31:24 -0800427 main_bm_inode = ocfs2_get_system_file_inode(osb,
428 GLOBAL_BITMAP_SYSTEM_INODE,
429 OCFS2_INVALID_SLOT);
430 if (!main_bm_inode) {
431 status = -EINVAL;
432 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700433 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800434 }
435
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700436 mutex_lock(&main_bm_inode->i_mutex);
437
Mark Fashehe63aecb62007-10-18 15:30:42 -0700438 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800439 if (status < 0) {
440 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700441 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -0800442 }
443
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700444 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800445 if (IS_ERR(handle)) {
446 status = PTR_ERR(handle);
447 handle = NULL;
448 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700449 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800450 }
451
452 /* we want the bitmap change to be recorded on disk asap */
Mark Fasheh1fabe142006-10-09 18:11:45 -0700453 handle->h_sync = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800454
455 status = ocfs2_sync_local_to_main(osb, handle, alloc,
456 main_bm_inode, main_bm_bh);
457 if (status < 0)
458 mlog_errno(status);
459
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700460 ocfs2_commit_trans(osb, handle);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700461
462out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700463 ocfs2_inode_unlock(main_bm_inode, 1);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700464
465out_mutex:
466 mutex_unlock(&main_bm_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800467
Mark Fasheha81cb882008-10-07 14:25:16 -0700468 brelse(main_bm_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800469
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700470 iput(main_bm_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800471
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700472out:
Tao Ma4d0ddb22008-03-05 16:11:46 +0800473 if (!status)
Tiger Yangb89c5422010-01-25 14:11:06 +0800474 ocfs2_init_steal_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800475 mlog_exit(status);
476 return status;
477}
478
Joel Becker1187c962008-09-03 20:03:39 -0700479/* Check to see if the local alloc window is within ac->ac_max_block */
480static int ocfs2_local_alloc_in_range(struct inode *inode,
481 struct ocfs2_alloc_context *ac,
482 u32 bits_wanted)
483{
484 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
485 struct ocfs2_dinode *alloc;
486 struct ocfs2_local_alloc *la;
487 int start;
488 u64 block_off;
489
490 if (!ac->ac_max_block)
491 return 1;
492
493 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
494 la = OCFS2_LOCAL_ALLOC(alloc);
495
496 start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted);
497 if (start == -1) {
498 mlog_errno(-ENOSPC);
499 return 0;
500 }
501
502 /*
503 * Converting (bm_off + start + bits_wanted) to blocks gives us
504 * the blkno just past our actual allocation. This is perfect
505 * to compare with ac_max_block.
506 */
507 block_off = ocfs2_clusters_to_blocks(inode->i_sb,
508 le32_to_cpu(la->la_bm_off) +
509 start + bits_wanted);
510 mlog(0, "Checking %llu against %llu\n",
511 (unsigned long long)block_off,
512 (unsigned long long)ac->ac_max_block);
513 if (block_off > ac->ac_max_block)
514 return 0;
515
516 return 1;
517}
518
Mark Fashehccd979b2005-12-15 14:31:24 -0800519/*
Mark Fasheh9c7af402008-07-28 18:02:53 -0700520 * make sure we've got at least bits_wanted contiguous bits in the
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800521 * local alloc. You lose them when you drop i_mutex.
Mark Fashehccd979b2005-12-15 14:31:24 -0800522 *
523 * We will add ourselves to the transaction passed in, but may start
524 * our own in order to shift windows.
525 */
526int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800527 u32 bits_wanted,
528 struct ocfs2_alloc_context *ac)
529{
530 int status;
531 struct ocfs2_dinode *alloc;
532 struct inode *local_alloc_inode;
533 unsigned int free_bits;
534
535 mlog_entry_void();
536
Mark Fashehccd979b2005-12-15 14:31:24 -0800537 BUG_ON(!ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800538
539 local_alloc_inode =
540 ocfs2_get_system_file_inode(osb,
541 LOCAL_ALLOC_SYSTEM_INODE,
542 osb->slot_num);
543 if (!local_alloc_inode) {
544 status = -ENOENT;
545 mlog_errno(status);
546 goto bail;
547 }
Mark Fashehda5cbf22006-10-06 18:34:35 -0700548
549 mutex_lock(&local_alloc_inode->i_mutex);
550
Mark Fasheh9c7af402008-07-28 18:02:53 -0700551 /*
552 * We must double check state and allocator bits because
553 * another process may have changed them while holding i_mutex.
554 */
555 spin_lock(&osb->osb_lock);
556 if (!ocfs2_la_state_enabled(osb) ||
557 (bits_wanted > osb->local_alloc_bits)) {
558 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800559 status = -ENOSPC;
560 goto bail;
561 }
Mark Fasheh9c7af402008-07-28 18:02:53 -0700562 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800563
564 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
565
Joel Beckere407e392008-06-12 22:35:39 -0700566#ifdef CONFIG_OCFS2_DEBUG_FS
Mark Fashehccd979b2005-12-15 14:31:24 -0800567 if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
568 ocfs2_local_alloc_count_bits(alloc)) {
Mark Fashehb06970532006-03-03 10:24:33 -0800569 ocfs2_error(osb->sb, "local alloc inode %llu says it has "
Mark Fashehccd979b2005-12-15 14:31:24 -0800570 "%u free bits, but a count shows %u",
Mark Fashehb06970532006-03-03 10:24:33 -0800571 (unsigned long long)le64_to_cpu(alloc->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -0800572 le32_to_cpu(alloc->id1.bitmap1.i_used),
573 ocfs2_local_alloc_count_bits(alloc));
574 status = -EIO;
575 goto bail;
576 }
Jan Kara5a58c3e2007-11-13 19:59:33 +0100577#endif
Mark Fashehccd979b2005-12-15 14:31:24 -0800578
579 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
580 le32_to_cpu(alloc->id1.bitmap1.i_used);
581 if (bits_wanted > free_bits) {
582 /* uhoh, window change time. */
583 status =
584 ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
585 if (status < 0) {
586 if (status != -ENOSPC)
587 mlog_errno(status);
588 goto bail;
589 }
Mark Fasheh9c7af402008-07-28 18:02:53 -0700590
591 /*
592 * Under certain conditions, the window slide code
593 * might have reduced the number of bits available or
594 * disabled the the local alloc entirely. Re-check
595 * here and return -ENOSPC if necessary.
596 */
597 status = -ENOSPC;
598 if (!ocfs2_la_state_enabled(osb))
599 goto bail;
600
601 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
602 le32_to_cpu(alloc->id1.bitmap1.i_used);
603 if (bits_wanted > free_bits)
604 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800605 }
606
Joel Becker1187c962008-09-03 20:03:39 -0700607 if (ac->ac_max_block)
608 mlog(0, "Calling in_range for max block %llu\n",
609 (unsigned long long)ac->ac_max_block);
610
611 if (!ocfs2_local_alloc_in_range(local_alloc_inode, ac,
612 bits_wanted)) {
613 /*
614 * The window is outside ac->ac_max_block.
615 * This errno tells the caller to keep localalloc enabled
616 * but to get the allocation from the main bitmap.
617 */
618 status = -EFBIG;
619 goto bail;
620 }
621
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700622 ac->ac_inode = local_alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800623 /* We should never use localalloc from another slot */
624 ac->ac_alloc_slot = osb->slot_num;
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700625 ac->ac_which = OCFS2_AC_USE_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800626 get_bh(osb->local_alloc_bh);
627 ac->ac_bh = osb->local_alloc_bh;
Mark Fashehccd979b2005-12-15 14:31:24 -0800628 status = 0;
629bail:
Sunil Mushranbda02332007-09-21 11:41:43 -0700630 if (status < 0 && local_alloc_inode) {
631 mutex_unlock(&local_alloc_inode->i_mutex);
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700632 iput(local_alloc_inode);
Sunil Mushranbda02332007-09-21 11:41:43 -0700633 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800634
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800635 mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num,
636 status);
637
Mark Fashehccd979b2005-12-15 14:31:24 -0800638 mlog_exit(status);
639 return status;
640}
641
642int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700643 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800644 struct ocfs2_alloc_context *ac,
Mark Fasheh415cb802007-09-16 20:10:16 -0700645 u32 bits_wanted,
Mark Fashehccd979b2005-12-15 14:31:24 -0800646 u32 *bit_off,
647 u32 *num_bits)
648{
649 int status, start;
650 struct inode *local_alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800651 void *bitmap;
652 struct ocfs2_dinode *alloc;
653 struct ocfs2_local_alloc *la;
654
655 mlog_entry_void();
656 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
657
Mark Fashehccd979b2005-12-15 14:31:24 -0800658 local_alloc_inode = ac->ac_inode;
659 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
660 la = OCFS2_LOCAL_ALLOC(alloc);
661
662 start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted);
663 if (start == -1) {
664 /* TODO: Shouldn't we just BUG here? */
665 status = -ENOSPC;
666 mlog_errno(status);
667 goto bail;
668 }
669
670 bitmap = la->la_bitmap;
671 *bit_off = le32_to_cpu(la->la_bm_off) + start;
672 /* local alloc is always contiguous by nature -- we never
673 * delete bits from it! */
674 *num_bits = bits_wanted;
675
Joel Becker0cf2f762009-02-12 16:41:25 -0800676 status = ocfs2_journal_access_di(handle,
677 INODE_CACHE(local_alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700678 osb->local_alloc_bh,
679 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800680 if (status < 0) {
681 mlog_errno(status);
682 goto bail;
683 }
684
685 while(bits_wanted--)
686 ocfs2_set_bit(start++, bitmap);
687
Marcin Slusarz0dd32562008-02-13 00:06:18 +0100688 le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits);
Joel Beckerec20cec2010-03-19 14:13:52 -0700689 ocfs2_journal_dirty(handle, osb->local_alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800690
Mark Fashehccd979b2005-12-15 14:31:24 -0800691bail:
692 mlog_exit(status);
693 return status;
694}
695
696static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
697{
698 int i;
699 u8 *buffer;
700 u32 count = 0;
701 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
702
703 mlog_entry_void();
704
705 buffer = la->la_bitmap;
706 for (i = 0; i < le16_to_cpu(la->la_size); i++)
707 count += hweight8(buffer[i]);
708
709 mlog_exit(count);
710 return count;
711}
712
713static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
714 struct ocfs2_dinode *alloc,
715 u32 numbits)
716{
717 int numfound, bitoff, left, startoff, lastzero;
718 void *bitmap = NULL;
719
720 mlog_entry("(numbits wanted = %u)\n", numbits);
721
722 if (!alloc->id1.bitmap1.i_total) {
723 mlog(0, "No bits in my window!\n");
724 bitoff = -1;
725 goto bail;
726 }
727
728 bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
729
730 numfound = bitoff = startoff = 0;
731 lastzero = -1;
732 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
733 while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
734 if (bitoff == left) {
735 /* mlog(0, "bitoff (%d) == left", bitoff); */
736 break;
737 }
738 /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
739 "numfound = %d\n", bitoff, startoff, numfound);*/
740
741 /* Ok, we found a zero bit... is it contig. or do we
742 * start over?*/
743 if (bitoff == startoff) {
744 /* we found a zero */
745 numfound++;
746 startoff++;
747 } else {
748 /* got a zero after some ones */
749 numfound = 1;
750 startoff = bitoff+1;
751 }
752 /* we got everything we needed */
753 if (numfound == numbits) {
754 /* mlog(0, "Found it all!\n"); */
755 break;
756 }
757 }
758
759 mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff,
760 numfound);
761
762 if (numfound == numbits)
763 bitoff = startoff - numfound;
764 else
765 bitoff = -1;
766
767bail:
768 mlog_exit(bitoff);
769 return bitoff;
770}
771
772static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
773{
774 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
775 int i;
776 mlog_entry_void();
777
778 alloc->id1.bitmap1.i_total = 0;
779 alloc->id1.bitmap1.i_used = 0;
780 la->la_bm_off = 0;
781 for(i = 0; i < le16_to_cpu(la->la_size); i++)
782 la->la_bitmap[i] = 0;
783
784 mlog_exit_void();
785}
786
787#if 0
788/* turn this on and uncomment below to aid debugging window shifts. */
789static void ocfs2_verify_zero_bits(unsigned long *bitmap,
790 unsigned int start,
791 unsigned int count)
792{
793 unsigned int tmp = count;
794 while(tmp--) {
795 if (ocfs2_test_bit(start + tmp, bitmap)) {
796 printk("ocfs2_verify_zero_bits: start = %u, count = "
797 "%u\n", start, count);
798 printk("ocfs2_verify_zero_bits: bit %u is set!",
799 start + tmp);
800 BUG();
801 }
802 }
803}
804#endif
805
806/*
807 * sync the local alloc to main bitmap.
808 *
809 * assumes you've already locked the main bitmap -- the bitmap inode
810 * passed is used for caching.
811 */
812static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700813 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800814 struct ocfs2_dinode *alloc,
815 struct inode *main_bm_inode,
816 struct buffer_head *main_bm_bh)
817{
818 int status = 0;
819 int bit_off, left, count, start;
820 u64 la_start_blk;
821 u64 blkno;
822 void *bitmap;
823 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
824
Jan Kara5a58c3e2007-11-13 19:59:33 +0100825 mlog_entry("total = %u, used = %u\n",
Mark Fashehccd979b2005-12-15 14:31:24 -0800826 le32_to_cpu(alloc->id1.bitmap1.i_total),
Mark Fashehccd979b2005-12-15 14:31:24 -0800827 le32_to_cpu(alloc->id1.bitmap1.i_used));
828
829 if (!alloc->id1.bitmap1.i_total) {
830 mlog(0, "nothing to sync!\n");
831 goto bail;
832 }
833
834 if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
835 le32_to_cpu(alloc->id1.bitmap1.i_total)) {
836 mlog(0, "all bits were taken!\n");
837 goto bail;
838 }
839
840 la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
841 le32_to_cpu(la->la_bm_off));
842 bitmap = la->la_bitmap;
843 start = count = bit_off = 0;
844 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
845
846 while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
847 != -1) {
848 if ((bit_off < left) && (bit_off == start)) {
849 count++;
850 start++;
851 continue;
852 }
853 if (count) {
854 blkno = la_start_blk +
855 ocfs2_clusters_to_blocks(osb->sb,
856 start - count);
857
Mark Fashehb06970532006-03-03 10:24:33 -0800858 mlog(0, "freeing %u bits starting at local alloc bit "
859 "%u (la_start_blk = %llu, blkno = %llu)\n",
860 count, start - count,
861 (unsigned long long)la_start_blk,
862 (unsigned long long)blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800863
Mark Fashehb4414ee2010-03-11 18:31:09 -0800864 status = ocfs2_release_clusters(handle,
865 main_bm_inode,
866 main_bm_bh, blkno,
867 count);
Mark Fashehccd979b2005-12-15 14:31:24 -0800868 if (status < 0) {
869 mlog_errno(status);
870 goto bail;
871 }
872 }
873 if (bit_off >= left)
874 break;
875 count = 1;
876 start = bit_off + 1;
877 }
878
879bail:
880 mlog_exit(status);
881 return status;
882}
883
Mark Fasheh9c7af402008-07-28 18:02:53 -0700884enum ocfs2_la_event {
885 OCFS2_LA_EVENT_SLIDE, /* Normal window slide. */
886 OCFS2_LA_EVENT_FRAGMENTED, /* The global bitmap has
887 * enough bits theoretically
888 * free, but a contiguous
889 * allocation could not be
890 * found. */
891 OCFS2_LA_EVENT_ENOSPC, /* Global bitmap doesn't have
892 * enough bits free to satisfy
893 * our request. */
894};
895#define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
896/*
897 * Given an event, calculate the size of our next local alloc window.
898 *
899 * This should always be called under i_mutex of the local alloc inode
900 * so that local alloc disabling doesn't race with processes trying to
901 * use the allocator.
902 *
903 * Returns the state which the local alloc was left in. This value can
904 * be ignored by some paths.
905 */
906static int ocfs2_recalc_la_window(struct ocfs2_super *osb,
907 enum ocfs2_la_event event)
908{
909 unsigned int bits;
910 int state;
911
912 spin_lock(&osb->osb_lock);
913 if (osb->local_alloc_state == OCFS2_LA_DISABLED) {
914 WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED);
915 goto out_unlock;
916 }
917
918 /*
919 * ENOSPC and fragmentation are treated similarly for now.
920 */
921 if (event == OCFS2_LA_EVENT_ENOSPC ||
922 event == OCFS2_LA_EVENT_FRAGMENTED) {
923 /*
924 * We ran out of contiguous space in the primary
925 * bitmap. Drastically reduce the number of bits used
926 * by local alloc until we have to disable it.
927 */
928 bits = osb->local_alloc_bits >> 1;
929 if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) {
930 /*
931 * By setting state to THROTTLED, we'll keep
932 * the number of local alloc bits used down
933 * until an event occurs which would give us
934 * reason to assume the bitmap situation might
935 * have changed.
936 */
937 osb->local_alloc_state = OCFS2_LA_THROTTLED;
938 osb->local_alloc_bits = bits;
939 } else {
940 osb->local_alloc_state = OCFS2_LA_DISABLED;
941 }
942 queue_delayed_work(ocfs2_wq, &osb->la_enable_wq,
943 OCFS2_LA_ENABLE_INTERVAL);
944 goto out_unlock;
945 }
946
947 /*
948 * Don't increase the size of the local alloc window until we
949 * know we might be able to fulfill the request. Otherwise, we
950 * risk bouncing around the global bitmap during periods of
951 * low space.
952 */
953 if (osb->local_alloc_state != OCFS2_LA_THROTTLED)
954 osb->local_alloc_bits = osb->local_alloc_default_bits;
955
956out_unlock:
957 state = osb->local_alloc_state;
958 spin_unlock(&osb->osb_lock);
959
960 return state;
961}
962
Mark Fashehccd979b2005-12-15 14:31:24 -0800963static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800964 struct ocfs2_alloc_context **ac,
965 struct inode **bitmap_inode,
966 struct buffer_head **bitmap_bh)
967{
968 int status;
969
Robert P. J. Daycd861282006-12-13 00:34:52 -0800970 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800971 if (!(*ac)) {
972 status = -ENOMEM;
973 mlog_errno(status);
974 goto bail;
975 }
976
Mark Fasheh9c7af402008-07-28 18:02:53 -0700977retry_enospc:
Mark Fashehb22b63e2010-03-11 18:43:46 -0800978 (*ac)->ac_bits_wanted = osb->local_alloc_default_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800979 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
Mark Fasheh9c7af402008-07-28 18:02:53 -0700980 if (status == -ENOSPC) {
981 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) ==
982 OCFS2_LA_DISABLED)
983 goto bail;
984
985 ocfs2_free_ac_resource(*ac);
986 memset(*ac, 0, sizeof(struct ocfs2_alloc_context));
987 goto retry_enospc;
988 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800989 if (status < 0) {
Mark Fasheh9c7af402008-07-28 18:02:53 -0700990 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800991 goto bail;
992 }
993
994 *bitmap_inode = (*ac)->ac_inode;
995 igrab(*bitmap_inode);
996 *bitmap_bh = (*ac)->ac_bh;
997 get_bh(*bitmap_bh);
998 status = 0;
999bail:
1000 if ((status < 0) && *ac) {
1001 ocfs2_free_alloc_context(*ac);
1002 *ac = NULL;
1003 }
1004
1005 mlog_exit(status);
1006 return status;
1007}
1008
1009/*
1010 * pass it the bitmap lock in lock_bh if you have it.
1011 */
1012static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001013 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001014 struct ocfs2_alloc_context *ac)
1015{
1016 int status = 0;
1017 u32 cluster_off, cluster_count;
1018 struct ocfs2_dinode *alloc = NULL;
1019 struct ocfs2_local_alloc *la;
1020
1021 mlog_entry_void();
1022
1023 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
1024 la = OCFS2_LOCAL_ALLOC(alloc);
1025
1026 if (alloc->id1.bitmap1.i_total)
1027 mlog(0, "asking me to alloc a new window over a non-empty "
1028 "one\n");
1029
1030 mlog(0, "Allocating %u clusters for a new window.\n",
Mark Fashehebcee4b2008-07-28 14:55:20 -07001031 osb->local_alloc_bits);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001032
1033 /* Instruct the allocation code to try the most recently used
1034 * cluster group. We'll re-record the group used this pass
1035 * below. */
1036 ac->ac_last_group = osb->la_last_gd;
1037
Mark Fashehccd979b2005-12-15 14:31:24 -08001038 /* we used the generic suballoc reserve function, but we set
1039 * everything up nicely, so there's no reason why we can't use
1040 * the more specific cluster api to claim bits. */
Mark Fashehebcee4b2008-07-28 14:55:20 -07001041 status = ocfs2_claim_clusters(osb, handle, ac, osb->local_alloc_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -08001042 &cluster_off, &cluster_count);
Mark Fasheh9c7af402008-07-28 18:02:53 -07001043 if (status == -ENOSPC) {
1044retry_enospc:
1045 /*
1046 * Note: We could also try syncing the journal here to
1047 * allow use of any free bits which the current
1048 * transaction can't give us access to. --Mark
1049 */
1050 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) ==
1051 OCFS2_LA_DISABLED)
1052 goto bail;
1053
Mark Fashehb22b63e2010-03-11 18:43:46 -08001054 ac->ac_bits_wanted = osb->local_alloc_default_bits;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001055 status = ocfs2_claim_clusters(osb, handle, ac,
1056 osb->local_alloc_bits,
1057 &cluster_off,
1058 &cluster_count);
1059 if (status == -ENOSPC)
1060 goto retry_enospc;
1061 /*
1062 * We only shrunk the *minimum* number of in our
1063 * request - it's entirely possible that the allocator
1064 * might give us more than we asked for.
1065 */
1066 if (status == 0) {
1067 spin_lock(&osb->osb_lock);
1068 osb->local_alloc_bits = cluster_count;
1069 spin_unlock(&osb->osb_lock);
1070 }
1071 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001072 if (status < 0) {
1073 if (status != -ENOSPC)
1074 mlog_errno(status);
1075 goto bail;
1076 }
1077
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001078 osb->la_last_gd = ac->ac_last_group;
1079
Mark Fashehccd979b2005-12-15 14:31:24 -08001080 la->la_bm_off = cpu_to_le32(cluster_off);
1081 alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
1082 /* just in case... In the future when we find space ourselves,
1083 * we don't have to get all contiguous -- but we'll have to
1084 * set all previously used bits in bitmap and update
1085 * la_bits_set before setting the bits in the main bitmap. */
1086 alloc->id1.bitmap1.i_used = 0;
1087 memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
1088 le16_to_cpu(la->la_size));
1089
1090 mlog(0, "New window allocated:\n");
1091 mlog(0, "window la_bm_off = %u\n",
1092 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
1093 mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total));
1094
1095bail:
1096 mlog_exit(status);
1097 return status;
1098}
1099
1100/* Note that we do *NOT* lock the local alloc inode here as
1101 * it's been locked already for us. */
1102static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
1103 struct inode *local_alloc_inode)
1104{
1105 int status = 0;
1106 struct buffer_head *main_bm_bh = NULL;
1107 struct inode *main_bm_inode = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001108 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001109 struct ocfs2_dinode *alloc;
1110 struct ocfs2_dinode *alloc_copy = NULL;
1111 struct ocfs2_alloc_context *ac = NULL;
1112
1113 mlog_entry_void();
1114
Mark Fasheh9c7af402008-07-28 18:02:53 -07001115 ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
1116
Mark Fashehccd979b2005-12-15 14:31:24 -08001117 /* This will lock the main bitmap for us. */
1118 status = ocfs2_local_alloc_reserve_for_window(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001119 &ac,
1120 &main_bm_inode,
1121 &main_bm_bh);
1122 if (status < 0) {
1123 if (status != -ENOSPC)
1124 mlog_errno(status);
1125 goto bail;
1126 }
1127
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001128 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001129 if (IS_ERR(handle)) {
1130 status = PTR_ERR(handle);
1131 handle = NULL;
1132 mlog_errno(status);
1133 goto bail;
1134 }
1135
1136 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
1137
1138 /* We want to clear the local alloc before doing anything
1139 * else, so that if we error later during this operation,
1140 * local alloc shutdown won't try to double free main bitmap
1141 * bits. Make a copy so the sync function knows which bits to
1142 * free. */
Sunil Mushran4ba1c5b2008-04-18 15:03:59 -07001143 alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001144 if (!alloc_copy) {
1145 status = -ENOMEM;
1146 mlog_errno(status);
1147 goto bail;
1148 }
1149 memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
1150
Joel Becker0cf2f762009-02-12 16:41:25 -08001151 status = ocfs2_journal_access_di(handle,
1152 INODE_CACHE(local_alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001153 osb->local_alloc_bh,
1154 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001155 if (status < 0) {
1156 mlog_errno(status);
1157 goto bail;
1158 }
1159
1160 ocfs2_clear_local_alloc(alloc);
Joel Beckerec20cec2010-03-19 14:13:52 -07001161 ocfs2_journal_dirty(handle, osb->local_alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001162
1163 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
1164 main_bm_inode, main_bm_bh);
1165 if (status < 0) {
1166 mlog_errno(status);
1167 goto bail;
1168 }
1169
1170 status = ocfs2_local_alloc_new_window(osb, handle, ac);
1171 if (status < 0) {
1172 if (status != -ENOSPC)
1173 mlog_errno(status);
1174 goto bail;
1175 }
1176
1177 atomic_inc(&osb->alloc_stats.moves);
1178
Mark Fashehccd979b2005-12-15 14:31:24 -08001179bail:
1180 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001181 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001182
Mark Fasheha81cb882008-10-07 14:25:16 -07001183 brelse(main_bm_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001184
1185 if (main_bm_inode)
1186 iput(main_bm_inode);
1187
1188 if (alloc_copy)
1189 kfree(alloc_copy);
1190
1191 if (ac)
1192 ocfs2_free_alloc_context(ac);
1193
1194 mlog_exit(status);
1195 return status;
1196}
1197