mac80211: Check port authorization in the ieee80211_tx_dequeue() case
[linux/fpc-iii.git] / fs / ocfs2 / resize.c
blob18451e0fab8192628412db781eae9e1fad07247b
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * resize.c
6 * volume resize.
7 * Inspired by ext3/resize.c.
9 * Copyright (C) 2007 Oracle. All rights reserved.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
27 #include <linux/fs.h>
28 #include <linux/types.h>
30 #include <cluster/masklog.h>
32 #include "ocfs2.h"
34 #include "alloc.h"
35 #include "dlmglue.h"
36 #include "inode.h"
37 #include "journal.h"
38 #include "super.h"
39 #include "sysfile.h"
40 #include "uptodate.h"
41 #include "ocfs2_trace.h"
43 #include "buffer_head_io.h"
44 #include "suballoc.h"
45 #include "resize.h"
48 * Check whether there are new backup superblocks exist
49 * in the last group. If there are some, mark them or clear
50 * them in the bitmap.
52 * Return how many backups we find in the last group.
54 static u16 ocfs2_calc_new_backup_super(struct inode *inode,
55 struct ocfs2_group_desc *gd,
56 u16 cl_cpg,
57 u16 old_bg_clusters,
58 int set)
60 int i;
61 u16 backups = 0;
62 u32 cluster, lgd_cluster;
63 u64 blkno, gd_blkno, lgd_blkno = le64_to_cpu(gd->bg_blkno);
65 for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
66 blkno = ocfs2_backup_super_blkno(inode->i_sb, i);
67 cluster = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
69 gd_blkno = ocfs2_which_cluster_group(inode, cluster);
70 if (gd_blkno < lgd_blkno)
71 continue;
72 else if (gd_blkno > lgd_blkno)
73 break;
75 /* check if already done backup super */
76 lgd_cluster = ocfs2_blocks_to_clusters(inode->i_sb, lgd_blkno);
77 lgd_cluster += old_bg_clusters;
78 if (lgd_cluster >= cluster)
79 continue;
81 if (set)
82 ocfs2_set_bit(cluster % cl_cpg,
83 (unsigned long *)gd->bg_bitmap);
84 else
85 ocfs2_clear_bit(cluster % cl_cpg,
86 (unsigned long *)gd->bg_bitmap);
87 backups++;
90 return backups;
93 static int ocfs2_update_last_group_and_inode(handle_t *handle,
94 struct inode *bm_inode,
95 struct buffer_head *bm_bh,
96 struct buffer_head *group_bh,
97 u32 first_new_cluster,
98 int new_clusters)
100 int ret = 0;
101 struct ocfs2_super *osb = OCFS2_SB(bm_inode->i_sb);
102 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bm_bh->b_data;
103 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
104 struct ocfs2_chain_rec *cr;
105 struct ocfs2_group_desc *group;
106 u16 chain, num_bits, backups = 0;
107 u16 cl_bpc = le16_to_cpu(cl->cl_bpc);
108 u16 cl_cpg = le16_to_cpu(cl->cl_cpg);
109 u16 old_bg_clusters;
111 trace_ocfs2_update_last_group_and_inode(new_clusters,
112 first_new_cluster);
114 ret = ocfs2_journal_access_gd(handle, INODE_CACHE(bm_inode),
115 group_bh, OCFS2_JOURNAL_ACCESS_WRITE);
116 if (ret < 0) {
117 mlog_errno(ret);
118 goto out;
121 group = (struct ocfs2_group_desc *)group_bh->b_data;
123 old_bg_clusters = le16_to_cpu(group->bg_bits) / cl_bpc;
124 /* update the group first. */
125 num_bits = new_clusters * cl_bpc;
126 le16_add_cpu(&group->bg_bits, num_bits);
127 le16_add_cpu(&group->bg_free_bits_count, num_bits);
130 * check whether there are some new backup superblocks exist in
131 * this group and update the group bitmap accordingly.
133 if (OCFS2_HAS_COMPAT_FEATURE(osb->sb,
134 OCFS2_FEATURE_COMPAT_BACKUP_SB)) {
135 backups = ocfs2_calc_new_backup_super(bm_inode,
136 group,
137 cl_cpg, old_bg_clusters, 1);
138 le16_add_cpu(&group->bg_free_bits_count, -1 * backups);
141 ocfs2_journal_dirty(handle, group_bh);
143 /* update the inode accordingly. */
144 ret = ocfs2_journal_access_di(handle, INODE_CACHE(bm_inode), bm_bh,
145 OCFS2_JOURNAL_ACCESS_WRITE);
146 if (ret < 0) {
147 mlog_errno(ret);
148 goto out_rollback;
151 chain = le16_to_cpu(group->bg_chain);
152 cr = (&cl->cl_recs[chain]);
153 le32_add_cpu(&cr->c_total, num_bits);
154 le32_add_cpu(&cr->c_free, num_bits);
155 le32_add_cpu(&fe->id1.bitmap1.i_total, num_bits);
156 le32_add_cpu(&fe->i_clusters, new_clusters);
158 if (backups) {
159 le32_add_cpu(&cr->c_free, -1 * backups);
160 le32_add_cpu(&fe->id1.bitmap1.i_used, backups);
163 spin_lock(&OCFS2_I(bm_inode)->ip_lock);
164 OCFS2_I(bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
165 le64_add_cpu(&fe->i_size, (u64)new_clusters << osb->s_clustersize_bits);
166 spin_unlock(&OCFS2_I(bm_inode)->ip_lock);
167 i_size_write(bm_inode, le64_to_cpu(fe->i_size));
169 ocfs2_journal_dirty(handle, bm_bh);
171 out_rollback:
172 if (ret < 0) {
173 ocfs2_calc_new_backup_super(bm_inode,
174 group,
175 cl_cpg, old_bg_clusters, 0);
176 le16_add_cpu(&group->bg_free_bits_count, backups);
177 le16_add_cpu(&group->bg_bits, -1 * num_bits);
178 le16_add_cpu(&group->bg_free_bits_count, -1 * num_bits);
180 out:
181 if (ret)
182 mlog_errno(ret);
183 return ret;
186 static int update_backups(struct inode * inode, u32 clusters, char *data)
188 int i, ret = 0;
189 u32 cluster;
190 u64 blkno;
191 struct buffer_head *backup = NULL;
192 struct ocfs2_dinode *backup_di = NULL;
193 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
195 /* calculate the real backups we need to update. */
196 for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
197 blkno = ocfs2_backup_super_blkno(inode->i_sb, i);
198 cluster = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
199 if (cluster >= clusters)
200 break;
202 ret = ocfs2_read_blocks_sync(osb, blkno, 1, &backup);
203 if (ret < 0) {
204 mlog_errno(ret);
205 break;
208 memcpy(backup->b_data, data, inode->i_sb->s_blocksize);
210 backup_di = (struct ocfs2_dinode *)backup->b_data;
211 backup_di->i_blkno = cpu_to_le64(blkno);
213 ret = ocfs2_write_super_or_backup(osb, backup);
214 brelse(backup);
215 backup = NULL;
216 if (ret < 0) {
217 mlog_errno(ret);
218 break;
222 return ret;
225 static void ocfs2_update_super_and_backups(struct inode *inode,
226 int new_clusters)
228 int ret;
229 u32 clusters = 0;
230 struct buffer_head *super_bh = NULL;
231 struct ocfs2_dinode *super_di = NULL;
232 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
235 * update the superblock last.
236 * It doesn't matter if the write failed.
238 ret = ocfs2_read_blocks_sync(osb, OCFS2_SUPER_BLOCK_BLKNO, 1,
239 &super_bh);
240 if (ret < 0) {
241 mlog_errno(ret);
242 goto out;
245 super_di = (struct ocfs2_dinode *)super_bh->b_data;
246 le32_add_cpu(&super_di->i_clusters, new_clusters);
247 clusters = le32_to_cpu(super_di->i_clusters);
249 ret = ocfs2_write_super_or_backup(osb, super_bh);
250 if (ret < 0) {
251 mlog_errno(ret);
252 goto out;
255 if (OCFS2_HAS_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_COMPAT_BACKUP_SB))
256 ret = update_backups(inode, clusters, super_bh->b_data);
258 out:
259 brelse(super_bh);
260 if (ret)
261 printk(KERN_WARNING "ocfs2: Failed to update super blocks on %s"
262 " during fs resize. This condition is not fatal,"
263 " but fsck.ocfs2 should be run to fix it\n",
264 osb->dev_str);
265 return;
269 * Extend the filesystem to the new number of clusters specified. This entry
270 * point is only used to extend the current filesystem to the end of the last
271 * existing group.
273 int ocfs2_group_extend(struct inode * inode, int new_clusters)
275 int ret;
276 handle_t *handle;
277 struct buffer_head *main_bm_bh = NULL;
278 struct buffer_head *group_bh = NULL;
279 struct inode *main_bm_inode = NULL;
280 struct ocfs2_dinode *fe = NULL;
281 struct ocfs2_group_desc *group = NULL;
282 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
283 u16 cl_bpc;
284 u32 first_new_cluster;
285 u64 lgd_blkno;
287 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
288 return -EROFS;
290 if (new_clusters < 0)
291 return -EINVAL;
292 else if (new_clusters == 0)
293 return 0;
295 main_bm_inode = ocfs2_get_system_file_inode(osb,
296 GLOBAL_BITMAP_SYSTEM_INODE,
297 OCFS2_INVALID_SLOT);
298 if (!main_bm_inode) {
299 ret = -EINVAL;
300 mlog_errno(ret);
301 goto out;
304 inode_lock(main_bm_inode);
306 ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
307 if (ret < 0) {
308 mlog_errno(ret);
309 goto out_mutex;
312 fe = (struct ocfs2_dinode *)main_bm_bh->b_data;
314 /* main_bm_bh is validated by inode read inside ocfs2_inode_lock(),
315 * so any corruption is a code bug. */
316 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
318 if (le16_to_cpu(fe->id2.i_chain.cl_cpg) !=
319 ocfs2_group_bitmap_size(osb->sb, 0,
320 osb->s_feature_incompat) * 8) {
321 mlog(ML_ERROR, "The disk is too old and small. "
322 "Force to do offline resize.");
323 ret = -EINVAL;
324 goto out_unlock;
327 first_new_cluster = le32_to_cpu(fe->i_clusters);
328 lgd_blkno = ocfs2_which_cluster_group(main_bm_inode,
329 first_new_cluster - 1);
331 ret = ocfs2_read_group_descriptor(main_bm_inode, fe, lgd_blkno,
332 &group_bh);
333 if (ret < 0) {
334 mlog_errno(ret);
335 goto out_unlock;
337 group = (struct ocfs2_group_desc *)group_bh->b_data;
339 cl_bpc = le16_to_cpu(fe->id2.i_chain.cl_bpc);
340 if (le16_to_cpu(group->bg_bits) / cl_bpc + new_clusters >
341 le16_to_cpu(fe->id2.i_chain.cl_cpg)) {
342 ret = -EINVAL;
343 goto out_unlock;
347 trace_ocfs2_group_extend(
348 (unsigned long long)le64_to_cpu(group->bg_blkno), new_clusters);
350 handle = ocfs2_start_trans(osb, OCFS2_GROUP_EXTEND_CREDITS);
351 if (IS_ERR(handle)) {
352 mlog_errno(PTR_ERR(handle));
353 ret = -EINVAL;
354 goto out_unlock;
357 /* update the last group descriptor and inode. */
358 ret = ocfs2_update_last_group_and_inode(handle, main_bm_inode,
359 main_bm_bh, group_bh,
360 first_new_cluster,
361 new_clusters);
362 if (ret) {
363 mlog_errno(ret);
364 goto out_commit;
367 ocfs2_update_super_and_backups(main_bm_inode, new_clusters);
369 out_commit:
370 ocfs2_commit_trans(osb, handle);
371 out_unlock:
372 brelse(group_bh);
373 brelse(main_bm_bh);
375 ocfs2_inode_unlock(main_bm_inode, 1);
377 out_mutex:
378 inode_unlock(main_bm_inode);
379 iput(main_bm_inode);
381 out:
382 return ret;
385 static int ocfs2_check_new_group(struct inode *inode,
386 struct ocfs2_dinode *di,
387 struct ocfs2_new_group_input *input,
388 struct buffer_head *group_bh)
390 int ret;
391 struct ocfs2_group_desc *gd =
392 (struct ocfs2_group_desc *)group_bh->b_data;
393 u16 cl_bpc = le16_to_cpu(di->id2.i_chain.cl_bpc);
395 ret = ocfs2_check_group_descriptor(inode->i_sb, di, group_bh);
396 if (ret)
397 goto out;
399 ret = -EINVAL;
400 if (le16_to_cpu(gd->bg_chain) != input->chain)
401 mlog(ML_ERROR, "Group descriptor # %llu has bad chain %u "
402 "while input has %u set.\n",
403 (unsigned long long)le64_to_cpu(gd->bg_blkno),
404 le16_to_cpu(gd->bg_chain), input->chain);
405 else if (le16_to_cpu(gd->bg_bits) != input->clusters * cl_bpc)
406 mlog(ML_ERROR, "Group descriptor # %llu has bit count %u but "
407 "input has %u clusters set\n",
408 (unsigned long long)le64_to_cpu(gd->bg_blkno),
409 le16_to_cpu(gd->bg_bits), input->clusters);
410 else if (le16_to_cpu(gd->bg_free_bits_count) != input->frees * cl_bpc)
411 mlog(ML_ERROR, "Group descriptor # %llu has free bit count %u "
412 "but it should have %u set\n",
413 (unsigned long long)le64_to_cpu(gd->bg_blkno),
414 le16_to_cpu(gd->bg_bits),
415 input->frees * cl_bpc);
416 else
417 ret = 0;
419 out:
420 return ret;
423 static int ocfs2_verify_group_and_input(struct inode *inode,
424 struct ocfs2_dinode *di,
425 struct ocfs2_new_group_input *input,
426 struct buffer_head *group_bh)
428 u16 cl_count = le16_to_cpu(di->id2.i_chain.cl_count);
429 u16 cl_cpg = le16_to_cpu(di->id2.i_chain.cl_cpg);
430 u16 next_free = le16_to_cpu(di->id2.i_chain.cl_next_free_rec);
431 u32 cluster = ocfs2_blocks_to_clusters(inode->i_sb, input->group);
432 u32 total_clusters = le32_to_cpu(di->i_clusters);
433 int ret = -EINVAL;
435 if (cluster < total_clusters)
436 mlog(ML_ERROR, "add a group which is in the current volume.\n");
437 else if (input->chain >= cl_count)
438 mlog(ML_ERROR, "input chain exceeds the limit.\n");
439 else if (next_free != cl_count && next_free != input->chain)
440 mlog(ML_ERROR,
441 "the add group should be in chain %u\n", next_free);
442 else if (total_clusters + input->clusters < total_clusters)
443 mlog(ML_ERROR, "add group's clusters overflow.\n");
444 else if (input->clusters > cl_cpg)
445 mlog(ML_ERROR, "the cluster exceeds the maximum of a group\n");
446 else if (input->frees > input->clusters)
447 mlog(ML_ERROR, "the free cluster exceeds the total clusters\n");
448 else if (total_clusters % cl_cpg != 0)
449 mlog(ML_ERROR,
450 "the last group isn't full. Use group extend first.\n");
451 else if (input->group != ocfs2_which_cluster_group(inode, cluster))
452 mlog(ML_ERROR, "group blkno is invalid\n");
453 else if ((ret = ocfs2_check_new_group(inode, di, input, group_bh)))
454 mlog(ML_ERROR, "group descriptor check failed.\n");
455 else
456 ret = 0;
458 return ret;
461 /* Add a new group descriptor to global_bitmap. */
462 int ocfs2_group_add(struct inode *inode, struct ocfs2_new_group_input *input)
464 int ret;
465 handle_t *handle;
466 struct buffer_head *main_bm_bh = NULL;
467 struct inode *main_bm_inode = NULL;
468 struct ocfs2_dinode *fe = NULL;
469 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
470 struct buffer_head *group_bh = NULL;
471 struct ocfs2_group_desc *group = NULL;
472 struct ocfs2_chain_list *cl;
473 struct ocfs2_chain_rec *cr;
474 u16 cl_bpc;
475 u64 bg_ptr;
477 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
478 return -EROFS;
480 main_bm_inode = ocfs2_get_system_file_inode(osb,
481 GLOBAL_BITMAP_SYSTEM_INODE,
482 OCFS2_INVALID_SLOT);
483 if (!main_bm_inode) {
484 ret = -EINVAL;
485 mlog_errno(ret);
486 goto out;
489 inode_lock(main_bm_inode);
491 ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
492 if (ret < 0) {
493 mlog_errno(ret);
494 goto out_mutex;
497 fe = (struct ocfs2_dinode *)main_bm_bh->b_data;
499 if (le16_to_cpu(fe->id2.i_chain.cl_cpg) !=
500 ocfs2_group_bitmap_size(osb->sb, 0,
501 osb->s_feature_incompat) * 8) {
502 mlog(ML_ERROR, "The disk is too old and small."
503 " Force to do offline resize.");
504 ret = -EINVAL;
505 goto out_unlock;
508 ret = ocfs2_read_blocks_sync(osb, input->group, 1, &group_bh);
509 if (ret < 0) {
510 mlog(ML_ERROR, "Can't read the group descriptor # %llu "
511 "from the device.", (unsigned long long)input->group);
512 goto out_unlock;
515 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), group_bh);
517 ret = ocfs2_verify_group_and_input(main_bm_inode, fe, input, group_bh);
518 if (ret) {
519 mlog_errno(ret);
520 goto out_free_group_bh;
523 trace_ocfs2_group_add((unsigned long long)input->group,
524 input->chain, input->clusters, input->frees);
526 handle = ocfs2_start_trans(osb, OCFS2_GROUP_ADD_CREDITS);
527 if (IS_ERR(handle)) {
528 mlog_errno(PTR_ERR(handle));
529 ret = -EINVAL;
530 goto out_free_group_bh;
533 cl_bpc = le16_to_cpu(fe->id2.i_chain.cl_bpc);
534 cl = &fe->id2.i_chain;
535 cr = &cl->cl_recs[input->chain];
537 ret = ocfs2_journal_access_gd(handle, INODE_CACHE(main_bm_inode),
538 group_bh, OCFS2_JOURNAL_ACCESS_WRITE);
539 if (ret < 0) {
540 mlog_errno(ret);
541 goto out_commit;
544 group = (struct ocfs2_group_desc *)group_bh->b_data;
545 bg_ptr = le64_to_cpu(group->bg_next_group);
546 group->bg_next_group = cr->c_blkno;
547 ocfs2_journal_dirty(handle, group_bh);
549 ret = ocfs2_journal_access_di(handle, INODE_CACHE(main_bm_inode),
550 main_bm_bh, OCFS2_JOURNAL_ACCESS_WRITE);
551 if (ret < 0) {
552 group->bg_next_group = cpu_to_le64(bg_ptr);
553 mlog_errno(ret);
554 goto out_commit;
557 if (input->chain == le16_to_cpu(cl->cl_next_free_rec)) {
558 le16_add_cpu(&cl->cl_next_free_rec, 1);
559 memset(cr, 0, sizeof(struct ocfs2_chain_rec));
562 cr->c_blkno = cpu_to_le64(input->group);
563 le32_add_cpu(&cr->c_total, input->clusters * cl_bpc);
564 le32_add_cpu(&cr->c_free, input->frees * cl_bpc);
566 le32_add_cpu(&fe->id1.bitmap1.i_total, input->clusters *cl_bpc);
567 le32_add_cpu(&fe->id1.bitmap1.i_used,
568 (input->clusters - input->frees) * cl_bpc);
569 le32_add_cpu(&fe->i_clusters, input->clusters);
571 ocfs2_journal_dirty(handle, main_bm_bh);
573 spin_lock(&OCFS2_I(main_bm_inode)->ip_lock);
574 OCFS2_I(main_bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
575 le64_add_cpu(&fe->i_size, (u64)input->clusters << osb->s_clustersize_bits);
576 spin_unlock(&OCFS2_I(main_bm_inode)->ip_lock);
577 i_size_write(main_bm_inode, le64_to_cpu(fe->i_size));
579 ocfs2_update_super_and_backups(main_bm_inode, input->clusters);
581 out_commit:
582 ocfs2_commit_trans(osb, handle);
584 out_free_group_bh:
585 brelse(group_bh);
587 out_unlock:
588 brelse(main_bm_bh);
590 ocfs2_inode_unlock(main_bm_inode, 1);
592 out_mutex:
593 inode_unlock(main_bm_inode);
594 iput(main_bm_inode);
596 out:
597 return ret;