ocfs2: clean up localalloc mount option size parsing
[safe/jmp/linux-2.6] / fs / ocfs2 / localalloc.c
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
51 static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc);
52
53 static 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
58 static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc);
59
60 static 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
66 static 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
71 static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
72                                         handle_t *handle,
73                                         struct ocfs2_alloc_context *ac);
74
75 static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
76                                           struct inode *local_alloc_inode);
77
78 void 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
106 static 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
112 void 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
125 void 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
135 /*
136  * Tell us whether a given allocation should use the local alloc
137  * file. Otherwise, it has to go to the main bitmap.
138  *
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.
141  */
142 int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
143 {
144         int ret = 0;
145         int la_bits;
146
147         spin_lock(&osb->osb_lock);
148         la_bits = osb->local_alloc_bits;
149
150         if (!ocfs2_la_state_enabled(osb))
151                 goto bail;
152
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))
158                 goto bail;
159
160         ret = 1;
161 bail:
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);
164         spin_unlock(&osb->osb_lock);
165         return ret;
166 }
167
168 int 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
179         if (osb->local_alloc_bits == 0)
180                 goto bail;
181
182         if (osb->local_alloc_bits >= osb->bitmap_cpg) {
183                 mlog(ML_NOTICE, "Requested local alloc window %d is larger "
184                      "than max possible %u. Using defaults.\n",
185                      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);
189         }
190
191         /* 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
200         status = ocfs2_read_inode_block_full(inode, &alloc_bh,
201                                              OCFS2_BH_IGNORE_CACHE);
202         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))) {
212                 mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
213                      (unsigned long long)OCFS2_I(inode)->ip_blkno);
214                 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
244 bail:
245         if (status < 0)
246                 brelse(alloc_bh);
247         if (inode)
248                 iput(inode);
249
250         mlog(0, "Local alloc window bits = %d\n", osb->local_alloc_bits);
251
252         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.*/
263 void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
264 {
265         int status;
266         handle_t *handle;
267         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
276         cancel_delayed_work(&osb->la_enable_wq);
277         flush_workqueue(ocfs2_wq);
278
279         if (osb->local_alloc_state == OCFS2_LA_UNUSED)
280                 goto out;
281
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);
289                 goto out;
290         }
291
292         osb->local_alloc_state = OCFS2_LA_DISABLED;
293
294         ocfs2_resmap_uninit(&osb->osb_la_resmap);
295
296         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);
302                 goto out;
303         }
304
305         mutex_lock(&main_bm_inode->i_mutex);
306
307         status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
308         if (status < 0) {
309                 mlog_errno(status);
310                 goto out_mutex;
311         }
312
313         /* WINDOW_MOVE_CREDITS is a bit heavy... */
314         handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
315         if (IS_ERR(handle)) {
316                 mlog_errno(PTR_ERR(handle));
317                 handle = NULL;
318                 goto out_unlock;
319         }
320
321         bh = osb->local_alloc_bh;
322         alloc = (struct ocfs2_dinode *) bh->b_data;
323
324         alloc_copy = kmalloc(bh->b_size, GFP_NOFS);
325         if (!alloc_copy) {
326                 status = -ENOMEM;
327                 goto out_commit;
328         }
329         memcpy(alloc_copy, alloc, bh->b_size);
330
331         status = ocfs2_journal_access_di(handle, INODE_CACHE(local_alloc_inode),
332                                          bh, OCFS2_JOURNAL_ACCESS_WRITE);
333         if (status < 0) {
334                 mlog_errno(status);
335                 goto out_commit;
336         }
337
338         ocfs2_clear_local_alloc(alloc);
339         ocfs2_journal_dirty(handle, bh);
340
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
350 out_commit:
351         ocfs2_commit_trans(osb, handle);
352
353 out_unlock:
354         brelse(main_bm_bh);
355
356         ocfs2_inode_unlock(main_bm_inode, 1);
357
358 out_mutex:
359         mutex_unlock(&main_bm_inode->i_mutex);
360         iput(main_bm_inode);
361
362 out:
363         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  */
379 int 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
401         mutex_lock(&inode->i_mutex);
402
403         status = ocfs2_read_inode_block_full(inode, &alloc_bh,
404                                              OCFS2_BH_IGNORE_CACHE);
405         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
420         ocfs2_compute_meta_ecc(osb->sb, alloc_bh->b_data, &alloc->i_check);
421         status = ocfs2_write_block(osb, alloc_bh, INODE_CACHE(inode));
422         if (status < 0)
423                 mlog_errno(status);
424
425 bail:
426         if ((status < 0) && (*alloc_copy)) {
427                 kfree(*alloc_copy);
428                 *alloc_copy = NULL;
429         }
430
431         brelse(alloc_bh);
432
433         if (inode) {
434                 mutex_unlock(&inode->i_mutex);
435                 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  */
448 int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
449                                         struct ocfs2_dinode *alloc)
450 {
451         int status;
452         handle_t *handle;
453         struct buffer_head *main_bm_bh = NULL;
454         struct inode *main_bm_inode;
455
456         mlog_entry_void();
457
458         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);
464                 goto out;
465         }
466
467         mutex_lock(&main_bm_inode->i_mutex);
468
469         status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
470         if (status < 0) {
471                 mlog_errno(status);
472                 goto out_mutex;
473         }
474
475         handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
476         if (IS_ERR(handle)) {
477                 status = PTR_ERR(handle);
478                 handle = NULL;
479                 mlog_errno(status);
480                 goto out_unlock;
481         }
482
483         /* we want the bitmap change to be recorded on disk asap */
484         handle->h_sync = 1;
485
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
491         ocfs2_commit_trans(osb, handle);
492
493 out_unlock:
494         ocfs2_inode_unlock(main_bm_inode, 1);
495
496 out_mutex:
497         mutex_unlock(&main_bm_inode->i_mutex);
498
499         brelse(main_bm_bh);
500
501         iput(main_bm_inode);
502
503 out:
504         if (!status)
505                 ocfs2_init_steal_slots(osb);
506         mlog_exit(status);
507         return status;
508 }
509
510 /*
511  * make sure we've got at least bits_wanted contiguous bits in the
512  * local alloc. You lose them when you drop i_mutex.
513  *
514  * We will add ourselves to the transaction passed in, but may start
515  * our own in order to shift windows.
516  */
517 int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
518                                    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
528         BUG_ON(!ac);
529
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         }
539
540         mutex_lock(&local_alloc_inode->i_mutex);
541
542         /*
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);
550                 status = -ENOSPC;
551                 goto bail;
552         }
553         spin_unlock(&osb->osb_lock);
554
555         alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
556
557 #ifdef CONFIG_OCFS2_DEBUG_FS
558         if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
559             ocfs2_local_alloc_count_bits(alloc)) {
560                 ocfs2_error(osb->sb, "local alloc inode %llu says it has "
561                             "%u free bits, but a count shows %u",
562                             (unsigned long long)le64_to_cpu(alloc->i_blkno),
563                             le32_to_cpu(alloc->id1.bitmap1.i_used),
564                             ocfs2_local_alloc_count_bits(alloc));
565                 status = -EIO;
566                 goto bail;
567         }
568 #endif
569
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                 }
581
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;
596         }
597
598         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
602         ac->ac_inode = local_alloc_inode;
603         /* We should never use localalloc from another slot */
604         ac->ac_alloc_slot = osb->slot_num;
605         ac->ac_which = OCFS2_AC_USE_LOCAL;
606         get_bh(osb->local_alloc_bh);
607         ac->ac_bh = osb->local_alloc_bh;
608         status = 0;
609 bail:
610         if (status < 0 && local_alloc_inode) {
611                 mutex_unlock(&local_alloc_inode->i_mutex);
612                 iput(local_alloc_inode);
613         }
614
615         mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num,
616              status);
617
618         mlog_exit(status);
619         return status;
620 }
621
622 int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
623                                  handle_t *handle,
624                                  struct ocfs2_alloc_context *ac,
625                                  u32 bits_wanted,
626                                  u32 *bit_off,
627                                  u32 *num_bits)
628 {
629         int status, start;
630         struct inode *local_alloc_inode;
631         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
638         local_alloc_inode = ac->ac_inode;
639         alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
640         la = OCFS2_LOCAL_ALLOC(alloc);
641
642         start = ocfs2_local_alloc_find_clear_bits(osb, alloc, &bits_wanted,
643                                                   ac->ac_resv);
644         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;
653         *num_bits = bits_wanted;
654
655         status = ocfs2_journal_access_di(handle,
656                                          INODE_CACHE(local_alloc_inode),
657                                          osb->local_alloc_bh,
658                                          OCFS2_JOURNAL_ACCESS_WRITE);
659         if (status < 0) {
660                 mlog_errno(status);
661                 goto bail;
662         }
663
664         ocfs2_resmap_claimed_bits(&osb->osb_la_resmap, ac->ac_resv, start,
665                                   bits_wanted);
666
667         while(bits_wanted--)
668                 ocfs2_set_bit(start++, bitmap);
669
670         le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits);
671         ocfs2_journal_dirty(handle, osb->local_alloc_bh);
672
673 bail:
674         mlog_exit(status);
675         return status;
676 }
677
678 static 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
695 static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
696                                      struct ocfs2_dinode *alloc,
697                                      u32 *numbits,
698                                      struct ocfs2_alloc_reservation *resv)
699 {
700         int numfound, bitoff, left, startoff, lastzero;
701         int local_resv = 0;
702         struct ocfs2_alloc_reservation r;
703         void *bitmap = NULL;
704         struct ocfs2_reservation_map *resmap = &osb->osb_la_resmap;
705
706         mlog_entry("(numbits wanted = %u)\n", *numbits);
707
708         if (!alloc->id1.bitmap1.i_total) {
709                 mlog(0, "No bits in my window!\n");
710                 bitoff = -1;
711                 goto bail;
712         }
713
714         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
738         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 */
763                 if (numfound == *numbits) {
764                         /* 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
772         if (numfound == *numbits) {
773                 bitoff = startoff - numfound;
774                 *numbits = numfound;
775         } else {
776                 numfound = 0;
777                 bitoff = -1;
778         }
779
780 bail:
781         if (local_resv)
782                 ocfs2_resv_discard(resmap, resv);
783
784         mlog_exit(bitoff);
785         return bitoff;
786 }
787
788 static 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. */
805 static 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  */
828 static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
829                                     handle_t *handle,
830                                     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
841         mlog_entry("total = %u, used = %u\n",
842                    le32_to_cpu(alloc->id1.bitmap1.i_total),
843                    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
874                         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);
879
880                         status = ocfs2_release_clusters(handle,
881                                                         main_bm_inode,
882                                                         main_bm_bh, blkno,
883                                                         count);
884                         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
895 bail:
896         mlog_exit(status);
897         return status;
898 }
899
900 enum 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  */
922 static 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
972 out_unlock:
973         state = osb->local_alloc_state;
974         spin_unlock(&osb->osb_lock);
975
976         return state;
977 }
978
979 static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
980                                                 struct ocfs2_alloc_context **ac,
981                                                 struct inode **bitmap_inode,
982                                                 struct buffer_head **bitmap_bh)
983 {
984         int status;
985
986         *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
987         if (!(*ac)) {
988                 status = -ENOMEM;
989                 mlog_errno(status);
990                 goto bail;
991         }
992
993 retry_enospc:
994         (*ac)->ac_bits_wanted = osb->local_alloc_default_bits;
995         status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
996         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         }
1005         if (status < 0) {
1006                 mlog_errno(status);
1007                 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;
1015 bail:
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  */
1028 static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
1029                                         handle_t *handle,
1030                                         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",
1047              osb->local_alloc_bits);
1048
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
1054         /* 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. */
1057         status = ocfs2_claim_clusters(osb, handle, ac, osb->local_alloc_bits,
1058                                       &cluster_off, &cluster_count);
1059         if (status == -ENOSPC) {
1060 retry_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
1070                 ac->ac_bits_wanted = osb->local_alloc_default_bits;
1071                 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         }
1088         if (status < 0) {
1089                 if (status != -ENOSPC)
1090                         mlog_errno(status);
1091                 goto bail;
1092         }
1093
1094         osb->la_last_gd = ac->ac_last_group;
1095
1096         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
1106         ocfs2_resmap_restart(&osb->osb_la_resmap, cluster_count,
1107                              OCFS2_LOCAL_ALLOC(alloc)->la_bitmap);
1108
1109         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
1114 bail:
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. */
1121 static 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;
1127         handle_t *handle = NULL;
1128         struct ocfs2_dinode *alloc;
1129         struct ocfs2_dinode *alloc_copy = NULL;
1130         struct ocfs2_alloc_context *ac = NULL;
1131
1132         mlog_entry_void();
1133
1134         ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
1135
1136         /* This will lock the main bitmap for us. */
1137         status = ocfs2_local_alloc_reserve_for_window(osb,
1138                                                       &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
1147         handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
1148         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. */
1162         alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS);
1163         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
1170         status = ocfs2_journal_access_di(handle,
1171                                          INODE_CACHE(local_alloc_inode),
1172                                          osb->local_alloc_bh,
1173                                          OCFS2_JOURNAL_ACCESS_WRITE);
1174         if (status < 0) {
1175                 mlog_errno(status);
1176                 goto bail;
1177         }
1178
1179         ocfs2_clear_local_alloc(alloc);
1180         ocfs2_journal_dirty(handle, osb->local_alloc_bh);
1181
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
1198 bail:
1199         if (handle)
1200                 ocfs2_commit_trans(osb, handle);
1201
1202         brelse(main_bm_bh);
1203
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