super.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/crc32.h>
  15. #include <linux/gfs2_ondisk.h>
  16. #include <linux/bio.h>
  17. #include <linux/lm_interface.h>
  18. #include "gfs2.h"
  19. #include "incore.h"
  20. #include "bmap.h"
  21. #include "dir.h"
  22. #include "glock.h"
  23. #include "glops.h"
  24. #include "inode.h"
  25. #include "log.h"
  26. #include "meta_io.h"
  27. #include "quota.h"
  28. #include "recovery.h"
  29. #include "rgrp.h"
  30. #include "super.h"
  31. #include "trans.h"
  32. #include "util.h"
  33. /**
  34. * gfs2_jindex_free - Clear all the journal index information
  35. * @sdp: The GFS2 superblock
  36. *
  37. */
  38. void gfs2_jindex_free(struct gfs2_sbd *sdp)
  39. {
  40. struct list_head list, *head;
  41. struct gfs2_jdesc *jd;
  42. struct gfs2_journal_extent *jext;
  43. spin_lock(&sdp->sd_jindex_spin);
  44. list_add(&list, &sdp->sd_jindex_list);
  45. list_del_init(&sdp->sd_jindex_list);
  46. sdp->sd_journals = 0;
  47. spin_unlock(&sdp->sd_jindex_spin);
  48. while (!list_empty(&list)) {
  49. jd = list_entry(list.next, struct gfs2_jdesc, jd_list);
  50. head = &jd->extent_list;
  51. while (!list_empty(head)) {
  52. jext = list_entry(head->next,
  53. struct gfs2_journal_extent,
  54. extent_list);
  55. list_del(&jext->extent_list);
  56. kfree(jext);
  57. }
  58. list_del(&jd->jd_list);
  59. iput(jd->jd_inode);
  60. kfree(jd);
  61. }
  62. }
  63. static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
  64. {
  65. struct gfs2_jdesc *jd;
  66. int found = 0;
  67. list_for_each_entry(jd, head, jd_list) {
  68. if (jd->jd_jid == jid) {
  69. found = 1;
  70. break;
  71. }
  72. }
  73. if (!found)
  74. jd = NULL;
  75. return jd;
  76. }
  77. struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
  78. {
  79. struct gfs2_jdesc *jd;
  80. spin_lock(&sdp->sd_jindex_spin);
  81. jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
  82. spin_unlock(&sdp->sd_jindex_spin);
  83. return jd;
  84. }
  85. int gfs2_jdesc_check(struct gfs2_jdesc *jd)
  86. {
  87. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  88. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  89. int ar;
  90. int error;
  91. if (ip->i_disksize < (8 << 20) || ip->i_disksize > (1 << 30) ||
  92. (ip->i_disksize & (sdp->sd_sb.sb_bsize - 1))) {
  93. gfs2_consist_inode(ip);
  94. return -EIO;
  95. }
  96. jd->jd_blocks = ip->i_disksize >> sdp->sd_sb.sb_bsize_shift;
  97. error = gfs2_write_alloc_required(ip, 0, ip->i_disksize, &ar);
  98. if (!error && ar) {
  99. gfs2_consist_inode(ip);
  100. error = -EIO;
  101. }
  102. return error;
  103. }
  104. /**
  105. * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
  106. * @sdp: the filesystem
  107. *
  108. * Returns: errno
  109. */
  110. int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
  111. {
  112. struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
  113. struct gfs2_glock *j_gl = ip->i_gl;
  114. struct gfs2_holder t_gh;
  115. struct gfs2_log_header_host head;
  116. int error;
  117. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, 0, &t_gh);
  118. if (error)
  119. return error;
  120. j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
  121. error = gfs2_find_jhead(sdp->sd_jdesc, &head);
  122. if (error)
  123. goto fail;
  124. if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  125. gfs2_consist(sdp);
  126. error = -EIO;
  127. goto fail;
  128. }
  129. /* Initialize some head of the log stuff */
  130. sdp->sd_log_sequence = head.lh_sequence + 1;
  131. gfs2_log_pointers_init(sdp, head.lh_blkno);
  132. error = gfs2_quota_init(sdp);
  133. if (error)
  134. goto fail;
  135. set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  136. gfs2_glock_dq_uninit(&t_gh);
  137. return 0;
  138. fail:
  139. t_gh.gh_flags |= GL_NOCACHE;
  140. gfs2_glock_dq_uninit(&t_gh);
  141. return error;
  142. }
  143. static void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
  144. {
  145. const struct gfs2_statfs_change *str = buf;
  146. sc->sc_total = be64_to_cpu(str->sc_total);
  147. sc->sc_free = be64_to_cpu(str->sc_free);
  148. sc->sc_dinodes = be64_to_cpu(str->sc_dinodes);
  149. }
  150. static void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
  151. {
  152. struct gfs2_statfs_change *str = buf;
  153. str->sc_total = cpu_to_be64(sc->sc_total);
  154. str->sc_free = cpu_to_be64(sc->sc_free);
  155. str->sc_dinodes = cpu_to_be64(sc->sc_dinodes);
  156. }
  157. int gfs2_statfs_init(struct gfs2_sbd *sdp)
  158. {
  159. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  160. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  161. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  162. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  163. struct buffer_head *m_bh, *l_bh;
  164. struct gfs2_holder gh;
  165. int error;
  166. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
  167. &gh);
  168. if (error)
  169. return error;
  170. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  171. if (error)
  172. goto out;
  173. if (sdp->sd_args.ar_spectator) {
  174. spin_lock(&sdp->sd_statfs_spin);
  175. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  176. sizeof(struct gfs2_dinode));
  177. spin_unlock(&sdp->sd_statfs_spin);
  178. } else {
  179. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  180. if (error)
  181. goto out_m_bh;
  182. spin_lock(&sdp->sd_statfs_spin);
  183. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  184. sizeof(struct gfs2_dinode));
  185. gfs2_statfs_change_in(l_sc, l_bh->b_data +
  186. sizeof(struct gfs2_dinode));
  187. spin_unlock(&sdp->sd_statfs_spin);
  188. brelse(l_bh);
  189. }
  190. out_m_bh:
  191. brelse(m_bh);
  192. out:
  193. gfs2_glock_dq_uninit(&gh);
  194. return 0;
  195. }
  196. void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
  197. s64 dinodes)
  198. {
  199. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  200. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  201. struct buffer_head *l_bh;
  202. int error;
  203. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  204. if (error)
  205. return;
  206. gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
  207. spin_lock(&sdp->sd_statfs_spin);
  208. l_sc->sc_total += total;
  209. l_sc->sc_free += free;
  210. l_sc->sc_dinodes += dinodes;
  211. gfs2_statfs_change_out(l_sc, l_bh->b_data + sizeof(struct gfs2_dinode));
  212. spin_unlock(&sdp->sd_statfs_spin);
  213. brelse(l_bh);
  214. }
  215. int gfs2_statfs_sync(struct gfs2_sbd *sdp)
  216. {
  217. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  218. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  219. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  220. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  221. struct gfs2_holder gh;
  222. struct buffer_head *m_bh, *l_bh;
  223. int error;
  224. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
  225. &gh);
  226. if (error)
  227. return error;
  228. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  229. if (error)
  230. goto out;
  231. spin_lock(&sdp->sd_statfs_spin);
  232. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  233. sizeof(struct gfs2_dinode));
  234. if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
  235. spin_unlock(&sdp->sd_statfs_spin);
  236. goto out_bh;
  237. }
  238. spin_unlock(&sdp->sd_statfs_spin);
  239. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  240. if (error)
  241. goto out_bh;
  242. error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
  243. if (error)
  244. goto out_bh2;
  245. gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
  246. spin_lock(&sdp->sd_statfs_spin);
  247. m_sc->sc_total += l_sc->sc_total;
  248. m_sc->sc_free += l_sc->sc_free;
  249. m_sc->sc_dinodes += l_sc->sc_dinodes;
  250. memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
  251. memset(l_bh->b_data + sizeof(struct gfs2_dinode),
  252. 0, sizeof(struct gfs2_statfs_change));
  253. spin_unlock(&sdp->sd_statfs_spin);
  254. gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
  255. gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
  256. gfs2_trans_end(sdp);
  257. out_bh2:
  258. brelse(l_bh);
  259. out_bh:
  260. brelse(m_bh);
  261. out:
  262. gfs2_glock_dq_uninit(&gh);
  263. return error;
  264. }
  265. struct lfcc {
  266. struct list_head list;
  267. struct gfs2_holder gh;
  268. };
  269. /**
  270. * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
  271. * journals are clean
  272. * @sdp: the file system
  273. * @state: the state to put the transaction lock into
  274. * @t_gh: the hold on the transaction lock
  275. *
  276. * Returns: errno
  277. */
  278. static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp,
  279. struct gfs2_holder *t_gh)
  280. {
  281. struct gfs2_inode *ip;
  282. struct gfs2_holder ji_gh;
  283. struct gfs2_jdesc *jd;
  284. struct lfcc *lfcc;
  285. LIST_HEAD(list);
  286. struct gfs2_log_header_host lh;
  287. int error;
  288. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  289. lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
  290. if (!lfcc) {
  291. error = -ENOMEM;
  292. goto out;
  293. }
  294. ip = GFS2_I(jd->jd_inode);
  295. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
  296. if (error) {
  297. kfree(lfcc);
  298. goto out;
  299. }
  300. list_add(&lfcc->list, &list);
  301. }
  302. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_DEFERRED,
  303. GL_NOCACHE, t_gh);
  304. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  305. error = gfs2_jdesc_check(jd);
  306. if (error)
  307. break;
  308. error = gfs2_find_jhead(jd, &lh);
  309. if (error)
  310. break;
  311. if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  312. error = -EBUSY;
  313. break;
  314. }
  315. }
  316. if (error)
  317. gfs2_glock_dq_uninit(t_gh);
  318. out:
  319. while (!list_empty(&list)) {
  320. lfcc = list_entry(list.next, struct lfcc, list);
  321. list_del(&lfcc->list);
  322. gfs2_glock_dq_uninit(&lfcc->gh);
  323. kfree(lfcc);
  324. }
  325. gfs2_glock_dq_uninit(&ji_gh);
  326. return error;
  327. }
  328. /**
  329. * gfs2_freeze_fs - freezes the file system
  330. * @sdp: the file system
  331. *
  332. * This function flushes data and meta data for all machines by
  333. * aquiring the transaction log exclusively. All journals are
  334. * ensured to be in a clean state as well.
  335. *
  336. * Returns: errno
  337. */
  338. int gfs2_freeze_fs(struct gfs2_sbd *sdp)
  339. {
  340. int error = 0;
  341. mutex_lock(&sdp->sd_freeze_lock);
  342. if (!sdp->sd_freeze_count++) {
  343. error = gfs2_lock_fs_check_clean(sdp, &sdp->sd_freeze_gh);
  344. if (error)
  345. sdp->sd_freeze_count--;
  346. }
  347. mutex_unlock(&sdp->sd_freeze_lock);
  348. return error;
  349. }
  350. /**
  351. * gfs2_unfreeze_fs - unfreezes the file system
  352. * @sdp: the file system
  353. *
  354. * This function allows the file system to proceed by unlocking
  355. * the exclusively held transaction lock. Other GFS2 nodes are
  356. * now free to acquire the lock shared and go on with their lives.
  357. *
  358. */
  359. void gfs2_unfreeze_fs(struct gfs2_sbd *sdp)
  360. {
  361. mutex_lock(&sdp->sd_freeze_lock);
  362. if (sdp->sd_freeze_count && !--sdp->sd_freeze_count)
  363. gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
  364. mutex_unlock(&sdp->sd_freeze_lock);
  365. }