super.c 9.8 KB

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