super.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. void gfs2_jdesc_make_dirty(struct gfs2_sbd *sdp, unsigned int jid)
  86. {
  87. struct gfs2_jdesc *jd;
  88. spin_lock(&sdp->sd_jindex_spin);
  89. jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
  90. if (jd)
  91. jd->jd_dirty = 1;
  92. spin_unlock(&sdp->sd_jindex_spin);
  93. }
  94. struct gfs2_jdesc *gfs2_jdesc_find_dirty(struct gfs2_sbd *sdp)
  95. {
  96. struct gfs2_jdesc *jd;
  97. int found = 0;
  98. spin_lock(&sdp->sd_jindex_spin);
  99. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  100. if (jd->jd_dirty) {
  101. jd->jd_dirty = 0;
  102. found = 1;
  103. break;
  104. }
  105. }
  106. spin_unlock(&sdp->sd_jindex_spin);
  107. if (!found)
  108. jd = NULL;
  109. return jd;
  110. }
  111. int gfs2_jdesc_check(struct gfs2_jdesc *jd)
  112. {
  113. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  114. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  115. int ar;
  116. int error;
  117. if (ip->i_disksize < (8 << 20) || ip->i_disksize > (1 << 30) ||
  118. (ip->i_disksize & (sdp->sd_sb.sb_bsize - 1))) {
  119. gfs2_consist_inode(ip);
  120. return -EIO;
  121. }
  122. jd->jd_blocks = ip->i_disksize >> sdp->sd_sb.sb_bsize_shift;
  123. error = gfs2_write_alloc_required(ip, 0, ip->i_disksize, &ar);
  124. if (!error && ar) {
  125. gfs2_consist_inode(ip);
  126. error = -EIO;
  127. }
  128. return error;
  129. }
  130. /**
  131. * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
  132. * @sdp: the filesystem
  133. *
  134. * Returns: errno
  135. */
  136. int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
  137. {
  138. struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
  139. struct gfs2_glock *j_gl = ip->i_gl;
  140. struct gfs2_holder t_gh;
  141. struct gfs2_log_header_host head;
  142. int error;
  143. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, 0, &t_gh);
  144. if (error)
  145. return error;
  146. j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
  147. error = gfs2_find_jhead(sdp->sd_jdesc, &head);
  148. if (error)
  149. goto fail;
  150. if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  151. gfs2_consist(sdp);
  152. error = -EIO;
  153. goto fail;
  154. }
  155. /* Initialize some head of the log stuff */
  156. sdp->sd_log_sequence = head.lh_sequence + 1;
  157. gfs2_log_pointers_init(sdp, head.lh_blkno);
  158. error = gfs2_quota_init(sdp);
  159. if (error)
  160. goto fail;
  161. set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  162. gfs2_glock_dq_uninit(&t_gh);
  163. return 0;
  164. fail:
  165. t_gh.gh_flags |= GL_NOCACHE;
  166. gfs2_glock_dq_uninit(&t_gh);
  167. return error;
  168. }
  169. static void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
  170. {
  171. const struct gfs2_statfs_change *str = buf;
  172. sc->sc_total = be64_to_cpu(str->sc_total);
  173. sc->sc_free = be64_to_cpu(str->sc_free);
  174. sc->sc_dinodes = be64_to_cpu(str->sc_dinodes);
  175. }
  176. static void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
  177. {
  178. struct gfs2_statfs_change *str = buf;
  179. str->sc_total = cpu_to_be64(sc->sc_total);
  180. str->sc_free = cpu_to_be64(sc->sc_free);
  181. str->sc_dinodes = cpu_to_be64(sc->sc_dinodes);
  182. }
  183. int gfs2_statfs_init(struct gfs2_sbd *sdp)
  184. {
  185. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  186. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  187. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  188. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  189. struct buffer_head *m_bh, *l_bh;
  190. struct gfs2_holder gh;
  191. int error;
  192. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
  193. &gh);
  194. if (error)
  195. return error;
  196. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  197. if (error)
  198. goto out;
  199. if (sdp->sd_args.ar_spectator) {
  200. spin_lock(&sdp->sd_statfs_spin);
  201. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  202. sizeof(struct gfs2_dinode));
  203. spin_unlock(&sdp->sd_statfs_spin);
  204. } else {
  205. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  206. if (error)
  207. goto out_m_bh;
  208. spin_lock(&sdp->sd_statfs_spin);
  209. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  210. sizeof(struct gfs2_dinode));
  211. gfs2_statfs_change_in(l_sc, l_bh->b_data +
  212. sizeof(struct gfs2_dinode));
  213. spin_unlock(&sdp->sd_statfs_spin);
  214. brelse(l_bh);
  215. }
  216. out_m_bh:
  217. brelse(m_bh);
  218. out:
  219. gfs2_glock_dq_uninit(&gh);
  220. return 0;
  221. }
  222. void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
  223. s64 dinodes)
  224. {
  225. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  226. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  227. struct buffer_head *l_bh;
  228. int error;
  229. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  230. if (error)
  231. return;
  232. gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
  233. spin_lock(&sdp->sd_statfs_spin);
  234. l_sc->sc_total += total;
  235. l_sc->sc_free += free;
  236. l_sc->sc_dinodes += dinodes;
  237. gfs2_statfs_change_out(l_sc, l_bh->b_data + sizeof(struct gfs2_dinode));
  238. spin_unlock(&sdp->sd_statfs_spin);
  239. brelse(l_bh);
  240. }
  241. int gfs2_statfs_sync(struct gfs2_sbd *sdp)
  242. {
  243. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  244. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  245. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  246. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  247. struct gfs2_holder gh;
  248. struct buffer_head *m_bh, *l_bh;
  249. int error;
  250. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
  251. &gh);
  252. if (error)
  253. return error;
  254. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  255. if (error)
  256. goto out;
  257. spin_lock(&sdp->sd_statfs_spin);
  258. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  259. sizeof(struct gfs2_dinode));
  260. if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
  261. spin_unlock(&sdp->sd_statfs_spin);
  262. goto out_bh;
  263. }
  264. spin_unlock(&sdp->sd_statfs_spin);
  265. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  266. if (error)
  267. goto out_bh;
  268. error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
  269. if (error)
  270. goto out_bh2;
  271. gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
  272. spin_lock(&sdp->sd_statfs_spin);
  273. m_sc->sc_total += l_sc->sc_total;
  274. m_sc->sc_free += l_sc->sc_free;
  275. m_sc->sc_dinodes += l_sc->sc_dinodes;
  276. memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
  277. memset(l_bh->b_data + sizeof(struct gfs2_dinode),
  278. 0, sizeof(struct gfs2_statfs_change));
  279. spin_unlock(&sdp->sd_statfs_spin);
  280. gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
  281. gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
  282. gfs2_trans_end(sdp);
  283. out_bh2:
  284. brelse(l_bh);
  285. out_bh:
  286. brelse(m_bh);
  287. out:
  288. gfs2_glock_dq_uninit(&gh);
  289. return error;
  290. }
  291. /**
  292. * gfs2_statfs_i - Do a statfs
  293. * @sdp: the filesystem
  294. * @sg: the sg structure
  295. *
  296. * Returns: errno
  297. */
  298. int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
  299. {
  300. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  301. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  302. spin_lock(&sdp->sd_statfs_spin);
  303. *sc = *m_sc;
  304. sc->sc_total += l_sc->sc_total;
  305. sc->sc_free += l_sc->sc_free;
  306. sc->sc_dinodes += l_sc->sc_dinodes;
  307. spin_unlock(&sdp->sd_statfs_spin);
  308. if (sc->sc_free < 0)
  309. sc->sc_free = 0;
  310. if (sc->sc_free > sc->sc_total)
  311. sc->sc_free = sc->sc_total;
  312. if (sc->sc_dinodes < 0)
  313. sc->sc_dinodes = 0;
  314. return 0;
  315. }
  316. /**
  317. * statfs_fill - fill in the sg for a given RG
  318. * @rgd: the RG
  319. * @sc: the sc structure
  320. *
  321. * Returns: 0 on success, -ESTALE if the LVB is invalid
  322. */
  323. static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
  324. struct gfs2_statfs_change_host *sc)
  325. {
  326. gfs2_rgrp_verify(rgd);
  327. sc->sc_total += rgd->rd_data;
  328. sc->sc_free += rgd->rd_free;
  329. sc->sc_dinodes += rgd->rd_dinodes;
  330. return 0;
  331. }
  332. /**
  333. * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
  334. * @sdp: the filesystem
  335. * @sc: the sc info that will be returned
  336. *
  337. * Any error (other than a signal) will cause this routine to fall back
  338. * to the synchronous version.
  339. *
  340. * FIXME: This really shouldn't busy wait like this.
  341. *
  342. * Returns: errno
  343. */
  344. int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
  345. {
  346. struct gfs2_holder ri_gh;
  347. struct gfs2_rgrpd *rgd_next;
  348. struct gfs2_holder *gha, *gh;
  349. unsigned int slots = 64;
  350. unsigned int x;
  351. int done;
  352. int error = 0, err;
  353. memset(sc, 0, sizeof(struct gfs2_statfs_change_host));
  354. gha = kcalloc(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
  355. if (!gha)
  356. return -ENOMEM;
  357. error = gfs2_rindex_hold(sdp, &ri_gh);
  358. if (error)
  359. goto out;
  360. rgd_next = gfs2_rgrpd_get_first(sdp);
  361. for (;;) {
  362. done = 1;
  363. for (x = 0; x < slots; x++) {
  364. gh = gha + x;
  365. if (gh->gh_gl && gfs2_glock_poll(gh)) {
  366. err = gfs2_glock_wait(gh);
  367. if (err) {
  368. gfs2_holder_uninit(gh);
  369. error = err;
  370. } else {
  371. if (!error)
  372. error = statfs_slow_fill(
  373. gh->gh_gl->gl_object, sc);
  374. gfs2_glock_dq_uninit(gh);
  375. }
  376. }
  377. if (gh->gh_gl)
  378. done = 0;
  379. else if (rgd_next && !error) {
  380. error = gfs2_glock_nq_init(rgd_next->rd_gl,
  381. LM_ST_SHARED,
  382. GL_ASYNC,
  383. gh);
  384. rgd_next = gfs2_rgrpd_get_next(rgd_next);
  385. done = 0;
  386. }
  387. if (signal_pending(current))
  388. error = -ERESTARTSYS;
  389. }
  390. if (done)
  391. break;
  392. yield();
  393. }
  394. gfs2_glock_dq_uninit(&ri_gh);
  395. out:
  396. kfree(gha);
  397. return error;
  398. }
  399. struct lfcc {
  400. struct list_head list;
  401. struct gfs2_holder gh;
  402. };
  403. /**
  404. * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
  405. * journals are clean
  406. * @sdp: the file system
  407. * @state: the state to put the transaction lock into
  408. * @t_gh: the hold on the transaction lock
  409. *
  410. * Returns: errno
  411. */
  412. static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp,
  413. struct gfs2_holder *t_gh)
  414. {
  415. struct gfs2_inode *ip;
  416. struct gfs2_holder ji_gh;
  417. struct gfs2_jdesc *jd;
  418. struct lfcc *lfcc;
  419. LIST_HEAD(list);
  420. struct gfs2_log_header_host lh;
  421. int error;
  422. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  423. lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
  424. if (!lfcc) {
  425. error = -ENOMEM;
  426. goto out;
  427. }
  428. ip = GFS2_I(jd->jd_inode);
  429. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
  430. if (error) {
  431. kfree(lfcc);
  432. goto out;
  433. }
  434. list_add(&lfcc->list, &list);
  435. }
  436. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_DEFERRED,
  437. GL_NOCACHE, t_gh);
  438. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  439. error = gfs2_jdesc_check(jd);
  440. if (error)
  441. break;
  442. error = gfs2_find_jhead(jd, &lh);
  443. if (error)
  444. break;
  445. if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  446. error = -EBUSY;
  447. break;
  448. }
  449. }
  450. if (error)
  451. gfs2_glock_dq_uninit(t_gh);
  452. out:
  453. while (!list_empty(&list)) {
  454. lfcc = list_entry(list.next, struct lfcc, list);
  455. list_del(&lfcc->list);
  456. gfs2_glock_dq_uninit(&lfcc->gh);
  457. kfree(lfcc);
  458. }
  459. gfs2_glock_dq_uninit(&ji_gh);
  460. return error;
  461. }
  462. /**
  463. * gfs2_freeze_fs - freezes the file system
  464. * @sdp: the file system
  465. *
  466. * This function flushes data and meta data for all machines by
  467. * aquiring the transaction log exclusively. All journals are
  468. * ensured to be in a clean state as well.
  469. *
  470. * Returns: errno
  471. */
  472. int gfs2_freeze_fs(struct gfs2_sbd *sdp)
  473. {
  474. int error = 0;
  475. mutex_lock(&sdp->sd_freeze_lock);
  476. if (!sdp->sd_freeze_count++) {
  477. error = gfs2_lock_fs_check_clean(sdp, &sdp->sd_freeze_gh);
  478. if (error)
  479. sdp->sd_freeze_count--;
  480. }
  481. mutex_unlock(&sdp->sd_freeze_lock);
  482. return error;
  483. }
  484. /**
  485. * gfs2_unfreeze_fs - unfreezes the file system
  486. * @sdp: the file system
  487. *
  488. * This function allows the file system to proceed by unlocking
  489. * the exclusively held transaction lock. Other GFS2 nodes are
  490. * now free to acquire the lock shared and go on with their lives.
  491. *
  492. */
  493. void gfs2_unfreeze_fs(struct gfs2_sbd *sdp)
  494. {
  495. mutex_lock(&sdp->sd_freeze_lock);
  496. if (sdp->sd_freeze_count && !--sdp->sd_freeze_count)
  497. gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
  498. mutex_unlock(&sdp->sd_freeze_lock);
  499. }