blob: e39a3e7146c98d9ad93fc278bd442f7979ed6c61 [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 Fasheh73c8a802010-04-05 18:17:13 -070078void ocfs2_la_set_sizes(struct ocfs2_super *osb, int requested_mb)
79{
80 struct super_block *sb = osb->sb;
81 unsigned int la_default_mb = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE;
82 unsigned int la_max_mb;
83
84 la_max_mb = ocfs2_clusters_to_megabytes(sb,
85 ocfs2_local_alloc_size(sb) * 8);
86
87 mlog(0, "requested: %dM, max: %uM, default: %uM\n",
88 requested_mb, la_max_mb, la_default_mb);
89
90 if (requested_mb == -1) {
91 /* No user request - use defaults */
92 osb->local_alloc_default_bits =
93 ocfs2_megabytes_to_clusters(sb, la_default_mb);
94 } else if (requested_mb > la_max_mb) {
95 /* Request is too big, we give the maximum available */
96 osb->local_alloc_default_bits =
97 ocfs2_megabytes_to_clusters(sb, la_max_mb);
98 } else {
99 osb->local_alloc_default_bits =
100 ocfs2_megabytes_to_clusters(sb, requested_mb);
101 }
102
103 osb->local_alloc_bits = osb->local_alloc_default_bits;
104}
105
Mark Fasheh9c7af402008-07-28 18:02:53 -0700106static inline int ocfs2_la_state_enabled(struct ocfs2_super *osb)
107{
108 return (osb->local_alloc_state == OCFS2_LA_THROTTLED ||
109 osb->local_alloc_state == OCFS2_LA_ENABLED);
110}
111
112void ocfs2_local_alloc_seen_free_bits(struct ocfs2_super *osb,
113 unsigned int num_clusters)
114{
115 spin_lock(&osb->osb_lock);
116 if (osb->local_alloc_state == OCFS2_LA_DISABLED ||
117 osb->local_alloc_state == OCFS2_LA_THROTTLED)
118 if (num_clusters >= osb->local_alloc_default_bits) {
119 cancel_delayed_work(&osb->la_enable_wq);
120 osb->local_alloc_state = OCFS2_LA_ENABLED;
121 }
122 spin_unlock(&osb->osb_lock);
123}
124
125void ocfs2_la_enable_worker(struct work_struct *work)
126{
127 struct ocfs2_super *osb =
128 container_of(work, struct ocfs2_super,
129 la_enable_wq.work);
130 spin_lock(&osb->osb_lock);
131 osb->local_alloc_state = OCFS2_LA_ENABLED;
132 spin_unlock(&osb->osb_lock);
133}
134
Mark Fashehccd979b2005-12-15 14:31:24 -0800135/*
136 * Tell us whether a given allocation should use the local alloc
137 * file. Otherwise, it has to go to the main bitmap.
Mark Fasheh9c7af402008-07-28 18:02:53 -0700138 *
139 * This function does semi-dirty reads of local alloc size and state!
140 * This is ok however, as the values are re-checked once under mutex.
Mark Fashehccd979b2005-12-15 14:31:24 -0800141 */
142int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
143{
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800144 int ret = 0;
Mark Fasheh9c7af402008-07-28 18:02:53 -0700145 int la_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800146
Mark Fasheh9c7af402008-07-28 18:02:53 -0700147 spin_lock(&osb->osb_lock);
148 la_bits = osb->local_alloc_bits;
149
150 if (!ocfs2_la_state_enabled(osb))
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800151 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800152
153 /* la_bits should be at least twice the size (in clusters) of
154 * a new block group. We want to be sure block group
155 * allocations go through the local alloc, so allow an
156 * allocation to take up to half the bitmap. */
157 if (bits > (la_bits / 2))
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800158 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800159
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800160 ret = 1;
161bail:
162 mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n",
163 osb->local_alloc_state, (unsigned long long)bits, la_bits, ret);
Mark Fasheh9c7af402008-07-28 18:02:53 -0700164 spin_unlock(&osb->osb_lock);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800165 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -0800166}
167
168int ocfs2_load_local_alloc(struct ocfs2_super *osb)
169{
170 int status = 0;
171 struct ocfs2_dinode *alloc = NULL;
172 struct buffer_head *alloc_bh = NULL;
173 u32 num_used;
174 struct inode *inode = NULL;
175 struct ocfs2_local_alloc *la;
176
177 mlog_entry_void();
178
Mark Fashehebcee4b2008-07-28 14:55:20 -0700179 if (osb->local_alloc_bits == 0)
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800180 goto bail;
181
Mark Fashehebcee4b2008-07-28 14:55:20 -0700182 if (osb->local_alloc_bits >= osb->bitmap_cpg) {
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800183 mlog(ML_NOTICE, "Requested local alloc window %d is larger "
184 "than max possible %u. Using defaults.\n",
Mark Fashehebcee4b2008-07-28 14:55:20 -0700185 osb->local_alloc_bits, (osb->bitmap_cpg - 1));
186 osb->local_alloc_bits =
187 ocfs2_megabytes_to_clusters(osb->sb,
188 OCFS2_DEFAULT_LOCAL_ALLOC_SIZE);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800189 }
190
Mark Fashehccd979b2005-12-15 14:31:24 -0800191 /* read the alloc off disk */
192 inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
193 osb->slot_num);
194 if (!inode) {
195 status = -EINVAL;
196 mlog_errno(status);
197 goto bail;
198 }
199
Joel Beckerb657c952008-11-13 14:49:11 -0800200 status = ocfs2_read_inode_block_full(inode, &alloc_bh,
201 OCFS2_BH_IGNORE_CACHE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800202 if (status < 0) {
203 mlog_errno(status);
204 goto bail;
205 }
206
207 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
208 la = OCFS2_LOCAL_ALLOC(alloc);
209
210 if (!(le32_to_cpu(alloc->i_flags) &
211 (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
Mark Fashehb06970532006-03-03 10:24:33 -0800212 mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
213 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800214 status = -EINVAL;
215 goto bail;
216 }
217
218 if ((la->la_size == 0) ||
219 (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) {
220 mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n",
221 le16_to_cpu(la->la_size));
222 status = -EINVAL;
223 goto bail;
224 }
225
226 /* do a little verification. */
227 num_used = ocfs2_local_alloc_count_bits(alloc);
228
229 /* hopefully the local alloc has always been recovered before
230 * we load it. */
231 if (num_used
232 || alloc->id1.bitmap1.i_used
233 || alloc->id1.bitmap1.i_total
234 || la->la_bm_off)
235 mlog(ML_ERROR, "Local alloc hasn't been recovered!\n"
236 "found = %u, set = %u, taken = %u, off = %u\n",
237 num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
238 le32_to_cpu(alloc->id1.bitmap1.i_total),
239 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
240
241 osb->local_alloc_bh = alloc_bh;
242 osb->local_alloc_state = OCFS2_LA_ENABLED;
243
244bail:
245 if (status < 0)
Mark Fasheha81cb882008-10-07 14:25:16 -0700246 brelse(alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800247 if (inode)
248 iput(inode);
249
Mark Fashehebcee4b2008-07-28 14:55:20 -0700250 mlog(0, "Local alloc window bits = %d\n", osb->local_alloc_bits);
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800251
Mark Fashehccd979b2005-12-15 14:31:24 -0800252 mlog_exit(status);
253 return status;
254}
255
256/*
257 * return any unused bits to the bitmap and write out a clean
258 * local_alloc.
259 *
260 * local_alloc_bh is optional. If not passed, we will simply use the
261 * one off osb. If you do pass it however, be warned that it *will* be
262 * returned brelse'd and NULL'd out.*/
263void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
264{
265 int status;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700266 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800267 struct inode *local_alloc_inode = NULL;
268 struct buffer_head *bh = NULL;
269 struct buffer_head *main_bm_bh = NULL;
270 struct inode *main_bm_inode = NULL;
271 struct ocfs2_dinode *alloc_copy = NULL;
272 struct ocfs2_dinode *alloc = NULL;
273
274 mlog_entry_void();
275
Mark Fasheh9c7af402008-07-28 18:02:53 -0700276 cancel_delayed_work(&osb->la_enable_wq);
277 flush_workqueue(ocfs2_wq);
278
Mark Fashehccd979b2005-12-15 14:31:24 -0800279 if (osb->local_alloc_state == OCFS2_LA_UNUSED)
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700280 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800281
282 local_alloc_inode =
283 ocfs2_get_system_file_inode(osb,
284 LOCAL_ALLOC_SYSTEM_INODE,
285 osb->slot_num);
286 if (!local_alloc_inode) {
287 status = -ENOENT;
288 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700289 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800290 }
291
292 osb->local_alloc_state = OCFS2_LA_DISABLED;
293
Mark Fashehd02f00c2009-12-07 13:10:48 -0800294 ocfs2_resmap_uninit(&osb->osb_la_resmap);
295
Mark Fashehccd979b2005-12-15 14:31:24 -0800296 main_bm_inode = ocfs2_get_system_file_inode(osb,
297 GLOBAL_BITMAP_SYSTEM_INODE,
298 OCFS2_INVALID_SLOT);
299 if (!main_bm_inode) {
300 status = -EINVAL;
301 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700302 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800303 }
304
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700305 mutex_lock(&main_bm_inode->i_mutex);
306
Mark Fashehe63aecb62007-10-18 15:30:42 -0700307 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800308 if (status < 0) {
309 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700310 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -0800311 }
312
313 /* WINDOW_MOVE_CREDITS is a bit heavy... */
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700314 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800315 if (IS_ERR(handle)) {
316 mlog_errno(PTR_ERR(handle));
317 handle = NULL;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700318 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800319 }
320
321 bh = osb->local_alloc_bh;
322 alloc = (struct ocfs2_dinode *) bh->b_data;
323
Sunil Mushran4ba1c5b2008-04-18 15:03:59 -0700324 alloc_copy = kmalloc(bh->b_size, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800325 if (!alloc_copy) {
326 status = -ENOMEM;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700327 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800328 }
329 memcpy(alloc_copy, alloc, bh->b_size);
330
Joel Becker0cf2f762009-02-12 16:41:25 -0800331 status = ocfs2_journal_access_di(handle, INODE_CACHE(local_alloc_inode),
332 bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800333 if (status < 0) {
334 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700335 goto out_commit;
Mark Fashehccd979b2005-12-15 14:31:24 -0800336 }
337
338 ocfs2_clear_local_alloc(alloc);
Joel Beckerec20cec2010-03-19 14:13:52 -0700339 ocfs2_journal_dirty(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800340
341 brelse(bh);
342 osb->local_alloc_bh = NULL;
343 osb->local_alloc_state = OCFS2_LA_UNUSED;
344
345 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
346 main_bm_inode, main_bm_bh);
347 if (status < 0)
348 mlog_errno(status);
349
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700350out_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700351 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800352
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700353out_unlock:
Mark Fasheha81cb882008-10-07 14:25:16 -0700354 brelse(main_bm_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800355
Mark Fashehe63aecb62007-10-18 15:30:42 -0700356 ocfs2_inode_unlock(main_bm_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800357
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700358out_mutex:
359 mutex_unlock(&main_bm_inode->i_mutex);
360 iput(main_bm_inode);
361
362out:
Mark Fashehccd979b2005-12-15 14:31:24 -0800363 if (local_alloc_inode)
364 iput(local_alloc_inode);
365
366 if (alloc_copy)
367 kfree(alloc_copy);
368
369 mlog_exit_void();
370}
371
372/*
373 * We want to free the bitmap bits outside of any recovery context as
374 * we'll need a cluster lock to do so, but we must clear the local
375 * alloc before giving up the recovered nodes journal. To solve this,
376 * we kmalloc a copy of the local alloc before it's change for the
377 * caller to process with ocfs2_complete_local_alloc_recovery
378 */
379int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
380 int slot_num,
381 struct ocfs2_dinode **alloc_copy)
382{
383 int status = 0;
384 struct buffer_head *alloc_bh = NULL;
385 struct inode *inode = NULL;
386 struct ocfs2_dinode *alloc;
387
388 mlog_entry("(slot_num = %d)\n", slot_num);
389
390 *alloc_copy = NULL;
391
392 inode = ocfs2_get_system_file_inode(osb,
393 LOCAL_ALLOC_SYSTEM_INODE,
394 slot_num);
395 if (!inode) {
396 status = -EINVAL;
397 mlog_errno(status);
398 goto bail;
399 }
400
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800401 mutex_lock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800402
Joel Beckerb657c952008-11-13 14:49:11 -0800403 status = ocfs2_read_inode_block_full(inode, &alloc_bh,
404 OCFS2_BH_IGNORE_CACHE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800405 if (status < 0) {
406 mlog_errno(status);
407 goto bail;
408 }
409
410 *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL);
411 if (!(*alloc_copy)) {
412 status = -ENOMEM;
413 goto bail;
414 }
415 memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
416
417 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
418 ocfs2_clear_local_alloc(alloc);
419
Joel Becker13723d02008-10-17 19:25:01 -0700420 ocfs2_compute_meta_ecc(osb->sb, alloc_bh->b_data, &alloc->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -0800421 status = ocfs2_write_block(osb, alloc_bh, INODE_CACHE(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -0800422 if (status < 0)
423 mlog_errno(status);
424
425bail:
426 if ((status < 0) && (*alloc_copy)) {
427 kfree(*alloc_copy);
428 *alloc_copy = NULL;
429 }
430
Mark Fasheha81cb882008-10-07 14:25:16 -0700431 brelse(alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800432
433 if (inode) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800434 mutex_unlock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800435 iput(inode);
436 }
437
438 mlog_exit(status);
439 return status;
440}
441
442/*
443 * Step 2: By now, we've completed the journal recovery, we've stamped
444 * a clean local alloc on disk and dropped the node out of the
445 * recovery map. Dlm locks will no longer stall, so lets clear out the
446 * main bitmap.
447 */
448int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
449 struct ocfs2_dinode *alloc)
450{
451 int status;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700452 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800453 struct buffer_head *main_bm_bh = NULL;
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700454 struct inode *main_bm_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800455
456 mlog_entry_void();
457
Mark Fashehccd979b2005-12-15 14:31:24 -0800458 main_bm_inode = ocfs2_get_system_file_inode(osb,
459 GLOBAL_BITMAP_SYSTEM_INODE,
460 OCFS2_INVALID_SLOT);
461 if (!main_bm_inode) {
462 status = -EINVAL;
463 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700464 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800465 }
466
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700467 mutex_lock(&main_bm_inode->i_mutex);
468
Mark Fashehe63aecb62007-10-18 15:30:42 -0700469 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800470 if (status < 0) {
471 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700472 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -0800473 }
474
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700475 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800476 if (IS_ERR(handle)) {
477 status = PTR_ERR(handle);
478 handle = NULL;
479 mlog_errno(status);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700480 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800481 }
482
483 /* we want the bitmap change to be recorded on disk asap */
Mark Fasheh1fabe142006-10-09 18:11:45 -0700484 handle->h_sync = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800485
486 status = ocfs2_sync_local_to_main(osb, handle, alloc,
487 main_bm_inode, main_bm_bh);
488 if (status < 0)
489 mlog_errno(status);
490
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700491 ocfs2_commit_trans(osb, handle);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700492
493out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700494 ocfs2_inode_unlock(main_bm_inode, 1);
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700495
496out_mutex:
497 mutex_unlock(&main_bm_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800498
Mark Fasheha81cb882008-10-07 14:25:16 -0700499 brelse(main_bm_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800500
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700501 iput(main_bm_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800502
Mark Fasheh8898a5a2006-10-05 15:42:08 -0700503out:
Tao Ma4d0ddb22008-03-05 16:11:46 +0800504 if (!status)
Tiger Yangb89c5422010-01-25 14:11:06 +0800505 ocfs2_init_steal_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800506 mlog_exit(status);
507 return status;
508}
509
510/*
Mark Fasheh9c7af402008-07-28 18:02:53 -0700511 * make sure we've got at least bits_wanted contiguous bits in the
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800512 * local alloc. You lose them when you drop i_mutex.
Mark Fashehccd979b2005-12-15 14:31:24 -0800513 *
514 * We will add ourselves to the transaction passed in, but may start
515 * our own in order to shift windows.
516 */
517int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800518 u32 bits_wanted,
519 struct ocfs2_alloc_context *ac)
520{
521 int status;
522 struct ocfs2_dinode *alloc;
523 struct inode *local_alloc_inode;
524 unsigned int free_bits;
525
526 mlog_entry_void();
527
Mark Fashehccd979b2005-12-15 14:31:24 -0800528 BUG_ON(!ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800529
530 local_alloc_inode =
531 ocfs2_get_system_file_inode(osb,
532 LOCAL_ALLOC_SYSTEM_INODE,
533 osb->slot_num);
534 if (!local_alloc_inode) {
535 status = -ENOENT;
536 mlog_errno(status);
537 goto bail;
538 }
Mark Fashehda5cbf22006-10-06 18:34:35 -0700539
540 mutex_lock(&local_alloc_inode->i_mutex);
541
Mark Fasheh9c7af402008-07-28 18:02:53 -0700542 /*
543 * We must double check state and allocator bits because
544 * another process may have changed them while holding i_mutex.
545 */
546 spin_lock(&osb->osb_lock);
547 if (!ocfs2_la_state_enabled(osb) ||
548 (bits_wanted > osb->local_alloc_bits)) {
549 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800550 status = -ENOSPC;
551 goto bail;
552 }
Mark Fasheh9c7af402008-07-28 18:02:53 -0700553 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800554
555 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
556
Joel Beckere407e392008-06-12 22:35:39 -0700557#ifdef CONFIG_OCFS2_DEBUG_FS
Mark Fashehccd979b2005-12-15 14:31:24 -0800558 if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
559 ocfs2_local_alloc_count_bits(alloc)) {
Mark Fashehb06970532006-03-03 10:24:33 -0800560 ocfs2_error(osb->sb, "local alloc inode %llu says it has "
Mark Fashehccd979b2005-12-15 14:31:24 -0800561 "%u free bits, but a count shows %u",
Mark Fashehb06970532006-03-03 10:24:33 -0800562 (unsigned long long)le64_to_cpu(alloc->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -0800563 le32_to_cpu(alloc->id1.bitmap1.i_used),
564 ocfs2_local_alloc_count_bits(alloc));
565 status = -EIO;
566 goto bail;
567 }
Jan Kara5a58c3e2007-11-13 19:59:33 +0100568#endif
Mark Fashehccd979b2005-12-15 14:31:24 -0800569
570 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
571 le32_to_cpu(alloc->id1.bitmap1.i_used);
572 if (bits_wanted > free_bits) {
573 /* uhoh, window change time. */
574 status =
575 ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
576 if (status < 0) {
577 if (status != -ENOSPC)
578 mlog_errno(status);
579 goto bail;
580 }
Mark Fasheh9c7af402008-07-28 18:02:53 -0700581
582 /*
583 * Under certain conditions, the window slide code
584 * might have reduced the number of bits available or
585 * disabled the the local alloc entirely. Re-check
586 * here and return -ENOSPC if necessary.
587 */
588 status = -ENOSPC;
589 if (!ocfs2_la_state_enabled(osb))
590 goto bail;
591
592 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
593 le32_to_cpu(alloc->id1.bitmap1.i_used);
594 if (bits_wanted > free_bits)
595 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800596 }
597
Joel Becker1187c962008-09-03 20:03:39 -0700598 if (ac->ac_max_block)
599 mlog(0, "Calling in_range for max block %llu\n",
600 (unsigned long long)ac->ac_max_block);
601
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700602 ac->ac_inode = local_alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800603 /* We should never use localalloc from another slot */
604 ac->ac_alloc_slot = osb->slot_num;
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700605 ac->ac_which = OCFS2_AC_USE_LOCAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800606 get_bh(osb->local_alloc_bh);
607 ac->ac_bh = osb->local_alloc_bh;
Mark Fashehccd979b2005-12-15 14:31:24 -0800608 status = 0;
609bail:
Sunil Mushranbda02332007-09-21 11:41:43 -0700610 if (status < 0 && local_alloc_inode) {
611 mutex_unlock(&local_alloc_inode->i_mutex);
Mark Fasheh8fccfc82007-05-09 17:34:26 -0700612 iput(local_alloc_inode);
Sunil Mushranbda02332007-09-21 11:41:43 -0700613 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800614
Sunil Mushran2fbe8d12007-12-20 14:58:11 -0800615 mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num,
616 status);
617
Mark Fashehccd979b2005-12-15 14:31:24 -0800618 mlog_exit(status);
619 return status;
620}
621
622int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700623 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800624 struct ocfs2_alloc_context *ac,
Mark Fasheh415cb802007-09-16 20:10:16 -0700625 u32 bits_wanted,
Mark Fashehccd979b2005-12-15 14:31:24 -0800626 u32 *bit_off,
627 u32 *num_bits)
628{
629 int status, start;
630 struct inode *local_alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800631 void *bitmap;
632 struct ocfs2_dinode *alloc;
633 struct ocfs2_local_alloc *la;
634
635 mlog_entry_void();
636 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
637
Mark Fashehccd979b2005-12-15 14:31:24 -0800638 local_alloc_inode = ac->ac_inode;
639 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
640 la = OCFS2_LOCAL_ALLOC(alloc);
641
Mark Fashehd02f00c2009-12-07 13:10:48 -0800642 start = ocfs2_local_alloc_find_clear_bits(osb, alloc, &bits_wanted,
643 ac->ac_resv);
Mark Fashehccd979b2005-12-15 14:31:24 -0800644 if (start == -1) {
645 /* TODO: Shouldn't we just BUG here? */
646 status = -ENOSPC;
647 mlog_errno(status);
648 goto bail;
649 }
650
651 bitmap = la->la_bitmap;
652 *bit_off = le32_to_cpu(la->la_bm_off) + start;
Mark Fashehccd979b2005-12-15 14:31:24 -0800653 *num_bits = bits_wanted;
654
Joel Becker0cf2f762009-02-12 16:41:25 -0800655 status = ocfs2_journal_access_di(handle,
656 INODE_CACHE(local_alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700657 osb->local_alloc_bh,
658 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800659 if (status < 0) {
660 mlog_errno(status);
661 goto bail;
662 }
663
Mark Fashehd02f00c2009-12-07 13:10:48 -0800664 ocfs2_resmap_claimed_bits(&osb->osb_la_resmap, ac->ac_resv, start,
665 bits_wanted);
666
Mark Fashehccd979b2005-12-15 14:31:24 -0800667 while(bits_wanted--)
668 ocfs2_set_bit(start++, bitmap);
669
Marcin Slusarz0dd32562008-02-13 00:06:18 +0100670 le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits);
Joel Beckerec20cec2010-03-19 14:13:52 -0700671 ocfs2_journal_dirty(handle, osb->local_alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800672
Mark Fashehccd979b2005-12-15 14:31:24 -0800673bail:
674 mlog_exit(status);
675 return status;
676}
677
678static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
679{
680 int i;
681 u8 *buffer;
682 u32 count = 0;
683 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
684
685 mlog_entry_void();
686
687 buffer = la->la_bitmap;
688 for (i = 0; i < le16_to_cpu(la->la_size); i++)
689 count += hweight8(buffer[i]);
690
691 mlog_exit(count);
692 return count;
693}
694
695static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
Mark Fashehd02f00c2009-12-07 13:10:48 -0800696 struct ocfs2_dinode *alloc,
697 u32 *numbits,
698 struct ocfs2_alloc_reservation *resv)
Mark Fashehccd979b2005-12-15 14:31:24 -0800699{
700 int numfound, bitoff, left, startoff, lastzero;
Mark Fashehd02f00c2009-12-07 13:10:48 -0800701 int local_resv = 0;
702 struct ocfs2_alloc_reservation r;
Mark Fashehccd979b2005-12-15 14:31:24 -0800703 void *bitmap = NULL;
Mark Fashehd02f00c2009-12-07 13:10:48 -0800704 struct ocfs2_reservation_map *resmap = &osb->osb_la_resmap;
Mark Fashehccd979b2005-12-15 14:31:24 -0800705
Mark Fashehd02f00c2009-12-07 13:10:48 -0800706 mlog_entry("(numbits wanted = %u)\n", *numbits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800707
708 if (!alloc->id1.bitmap1.i_total) {
709 mlog(0, "No bits in my window!\n");
710 bitoff = -1;
711 goto bail;
712 }
713
Mark Fashehd02f00c2009-12-07 13:10:48 -0800714 if (!resv) {
715 local_resv = 1;
716 ocfs2_resv_init_once(&r);
717 ocfs2_resv_set_type(&r, OCFS2_RESV_FLAG_TMP);
718 resv = &r;
719 }
720
721 numfound = *numbits;
722 if (ocfs2_resmap_resv_bits(resmap, resv, &bitoff, &numfound) == 0) {
723 if (numfound < *numbits)
724 *numbits = numfound;
725 goto bail;
726 }
727
728 /*
729 * Code error. While reservations are enabled, local
730 * allocation should _always_ go through them.
731 */
732 BUG_ON(osb->osb_resv_level != 0);
733
734 /*
735 * Reservations are disabled. Handle this the old way.
736 */
737
Mark Fashehccd979b2005-12-15 14:31:24 -0800738 bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
739
740 numfound = bitoff = startoff = 0;
741 lastzero = -1;
742 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
743 while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
744 if (bitoff == left) {
745 /* mlog(0, "bitoff (%d) == left", bitoff); */
746 break;
747 }
748 /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
749 "numfound = %d\n", bitoff, startoff, numfound);*/
750
751 /* Ok, we found a zero bit... is it contig. or do we
752 * start over?*/
753 if (bitoff == startoff) {
754 /* we found a zero */
755 numfound++;
756 startoff++;
757 } else {
758 /* got a zero after some ones */
759 numfound = 1;
760 startoff = bitoff+1;
761 }
762 /* we got everything we needed */
Mark Fashehd02f00c2009-12-07 13:10:48 -0800763 if (numfound == *numbits) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800764 /* mlog(0, "Found it all!\n"); */
765 break;
766 }
767 }
768
769 mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff,
770 numfound);
771
Mark Fashehd02f00c2009-12-07 13:10:48 -0800772 if (numfound == *numbits) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800773 bitoff = startoff - numfound;
Mark Fashehd02f00c2009-12-07 13:10:48 -0800774 *numbits = numfound;
775 } else {
776 numfound = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800777 bitoff = -1;
Mark Fashehd02f00c2009-12-07 13:10:48 -0800778 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800779
780bail:
Mark Fashehd02f00c2009-12-07 13:10:48 -0800781 if (local_resv)
782 ocfs2_resv_discard(resmap, resv);
783
Mark Fashehccd979b2005-12-15 14:31:24 -0800784 mlog_exit(bitoff);
785 return bitoff;
786}
787
788static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
789{
790 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
791 int i;
792 mlog_entry_void();
793
794 alloc->id1.bitmap1.i_total = 0;
795 alloc->id1.bitmap1.i_used = 0;
796 la->la_bm_off = 0;
797 for(i = 0; i < le16_to_cpu(la->la_size); i++)
798 la->la_bitmap[i] = 0;
799
800 mlog_exit_void();
801}
802
803#if 0
804/* turn this on and uncomment below to aid debugging window shifts. */
805static void ocfs2_verify_zero_bits(unsigned long *bitmap,
806 unsigned int start,
807 unsigned int count)
808{
809 unsigned int tmp = count;
810 while(tmp--) {
811 if (ocfs2_test_bit(start + tmp, bitmap)) {
812 printk("ocfs2_verify_zero_bits: start = %u, count = "
813 "%u\n", start, count);
814 printk("ocfs2_verify_zero_bits: bit %u is set!",
815 start + tmp);
816 BUG();
817 }
818 }
819}
820#endif
821
822/*
823 * sync the local alloc to main bitmap.
824 *
825 * assumes you've already locked the main bitmap -- the bitmap inode
826 * passed is used for caching.
827 */
828static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700829 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800830 struct ocfs2_dinode *alloc,
831 struct inode *main_bm_inode,
832 struct buffer_head *main_bm_bh)
833{
834 int status = 0;
835 int bit_off, left, count, start;
836 u64 la_start_blk;
837 u64 blkno;
838 void *bitmap;
839 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
840
Jan Kara5a58c3e2007-11-13 19:59:33 +0100841 mlog_entry("total = %u, used = %u\n",
Mark Fashehccd979b2005-12-15 14:31:24 -0800842 le32_to_cpu(alloc->id1.bitmap1.i_total),
Mark Fashehccd979b2005-12-15 14:31:24 -0800843 le32_to_cpu(alloc->id1.bitmap1.i_used));
844
845 if (!alloc->id1.bitmap1.i_total) {
846 mlog(0, "nothing to sync!\n");
847 goto bail;
848 }
849
850 if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
851 le32_to_cpu(alloc->id1.bitmap1.i_total)) {
852 mlog(0, "all bits were taken!\n");
853 goto bail;
854 }
855
856 la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
857 le32_to_cpu(la->la_bm_off));
858 bitmap = la->la_bitmap;
859 start = count = bit_off = 0;
860 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
861
862 while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
863 != -1) {
864 if ((bit_off < left) && (bit_off == start)) {
865 count++;
866 start++;
867 continue;
868 }
869 if (count) {
870 blkno = la_start_blk +
871 ocfs2_clusters_to_blocks(osb->sb,
872 start - count);
873
Mark Fashehb06970532006-03-03 10:24:33 -0800874 mlog(0, "freeing %u bits starting at local alloc bit "
875 "%u (la_start_blk = %llu, blkno = %llu)\n",
876 count, start - count,
877 (unsigned long long)la_start_blk,
878 (unsigned long long)blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800879
Mark Fashehb4414ee2010-03-11 18:31:09 -0800880 status = ocfs2_release_clusters(handle,
881 main_bm_inode,
882 main_bm_bh, blkno,
883 count);
Mark Fashehccd979b2005-12-15 14:31:24 -0800884 if (status < 0) {
885 mlog_errno(status);
886 goto bail;
887 }
888 }
889 if (bit_off >= left)
890 break;
891 count = 1;
892 start = bit_off + 1;
893 }
894
895bail:
896 mlog_exit(status);
897 return status;
898}
899
Mark Fasheh9c7af402008-07-28 18:02:53 -0700900enum ocfs2_la_event {
901 OCFS2_LA_EVENT_SLIDE, /* Normal window slide. */
902 OCFS2_LA_EVENT_FRAGMENTED, /* The global bitmap has
903 * enough bits theoretically
904 * free, but a contiguous
905 * allocation could not be
906 * found. */
907 OCFS2_LA_EVENT_ENOSPC, /* Global bitmap doesn't have
908 * enough bits free to satisfy
909 * our request. */
910};
911#define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
912/*
913 * Given an event, calculate the size of our next local alloc window.
914 *
915 * This should always be called under i_mutex of the local alloc inode
916 * so that local alloc disabling doesn't race with processes trying to
917 * use the allocator.
918 *
919 * Returns the state which the local alloc was left in. This value can
920 * be ignored by some paths.
921 */
922static int ocfs2_recalc_la_window(struct ocfs2_super *osb,
923 enum ocfs2_la_event event)
924{
925 unsigned int bits;
926 int state;
927
928 spin_lock(&osb->osb_lock);
929 if (osb->local_alloc_state == OCFS2_LA_DISABLED) {
930 WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED);
931 goto out_unlock;
932 }
933
934 /*
935 * ENOSPC and fragmentation are treated similarly for now.
936 */
937 if (event == OCFS2_LA_EVENT_ENOSPC ||
938 event == OCFS2_LA_EVENT_FRAGMENTED) {
939 /*
940 * We ran out of contiguous space in the primary
941 * bitmap. Drastically reduce the number of bits used
942 * by local alloc until we have to disable it.
943 */
944 bits = osb->local_alloc_bits >> 1;
945 if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) {
946 /*
947 * By setting state to THROTTLED, we'll keep
948 * the number of local alloc bits used down
949 * until an event occurs which would give us
950 * reason to assume the bitmap situation might
951 * have changed.
952 */
953 osb->local_alloc_state = OCFS2_LA_THROTTLED;
954 osb->local_alloc_bits = bits;
955 } else {
956 osb->local_alloc_state = OCFS2_LA_DISABLED;
957 }
958 queue_delayed_work(ocfs2_wq, &osb->la_enable_wq,
959 OCFS2_LA_ENABLE_INTERVAL);
960 goto out_unlock;
961 }
962
963 /*
964 * Don't increase the size of the local alloc window until we
965 * know we might be able to fulfill the request. Otherwise, we
966 * risk bouncing around the global bitmap during periods of
967 * low space.
968 */
969 if (osb->local_alloc_state != OCFS2_LA_THROTTLED)
970 osb->local_alloc_bits = osb->local_alloc_default_bits;
971
972out_unlock:
973 state = osb->local_alloc_state;
974 spin_unlock(&osb->osb_lock);
975
976 return state;
977}
978
Mark Fashehccd979b2005-12-15 14:31:24 -0800979static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800980 struct ocfs2_alloc_context **ac,
981 struct inode **bitmap_inode,
982 struct buffer_head **bitmap_bh)
983{
984 int status;
985
Robert P. J. Daycd861282006-12-13 00:34:52 -0800986 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800987 if (!(*ac)) {
988 status = -ENOMEM;
989 mlog_errno(status);
990 goto bail;
991 }
992
Mark Fasheh9c7af402008-07-28 18:02:53 -0700993retry_enospc:
Mark Fashehb22b63e2010-03-11 18:43:46 -0800994 (*ac)->ac_bits_wanted = osb->local_alloc_default_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800995 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
Mark Fasheh9c7af402008-07-28 18:02:53 -0700996 if (status == -ENOSPC) {
997 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) ==
998 OCFS2_LA_DISABLED)
999 goto bail;
1000
1001 ocfs2_free_ac_resource(*ac);
1002 memset(*ac, 0, sizeof(struct ocfs2_alloc_context));
1003 goto retry_enospc;
1004 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001005 if (status < 0) {
Mark Fasheh9c7af402008-07-28 18:02:53 -07001006 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001007 goto bail;
1008 }
1009
1010 *bitmap_inode = (*ac)->ac_inode;
1011 igrab(*bitmap_inode);
1012 *bitmap_bh = (*ac)->ac_bh;
1013 get_bh(*bitmap_bh);
1014 status = 0;
1015bail:
1016 if ((status < 0) && *ac) {
1017 ocfs2_free_alloc_context(*ac);
1018 *ac = NULL;
1019 }
1020
1021 mlog_exit(status);
1022 return status;
1023}
1024
1025/*
1026 * pass it the bitmap lock in lock_bh if you have it.
1027 */
1028static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001029 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001030 struct ocfs2_alloc_context *ac)
1031{
1032 int status = 0;
1033 u32 cluster_off, cluster_count;
1034 struct ocfs2_dinode *alloc = NULL;
1035 struct ocfs2_local_alloc *la;
1036
1037 mlog_entry_void();
1038
1039 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
1040 la = OCFS2_LOCAL_ALLOC(alloc);
1041
1042 if (alloc->id1.bitmap1.i_total)
1043 mlog(0, "asking me to alloc a new window over a non-empty "
1044 "one\n");
1045
1046 mlog(0, "Allocating %u clusters for a new window.\n",
Mark Fashehebcee4b2008-07-28 14:55:20 -07001047 osb->local_alloc_bits);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001048
1049 /* Instruct the allocation code to try the most recently used
1050 * cluster group. We'll re-record the group used this pass
1051 * below. */
1052 ac->ac_last_group = osb->la_last_gd;
1053
Mark Fashehccd979b2005-12-15 14:31:24 -08001054 /* we used the generic suballoc reserve function, but we set
1055 * everything up nicely, so there's no reason why we can't use
1056 * the more specific cluster api to claim bits. */
Mark Fashehebcee4b2008-07-28 14:55:20 -07001057 status = ocfs2_claim_clusters(osb, handle, ac, osb->local_alloc_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -08001058 &cluster_off, &cluster_count);
Mark Fasheh9c7af402008-07-28 18:02:53 -07001059 if (status == -ENOSPC) {
1060retry_enospc:
1061 /*
1062 * Note: We could also try syncing the journal here to
1063 * allow use of any free bits which the current
1064 * transaction can't give us access to. --Mark
1065 */
1066 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) ==
1067 OCFS2_LA_DISABLED)
1068 goto bail;
1069
Mark Fashehb22b63e2010-03-11 18:43:46 -08001070 ac->ac_bits_wanted = osb->local_alloc_default_bits;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001071 status = ocfs2_claim_clusters(osb, handle, ac,
1072 osb->local_alloc_bits,
1073 &cluster_off,
1074 &cluster_count);
1075 if (status == -ENOSPC)
1076 goto retry_enospc;
1077 /*
1078 * We only shrunk the *minimum* number of in our
1079 * request - it's entirely possible that the allocator
1080 * might give us more than we asked for.
1081 */
1082 if (status == 0) {
1083 spin_lock(&osb->osb_lock);
1084 osb->local_alloc_bits = cluster_count;
1085 spin_unlock(&osb->osb_lock);
1086 }
1087 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001088 if (status < 0) {
1089 if (status != -ENOSPC)
1090 mlog_errno(status);
1091 goto bail;
1092 }
1093
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001094 osb->la_last_gd = ac->ac_last_group;
1095
Mark Fashehccd979b2005-12-15 14:31:24 -08001096 la->la_bm_off = cpu_to_le32(cluster_off);
1097 alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
1098 /* just in case... In the future when we find space ourselves,
1099 * we don't have to get all contiguous -- but we'll have to
1100 * set all previously used bits in bitmap and update
1101 * la_bits_set before setting the bits in the main bitmap. */
1102 alloc->id1.bitmap1.i_used = 0;
1103 memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
1104 le16_to_cpu(la->la_size));
1105
Mark Fashehd02f00c2009-12-07 13:10:48 -08001106 ocfs2_resmap_restart(&osb->osb_la_resmap, cluster_count,
1107 OCFS2_LOCAL_ALLOC(alloc)->la_bitmap);
1108
Mark Fashehccd979b2005-12-15 14:31:24 -08001109 mlog(0, "New window allocated:\n");
1110 mlog(0, "window la_bm_off = %u\n",
1111 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
1112 mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total));
1113
1114bail:
1115 mlog_exit(status);
1116 return status;
1117}
1118
1119/* Note that we do *NOT* lock the local alloc inode here as
1120 * it's been locked already for us. */
1121static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
1122 struct inode *local_alloc_inode)
1123{
1124 int status = 0;
1125 struct buffer_head *main_bm_bh = NULL;
1126 struct inode *main_bm_inode = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001127 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001128 struct ocfs2_dinode *alloc;
1129 struct ocfs2_dinode *alloc_copy = NULL;
1130 struct ocfs2_alloc_context *ac = NULL;
1131
1132 mlog_entry_void();
1133
Mark Fasheh9c7af402008-07-28 18:02:53 -07001134 ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
1135
Mark Fashehccd979b2005-12-15 14:31:24 -08001136 /* This will lock the main bitmap for us. */
1137 status = ocfs2_local_alloc_reserve_for_window(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001138 &ac,
1139 &main_bm_inode,
1140 &main_bm_bh);
1141 if (status < 0) {
1142 if (status != -ENOSPC)
1143 mlog_errno(status);
1144 goto bail;
1145 }
1146
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001147 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001148 if (IS_ERR(handle)) {
1149 status = PTR_ERR(handle);
1150 handle = NULL;
1151 mlog_errno(status);
1152 goto bail;
1153 }
1154
1155 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
1156
1157 /* We want to clear the local alloc before doing anything
1158 * else, so that if we error later during this operation,
1159 * local alloc shutdown won't try to double free main bitmap
1160 * bits. Make a copy so the sync function knows which bits to
1161 * free. */
Sunil Mushran4ba1c5b2008-04-18 15:03:59 -07001162 alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001163 if (!alloc_copy) {
1164 status = -ENOMEM;
1165 mlog_errno(status);
1166 goto bail;
1167 }
1168 memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
1169
Joel Becker0cf2f762009-02-12 16:41:25 -08001170 status = ocfs2_journal_access_di(handle,
1171 INODE_CACHE(local_alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001172 osb->local_alloc_bh,
1173 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001174 if (status < 0) {
1175 mlog_errno(status);
1176 goto bail;
1177 }
1178
1179 ocfs2_clear_local_alloc(alloc);
Joel Beckerec20cec2010-03-19 14:13:52 -07001180 ocfs2_journal_dirty(handle, osb->local_alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001181
1182 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
1183 main_bm_inode, main_bm_bh);
1184 if (status < 0) {
1185 mlog_errno(status);
1186 goto bail;
1187 }
1188
1189 status = ocfs2_local_alloc_new_window(osb, handle, ac);
1190 if (status < 0) {
1191 if (status != -ENOSPC)
1192 mlog_errno(status);
1193 goto bail;
1194 }
1195
1196 atomic_inc(&osb->alloc_stats.moves);
1197
Mark Fashehccd979b2005-12-15 14:31:24 -08001198bail:
1199 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001200 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001201
Mark Fasheha81cb882008-10-07 14:25:16 -07001202 brelse(main_bm_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001203
1204 if (main_bm_inode)
1205 iput(main_bm_inode);
1206
1207 if (alloc_copy)
1208 kfree(alloc_copy);
1209
1210 if (ac)
1211 ocfs2_free_alloc_context(ac);
1212
1213 mlog_exit(status);
1214 return status;
1215}
1216