resize.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * resize.c
  5. *
  6. * volume resize.
  7. * Inspired by ext3/resize.c.
  8. *
  9. * Copyright (C) 2007 Oracle. All rights reserved.
  10. *
  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.
  15. *
  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.
  20. *
  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.
  25. */
  26. #include <linux/fs.h>
  27. #include <linux/types.h>
  28. #define MLOG_MASK_PREFIX ML_DISK_ALLOC
  29. #include <cluster/masklog.h>
  30. #include "ocfs2.h"
  31. #include "alloc.h"
  32. #include "dlmglue.h"
  33. #include "inode.h"
  34. #include "journal.h"
  35. #include "super.h"
  36. #include "sysfile.h"
  37. #include "uptodate.h"
  38. #include "buffer_head_io.h"
  39. #include "suballoc.h"
  40. #include "resize.h"
  41. /*
  42. * Check whether there are new backup superblocks exist
  43. * in the last group. If there are some, mark them or clear
  44. * them in the bitmap.
  45. *
  46. * Return how many backups we find in the last group.
  47. */
  48. static u16 ocfs2_calc_new_backup_super(struct inode *inode,
  49. struct ocfs2_group_desc *gd,
  50. int new_clusters,
  51. u32 first_new_cluster,
  52. u16 cl_cpg,
  53. int set)
  54. {
  55. int i;
  56. u16 backups = 0;
  57. u32 cluster;
  58. u64 blkno, gd_blkno, lgd_blkno = le64_to_cpu(gd->bg_blkno);
  59. for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
  60. blkno = ocfs2_backup_super_blkno(inode->i_sb, i);
  61. cluster = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
  62. gd_blkno = ocfs2_which_cluster_group(inode, cluster);
  63. if (gd_blkno < lgd_blkno)
  64. continue;
  65. else if (gd_blkno > lgd_blkno)
  66. break;
  67. if (set)
  68. ocfs2_set_bit(cluster % cl_cpg,
  69. (unsigned long *)gd->bg_bitmap);
  70. else
  71. ocfs2_clear_bit(cluster % cl_cpg,
  72. (unsigned long *)gd->bg_bitmap);
  73. backups++;
  74. }
  75. mlog_exit_void();
  76. return backups;
  77. }
  78. static int ocfs2_update_last_group_and_inode(handle_t *handle,
  79. struct inode *bm_inode,
  80. struct buffer_head *bm_bh,
  81. struct buffer_head *group_bh,
  82. u32 first_new_cluster,
  83. int new_clusters)
  84. {
  85. int ret = 0;
  86. struct ocfs2_super *osb = OCFS2_SB(bm_inode->i_sb);
  87. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bm_bh->b_data;
  88. struct ocfs2_chain_list *cl = &fe->id2.i_chain;
  89. struct ocfs2_chain_rec *cr;
  90. struct ocfs2_group_desc *group;
  91. u16 chain, num_bits, backups = 0;
  92. u16 cl_bpc = le16_to_cpu(cl->cl_bpc);
  93. u16 cl_cpg = le16_to_cpu(cl->cl_cpg);
  94. mlog_entry("(new_clusters=%d, first_new_cluster = %u)\n",
  95. new_clusters, first_new_cluster);
  96. ret = ocfs2_journal_access(handle, bm_inode, group_bh,
  97. OCFS2_JOURNAL_ACCESS_WRITE);
  98. if (ret < 0) {
  99. mlog_errno(ret);
  100. goto out;
  101. }
  102. group = (struct ocfs2_group_desc *)group_bh->b_data;
  103. /* update the group first. */
  104. num_bits = new_clusters * cl_bpc;
  105. le16_add_cpu(&group->bg_bits, num_bits);
  106. le16_add_cpu(&group->bg_free_bits_count, num_bits);
  107. /*
  108. * check whether there are some new backup superblocks exist in
  109. * this group and update the group bitmap accordingly.
  110. */
  111. if (OCFS2_HAS_COMPAT_FEATURE(osb->sb,
  112. OCFS2_FEATURE_COMPAT_BACKUP_SB)) {
  113. backups = ocfs2_calc_new_backup_super(bm_inode,
  114. group,
  115. new_clusters,
  116. first_new_cluster,
  117. cl_cpg, 1);
  118. le16_add_cpu(&group->bg_free_bits_count, -1 * backups);
  119. }
  120. ret = ocfs2_journal_dirty(handle, group_bh);
  121. if (ret < 0) {
  122. mlog_errno(ret);
  123. goto out_rollback;
  124. }
  125. /* update the inode accordingly. */
  126. ret = ocfs2_journal_access(handle, bm_inode, bm_bh,
  127. OCFS2_JOURNAL_ACCESS_WRITE);
  128. if (ret < 0) {
  129. mlog_errno(ret);
  130. goto out_rollback;
  131. }
  132. chain = le16_to_cpu(group->bg_chain);
  133. cr = (&cl->cl_recs[chain]);
  134. le32_add_cpu(&cr->c_total, num_bits);
  135. le32_add_cpu(&cr->c_free, num_bits);
  136. le32_add_cpu(&fe->id1.bitmap1.i_total, num_bits);
  137. le32_add_cpu(&fe->i_clusters, new_clusters);
  138. if (backups) {
  139. le32_add_cpu(&cr->c_free, -1 * backups);
  140. le32_add_cpu(&fe->id1.bitmap1.i_used, backups);
  141. }
  142. spin_lock(&OCFS2_I(bm_inode)->ip_lock);
  143. OCFS2_I(bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  144. le64_add_cpu(&fe->i_size, new_clusters << osb->s_clustersize_bits);
  145. spin_unlock(&OCFS2_I(bm_inode)->ip_lock);
  146. i_size_write(bm_inode, le64_to_cpu(fe->i_size));
  147. ocfs2_journal_dirty(handle, bm_bh);
  148. out_rollback:
  149. if (ret < 0) {
  150. ocfs2_calc_new_backup_super(bm_inode,
  151. group,
  152. new_clusters,
  153. first_new_cluster,
  154. cl_cpg, 0);
  155. le16_add_cpu(&group->bg_free_bits_count, backups);
  156. le16_add_cpu(&group->bg_bits, -1 * num_bits);
  157. le16_add_cpu(&group->bg_free_bits_count, -1 * num_bits);
  158. }
  159. out:
  160. mlog_exit(ret);
  161. return ret;
  162. }
  163. static int update_backups(struct inode * inode, u32 clusters, char *data)
  164. {
  165. int i, ret = 0;
  166. u32 cluster;
  167. u64 blkno;
  168. struct buffer_head *backup = NULL;
  169. struct ocfs2_dinode *backup_di = NULL;
  170. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  171. /* calculate the real backups we need to update. */
  172. for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
  173. blkno = ocfs2_backup_super_blkno(inode->i_sb, i);
  174. cluster = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
  175. if (cluster > clusters)
  176. break;
  177. ret = ocfs2_read_block(osb, blkno, &backup, 0, NULL);
  178. if (ret < 0) {
  179. mlog_errno(ret);
  180. break;
  181. }
  182. memcpy(backup->b_data, data, inode->i_sb->s_blocksize);
  183. backup_di = (struct ocfs2_dinode *)backup->b_data;
  184. backup_di->i_blkno = cpu_to_le64(blkno);
  185. ret = ocfs2_write_super_or_backup(osb, backup);
  186. brelse(backup);
  187. backup = NULL;
  188. if (ret < 0) {
  189. mlog_errno(ret);
  190. break;
  191. }
  192. }
  193. return ret;
  194. }
  195. static void ocfs2_update_super_and_backups(struct inode *inode,
  196. int new_clusters)
  197. {
  198. int ret;
  199. u32 clusters = 0;
  200. struct buffer_head *super_bh = NULL;
  201. struct ocfs2_dinode *super_di = NULL;
  202. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  203. /*
  204. * update the superblock last.
  205. * It doesn't matter if the write failed.
  206. */
  207. ret = ocfs2_read_block(osb, OCFS2_SUPER_BLOCK_BLKNO,
  208. &super_bh, 0, NULL);
  209. if (ret < 0) {
  210. mlog_errno(ret);
  211. goto out;
  212. }
  213. super_di = (struct ocfs2_dinode *)super_bh->b_data;
  214. le32_add_cpu(&super_di->i_clusters, new_clusters);
  215. clusters = le32_to_cpu(super_di->i_clusters);
  216. ret = ocfs2_write_super_or_backup(osb, super_bh);
  217. if (ret < 0) {
  218. mlog_errno(ret);
  219. goto out;
  220. }
  221. if (OCFS2_HAS_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_COMPAT_BACKUP_SB))
  222. ret = update_backups(inode, clusters, super_bh->b_data);
  223. out:
  224. if (super_bh)
  225. brelse(super_bh);
  226. if (ret)
  227. printk(KERN_WARNING "ocfs2: Failed to update super blocks on %s"
  228. " during fs resize. This condition is not fatal,"
  229. " but fsck.ocfs2 should be run to fix it\n",
  230. osb->dev_str);
  231. return;
  232. }
  233. /*
  234. * Extend the filesystem to the new number of clusters specified. This entry
  235. * point is only used to extend the current filesystem to the end of the last
  236. * existing group.
  237. */
  238. int ocfs2_group_extend(struct inode * inode, int new_clusters)
  239. {
  240. int ret;
  241. handle_t *handle;
  242. struct buffer_head *main_bm_bh = NULL;
  243. struct buffer_head *group_bh = NULL;
  244. struct inode *main_bm_inode = NULL;
  245. struct ocfs2_dinode *fe = NULL;
  246. struct ocfs2_group_desc *group = NULL;
  247. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  248. u16 cl_bpc;
  249. u32 first_new_cluster;
  250. u64 lgd_blkno;
  251. mlog_entry_void();
  252. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  253. return -EROFS;
  254. if (new_clusters < 0)
  255. return -EINVAL;
  256. else if (new_clusters == 0)
  257. return 0;
  258. main_bm_inode = ocfs2_get_system_file_inode(osb,
  259. GLOBAL_BITMAP_SYSTEM_INODE,
  260. OCFS2_INVALID_SLOT);
  261. if (!main_bm_inode) {
  262. ret = -EINVAL;
  263. mlog_errno(ret);
  264. goto out;
  265. }
  266. mutex_lock(&main_bm_inode->i_mutex);
  267. ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
  268. if (ret < 0) {
  269. mlog_errno(ret);
  270. goto out_mutex;
  271. }
  272. fe = (struct ocfs2_dinode *)main_bm_bh->b_data;
  273. if (le16_to_cpu(fe->id2.i_chain.cl_cpg) !=
  274. ocfs2_group_bitmap_size(osb->sb) * 8) {
  275. mlog(ML_ERROR, "The disk is too old and small. "
  276. "Force to do offline resize.");
  277. ret = -EINVAL;
  278. goto out_unlock;
  279. }
  280. if (!OCFS2_IS_VALID_DINODE(fe)) {
  281. OCFS2_RO_ON_INVALID_DINODE(main_bm_inode->i_sb, fe);
  282. ret = -EIO;
  283. goto out_unlock;
  284. }
  285. first_new_cluster = le32_to_cpu(fe->i_clusters);
  286. lgd_blkno = ocfs2_which_cluster_group(main_bm_inode,
  287. first_new_cluster - 1);
  288. ret = ocfs2_read_block(osb, lgd_blkno, &group_bh, OCFS2_BH_CACHED,
  289. main_bm_inode);
  290. if (ret < 0) {
  291. mlog_errno(ret);
  292. goto out_unlock;
  293. }
  294. group = (struct ocfs2_group_desc *)group_bh->b_data;
  295. ret = ocfs2_check_group_descriptor(inode->i_sb, fe, group);
  296. if (ret) {
  297. mlog_errno(ret);
  298. goto out_unlock;
  299. }
  300. cl_bpc = le16_to_cpu(fe->id2.i_chain.cl_bpc);
  301. if (le16_to_cpu(group->bg_bits) / cl_bpc + new_clusters >
  302. le16_to_cpu(fe->id2.i_chain.cl_cpg)) {
  303. ret = -EINVAL;
  304. goto out_unlock;
  305. }
  306. mlog(0, "extend the last group at %llu, new clusters = %d\n",
  307. le64_to_cpu(group->bg_blkno), new_clusters);
  308. handle = ocfs2_start_trans(osb, OCFS2_GROUP_EXTEND_CREDITS);
  309. if (IS_ERR(handle)) {
  310. mlog_errno(PTR_ERR(handle));
  311. ret = -EINVAL;
  312. goto out_unlock;
  313. }
  314. /* update the last group descriptor and inode. */
  315. ret = ocfs2_update_last_group_and_inode(handle, main_bm_inode,
  316. main_bm_bh, group_bh,
  317. first_new_cluster,
  318. new_clusters);
  319. if (ret) {
  320. mlog_errno(ret);
  321. goto out_commit;
  322. }
  323. ocfs2_update_super_and_backups(main_bm_inode, new_clusters);
  324. out_commit:
  325. ocfs2_commit_trans(osb, handle);
  326. out_unlock:
  327. if (group_bh)
  328. brelse(group_bh);
  329. if (main_bm_bh)
  330. brelse(main_bm_bh);
  331. ocfs2_inode_unlock(main_bm_inode, 1);
  332. out_mutex:
  333. mutex_unlock(&main_bm_inode->i_mutex);
  334. iput(main_bm_inode);
  335. out:
  336. mlog_exit_void();
  337. return ret;
  338. }