util.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 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/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/crc32.h>
  14. #include <linux/gfs2_ondisk.h>
  15. #include <asm/uaccess.h>
  16. #include "gfs2.h"
  17. #include "incore.h"
  18. #include "glock.h"
  19. #include "util.h"
  20. struct kmem_cache *gfs2_glock_cachep __read_mostly;
  21. struct kmem_cache *gfs2_inode_cachep __read_mostly;
  22. struct kmem_cache *gfs2_bufdata_cachep __read_mostly;
  23. struct kmem_cache *gfs2_rgrpd_cachep __read_mostly;
  24. struct kmem_cache *gfs2_quotad_cachep __read_mostly;
  25. void gfs2_assert_i(struct gfs2_sbd *sdp)
  26. {
  27. printk(KERN_EMERG "GFS2: fsid=%s: fatal assertion failed\n",
  28. sdp->sd_fsname);
  29. }
  30. int gfs2_lm_withdraw(struct gfs2_sbd *sdp, char *fmt, ...)
  31. {
  32. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  33. const struct lm_lockops *lm = ls->ls_ops;
  34. va_list args;
  35. if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW &&
  36. test_and_set_bit(SDF_SHUTDOWN, &sdp->sd_flags))
  37. return 0;
  38. va_start(args, fmt);
  39. vprintk(fmt, args);
  40. va_end(args);
  41. if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW) {
  42. fs_err(sdp, "about to withdraw this file system\n");
  43. BUG_ON(sdp->sd_args.ar_debug);
  44. kobject_uevent(&sdp->sd_kobj, KOBJ_OFFLINE);
  45. if (lm->lm_unmount) {
  46. fs_err(sdp, "telling LM to unmount\n");
  47. lm->lm_unmount(sdp);
  48. }
  49. fs_err(sdp, "withdrawn\n");
  50. dump_stack();
  51. }
  52. if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC)
  53. panic("GFS2: fsid=%s: panic requested.\n", sdp->sd_fsname);
  54. return -1;
  55. }
  56. /**
  57. * gfs2_assert_withdraw_i - Cause the machine to withdraw if @assertion is false
  58. * Returns: -1 if this call withdrew the machine,
  59. * -2 if it was already withdrawn
  60. */
  61. int gfs2_assert_withdraw_i(struct gfs2_sbd *sdp, char *assertion,
  62. const char *function, char *file, unsigned int line)
  63. {
  64. int me;
  65. me = gfs2_lm_withdraw(sdp,
  66. "GFS2: fsid=%s: fatal: assertion \"%s\" failed\n"
  67. "GFS2: fsid=%s: function = %s, file = %s, line = %u\n",
  68. sdp->sd_fsname, assertion,
  69. sdp->sd_fsname, function, file, line);
  70. dump_stack();
  71. return (me) ? -1 : -2;
  72. }
  73. /**
  74. * gfs2_assert_warn_i - Print a message to the console if @assertion is false
  75. * Returns: -1 if we printed something
  76. * -2 if we didn't
  77. */
  78. int gfs2_assert_warn_i(struct gfs2_sbd *sdp, char *assertion,
  79. const char *function, char *file, unsigned int line)
  80. {
  81. if (time_before(jiffies,
  82. sdp->sd_last_warning +
  83. gfs2_tune_get(sdp, gt_complain_secs) * HZ))
  84. return -2;
  85. if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW)
  86. printk(KERN_WARNING
  87. "GFS2: fsid=%s: warning: assertion \"%s\" failed\n"
  88. "GFS2: fsid=%s: function = %s, file = %s, line = %u\n",
  89. sdp->sd_fsname, assertion,
  90. sdp->sd_fsname, function, file, line);
  91. if (sdp->sd_args.ar_debug)
  92. BUG();
  93. else
  94. dump_stack();
  95. if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC)
  96. panic("GFS2: fsid=%s: warning: assertion \"%s\" failed\n"
  97. "GFS2: fsid=%s: function = %s, file = %s, line = %u\n",
  98. sdp->sd_fsname, assertion,
  99. sdp->sd_fsname, function, file, line);
  100. sdp->sd_last_warning = jiffies;
  101. return -1;
  102. }
  103. /**
  104. * gfs2_consist_i - Flag a filesystem consistency error and withdraw
  105. * Returns: -1 if this call withdrew the machine,
  106. * 0 if it was already withdrawn
  107. */
  108. int gfs2_consist_i(struct gfs2_sbd *sdp, int cluster_wide, const char *function,
  109. char *file, unsigned int line)
  110. {
  111. int rv;
  112. rv = gfs2_lm_withdraw(sdp,
  113. "GFS2: fsid=%s: fatal: filesystem consistency error\n"
  114. "GFS2: fsid=%s: function = %s, file = %s, line = %u\n",
  115. sdp->sd_fsname,
  116. sdp->sd_fsname, function, file, line);
  117. return rv;
  118. }
  119. /**
  120. * gfs2_consist_inode_i - Flag an inode consistency error and withdraw
  121. * Returns: -1 if this call withdrew the machine,
  122. * 0 if it was already withdrawn
  123. */
  124. int gfs2_consist_inode_i(struct gfs2_inode *ip, int cluster_wide,
  125. const char *function, char *file, unsigned int line)
  126. {
  127. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  128. int rv;
  129. rv = gfs2_lm_withdraw(sdp,
  130. "GFS2: fsid=%s: fatal: filesystem consistency error\n"
  131. "GFS2: fsid=%s: inode = %llu %llu\n"
  132. "GFS2: fsid=%s: function = %s, file = %s, line = %u\n",
  133. sdp->sd_fsname,
  134. sdp->sd_fsname, (unsigned long long)ip->i_no_formal_ino,
  135. (unsigned long long)ip->i_no_addr,
  136. sdp->sd_fsname, function, file, line);
  137. return rv;
  138. }
  139. /**
  140. * gfs2_consist_rgrpd_i - Flag a RG consistency error and withdraw
  141. * Returns: -1 if this call withdrew the machine,
  142. * 0 if it was already withdrawn
  143. */
  144. int gfs2_consist_rgrpd_i(struct gfs2_rgrpd *rgd, int cluster_wide,
  145. const char *function, char *file, unsigned int line)
  146. {
  147. struct gfs2_sbd *sdp = rgd->rd_sbd;
  148. int rv;
  149. rv = gfs2_lm_withdraw(sdp,
  150. "GFS2: fsid=%s: fatal: filesystem consistency error\n"
  151. "GFS2: fsid=%s: RG = %llu\n"
  152. "GFS2: fsid=%s: function = %s, file = %s, line = %u\n",
  153. sdp->sd_fsname,
  154. sdp->sd_fsname, (unsigned long long)rgd->rd_addr,
  155. sdp->sd_fsname, function, file, line);
  156. return rv;
  157. }
  158. /**
  159. * gfs2_meta_check_ii - Flag a magic number consistency error and withdraw
  160. * Returns: -1 if this call withdrew the machine,
  161. * -2 if it was already withdrawn
  162. */
  163. int gfs2_meta_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh,
  164. const char *type, const char *function, char *file,
  165. unsigned int line)
  166. {
  167. int me;
  168. me = gfs2_lm_withdraw(sdp,
  169. "GFS2: fsid=%s: fatal: invalid metadata block\n"
  170. "GFS2: fsid=%s: bh = %llu (%s)\n"
  171. "GFS2: fsid=%s: function = %s, file = %s, line = %u\n",
  172. sdp->sd_fsname,
  173. sdp->sd_fsname, (unsigned long long)bh->b_blocknr, type,
  174. sdp->sd_fsname, function, file, line);
  175. return (me) ? -1 : -2;
  176. }
  177. /**
  178. * gfs2_metatype_check_ii - Flag a metadata type consistency error and withdraw
  179. * Returns: -1 if this call withdrew the machine,
  180. * -2 if it was already withdrawn
  181. */
  182. int gfs2_metatype_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh,
  183. u16 type, u16 t, const char *function,
  184. char *file, unsigned int line)
  185. {
  186. int me;
  187. me = gfs2_lm_withdraw(sdp,
  188. "GFS2: fsid=%s: fatal: invalid metadata block\n"
  189. "GFS2: fsid=%s: bh = %llu (type: exp=%u, found=%u)\n"
  190. "GFS2: fsid=%s: function = %s, file = %s, line = %u\n",
  191. sdp->sd_fsname,
  192. sdp->sd_fsname, (unsigned long long)bh->b_blocknr, type, t,
  193. sdp->sd_fsname, function, file, line);
  194. return (me) ? -1 : -2;
  195. }
  196. /**
  197. * gfs2_io_error_i - Flag an I/O error and withdraw
  198. * Returns: -1 if this call withdrew the machine,
  199. * 0 if it was already withdrawn
  200. */
  201. int gfs2_io_error_i(struct gfs2_sbd *sdp, const char *function, char *file,
  202. unsigned int line)
  203. {
  204. int rv;
  205. rv = gfs2_lm_withdraw(sdp,
  206. "GFS2: fsid=%s: fatal: I/O error\n"
  207. "GFS2: fsid=%s: function = %s, file = %s, line = %u\n",
  208. sdp->sd_fsname,
  209. sdp->sd_fsname, function, file, line);
  210. return rv;
  211. }
  212. /**
  213. * gfs2_io_error_bh_i - Flag a buffer I/O error and withdraw
  214. * Returns: -1 if this call withdrew the machine,
  215. * 0 if it was already withdrawn
  216. */
  217. int gfs2_io_error_bh_i(struct gfs2_sbd *sdp, struct buffer_head *bh,
  218. const char *function, char *file, unsigned int line)
  219. {
  220. int rv;
  221. rv = gfs2_lm_withdraw(sdp,
  222. "GFS2: fsid=%s: fatal: I/O error\n"
  223. "GFS2: fsid=%s: block = %llu\n"
  224. "GFS2: fsid=%s: function = %s, file = %s, line = %u\n",
  225. sdp->sd_fsname,
  226. sdp->sd_fsname, (unsigned long long)bh->b_blocknr,
  227. sdp->sd_fsname, function, file, line);
  228. return rv;
  229. }
  230. void gfs2_icbit_munge(struct gfs2_sbd *sdp, unsigned char **bitmap,
  231. unsigned int bit, int new_value)
  232. {
  233. unsigned int c, o, b = bit;
  234. int old_value;
  235. c = b / (8 * PAGE_SIZE);
  236. b %= 8 * PAGE_SIZE;
  237. o = b / 8;
  238. b %= 8;
  239. old_value = (bitmap[c][o] & (1 << b));
  240. gfs2_assert_withdraw(sdp, !old_value != !new_value);
  241. if (new_value)
  242. bitmap[c][o] |= 1 << b;
  243. else
  244. bitmap[c][o] &= ~(1 << b);
  245. }