1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * localalloc.c
5 *
6 * Node local data allocation
7 *
8 * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
26#include <linux/fs.h>
27#include <linux/types.h>
28#include <linux/slab.h>
29#include <linux/highmem.h>
30#include <linux/bitops.h>
31
32#define MLOG_MASK_PREFIX ML_DISK_ALLOC
33#include <cluster/masklog.h>
34
35#include "ocfs2.h"
36
37#include "alloc.h"
38#include "blockcheck.h"
39#include "dlmglue.h"
40#include "inode.h"
41#include "journal.h"
42#include "localalloc.h"
43#include "suballoc.h"
44#include "super.h"
45#include "sysfile.h"
46
47#include "buffer_head_io.h"
48
49#define OCFS2_LOCAL_ALLOC(dinode)	(&((dinode)->id2.i_lab))
50
51static 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					     struct ocfs2_alloc_reservation *resv);
57
58static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc);
59
60static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
61				    handle_t *handle,
62				    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,
67						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,
72					handle_t *handle,
73					struct ocfs2_alloc_context *ac);
74
75static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
76					  struct inode *local_alloc_inode);
77
78/*
79 * ocfs2_la_default_mb() - determine a default size, in megabytes of
80 * the local alloc.
81 *
82 * Generally, we'd like to pick as large a local alloc as
83 * possible. Performance on large workloads tends to scale
84 * proportionally to la size. In addition to that, the reservations
85 * code functions more efficiently as it can reserve more windows for
86 * write.
87 *
88 * Some things work against us when trying to choose a large local alloc:
89 *
90 * - We need to ensure our sizing is picked to leave enough space in
91 *   group descriptors for other allocations (such as block groups,
92 *   etc). Picking default sizes which are a multiple of 4 could help
93 *   - block groups are allocated in 2mb and 4mb chunks.
94 *
95 * - Likewise, we don't want to starve other nodes of bits on small
96 *   file systems. This can easily be taken care of by limiting our
97 *   default to a reasonable size (256M) on larger cluster sizes.
98 *
99 * - Some file systems can't support very large sizes - 4k and 8k in
100 *   particular are limited to less than 128 and 256 megabytes respectively.
101 *
102 * The following reference table shows group descriptor and local
103 * alloc maximums at various cluster sizes (4k blocksize)
104 *
105 * csize: 4K	group: 126M	la: 121M
106 * csize: 8K	group: 252M	la: 243M
107 * csize: 16K	group: 504M	la: 486M
108 * csize: 32K	group: 1008M	la: 972M
109 * csize: 64K	group: 2016M	la: 1944M
110 * csize: 128K	group: 4032M	la: 3888M
111 * csize: 256K	group: 8064M	la: 7776M
112 * csize: 512K	group: 16128M	la: 15552M
113 * csize: 1024K	group: 32256M	la: 31104M
114 */
115#define	OCFS2_LA_MAX_DEFAULT_MB	256
116#define	OCFS2_LA_OLD_DEFAULT	8
117unsigned int ocfs2_la_default_mb(struct ocfs2_super *osb)
118{
119	unsigned int la_mb;
120	unsigned int gd_mb;
121	unsigned int la_max_mb;
122	unsigned int megs_per_slot;
123	struct super_block *sb = osb->sb;
124
125	gd_mb = ocfs2_clusters_to_megabytes(osb->sb,
126		8 * ocfs2_group_bitmap_size(sb, 0, osb->s_feature_incompat));
127
128	/*
129	 * This takes care of files systems with very small group
130	 * descriptors - 512 byte blocksize at cluster sizes lower
131	 * than 16K and also 1k blocksize with 4k cluster size.
132	 */
133	if ((sb->s_blocksize == 512 && osb->s_clustersize <= 8192)
134	    || (sb->s_blocksize == 1024 && osb->s_clustersize == 4096))
135		return OCFS2_LA_OLD_DEFAULT;
136
137	/*
138	 * Leave enough room for some block groups and make the final
139	 * value we work from a multiple of 4.
140	 */
141	gd_mb -= 16;
142	gd_mb &= 0xFFFFFFFB;
143
144	la_mb = gd_mb;
145
146	/*
147	 * Keep window sizes down to a reasonable default
148	 */
149	if (la_mb > OCFS2_LA_MAX_DEFAULT_MB) {
150		/*
151		 * Some clustersize / blocksize combinations will have
152		 * given us a larger than OCFS2_LA_MAX_DEFAULT_MB
153		 * default size, but get poor distribution when
154		 * limited to exactly 256 megabytes.
155		 *
156		 * As an example, 16K clustersize at 4K blocksize
157		 * gives us a cluster group size of 504M. Paring the
158		 * local alloc size down to 256 however, would give us
159		 * only one window and around 200MB left in the
160		 * cluster group. Instead, find the first size below
161		 * 256 which would give us an even distribution.
162		 *
163		 * Larger cluster group sizes actually work out pretty
164		 * well when pared to 256, so we don't have to do this
165		 * for any group that fits more than two
166		 * OCFS2_LA_MAX_DEFAULT_MB windows.
167		 */
168		if (gd_mb > (2 * OCFS2_LA_MAX_DEFAULT_MB))
169			la_mb = 256;
170		else {
171			unsigned int gd_mult = gd_mb;
172
173			while (gd_mult > 256)
174				gd_mult = gd_mult >> 1;
175
176			la_mb = gd_mult;
177		}
178	}
179
180	megs_per_slot = osb->osb_clusters_at_boot / osb->max_slots;
181	megs_per_slot = ocfs2_clusters_to_megabytes(osb->sb, megs_per_slot);
182	/* Too many nodes, too few disk clusters. */
183	if (megs_per_slot < la_mb)
184		la_mb = megs_per_slot;
185
186	/* We can't store more bits than we can in a block. */
187	la_max_mb = ocfs2_clusters_to_megabytes(osb->sb,
188						ocfs2_local_alloc_size(sb) * 8);
189	if (la_mb > la_max_mb)
190		la_mb = la_max_mb;
191
192	return la_mb;
193}
194
195void ocfs2_la_set_sizes(struct ocfs2_super *osb, int requested_mb)
196{
197	struct super_block *sb = osb->sb;
198	unsigned int la_default_mb = ocfs2_la_default_mb(osb);
199	unsigned int la_max_mb;
200
201	la_max_mb = ocfs2_clusters_to_megabytes(sb,
202						ocfs2_local_alloc_size(sb) * 8);
203
204	mlog(0, "requested: %dM, max: %uM, default: %uM\n",
205	     requested_mb, la_max_mb, la_default_mb);
206
207	if (requested_mb == -1) {
208		/* No user request - use defaults */
209		osb->local_alloc_default_bits =
210			ocfs2_megabytes_to_clusters(sb, la_default_mb);
211	} else if (requested_mb > la_max_mb) {
212		/* Request is too big, we give the maximum available */
213		osb->local_alloc_default_bits =
214			ocfs2_megabytes_to_clusters(sb, la_max_mb);
215	} else {
216		osb->local_alloc_default_bits =
217			ocfs2_megabytes_to_clusters(sb, requested_mb);
218	}
219
220	osb->local_alloc_bits = osb->local_alloc_default_bits;
221}
222
223static inline int ocfs2_la_state_enabled(struct ocfs2_super *osb)
224{
225	return (osb->local_alloc_state == OCFS2_LA_THROTTLED ||
226		osb->local_alloc_state == OCFS2_LA_ENABLED);
227}
228
229void ocfs2_local_alloc_seen_free_bits(struct ocfs2_super *osb,
230				      unsigned int num_clusters)
231{
232	spin_lock(&osb->osb_lock);
233	if (osb->local_alloc_state == OCFS2_LA_DISABLED ||
234	    osb->local_alloc_state == OCFS2_LA_THROTTLED)
235		if (num_clusters >= osb->local_alloc_default_bits) {
236			cancel_delayed_work(&osb->la_enable_wq);
237			osb->local_alloc_state = OCFS2_LA_ENABLED;
238		}
239	spin_unlock(&osb->osb_lock);
240}
241
242void ocfs2_la_enable_worker(struct work_struct *work)
243{
244	struct ocfs2_super *osb =
245		container_of(work, struct ocfs2_super,
246			     la_enable_wq.work);
247	spin_lock(&osb->osb_lock);
248	osb->local_alloc_state = OCFS2_LA_ENABLED;
249	spin_unlock(&osb->osb_lock);
250}
251
252/*
253 * Tell us whether a given allocation should use the local alloc
254 * file. Otherwise, it has to go to the main bitmap.
255 *
256 * This function does semi-dirty reads of local alloc size and state!
257 * This is ok however, as the values are re-checked once under mutex.
258 */
259int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
260{
261	int ret = 0;
262	int la_bits;
263
264	spin_lock(&osb->osb_lock);
265	la_bits = osb->local_alloc_bits;
266
267	if (!ocfs2_la_state_enabled(osb))
268		goto bail;
269
270	/* la_bits should be at least twice the size (in clusters) of
271	 * a new block group. We want to be sure block group
272	 * allocations go through the local alloc, so allow an
273	 * allocation to take up to half the bitmap. */
274	if (bits > (la_bits / 2))
275		goto bail;
276
277	ret = 1;
278bail:
279	mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n",
280	     osb->local_alloc_state, (unsigned long long)bits, la_bits, ret);
281	spin_unlock(&osb->osb_lock);
282	return ret;
283}
284
285int ocfs2_load_local_alloc(struct ocfs2_super *osb)
286{
287	int status = 0;
288	struct ocfs2_dinode *alloc = NULL;
289	struct buffer_head *alloc_bh = NULL;
290	u32 num_used;
291	struct inode *inode = NULL;
292	struct ocfs2_local_alloc *la;
293
294	mlog_entry_void();
295
296	if (osb->local_alloc_bits == 0)
297		goto bail;
298
299	if (osb->local_alloc_bits >= osb->bitmap_cpg) {
300		mlog(ML_NOTICE, "Requested local alloc window %d is larger "
301		     "than max possible %u. Using defaults.\n",
302		     osb->local_alloc_bits, (osb->bitmap_cpg - 1));
303		osb->local_alloc_bits =
304			ocfs2_megabytes_to_clusters(osb->sb,
305						    ocfs2_la_default_mb(osb));
306	}
307
308	/* read the alloc off disk */
309	inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
310					    osb->slot_num);
311	if (!inode) {
312		status = -EINVAL;
313		mlog_errno(status);
314		goto bail;
315	}
316
317	status = ocfs2_read_inode_block_full(inode, &alloc_bh,
318					     OCFS2_BH_IGNORE_CACHE);
319	if (status < 0) {
320		mlog_errno(status);
321		goto bail;
322	}
323
324	alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
325	la = OCFS2_LOCAL_ALLOC(alloc);
326
327	if (!(le32_to_cpu(alloc->i_flags) &
328	    (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
329		mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
330		     (unsigned long long)OCFS2_I(inode)->ip_blkno);
331		status = -EINVAL;
332		goto bail;
333	}
334
335	if ((la->la_size == 0) ||
336	    (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) {
337		mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n",
338		     le16_to_cpu(la->la_size));
339		status = -EINVAL;
340		goto bail;
341	}
342
343	/* do a little verification. */
344	num_used = ocfs2_local_alloc_count_bits(alloc);
345
346	/* hopefully the local alloc has always been recovered before
347	 * we load it. */
348	if (num_used
349	    || alloc->id1.bitmap1.i_used
350	    || alloc->id1.bitmap1.i_total
351	    || la->la_bm_off)
352		mlog(ML_ERROR, "Local alloc hasn't been recovered!\n"
353		     "found = %u, set = %u, taken = %u, off = %u\n",
354		     num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
355		     le32_to_cpu(alloc->id1.bitmap1.i_total),
356		     OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
357
358	osb->local_alloc_bh = alloc_bh;
359	osb->local_alloc_state = OCFS2_LA_ENABLED;
360
361bail:
362	if (status < 0)
363		brelse(alloc_bh);
364	if (inode)
365		iput(inode);
366
367	mlog(0, "Local alloc window bits = %d\n", osb->local_alloc_bits);
368
369	mlog_exit(status);
370	return status;
371}
372
373/*
374 * return any unused bits to the bitmap and write out a clean
375 * local_alloc.
376 *
377 * local_alloc_bh is optional. If not passed, we will simply use the
378 * one off osb. If you do pass it however, be warned that it *will* be
379 * returned brelse'd and NULL'd out.*/
380void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
381{
382	int status;
383	handle_t *handle;
384	struct inode *local_alloc_inode = NULL;
385	struct buffer_head *bh = NULL;
386	struct buffer_head *main_bm_bh = NULL;
387	struct inode *main_bm_inode = NULL;
388	struct ocfs2_dinode *alloc_copy = NULL;
389	struct ocfs2_dinode *alloc = NULL;
390
391	mlog_entry_void();
392
393	cancel_delayed_work(&osb->la_enable_wq);
394	flush_workqueue(ocfs2_wq);
395
396	if (osb->local_alloc_state == OCFS2_LA_UNUSED)
397		goto out;
398
399	local_alloc_inode =
400		ocfs2_get_system_file_inode(osb,
401					    LOCAL_ALLOC_SYSTEM_INODE,
402					    osb->slot_num);
403	if (!local_alloc_inode) {
404		status = -ENOENT;
405		mlog_errno(status);
406		goto out;
407	}
408
409	osb->local_alloc_state = OCFS2_LA_DISABLED;
410
411	ocfs2_resmap_uninit(&osb->osb_la_resmap);
412
413	main_bm_inode = ocfs2_get_system_file_inode(osb,
414						    GLOBAL_BITMAP_SYSTEM_INODE,
415						    OCFS2_INVALID_SLOT);
416	if (!main_bm_inode) {
417		status = -EINVAL;
418		mlog_errno(status);
419		goto out;
420	}
421
422	mutex_lock(&main_bm_inode->i_mutex);
423
424	status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
425	if (status < 0) {
426		mlog_errno(status);
427		goto out_mutex;
428	}
429
430	/* WINDOW_MOVE_CREDITS is a bit heavy... */
431	handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
432	if (IS_ERR(handle)) {
433		mlog_errno(PTR_ERR(handle));
434		handle = NULL;
435		goto out_unlock;
436	}
437
438	bh = osb->local_alloc_bh;
439	alloc = (struct ocfs2_dinode *) bh->b_data;
440
441	alloc_copy = kmalloc(bh->b_size, GFP_NOFS);
442	if (!alloc_copy) {
443		status = -ENOMEM;
444		goto out_commit;
445	}
446	memcpy(alloc_copy, alloc, bh->b_size);
447
448	status = ocfs2_journal_access_di(handle, INODE_CACHE(local_alloc_inode),
449					 bh, OCFS2_JOURNAL_ACCESS_WRITE);
450	if (status < 0) {
451		mlog_errno(status);
452		goto out_commit;
453	}
454
455	ocfs2_clear_local_alloc(alloc);
456	ocfs2_journal_dirty(handle, bh);
457
458	brelse(bh);
459	osb->local_alloc_bh = NULL;
460	osb->local_alloc_state = OCFS2_LA_UNUSED;
461
462	status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
463					  main_bm_inode, main_bm_bh);
464	if (status < 0)
465		mlog_errno(status);
466
467out_commit:
468	ocfs2_commit_trans(osb, handle);
469
470out_unlock:
471	brelse(main_bm_bh);
472
473	ocfs2_inode_unlock(main_bm_inode, 1);
474
475out_mutex:
476	mutex_unlock(&main_bm_inode->i_mutex);
477	iput(main_bm_inode);
478
479out:
480	if (local_alloc_inode)
481		iput(local_alloc_inode);
482
483	if (alloc_copy)
484		kfree(alloc_copy);
485
486	mlog_exit_void();
487}
488
489/*
490 * We want to free the bitmap bits outside of any recovery context as
491 * we'll need a cluster lock to do so, but we must clear the local
492 * alloc before giving up the recovered nodes journal. To solve this,
493 * we kmalloc a copy of the local alloc before it's change for the
494 * caller to process with ocfs2_complete_local_alloc_recovery
495 */
496int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
497				     int slot_num,
498				     struct ocfs2_dinode **alloc_copy)
499{
500	int status = 0;
501	struct buffer_head *alloc_bh = NULL;
502	struct inode *inode = NULL;
503	struct ocfs2_dinode *alloc;
504
505	mlog_entry("(slot_num = %d)\n", slot_num);
506
507	*alloc_copy = NULL;
508
509	inode = ocfs2_get_system_file_inode(osb,
510					    LOCAL_ALLOC_SYSTEM_INODE,
511					    slot_num);
512	if (!inode) {
513		status = -EINVAL;
514		mlog_errno(status);
515		goto bail;
516	}
517
518	mutex_lock(&inode->i_mutex);
519
520	status = ocfs2_read_inode_block_full(inode, &alloc_bh,
521					     OCFS2_BH_IGNORE_CACHE);
522	if (status < 0) {
523		mlog_errno(status);
524		goto bail;
525	}
526
527	*alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL);
528	if (!(*alloc_copy)) {
529		status = -ENOMEM;
530		goto bail;
531	}
532	memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
533
534	alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
535	ocfs2_clear_local_alloc(alloc);
536
537	ocfs2_compute_meta_ecc(osb->sb, alloc_bh->b_data, &alloc->i_check);
538	status = ocfs2_write_block(osb, alloc_bh, INODE_CACHE(inode));
539	if (status < 0)
540		mlog_errno(status);
541
542bail:
543	if ((status < 0) && (*alloc_copy)) {
544		kfree(*alloc_copy);
545		*alloc_copy = NULL;
546	}
547
548	brelse(alloc_bh);
549
550	if (inode) {
551		mutex_unlock(&inode->i_mutex);
552		iput(inode);
553	}
554
555	mlog_exit(status);
556	return status;
557}
558
559/*
560 * Step 2: By now, we've completed the journal recovery, we've stamped
561 * a clean local alloc on disk and dropped the node out of the
562 * recovery map. Dlm locks will no longer stall, so lets clear out the
563 * main bitmap.
564 */
565int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
566					struct ocfs2_dinode *alloc)
567{
568	int status;
569	handle_t *handle;
570	struct buffer_head *main_bm_bh = NULL;
571	struct inode *main_bm_inode;
572
573	mlog_entry_void();
574
575	main_bm_inode = ocfs2_get_system_file_inode(osb,
576						    GLOBAL_BITMAP_SYSTEM_INODE,
577						    OCFS2_INVALID_SLOT);
578	if (!main_bm_inode) {
579		status = -EINVAL;
580		mlog_errno(status);
581		goto out;
582	}
583
584	mutex_lock(&main_bm_inode->i_mutex);
585
586	status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
587	if (status < 0) {
588		mlog_errno(status);
589		goto out_mutex;
590	}
591
592	handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
593	if (IS_ERR(handle)) {
594		status = PTR_ERR(handle);
595		handle = NULL;
596		mlog_errno(status);
597		goto out_unlock;
598	}
599
600	/* we want the bitmap change to be recorded on disk asap */
601	handle->h_sync = 1;
602
603	status = ocfs2_sync_local_to_main(osb, handle, alloc,
604					  main_bm_inode, main_bm_bh);
605	if (status < 0)
606		mlog_errno(status);
607
608	ocfs2_commit_trans(osb, handle);
609
610out_unlock:
611	ocfs2_inode_unlock(main_bm_inode, 1);
612
613out_mutex:
614	mutex_unlock(&main_bm_inode->i_mutex);
615
616	brelse(main_bm_bh);
617
618	iput(main_bm_inode);
619
620out:
621	if (!status)
622		ocfs2_init_steal_slots(osb);
623	mlog_exit(status);
624	return status;
625}
626
627/*
628 * make sure we've got at least bits_wanted contiguous bits in the
629 * local alloc. You lose them when you drop i_mutex.
630 *
631 * We will add ourselves to the transaction passed in, but may start
632 * our own in order to shift windows.
633 */
634int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
635				   u32 bits_wanted,
636				   struct ocfs2_alloc_context *ac)
637{
638	int status;
639	struct ocfs2_dinode *alloc;
640	struct inode *local_alloc_inode;
641	unsigned int free_bits;
642
643	mlog_entry_void();
644
645	BUG_ON(!ac);
646
647	local_alloc_inode =
648		ocfs2_get_system_file_inode(osb,
649					    LOCAL_ALLOC_SYSTEM_INODE,
650					    osb->slot_num);
651	if (!local_alloc_inode) {
652		status = -ENOENT;
653		mlog_errno(status);
654		goto bail;
655	}
656
657	mutex_lock(&local_alloc_inode->i_mutex);
658
659	/*
660	 * We must double check state and allocator bits because
661	 * another process may have changed them while holding i_mutex.
662	 */
663	spin_lock(&osb->osb_lock);
664	if (!ocfs2_la_state_enabled(osb) ||
665	    (bits_wanted > osb->local_alloc_bits)) {
666		spin_unlock(&osb->osb_lock);
667		status = -ENOSPC;
668		goto bail;
669	}
670	spin_unlock(&osb->osb_lock);
671
672	alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
673
674#ifdef CONFIG_OCFS2_DEBUG_FS
675	if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
676	    ocfs2_local_alloc_count_bits(alloc)) {
677		ocfs2_error(osb->sb, "local alloc inode %llu says it has "
678			    "%u free bits, but a count shows %u",
679			    (unsigned long long)le64_to_cpu(alloc->i_blkno),
680			    le32_to_cpu(alloc->id1.bitmap1.i_used),
681			    ocfs2_local_alloc_count_bits(alloc));
682		status = -EIO;
683		goto bail;
684	}
685#endif
686
687	free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
688		le32_to_cpu(alloc->id1.bitmap1.i_used);
689	if (bits_wanted > free_bits) {
690		/* uhoh, window change time. */
691		status =
692			ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
693		if (status < 0) {
694			if (status != -ENOSPC)
695				mlog_errno(status);
696			goto bail;
697		}
698
699		/*
700		 * Under certain conditions, the window slide code
701		 * might have reduced the number of bits available or
702		 * disabled the the local alloc entirely. Re-check
703		 * here and return -ENOSPC if necessary.
704		 */
705		status = -ENOSPC;
706		if (!ocfs2_la_state_enabled(osb))
707			goto bail;
708
709		free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
710			le32_to_cpu(alloc->id1.bitmap1.i_used);
711		if (bits_wanted > free_bits)
712			goto bail;
713	}
714
715	if (ac->ac_max_block)
716		mlog(0, "Calling in_range for max block %llu\n",
717		     (unsigned long long)ac->ac_max_block);
718
719	ac->ac_inode = local_alloc_inode;
720	/* We should never use localalloc from another slot */
721	ac->ac_alloc_slot = osb->slot_num;
722	ac->ac_which = OCFS2_AC_USE_LOCAL;
723	get_bh(osb->local_alloc_bh);
724	ac->ac_bh = osb->local_alloc_bh;
725	status = 0;
726bail:
727	if (status < 0 && local_alloc_inode) {
728		mutex_unlock(&local_alloc_inode->i_mutex);
729		iput(local_alloc_inode);
730	}
731
732	mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num,
733	     status);
734
735	mlog_exit(status);
736	return status;
737}
738
739int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
740				 handle_t *handle,
741				 struct ocfs2_alloc_context *ac,
742				 u32 bits_wanted,
743				 u32 *bit_off,
744				 u32 *num_bits)
745{
746	int status, start;
747	struct inode *local_alloc_inode;
748	void *bitmap;
749	struct ocfs2_dinode *alloc;
750	struct ocfs2_local_alloc *la;
751
752	mlog_entry_void();
753	BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
754
755	local_alloc_inode = ac->ac_inode;
756	alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
757	la = OCFS2_LOCAL_ALLOC(alloc);
758
759	start = ocfs2_local_alloc_find_clear_bits(osb, alloc, &bits_wanted,
760						  ac->ac_resv);
761	if (start == -1) {
762		/* TODO: Shouldn't we just BUG here? */
763		status = -ENOSPC;
764		mlog_errno(status);
765		goto bail;
766	}
767
768	bitmap = la->la_bitmap;
769	*bit_off = le32_to_cpu(la->la_bm_off) + start;
770	*num_bits = bits_wanted;
771
772	status = ocfs2_journal_access_di(handle,
773					 INODE_CACHE(local_alloc_inode),
774					 osb->local_alloc_bh,
775					 OCFS2_JOURNAL_ACCESS_WRITE);
776	if (status < 0) {
777		mlog_errno(status);
778		goto bail;
779	}
780
781	ocfs2_resmap_claimed_bits(&osb->osb_la_resmap, ac->ac_resv, start,
782				  bits_wanted);
783
784	while(bits_wanted--)
785		ocfs2_set_bit(start++, bitmap);
786
787	le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits);
788	ocfs2_journal_dirty(handle, osb->local_alloc_bh);
789
790bail:
791	mlog_exit(status);
792	return status;
793}
794
795static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
796{
797	int i;
798	u8 *buffer;
799	u32 count = 0;
800	struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
801
802	mlog_entry_void();
803
804	buffer = la->la_bitmap;
805	for (i = 0; i < le16_to_cpu(la->la_size); i++)
806		count += hweight8(buffer[i]);
807
808	mlog_exit(count);
809	return count;
810}
811
812static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
813				     struct ocfs2_dinode *alloc,
814				     u32 *numbits,
815				     struct ocfs2_alloc_reservation *resv)
816{
817	int numfound, bitoff, left, startoff, lastzero;
818	int local_resv = 0;
819	struct ocfs2_alloc_reservation r;
820	void *bitmap = NULL;
821	struct ocfs2_reservation_map *resmap = &osb->osb_la_resmap;
822
823	mlog_entry("(numbits wanted = %u)\n", *numbits);
824
825	if (!alloc->id1.bitmap1.i_total) {
826		mlog(0, "No bits in my window!\n");
827		bitoff = -1;
828		goto bail;
829	}
830
831	if (!resv) {
832		local_resv = 1;
833		ocfs2_resv_init_once(&r);
834		ocfs2_resv_set_type(&r, OCFS2_RESV_FLAG_TMP);
835		resv = &r;
836	}
837
838	numfound = *numbits;
839	if (ocfs2_resmap_resv_bits(resmap, resv, &bitoff, &numfound) == 0) {
840		if (numfound < *numbits)
841			*numbits = numfound;
842		goto bail;
843	}
844
845	/*
846	 * Code error. While reservations are enabled, local
847	 * allocation should _always_ go through them.
848	 */
849	BUG_ON(osb->osb_resv_level != 0);
850
851	/*
852	 * Reservations are disabled. Handle this the old way.
853	 */
854
855	bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
856
857	numfound = bitoff = startoff = 0;
858	lastzero = -1;
859	left = le32_to_cpu(alloc->id1.bitmap1.i_total);
860	while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
861		if (bitoff == left) {
862			/* mlog(0, "bitoff (%d) == left", bitoff); */
863			break;
864		}
865		/* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
866		   "numfound = %d\n", bitoff, startoff, numfound);*/
867
868		/* Ok, we found a zero bit... is it contig. or do we
869		 * start over?*/
870		if (bitoff == startoff) {
871			/* we found a zero */
872			numfound++;
873			startoff++;
874		} else {
875			/* got a zero after some ones */
876			numfound = 1;
877			startoff = bitoff+1;
878		}
879		/* we got everything we needed */
880		if (numfound == *numbits) {
881			/* mlog(0, "Found it all!\n"); */
882			break;
883		}
884	}
885
886	mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff,
887	     numfound);
888
889	if (numfound == *numbits)
890		bitoff = startoff - numfound;
891	else
892		bitoff = -1;
893
894bail:
895	if (local_resv)
896		ocfs2_resv_discard(resmap, resv);
897
898	mlog_exit(bitoff);
899	return bitoff;
900}
901
902static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
903{
904	struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
905	int i;
906	mlog_entry_void();
907
908	alloc->id1.bitmap1.i_total = 0;
909	alloc->id1.bitmap1.i_used = 0;
910	la->la_bm_off = 0;
911	for(i = 0; i < le16_to_cpu(la->la_size); i++)
912		la->la_bitmap[i] = 0;
913
914	mlog_exit_void();
915}
916
917
918/*
919 * sync the local alloc to main bitmap.
920 *
921 * assumes you've already locked the main bitmap -- the bitmap inode
922 * passed is used for caching.
923 */
924static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
925				    handle_t *handle,
926				    struct ocfs2_dinode *alloc,
927				    struct inode *main_bm_inode,
928				    struct buffer_head *main_bm_bh)
929{
930	int status = 0;
931	int bit_off, left, count, start;
932	u64 la_start_blk;
933	u64 blkno;
934	void *bitmap;
935	struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
936
937	mlog_entry("total = %u, used = %u\n",
938		   le32_to_cpu(alloc->id1.bitmap1.i_total),
939		   le32_to_cpu(alloc->id1.bitmap1.i_used));
940
941	if (!alloc->id1.bitmap1.i_total) {
942		mlog(0, "nothing to sync!\n");
943		goto bail;
944	}
945
946	if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
947	    le32_to_cpu(alloc->id1.bitmap1.i_total)) {
948		mlog(0, "all bits were taken!\n");
949		goto bail;
950	}
951
952	la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
953						le32_to_cpu(la->la_bm_off));
954	bitmap = la->la_bitmap;
955	start = count = bit_off = 0;
956	left = le32_to_cpu(alloc->id1.bitmap1.i_total);
957
958	while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
959	       != -1) {
960		if ((bit_off < left) && (bit_off == start)) {
961			count++;
962			start++;
963			continue;
964		}
965		if (count) {
966			blkno = la_start_blk +
967				ocfs2_clusters_to_blocks(osb->sb,
968							 start - count);
969
970			mlog(0, "freeing %u bits starting at local alloc bit "
971			     "%u (la_start_blk = %llu, blkno = %llu)\n",
972			     count, start - count,
973			     (unsigned long long)la_start_blk,
974			     (unsigned long long)blkno);
975
976			status = ocfs2_release_clusters(handle,
977							main_bm_inode,
978							main_bm_bh, blkno,
979							count);
980			if (status < 0) {
981				mlog_errno(status);
982				goto bail;
983			}
984		}
985		if (bit_off >= left)
986			break;
987		count = 1;
988		start = bit_off + 1;
989	}
990
991bail:
992	mlog_exit(status);
993	return status;
994}
995
996enum ocfs2_la_event {
997	OCFS2_LA_EVENT_SLIDE,		/* Normal window slide. */
998	OCFS2_LA_EVENT_FRAGMENTED,	/* The global bitmap has
999					 * enough bits theoretically
1000					 * free, but a contiguous
1001					 * allocation could not be
1002					 * found. */
1003	OCFS2_LA_EVENT_ENOSPC,		/* Global bitmap doesn't have
1004					 * enough bits free to satisfy
1005					 * our request. */
1006};
1007#define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
1008/*
1009 * Given an event, calculate the size of our next local alloc window.
1010 *
1011 * This should always be called under i_mutex of the local alloc inode
1012 * so that local alloc disabling doesn't race with processes trying to
1013 * use the allocator.
1014 *
1015 * Returns the state which the local alloc was left in. This value can
1016 * be ignored by some paths.
1017 */
1018static int ocfs2_recalc_la_window(struct ocfs2_super *osb,
1019				  enum ocfs2_la_event event)
1020{
1021	unsigned int bits;
1022	int state;
1023
1024	spin_lock(&osb->osb_lock);
1025	if (osb->local_alloc_state == OCFS2_LA_DISABLED) {
1026		WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED);
1027		goto out_unlock;
1028	}
1029
1030	/*
1031	 * ENOSPC and fragmentation are treated similarly for now.
1032	 */
1033	if (event == OCFS2_LA_EVENT_ENOSPC ||
1034	    event == OCFS2_LA_EVENT_FRAGMENTED) {
1035		/*
1036		 * We ran out of contiguous space in the primary
1037		 * bitmap. Drastically reduce the number of bits used
1038		 * by local alloc until we have to disable it.
1039		 */
1040		bits = osb->local_alloc_bits >> 1;
1041		if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) {
1042			/*
1043			 * By setting state to THROTTLED, we'll keep
1044			 * the number of local alloc bits used down
1045			 * until an event occurs which would give us
1046			 * reason to assume the bitmap situation might
1047			 * have changed.
1048			 */
1049			osb->local_alloc_state = OCFS2_LA_THROTTLED;
1050			osb->local_alloc_bits = bits;
1051		} else {
1052			osb->local_alloc_state = OCFS2_LA_DISABLED;
1053		}
1054		queue_delayed_work(ocfs2_wq, &osb->la_enable_wq,
1055				   OCFS2_LA_ENABLE_INTERVAL);
1056		goto out_unlock;
1057	}
1058
1059	/*
1060	 * Don't increase the size of the local alloc window until we
1061	 * know we might be able to fulfill the request. Otherwise, we
1062	 * risk bouncing around the global bitmap during periods of
1063	 * low space.
1064	 */
1065	if (osb->local_alloc_state != OCFS2_LA_THROTTLED)
1066		osb->local_alloc_bits = osb->local_alloc_default_bits;
1067
1068out_unlock:
1069	state = osb->local_alloc_state;
1070	spin_unlock(&osb->osb_lock);
1071
1072	return state;
1073}
1074
1075static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
1076						struct ocfs2_alloc_context **ac,
1077						struct inode **bitmap_inode,
1078						struct buffer_head **bitmap_bh)
1079{
1080	int status;
1081
1082	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
1083	if (!(*ac)) {
1084		status = -ENOMEM;
1085		mlog_errno(status);
1086		goto bail;
1087	}
1088
1089retry_enospc:
1090	(*ac)->ac_bits_wanted = osb->local_alloc_default_bits;
1091	status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
1092	if (status == -ENOSPC) {
1093		if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) ==
1094		    OCFS2_LA_DISABLED)
1095			goto bail;
1096
1097		ocfs2_free_ac_resource(*ac);
1098		memset(*ac, 0, sizeof(struct ocfs2_alloc_context));
1099		goto retry_enospc;
1100	}
1101	if (status < 0) {
1102		mlog_errno(status);
1103		goto bail;
1104	}
1105
1106	*bitmap_inode = (*ac)->ac_inode;
1107	igrab(*bitmap_inode);
1108	*bitmap_bh = (*ac)->ac_bh;
1109	get_bh(*bitmap_bh);
1110	status = 0;
1111bail:
1112	if ((status < 0) && *ac) {
1113		ocfs2_free_alloc_context(*ac);
1114		*ac = NULL;
1115	}
1116
1117	mlog_exit(status);
1118	return status;
1119}
1120
1121/*
1122 * pass it the bitmap lock in lock_bh if you have it.
1123 */
1124static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
1125					handle_t *handle,
1126					struct ocfs2_alloc_context *ac)
1127{
1128	int status = 0;
1129	u32 cluster_off, cluster_count;
1130	struct ocfs2_dinode *alloc = NULL;
1131	struct ocfs2_local_alloc *la;
1132
1133	mlog_entry_void();
1134
1135	alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
1136	la = OCFS2_LOCAL_ALLOC(alloc);
1137
1138	if (alloc->id1.bitmap1.i_total)
1139		mlog(0, "asking me to alloc a new window over a non-empty "
1140		     "one\n");
1141
1142	mlog(0, "Allocating %u clusters for a new window.\n",
1143	     osb->local_alloc_bits);
1144
1145	/* Instruct the allocation code to try the most recently used
1146	 * cluster group. We'll re-record the group used this pass
1147	 * below. */
1148	ac->ac_last_group = osb->la_last_gd;
1149
1150	/* we used the generic suballoc reserve function, but we set
1151	 * everything up nicely, so there's no reason why we can't use
1152	 * the more specific cluster api to claim bits. */
1153	status = ocfs2_claim_clusters(handle, ac, osb->local_alloc_bits,
1154				      &cluster_off, &cluster_count);
1155	if (status == -ENOSPC) {
1156retry_enospc:
1157		/*
1158		 * Note: We could also try syncing the journal here to
1159		 * allow use of any free bits which the current
1160		 * transaction can't give us access to. --Mark
1161		 */
1162		if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) ==
1163		    OCFS2_LA_DISABLED)
1164			goto bail;
1165
1166		ac->ac_bits_wanted = osb->local_alloc_default_bits;
1167		status = ocfs2_claim_clusters(handle, ac,
1168					      osb->local_alloc_bits,
1169					      &cluster_off,
1170					      &cluster_count);
1171		if (status == -ENOSPC)
1172			goto retry_enospc;
1173		/*
1174		 * We only shrunk the *minimum* number of in our
1175		 * request - it's entirely possible that the allocator
1176		 * might give us more than we asked for.
1177		 */
1178		if (status == 0) {
1179			spin_lock(&osb->osb_lock);
1180			osb->local_alloc_bits = cluster_count;
1181			spin_unlock(&osb->osb_lock);
1182		}
1183	}
1184	if (status < 0) {
1185		if (status != -ENOSPC)
1186			mlog_errno(status);
1187		goto bail;
1188	}
1189
1190	osb->la_last_gd = ac->ac_last_group;
1191
1192	la->la_bm_off = cpu_to_le32(cluster_off);
1193	alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
1194	/* just in case... In the future when we find space ourselves,
1195	 * we don't have to get all contiguous -- but we'll have to
1196	 * set all previously used bits in bitmap and update
1197	 * la_bits_set before setting the bits in the main bitmap. */
1198	alloc->id1.bitmap1.i_used = 0;
1199	memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
1200	       le16_to_cpu(la->la_size));
1201
1202	ocfs2_resmap_restart(&osb->osb_la_resmap, cluster_count,
1203			     OCFS2_LOCAL_ALLOC(alloc)->la_bitmap);
1204
1205	mlog(0, "New window allocated:\n");
1206	mlog(0, "window la_bm_off = %u\n",
1207	     OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
1208	mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total));
1209
1210bail:
1211	mlog_exit(status);
1212	return status;
1213}
1214
1215/* Note that we do *NOT* lock the local alloc inode here as
1216 * it's been locked already for us. */
1217static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
1218					  struct inode *local_alloc_inode)
1219{
1220	int status = 0;
1221	struct buffer_head *main_bm_bh = NULL;
1222	struct inode *main_bm_inode = NULL;
1223	handle_t *handle = NULL;
1224	struct ocfs2_dinode *alloc;
1225	struct ocfs2_dinode *alloc_copy = NULL;
1226	struct ocfs2_alloc_context *ac = NULL;
1227
1228	mlog_entry_void();
1229
1230	ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
1231
1232	/* This will lock the main bitmap for us. */
1233	status = ocfs2_local_alloc_reserve_for_window(osb,
1234						      &ac,
1235						      &main_bm_inode,
1236						      &main_bm_bh);
1237	if (status < 0) {
1238		if (status != -ENOSPC)
1239			mlog_errno(status);
1240		goto bail;
1241	}
1242
1243	handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
1244	if (IS_ERR(handle)) {
1245		status = PTR_ERR(handle);
1246		handle = NULL;
1247		mlog_errno(status);
1248		goto bail;
1249	}
1250
1251	alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
1252
1253	/* We want to clear the local alloc before doing anything
1254	 * else, so that if we error later during this operation,
1255	 * local alloc shutdown won't try to double free main bitmap
1256	 * bits. Make a copy so the sync function knows which bits to
1257	 * free. */
1258	alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS);
1259	if (!alloc_copy) {
1260		status = -ENOMEM;
1261		mlog_errno(status);
1262		goto bail;
1263	}
1264	memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
1265
1266	status = ocfs2_journal_access_di(handle,
1267					 INODE_CACHE(local_alloc_inode),
1268					 osb->local_alloc_bh,
1269					 OCFS2_JOURNAL_ACCESS_WRITE);
1270	if (status < 0) {
1271		mlog_errno(status);
1272		goto bail;
1273	}
1274
1275	ocfs2_clear_local_alloc(alloc);
1276	ocfs2_journal_dirty(handle, osb->local_alloc_bh);
1277
1278	status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
1279					  main_bm_inode, main_bm_bh);
1280	if (status < 0) {
1281		mlog_errno(status);
1282		goto bail;
1283	}
1284
1285	status = ocfs2_local_alloc_new_window(osb, handle, ac);
1286	if (status < 0) {
1287		if (status != -ENOSPC)
1288			mlog_errno(status);
1289		goto bail;
1290	}
1291
1292	atomic_inc(&osb->alloc_stats.moves);
1293
1294bail:
1295	if (handle)
1296		ocfs2_commit_trans(osb, handle);
1297
1298	brelse(main_bm_bh);
1299
1300	if (main_bm_inode)
1301		iput(main_bm_inode);
1302
1303	if (alloc_copy)
1304		kfree(alloc_copy);
1305
1306	if (ac)
1307		ocfs2_free_alloc_context(ac);
1308
1309	mlog_exit(status);
1310	return status;
1311}
1312