0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/fs.h>
0011 #include <linux/types.h>
0012 #include <linux/slab.h>
0013 #include <linux/highmem.h>
0014 #include <linux/bitops.h>
0015
0016 #include <cluster/masklog.h>
0017
0018 #include "ocfs2.h"
0019
0020 #include "alloc.h"
0021 #include "blockcheck.h"
0022 #include "dlmglue.h"
0023 #include "inode.h"
0024 #include "journal.h"
0025 #include "localalloc.h"
0026 #include "suballoc.h"
0027 #include "super.h"
0028 #include "sysfile.h"
0029 #include "ocfs2_trace.h"
0030
0031 #include "buffer_head_io.h"
0032
0033 #define OCFS2_LOCAL_ALLOC(dinode) (&((dinode)->id2.i_lab))
0034
0035 static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc);
0036
0037 static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
0038 struct ocfs2_dinode *alloc,
0039 u32 *numbits,
0040 struct ocfs2_alloc_reservation *resv);
0041
0042 static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc);
0043
0044 static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
0045 handle_t *handle,
0046 struct ocfs2_dinode *alloc,
0047 struct inode *main_bm_inode,
0048 struct buffer_head *main_bm_bh);
0049
0050 static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
0051 struct ocfs2_alloc_context **ac,
0052 struct inode **bitmap_inode,
0053 struct buffer_head **bitmap_bh);
0054
0055 static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
0056 handle_t *handle,
0057 struct ocfs2_alloc_context *ac);
0058
0059 static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
0060 struct inode *local_alloc_inode);
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099 #define OCFS2_LA_MAX_DEFAULT_MB 256
0100 #define OCFS2_LA_OLD_DEFAULT 8
0101 unsigned int ocfs2_la_default_mb(struct ocfs2_super *osb)
0102 {
0103 unsigned int la_mb;
0104 unsigned int gd_mb;
0105 unsigned int la_max_mb;
0106 unsigned int megs_per_slot;
0107 struct super_block *sb = osb->sb;
0108
0109 gd_mb = ocfs2_clusters_to_megabytes(osb->sb,
0110 8 * ocfs2_group_bitmap_size(sb, 0, osb->s_feature_incompat));
0111
0112
0113
0114
0115
0116
0117 if ((sb->s_blocksize == 512 && osb->s_clustersize <= 8192)
0118 || (sb->s_blocksize == 1024 && osb->s_clustersize == 4096))
0119 return OCFS2_LA_OLD_DEFAULT;
0120
0121
0122
0123
0124
0125 gd_mb -= 16;
0126 gd_mb &= 0xFFFFFFFB;
0127
0128 la_mb = gd_mb;
0129
0130
0131
0132
0133 if (la_mb > OCFS2_LA_MAX_DEFAULT_MB) {
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152 if (gd_mb > (2 * OCFS2_LA_MAX_DEFAULT_MB))
0153 la_mb = 256;
0154 else {
0155 unsigned int gd_mult = gd_mb;
0156
0157 while (gd_mult > 256)
0158 gd_mult = gd_mult >> 1;
0159
0160 la_mb = gd_mult;
0161 }
0162 }
0163
0164 megs_per_slot = osb->osb_clusters_at_boot / osb->max_slots;
0165 megs_per_slot = ocfs2_clusters_to_megabytes(osb->sb, megs_per_slot);
0166
0167 if (megs_per_slot < la_mb)
0168 la_mb = megs_per_slot;
0169
0170
0171 la_max_mb = ocfs2_clusters_to_megabytes(osb->sb,
0172 ocfs2_local_alloc_size(sb) * 8);
0173 if (la_mb > la_max_mb)
0174 la_mb = la_max_mb;
0175
0176 return la_mb;
0177 }
0178
0179 void ocfs2_la_set_sizes(struct ocfs2_super *osb, int requested_mb)
0180 {
0181 struct super_block *sb = osb->sb;
0182 unsigned int la_default_mb = ocfs2_la_default_mb(osb);
0183 unsigned int la_max_mb;
0184
0185 la_max_mb = ocfs2_clusters_to_megabytes(sb,
0186 ocfs2_local_alloc_size(sb) * 8);
0187
0188 trace_ocfs2_la_set_sizes(requested_mb, la_max_mb, la_default_mb);
0189
0190 if (requested_mb == -1) {
0191
0192 osb->local_alloc_default_bits =
0193 ocfs2_megabytes_to_clusters(sb, la_default_mb);
0194 } else if (requested_mb > la_max_mb) {
0195
0196 osb->local_alloc_default_bits =
0197 ocfs2_megabytes_to_clusters(sb, la_max_mb);
0198 } else {
0199 osb->local_alloc_default_bits =
0200 ocfs2_megabytes_to_clusters(sb, requested_mb);
0201 }
0202
0203 osb->local_alloc_bits = osb->local_alloc_default_bits;
0204 }
0205
0206 static inline int ocfs2_la_state_enabled(struct ocfs2_super *osb)
0207 {
0208 return (osb->local_alloc_state == OCFS2_LA_THROTTLED ||
0209 osb->local_alloc_state == OCFS2_LA_ENABLED);
0210 }
0211
0212 void ocfs2_local_alloc_seen_free_bits(struct ocfs2_super *osb,
0213 unsigned int num_clusters)
0214 {
0215 spin_lock(&osb->osb_lock);
0216 if (osb->local_alloc_state == OCFS2_LA_DISABLED ||
0217 osb->local_alloc_state == OCFS2_LA_THROTTLED)
0218 if (num_clusters >= osb->local_alloc_default_bits) {
0219 cancel_delayed_work(&osb->la_enable_wq);
0220 osb->local_alloc_state = OCFS2_LA_ENABLED;
0221 }
0222 spin_unlock(&osb->osb_lock);
0223 }
0224
0225 void ocfs2_la_enable_worker(struct work_struct *work)
0226 {
0227 struct ocfs2_super *osb =
0228 container_of(work, struct ocfs2_super,
0229 la_enable_wq.work);
0230 spin_lock(&osb->osb_lock);
0231 osb->local_alloc_state = OCFS2_LA_ENABLED;
0232 spin_unlock(&osb->osb_lock);
0233 }
0234
0235
0236
0237
0238
0239
0240
0241
0242 int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
0243 {
0244 int ret = 0;
0245 int la_bits;
0246
0247 spin_lock(&osb->osb_lock);
0248 la_bits = osb->local_alloc_bits;
0249
0250 if (!ocfs2_la_state_enabled(osb))
0251 goto bail;
0252
0253
0254
0255
0256
0257 if (bits > (la_bits / 2))
0258 goto bail;
0259
0260 ret = 1;
0261 bail:
0262 trace_ocfs2_alloc_should_use_local(
0263 (unsigned long long)bits, osb->local_alloc_state, la_bits, ret);
0264 spin_unlock(&osb->osb_lock);
0265 return ret;
0266 }
0267
0268 int ocfs2_load_local_alloc(struct ocfs2_super *osb)
0269 {
0270 int status = 0;
0271 struct ocfs2_dinode *alloc = NULL;
0272 struct buffer_head *alloc_bh = NULL;
0273 u32 num_used;
0274 struct inode *inode = NULL;
0275 struct ocfs2_local_alloc *la;
0276
0277 if (osb->local_alloc_bits == 0)
0278 goto bail;
0279
0280 if (osb->local_alloc_bits >= osb->bitmap_cpg) {
0281 mlog(ML_NOTICE, "Requested local alloc window %d is larger "
0282 "than max possible %u. Using defaults.\n",
0283 osb->local_alloc_bits, (osb->bitmap_cpg - 1));
0284 osb->local_alloc_bits =
0285 ocfs2_megabytes_to_clusters(osb->sb,
0286 ocfs2_la_default_mb(osb));
0287 }
0288
0289
0290 inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
0291 osb->slot_num);
0292 if (!inode) {
0293 status = -EINVAL;
0294 mlog_errno(status);
0295 goto bail;
0296 }
0297
0298 status = ocfs2_read_inode_block_full(inode, &alloc_bh,
0299 OCFS2_BH_IGNORE_CACHE);
0300 if (status < 0) {
0301 mlog_errno(status);
0302 goto bail;
0303 }
0304
0305 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
0306 la = OCFS2_LOCAL_ALLOC(alloc);
0307
0308 if (!(le32_to_cpu(alloc->i_flags) &
0309 (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
0310 mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
0311 (unsigned long long)OCFS2_I(inode)->ip_blkno);
0312 status = -EINVAL;
0313 goto bail;
0314 }
0315
0316 if ((la->la_size == 0) ||
0317 (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) {
0318 mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n",
0319 le16_to_cpu(la->la_size));
0320 status = -EINVAL;
0321 goto bail;
0322 }
0323
0324
0325 num_used = ocfs2_local_alloc_count_bits(alloc);
0326
0327
0328
0329 if (num_used
0330 || alloc->id1.bitmap1.i_used
0331 || alloc->id1.bitmap1.i_total
0332 || la->la_bm_off) {
0333 mlog(ML_ERROR, "inconsistent detected, clean journal with"
0334 " unrecovered local alloc, please run fsck.ocfs2!\n"
0335 "found = %u, set = %u, taken = %u, off = %u\n",
0336 num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
0337 le32_to_cpu(alloc->id1.bitmap1.i_total),
0338 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
0339
0340 status = -EINVAL;
0341 goto bail;
0342 }
0343
0344 osb->local_alloc_bh = alloc_bh;
0345 osb->local_alloc_state = OCFS2_LA_ENABLED;
0346
0347 bail:
0348 if (status < 0)
0349 brelse(alloc_bh);
0350 iput(inode);
0351
0352 trace_ocfs2_load_local_alloc(osb->local_alloc_bits);
0353
0354 if (status)
0355 mlog_errno(status);
0356 return status;
0357 }
0358
0359
0360
0361
0362
0363
0364
0365
0366 void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
0367 {
0368 int status;
0369 handle_t *handle;
0370 struct inode *local_alloc_inode = NULL;
0371 struct buffer_head *bh = NULL;
0372 struct buffer_head *main_bm_bh = NULL;
0373 struct inode *main_bm_inode = NULL;
0374 struct ocfs2_dinode *alloc_copy = NULL;
0375 struct ocfs2_dinode *alloc = NULL;
0376
0377 cancel_delayed_work(&osb->la_enable_wq);
0378 if (osb->ocfs2_wq)
0379 flush_workqueue(osb->ocfs2_wq);
0380
0381 if (osb->local_alloc_state == OCFS2_LA_UNUSED)
0382 goto out;
0383
0384 local_alloc_inode =
0385 ocfs2_get_system_file_inode(osb,
0386 LOCAL_ALLOC_SYSTEM_INODE,
0387 osb->slot_num);
0388 if (!local_alloc_inode) {
0389 status = -ENOENT;
0390 mlog_errno(status);
0391 goto out;
0392 }
0393
0394 osb->local_alloc_state = OCFS2_LA_DISABLED;
0395
0396 ocfs2_resmap_uninit(&osb->osb_la_resmap);
0397
0398 main_bm_inode = ocfs2_get_system_file_inode(osb,
0399 GLOBAL_BITMAP_SYSTEM_INODE,
0400 OCFS2_INVALID_SLOT);
0401 if (!main_bm_inode) {
0402 status = -EINVAL;
0403 mlog_errno(status);
0404 goto out;
0405 }
0406
0407 inode_lock(main_bm_inode);
0408
0409 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
0410 if (status < 0) {
0411 mlog_errno(status);
0412 goto out_mutex;
0413 }
0414
0415
0416 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
0417 if (IS_ERR(handle)) {
0418 mlog_errno(PTR_ERR(handle));
0419 handle = NULL;
0420 goto out_unlock;
0421 }
0422
0423 bh = osb->local_alloc_bh;
0424 alloc = (struct ocfs2_dinode *) bh->b_data;
0425
0426 alloc_copy = kmemdup(alloc, bh->b_size, GFP_NOFS);
0427 if (!alloc_copy) {
0428 status = -ENOMEM;
0429 goto out_commit;
0430 }
0431
0432 status = ocfs2_journal_access_di(handle, INODE_CACHE(local_alloc_inode),
0433 bh, OCFS2_JOURNAL_ACCESS_WRITE);
0434 if (status < 0) {
0435 mlog_errno(status);
0436 goto out_commit;
0437 }
0438
0439 ocfs2_clear_local_alloc(alloc);
0440 ocfs2_journal_dirty(handle, bh);
0441
0442 brelse(bh);
0443 osb->local_alloc_bh = NULL;
0444 osb->local_alloc_state = OCFS2_LA_UNUSED;
0445
0446 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
0447 main_bm_inode, main_bm_bh);
0448 if (status < 0)
0449 mlog_errno(status);
0450
0451 out_commit:
0452 ocfs2_commit_trans(osb, handle);
0453
0454 out_unlock:
0455 brelse(main_bm_bh);
0456
0457 ocfs2_inode_unlock(main_bm_inode, 1);
0458
0459 out_mutex:
0460 inode_unlock(main_bm_inode);
0461 iput(main_bm_inode);
0462
0463 out:
0464 iput(local_alloc_inode);
0465
0466 kfree(alloc_copy);
0467 }
0468
0469
0470
0471
0472
0473
0474
0475
0476 int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
0477 int slot_num,
0478 struct ocfs2_dinode **alloc_copy)
0479 {
0480 int status = 0;
0481 struct buffer_head *alloc_bh = NULL;
0482 struct inode *inode = NULL;
0483 struct ocfs2_dinode *alloc;
0484
0485 trace_ocfs2_begin_local_alloc_recovery(slot_num);
0486
0487 *alloc_copy = NULL;
0488
0489 inode = ocfs2_get_system_file_inode(osb,
0490 LOCAL_ALLOC_SYSTEM_INODE,
0491 slot_num);
0492 if (!inode) {
0493 status = -EINVAL;
0494 mlog_errno(status);
0495 goto bail;
0496 }
0497
0498 inode_lock(inode);
0499
0500 status = ocfs2_read_inode_block_full(inode, &alloc_bh,
0501 OCFS2_BH_IGNORE_CACHE);
0502 if (status < 0) {
0503 mlog_errno(status);
0504 goto bail;
0505 }
0506
0507 *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL);
0508 if (!(*alloc_copy)) {
0509 status = -ENOMEM;
0510 goto bail;
0511 }
0512 memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
0513
0514 alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
0515 ocfs2_clear_local_alloc(alloc);
0516
0517 ocfs2_compute_meta_ecc(osb->sb, alloc_bh->b_data, &alloc->i_check);
0518 status = ocfs2_write_block(osb, alloc_bh, INODE_CACHE(inode));
0519 if (status < 0)
0520 mlog_errno(status);
0521
0522 bail:
0523 if (status < 0) {
0524 kfree(*alloc_copy);
0525 *alloc_copy = NULL;
0526 }
0527
0528 brelse(alloc_bh);
0529
0530 if (inode) {
0531 inode_unlock(inode);
0532 iput(inode);
0533 }
0534
0535 if (status)
0536 mlog_errno(status);
0537 return status;
0538 }
0539
0540
0541
0542
0543
0544
0545
0546 int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
0547 struct ocfs2_dinode *alloc)
0548 {
0549 int status;
0550 handle_t *handle;
0551 struct buffer_head *main_bm_bh = NULL;
0552 struct inode *main_bm_inode;
0553
0554 main_bm_inode = ocfs2_get_system_file_inode(osb,
0555 GLOBAL_BITMAP_SYSTEM_INODE,
0556 OCFS2_INVALID_SLOT);
0557 if (!main_bm_inode) {
0558 status = -EINVAL;
0559 mlog_errno(status);
0560 goto out;
0561 }
0562
0563 inode_lock(main_bm_inode);
0564
0565 status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
0566 if (status < 0) {
0567 mlog_errno(status);
0568 goto out_mutex;
0569 }
0570
0571 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
0572 if (IS_ERR(handle)) {
0573 status = PTR_ERR(handle);
0574 handle = NULL;
0575 mlog_errno(status);
0576 goto out_unlock;
0577 }
0578
0579
0580 handle->h_sync = 1;
0581
0582 status = ocfs2_sync_local_to_main(osb, handle, alloc,
0583 main_bm_inode, main_bm_bh);
0584 if (status < 0)
0585 mlog_errno(status);
0586
0587 ocfs2_commit_trans(osb, handle);
0588
0589 out_unlock:
0590 ocfs2_inode_unlock(main_bm_inode, 1);
0591
0592 out_mutex:
0593 inode_unlock(main_bm_inode);
0594
0595 brelse(main_bm_bh);
0596
0597 iput(main_bm_inode);
0598
0599 out:
0600 if (!status)
0601 ocfs2_init_steal_slots(osb);
0602 if (status)
0603 mlog_errno(status);
0604 return status;
0605 }
0606
0607
0608
0609
0610
0611
0612
0613
0614 int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
0615 u32 bits_wanted,
0616 struct ocfs2_alloc_context *ac)
0617 {
0618 int status;
0619 struct ocfs2_dinode *alloc;
0620 struct inode *local_alloc_inode;
0621 unsigned int free_bits;
0622
0623 BUG_ON(!ac);
0624
0625 local_alloc_inode =
0626 ocfs2_get_system_file_inode(osb,
0627 LOCAL_ALLOC_SYSTEM_INODE,
0628 osb->slot_num);
0629 if (!local_alloc_inode) {
0630 status = -ENOENT;
0631 mlog_errno(status);
0632 goto bail;
0633 }
0634
0635 inode_lock(local_alloc_inode);
0636
0637
0638
0639
0640
0641 spin_lock(&osb->osb_lock);
0642 if (!ocfs2_la_state_enabled(osb) ||
0643 (bits_wanted > osb->local_alloc_bits)) {
0644 spin_unlock(&osb->osb_lock);
0645 status = -ENOSPC;
0646 goto bail;
0647 }
0648 spin_unlock(&osb->osb_lock);
0649
0650 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
0651
0652 #ifdef CONFIG_OCFS2_DEBUG_FS
0653 if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
0654 ocfs2_local_alloc_count_bits(alloc)) {
0655 status = ocfs2_error(osb->sb, "local alloc inode %llu says it has %u used bits, but a count shows %u\n",
0656 (unsigned long long)le64_to_cpu(alloc->i_blkno),
0657 le32_to_cpu(alloc->id1.bitmap1.i_used),
0658 ocfs2_local_alloc_count_bits(alloc));
0659 goto bail;
0660 }
0661 #endif
0662
0663 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
0664 le32_to_cpu(alloc->id1.bitmap1.i_used);
0665 if (bits_wanted > free_bits) {
0666
0667 status =
0668 ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
0669 if (status < 0) {
0670 if (status != -ENOSPC)
0671 mlog_errno(status);
0672 goto bail;
0673 }
0674
0675
0676
0677
0678
0679
0680
0681 status = -ENOSPC;
0682 if (!ocfs2_la_state_enabled(osb))
0683 goto bail;
0684
0685 free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
0686 le32_to_cpu(alloc->id1.bitmap1.i_used);
0687 if (bits_wanted > free_bits)
0688 goto bail;
0689 }
0690
0691 ac->ac_inode = local_alloc_inode;
0692
0693 ac->ac_alloc_slot = osb->slot_num;
0694 ac->ac_which = OCFS2_AC_USE_LOCAL;
0695 get_bh(osb->local_alloc_bh);
0696 ac->ac_bh = osb->local_alloc_bh;
0697 status = 0;
0698 bail:
0699 if (status < 0 && local_alloc_inode) {
0700 inode_unlock(local_alloc_inode);
0701 iput(local_alloc_inode);
0702 }
0703
0704 trace_ocfs2_reserve_local_alloc_bits(
0705 (unsigned long long)ac->ac_max_block,
0706 bits_wanted, osb->slot_num, status);
0707
0708 if (status)
0709 mlog_errno(status);
0710 return status;
0711 }
0712
0713 int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
0714 handle_t *handle,
0715 struct ocfs2_alloc_context *ac,
0716 u32 bits_wanted,
0717 u32 *bit_off,
0718 u32 *num_bits)
0719 {
0720 int status, start;
0721 struct inode *local_alloc_inode;
0722 void *bitmap;
0723 struct ocfs2_dinode *alloc;
0724 struct ocfs2_local_alloc *la;
0725
0726 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
0727
0728 local_alloc_inode = ac->ac_inode;
0729 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
0730 la = OCFS2_LOCAL_ALLOC(alloc);
0731
0732 start = ocfs2_local_alloc_find_clear_bits(osb, alloc, &bits_wanted,
0733 ac->ac_resv);
0734 if (start == -1) {
0735
0736 status = -ENOSPC;
0737 mlog_errno(status);
0738 goto bail;
0739 }
0740
0741 bitmap = la->la_bitmap;
0742 *bit_off = le32_to_cpu(la->la_bm_off) + start;
0743 *num_bits = bits_wanted;
0744
0745 status = ocfs2_journal_access_di(handle,
0746 INODE_CACHE(local_alloc_inode),
0747 osb->local_alloc_bh,
0748 OCFS2_JOURNAL_ACCESS_WRITE);
0749 if (status < 0) {
0750 mlog_errno(status);
0751 goto bail;
0752 }
0753
0754 ocfs2_resmap_claimed_bits(&osb->osb_la_resmap, ac->ac_resv, start,
0755 bits_wanted);
0756
0757 while(bits_wanted--)
0758 ocfs2_set_bit(start++, bitmap);
0759
0760 le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits);
0761 ocfs2_journal_dirty(handle, osb->local_alloc_bh);
0762
0763 bail:
0764 if (status)
0765 mlog_errno(status);
0766 return status;
0767 }
0768
0769 int ocfs2_free_local_alloc_bits(struct ocfs2_super *osb,
0770 handle_t *handle,
0771 struct ocfs2_alloc_context *ac,
0772 u32 bit_off,
0773 u32 num_bits)
0774 {
0775 int status, start;
0776 u32 clear_bits;
0777 struct inode *local_alloc_inode;
0778 void *bitmap;
0779 struct ocfs2_dinode *alloc;
0780 struct ocfs2_local_alloc *la;
0781
0782 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
0783
0784 local_alloc_inode = ac->ac_inode;
0785 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
0786 la = OCFS2_LOCAL_ALLOC(alloc);
0787
0788 bitmap = la->la_bitmap;
0789 start = bit_off - le32_to_cpu(la->la_bm_off);
0790 clear_bits = num_bits;
0791
0792 status = ocfs2_journal_access_di(handle,
0793 INODE_CACHE(local_alloc_inode),
0794 osb->local_alloc_bh,
0795 OCFS2_JOURNAL_ACCESS_WRITE);
0796 if (status < 0) {
0797 mlog_errno(status);
0798 goto bail;
0799 }
0800
0801 while (clear_bits--)
0802 ocfs2_clear_bit(start++, bitmap);
0803
0804 le32_add_cpu(&alloc->id1.bitmap1.i_used, -num_bits);
0805 ocfs2_journal_dirty(handle, osb->local_alloc_bh);
0806
0807 bail:
0808 return status;
0809 }
0810
0811 static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
0812 {
0813 u32 count;
0814 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
0815
0816 count = memweight(la->la_bitmap, le16_to_cpu(la->la_size));
0817
0818 trace_ocfs2_local_alloc_count_bits(count);
0819 return count;
0820 }
0821
0822 static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
0823 struct ocfs2_dinode *alloc,
0824 u32 *numbits,
0825 struct ocfs2_alloc_reservation *resv)
0826 {
0827 int numfound = 0, bitoff, left, startoff;
0828 int local_resv = 0;
0829 struct ocfs2_alloc_reservation r;
0830 void *bitmap = NULL;
0831 struct ocfs2_reservation_map *resmap = &osb->osb_la_resmap;
0832
0833 if (!alloc->id1.bitmap1.i_total) {
0834 bitoff = -1;
0835 goto bail;
0836 }
0837
0838 if (!resv) {
0839 local_resv = 1;
0840 ocfs2_resv_init_once(&r);
0841 ocfs2_resv_set_type(&r, OCFS2_RESV_FLAG_TMP);
0842 resv = &r;
0843 }
0844
0845 numfound = *numbits;
0846 if (ocfs2_resmap_resv_bits(resmap, resv, &bitoff, &numfound) == 0) {
0847 if (numfound < *numbits)
0848 *numbits = numfound;
0849 goto bail;
0850 }
0851
0852
0853
0854
0855
0856 BUG_ON(osb->osb_resv_level != 0);
0857
0858
0859
0860
0861
0862 bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
0863
0864 numfound = bitoff = startoff = 0;
0865 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
0866 while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
0867 if (bitoff == left) {
0868
0869 break;
0870 }
0871
0872
0873
0874
0875
0876 if (bitoff == startoff) {
0877
0878 numfound++;
0879 startoff++;
0880 } else {
0881
0882 numfound = 1;
0883 startoff = bitoff+1;
0884 }
0885
0886 if (numfound == *numbits) {
0887
0888 break;
0889 }
0890 }
0891
0892 trace_ocfs2_local_alloc_find_clear_bits_search_bitmap(bitoff, numfound);
0893
0894 if (numfound == *numbits)
0895 bitoff = startoff - numfound;
0896 else
0897 bitoff = -1;
0898
0899 bail:
0900 if (local_resv)
0901 ocfs2_resv_discard(resmap, resv);
0902
0903 trace_ocfs2_local_alloc_find_clear_bits(*numbits,
0904 le32_to_cpu(alloc->id1.bitmap1.i_total),
0905 bitoff, numfound);
0906
0907 return bitoff;
0908 }
0909
0910 static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
0911 {
0912 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
0913 int i;
0914
0915 alloc->id1.bitmap1.i_total = 0;
0916 alloc->id1.bitmap1.i_used = 0;
0917 la->la_bm_off = 0;
0918 for(i = 0; i < le16_to_cpu(la->la_size); i++)
0919 la->la_bitmap[i] = 0;
0920 }
0921
0922 #if 0
0923
0924 static void ocfs2_verify_zero_bits(unsigned long *bitmap,
0925 unsigned int start,
0926 unsigned int count)
0927 {
0928 unsigned int tmp = count;
0929 while(tmp--) {
0930 if (ocfs2_test_bit(start + tmp, bitmap)) {
0931 printk("ocfs2_verify_zero_bits: start = %u, count = "
0932 "%u\n", start, count);
0933 printk("ocfs2_verify_zero_bits: bit %u is set!",
0934 start + tmp);
0935 BUG();
0936 }
0937 }
0938 }
0939 #endif
0940
0941
0942
0943
0944
0945
0946
0947 static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
0948 handle_t *handle,
0949 struct ocfs2_dinode *alloc,
0950 struct inode *main_bm_inode,
0951 struct buffer_head *main_bm_bh)
0952 {
0953 int status = 0;
0954 int bit_off, left, count, start;
0955 u64 la_start_blk;
0956 u64 blkno;
0957 void *bitmap;
0958 struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
0959
0960 trace_ocfs2_sync_local_to_main(
0961 le32_to_cpu(alloc->id1.bitmap1.i_total),
0962 le32_to_cpu(alloc->id1.bitmap1.i_used));
0963
0964 if (!alloc->id1.bitmap1.i_total) {
0965 goto bail;
0966 }
0967
0968 if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
0969 le32_to_cpu(alloc->id1.bitmap1.i_total)) {
0970 goto bail;
0971 }
0972
0973 la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
0974 le32_to_cpu(la->la_bm_off));
0975 bitmap = la->la_bitmap;
0976 start = count = bit_off = 0;
0977 left = le32_to_cpu(alloc->id1.bitmap1.i_total);
0978
0979 while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
0980 != -1) {
0981 if ((bit_off < left) && (bit_off == start)) {
0982 count++;
0983 start++;
0984 continue;
0985 }
0986 if (count) {
0987 blkno = la_start_blk +
0988 ocfs2_clusters_to_blocks(osb->sb,
0989 start - count);
0990
0991 trace_ocfs2_sync_local_to_main_free(
0992 count, start - count,
0993 (unsigned long long)la_start_blk,
0994 (unsigned long long)blkno);
0995
0996 status = ocfs2_release_clusters(handle,
0997 main_bm_inode,
0998 main_bm_bh, blkno,
0999 count);
1000 if (status < 0) {
1001 mlog_errno(status);
1002 goto bail;
1003 }
1004 }
1005 if (bit_off >= left)
1006 break;
1007 count = 1;
1008 start = bit_off + 1;
1009 }
1010
1011 bail:
1012 if (status)
1013 mlog_errno(status);
1014 return status;
1015 }
1016
1017 enum ocfs2_la_event {
1018 OCFS2_LA_EVENT_SLIDE,
1019 OCFS2_LA_EVENT_FRAGMENTED,
1020
1021
1022
1023
1024 OCFS2_LA_EVENT_ENOSPC,
1025
1026
1027 };
1028 #define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039 static int ocfs2_recalc_la_window(struct ocfs2_super *osb,
1040 enum ocfs2_la_event event)
1041 {
1042 unsigned int bits;
1043 int state;
1044
1045 spin_lock(&osb->osb_lock);
1046 if (osb->local_alloc_state == OCFS2_LA_DISABLED) {
1047 WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED);
1048 goto out_unlock;
1049 }
1050
1051
1052
1053
1054 if (event == OCFS2_LA_EVENT_ENOSPC ||
1055 event == OCFS2_LA_EVENT_FRAGMENTED) {
1056
1057
1058
1059
1060
1061 bits = osb->local_alloc_bits >> 1;
1062 if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) {
1063
1064
1065
1066
1067
1068
1069
1070 osb->local_alloc_state = OCFS2_LA_THROTTLED;
1071 osb->local_alloc_bits = bits;
1072 } else {
1073 osb->local_alloc_state = OCFS2_LA_DISABLED;
1074 }
1075 queue_delayed_work(osb->ocfs2_wq, &osb->la_enable_wq,
1076 OCFS2_LA_ENABLE_INTERVAL);
1077 goto out_unlock;
1078 }
1079
1080
1081
1082
1083
1084
1085
1086 if (osb->local_alloc_state != OCFS2_LA_THROTTLED)
1087 osb->local_alloc_bits = osb->local_alloc_default_bits;
1088
1089 out_unlock:
1090 state = osb->local_alloc_state;
1091 spin_unlock(&osb->osb_lock);
1092
1093 return state;
1094 }
1095
1096 static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
1097 struct ocfs2_alloc_context **ac,
1098 struct inode **bitmap_inode,
1099 struct buffer_head **bitmap_bh)
1100 {
1101 int status;
1102
1103 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
1104 if (!(*ac)) {
1105 status = -ENOMEM;
1106 mlog_errno(status);
1107 goto bail;
1108 }
1109
1110 retry_enospc:
1111 (*ac)->ac_bits_wanted = osb->local_alloc_bits;
1112 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
1113 if (status == -ENOSPC) {
1114 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) ==
1115 OCFS2_LA_DISABLED)
1116 goto bail;
1117
1118 ocfs2_free_ac_resource(*ac);
1119 memset(*ac, 0, sizeof(struct ocfs2_alloc_context));
1120 goto retry_enospc;
1121 }
1122 if (status < 0) {
1123 mlog_errno(status);
1124 goto bail;
1125 }
1126
1127 *bitmap_inode = (*ac)->ac_inode;
1128 igrab(*bitmap_inode);
1129 *bitmap_bh = (*ac)->ac_bh;
1130 get_bh(*bitmap_bh);
1131 status = 0;
1132 bail:
1133 if ((status < 0) && *ac) {
1134 ocfs2_free_alloc_context(*ac);
1135 *ac = NULL;
1136 }
1137
1138 if (status)
1139 mlog_errno(status);
1140 return status;
1141 }
1142
1143
1144
1145
1146 static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
1147 handle_t *handle,
1148 struct ocfs2_alloc_context *ac)
1149 {
1150 int status = 0;
1151 u32 cluster_off, cluster_count;
1152 struct ocfs2_dinode *alloc = NULL;
1153 struct ocfs2_local_alloc *la;
1154
1155 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
1156 la = OCFS2_LOCAL_ALLOC(alloc);
1157
1158 trace_ocfs2_local_alloc_new_window(
1159 le32_to_cpu(alloc->id1.bitmap1.i_total),
1160 osb->local_alloc_bits);
1161
1162
1163
1164
1165 ac->ac_last_group = osb->la_last_gd;
1166
1167
1168
1169
1170 status = ocfs2_claim_clusters(handle, ac, osb->local_alloc_bits,
1171 &cluster_off, &cluster_count);
1172 if (status == -ENOSPC) {
1173 retry_enospc:
1174
1175
1176
1177
1178
1179 if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) ==
1180 OCFS2_LA_DISABLED)
1181 goto bail;
1182
1183 ac->ac_bits_wanted = osb->local_alloc_bits;
1184 status = ocfs2_claim_clusters(handle, ac,
1185 osb->local_alloc_bits,
1186 &cluster_off,
1187 &cluster_count);
1188 if (status == -ENOSPC)
1189 goto retry_enospc;
1190
1191
1192
1193
1194
1195 if (status == 0) {
1196 spin_lock(&osb->osb_lock);
1197 osb->local_alloc_bits = cluster_count;
1198 spin_unlock(&osb->osb_lock);
1199 }
1200 }
1201 if (status < 0) {
1202 if (status != -ENOSPC)
1203 mlog_errno(status);
1204 goto bail;
1205 }
1206
1207 osb->la_last_gd = ac->ac_last_group;
1208
1209 la->la_bm_off = cpu_to_le32(cluster_off);
1210 alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
1211
1212
1213
1214
1215 alloc->id1.bitmap1.i_used = 0;
1216 memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
1217 le16_to_cpu(la->la_size));
1218
1219 ocfs2_resmap_restart(&osb->osb_la_resmap, cluster_count,
1220 OCFS2_LOCAL_ALLOC(alloc)->la_bitmap);
1221
1222 trace_ocfs2_local_alloc_new_window_result(
1223 OCFS2_LOCAL_ALLOC(alloc)->la_bm_off,
1224 le32_to_cpu(alloc->id1.bitmap1.i_total));
1225
1226 bail:
1227 if (status)
1228 mlog_errno(status);
1229 return status;
1230 }
1231
1232
1233
1234 static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
1235 struct inode *local_alloc_inode)
1236 {
1237 int status = 0;
1238 struct buffer_head *main_bm_bh = NULL;
1239 struct inode *main_bm_inode = NULL;
1240 handle_t *handle = NULL;
1241 struct ocfs2_dinode *alloc;
1242 struct ocfs2_dinode *alloc_copy = NULL;
1243 struct ocfs2_alloc_context *ac = NULL;
1244
1245 ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
1246
1247
1248 status = ocfs2_local_alloc_reserve_for_window(osb,
1249 &ac,
1250 &main_bm_inode,
1251 &main_bm_bh);
1252 if (status < 0) {
1253 if (status != -ENOSPC)
1254 mlog_errno(status);
1255 goto bail;
1256 }
1257
1258 handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
1259 if (IS_ERR(handle)) {
1260 status = PTR_ERR(handle);
1261 handle = NULL;
1262 mlog_errno(status);
1263 goto bail;
1264 }
1265
1266 alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
1267
1268
1269
1270
1271
1272
1273 alloc_copy = kmemdup(alloc, osb->local_alloc_bh->b_size, GFP_NOFS);
1274 if (!alloc_copy) {
1275 status = -ENOMEM;
1276 mlog_errno(status);
1277 goto bail;
1278 }
1279
1280 status = ocfs2_journal_access_di(handle,
1281 INODE_CACHE(local_alloc_inode),
1282 osb->local_alloc_bh,
1283 OCFS2_JOURNAL_ACCESS_WRITE);
1284 if (status < 0) {
1285 mlog_errno(status);
1286 goto bail;
1287 }
1288
1289 ocfs2_clear_local_alloc(alloc);
1290 ocfs2_journal_dirty(handle, osb->local_alloc_bh);
1291
1292 status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
1293 main_bm_inode, main_bm_bh);
1294 if (status < 0) {
1295 mlog_errno(status);
1296 goto bail;
1297 }
1298
1299 status = ocfs2_local_alloc_new_window(osb, handle, ac);
1300 if (status < 0) {
1301 if (status != -ENOSPC)
1302 mlog_errno(status);
1303 goto bail;
1304 }
1305
1306 atomic_inc(&osb->alloc_stats.moves);
1307
1308 bail:
1309 if (handle)
1310 ocfs2_commit_trans(osb, handle);
1311
1312 brelse(main_bm_bh);
1313
1314 iput(main_bm_inode);
1315 kfree(alloc_copy);
1316
1317 if (ac)
1318 ocfs2_free_alloc_context(ac);
1319
1320 if (status)
1321 mlog_errno(status);
1322 return status;
1323 }
1324