super.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
  34. {
  35. struct gfs2_jdesc *jd;
  36. int found = 0;
  37. list_for_each_entry(jd, head, jd_list) {
  38. if (jd->jd_jid == jid) {
  39. found = 1;
  40. break;
  41. }
  42. }
  43. if (!found)
  44. jd = NULL;
  45. return jd;
  46. }
  47. struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
  48. {
  49. struct gfs2_jdesc *jd;
  50. spin_lock(&sdp->sd_jindex_spin);
  51. jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
  52. spin_unlock(&sdp->sd_jindex_spin);
  53. return jd;
  54. }
  55. int gfs2_jdesc_check(struct gfs2_jdesc *jd)
  56. {
  57. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  58. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  59. int ar;
  60. int error;
  61. if (ip->i_disksize < (8 << 20) || ip->i_disksize > (1 << 30) ||
  62. (ip->i_disksize & (sdp->sd_sb.sb_bsize - 1))) {
  63. gfs2_consist_inode(ip);
  64. return -EIO;
  65. }
  66. jd->jd_blocks = ip->i_disksize >> sdp->sd_sb.sb_bsize_shift;
  67. error = gfs2_write_alloc_required(ip, 0, ip->i_disksize, &ar);
  68. if (!error && ar) {
  69. gfs2_consist_inode(ip);
  70. error = -EIO;
  71. }
  72. return error;
  73. }
  74. /**
  75. * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
  76. * @sdp: the filesystem
  77. *
  78. * Returns: errno
  79. */
  80. int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
  81. {
  82. struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
  83. struct gfs2_glock *j_gl = ip->i_gl;
  84. struct gfs2_holder t_gh;
  85. struct gfs2_log_header_host head;
  86. int error;
  87. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, 0, &t_gh);
  88. if (error)
  89. return error;
  90. j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
  91. error = gfs2_find_jhead(sdp->sd_jdesc, &head);
  92. if (error)
  93. goto fail;
  94. if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  95. gfs2_consist(sdp);
  96. error = -EIO;
  97. goto fail;
  98. }
  99. /* Initialize some head of the log stuff */
  100. sdp->sd_log_sequence = head.lh_sequence + 1;
  101. gfs2_log_pointers_init(sdp, head.lh_blkno);
  102. error = gfs2_quota_init(sdp);
  103. if (error)
  104. goto fail;
  105. set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  106. gfs2_glock_dq_uninit(&t_gh);
  107. return 0;
  108. fail:
  109. t_gh.gh_flags |= GL_NOCACHE;
  110. gfs2_glock_dq_uninit(&t_gh);
  111. return error;
  112. }
  113. static void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
  114. {
  115. const struct gfs2_statfs_change *str = buf;
  116. sc->sc_total = be64_to_cpu(str->sc_total);
  117. sc->sc_free = be64_to_cpu(str->sc_free);
  118. sc->sc_dinodes = be64_to_cpu(str->sc_dinodes);
  119. }
  120. static void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
  121. {
  122. struct gfs2_statfs_change *str = buf;
  123. str->sc_total = cpu_to_be64(sc->sc_total);
  124. str->sc_free = cpu_to_be64(sc->sc_free);
  125. str->sc_dinodes = cpu_to_be64(sc->sc_dinodes);
  126. }
  127. int gfs2_statfs_init(struct gfs2_sbd *sdp)
  128. {
  129. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  130. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  131. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  132. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  133. struct buffer_head *m_bh, *l_bh;
  134. struct gfs2_holder gh;
  135. int error;
  136. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
  137. &gh);
  138. if (error)
  139. return error;
  140. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  141. if (error)
  142. goto out;
  143. if (sdp->sd_args.ar_spectator) {
  144. spin_lock(&sdp->sd_statfs_spin);
  145. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  146. sizeof(struct gfs2_dinode));
  147. spin_unlock(&sdp->sd_statfs_spin);
  148. } else {
  149. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  150. if (error)
  151. goto out_m_bh;
  152. spin_lock(&sdp->sd_statfs_spin);
  153. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  154. sizeof(struct gfs2_dinode));
  155. gfs2_statfs_change_in(l_sc, l_bh->b_data +
  156. sizeof(struct gfs2_dinode));
  157. spin_unlock(&sdp->sd_statfs_spin);
  158. brelse(l_bh);
  159. }
  160. out_m_bh:
  161. brelse(m_bh);
  162. out:
  163. gfs2_glock_dq_uninit(&gh);
  164. return 0;
  165. }
  166. void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
  167. s64 dinodes)
  168. {
  169. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  170. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  171. struct buffer_head *l_bh;
  172. int error;
  173. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  174. if (error)
  175. return;
  176. gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
  177. spin_lock(&sdp->sd_statfs_spin);
  178. l_sc->sc_total += total;
  179. l_sc->sc_free += free;
  180. l_sc->sc_dinodes += dinodes;
  181. gfs2_statfs_change_out(l_sc, l_bh->b_data + sizeof(struct gfs2_dinode));
  182. spin_unlock(&sdp->sd_statfs_spin);
  183. brelse(l_bh);
  184. }
  185. int gfs2_statfs_sync(struct gfs2_sbd *sdp)
  186. {
  187. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  188. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  189. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  190. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  191. struct gfs2_holder gh;
  192. struct buffer_head *m_bh, *l_bh;
  193. int error;
  194. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
  195. &gh);
  196. if (error)
  197. return error;
  198. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  199. if (error)
  200. goto out;
  201. spin_lock(&sdp->sd_statfs_spin);
  202. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  203. sizeof(struct gfs2_dinode));
  204. if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
  205. spin_unlock(&sdp->sd_statfs_spin);
  206. goto out_bh;
  207. }
  208. spin_unlock(&sdp->sd_statfs_spin);
  209. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  210. if (error)
  211. goto out_bh;
  212. error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
  213. if (error)
  214. goto out_bh2;
  215. gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
  216. spin_lock(&sdp->sd_statfs_spin);
  217. m_sc->sc_total += l_sc->sc_total;
  218. m_sc->sc_free += l_sc->sc_free;
  219. m_sc->sc_dinodes += l_sc->sc_dinodes;
  220. memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
  221. memset(l_bh->b_data + sizeof(struct gfs2_dinode),
  222. 0, sizeof(struct gfs2_statfs_change));
  223. spin_unlock(&sdp->sd_statfs_spin);
  224. gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
  225. gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
  226. gfs2_trans_end(sdp);
  227. out_bh2:
  228. brelse(l_bh);
  229. out_bh:
  230. brelse(m_bh);
  231. out:
  232. gfs2_glock_dq_uninit(&gh);
  233. return error;
  234. }
  235. struct lfcc {
  236. struct list_head list;
  237. struct gfs2_holder gh;
  238. };
  239. /**
  240. * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
  241. * journals are clean
  242. * @sdp: the file system
  243. * @state: the state to put the transaction lock into
  244. * @t_gh: the hold on the transaction lock
  245. *
  246. * Returns: errno
  247. */
  248. static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp,
  249. struct gfs2_holder *t_gh)
  250. {
  251. struct gfs2_inode *ip;
  252. struct gfs2_holder ji_gh;
  253. struct gfs2_jdesc *jd;
  254. struct lfcc *lfcc;
  255. LIST_HEAD(list);
  256. struct gfs2_log_header_host lh;
  257. int error;
  258. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  259. lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
  260. if (!lfcc) {
  261. error = -ENOMEM;
  262. goto out;
  263. }
  264. ip = GFS2_I(jd->jd_inode);
  265. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
  266. if (error) {
  267. kfree(lfcc);
  268. goto out;
  269. }
  270. list_add(&lfcc->list, &list);
  271. }
  272. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_DEFERRED,
  273. GL_NOCACHE, t_gh);
  274. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  275. error = gfs2_jdesc_check(jd);
  276. if (error)
  277. break;
  278. error = gfs2_find_jhead(jd, &lh);
  279. if (error)
  280. break;
  281. if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  282. error = -EBUSY;
  283. break;
  284. }
  285. }
  286. if (error)
  287. gfs2_glock_dq_uninit(t_gh);
  288. out:
  289. while (!list_empty(&list)) {
  290. lfcc = list_entry(list.next, struct lfcc, list);
  291. list_del(&lfcc->list);
  292. gfs2_glock_dq_uninit(&lfcc->gh);
  293. kfree(lfcc);
  294. }
  295. gfs2_glock_dq_uninit(&ji_gh);
  296. return error;
  297. }
  298. /**
  299. * gfs2_freeze_fs - freezes the file system
  300. * @sdp: the file system
  301. *
  302. * This function flushes data and meta data for all machines by
  303. * aquiring the transaction log exclusively. All journals are
  304. * ensured to be in a clean state as well.
  305. *
  306. * Returns: errno
  307. */
  308. int gfs2_freeze_fs(struct gfs2_sbd *sdp)
  309. {
  310. int error = 0;
  311. mutex_lock(&sdp->sd_freeze_lock);
  312. if (!sdp->sd_freeze_count++) {
  313. error = gfs2_lock_fs_check_clean(sdp, &sdp->sd_freeze_gh);
  314. if (error)
  315. sdp->sd_freeze_count--;
  316. }
  317. mutex_unlock(&sdp->sd_freeze_lock);
  318. return error;
  319. }
  320. /**
  321. * gfs2_unfreeze_fs - unfreezes the file system
  322. * @sdp: the file system
  323. *
  324. * This function allows the file system to proceed by unlocking
  325. * the exclusively held transaction lock. Other GFS2 nodes are
  326. * now free to acquire the lock shared and go on with their lives.
  327. *
  328. */
  329. void gfs2_unfreeze_fs(struct gfs2_sbd *sdp)
  330. {
  331. mutex_lock(&sdp->sd_freeze_lock);
  332. if (sdp->sd_freeze_count && !--sdp->sd_freeze_count)
  333. gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
  334. mutex_unlock(&sdp->sd_freeze_lock);
  335. }