xfs_rw.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_dir2_sf.h"
  34. #include "xfs_attr_sf.h"
  35. #include "xfs_dinode.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_inode_item.h"
  38. #include "xfs_itable.h"
  39. #include "xfs_btree.h"
  40. #include "xfs_alloc.h"
  41. #include "xfs_ialloc.h"
  42. #include "xfs_attr.h"
  43. #include "xfs_bmap.h"
  44. #include "xfs_error.h"
  45. #include "xfs_buf_item.h"
  46. #include "xfs_rw.h"
  47. #include "xfs_trace.h"
  48. /*
  49. * Force a shutdown of the filesystem instantly while keeping
  50. * the filesystem consistent. We don't do an unmount here; just shutdown
  51. * the shop, make sure that absolutely nothing persistent happens to
  52. * this filesystem after this point.
  53. */
  54. void
  55. xfs_do_force_shutdown(
  56. xfs_mount_t *mp,
  57. int flags,
  58. char *fname,
  59. int lnnum)
  60. {
  61. int logerror;
  62. logerror = flags & SHUTDOWN_LOG_IO_ERROR;
  63. if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  64. cmn_err(CE_NOTE, "xfs_force_shutdown(%s,0x%x) called from "
  65. "line %d of file %s. Return address = 0x%p",
  66. mp->m_fsname, flags, lnnum, fname, __return_address);
  67. }
  68. /*
  69. * No need to duplicate efforts.
  70. */
  71. if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
  72. return;
  73. /*
  74. * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
  75. * queue up anybody new on the log reservations, and wakes up
  76. * everybody who's sleeping on log reservations to tell them
  77. * the bad news.
  78. */
  79. if (xfs_log_force_umount(mp, logerror))
  80. return;
  81. if (flags & SHUTDOWN_CORRUPT_INCORE) {
  82. xfs_cmn_err(XFS_PTAG_SHUTDOWN_CORRUPT, CE_ALERT, mp,
  83. "Corruption of in-memory data detected. Shutting down filesystem: %s",
  84. mp->m_fsname);
  85. if (XFS_ERRLEVEL_HIGH <= xfs_error_level) {
  86. xfs_stack_trace();
  87. }
  88. } else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  89. if (logerror) {
  90. xfs_cmn_err(XFS_PTAG_SHUTDOWN_LOGERROR, CE_ALERT, mp,
  91. "Log I/O Error Detected. Shutting down filesystem: %s",
  92. mp->m_fsname);
  93. } else if (flags & SHUTDOWN_DEVICE_REQ) {
  94. xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
  95. "All device paths lost. Shutting down filesystem: %s",
  96. mp->m_fsname);
  97. } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
  98. xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
  99. "I/O Error Detected. Shutting down filesystem: %s",
  100. mp->m_fsname);
  101. }
  102. }
  103. if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  104. cmn_err(CE_ALERT, "Please umount the filesystem, "
  105. "and rectify the problem(s)");
  106. }
  107. }
  108. /*
  109. * Prints out an ALERT message about I/O error.
  110. */
  111. void
  112. xfs_ioerror_alert(
  113. char *func,
  114. struct xfs_mount *mp,
  115. xfs_buf_t *bp,
  116. xfs_daddr_t blkno)
  117. {
  118. cmn_err(CE_ALERT,
  119. "I/O error in filesystem (\"%s\") meta-data dev %s block 0x%llx"
  120. " (\"%s\") error %d buf count %zd",
  121. (!mp || !mp->m_fsname) ? "(fs name not set)" : mp->m_fsname,
  122. XFS_BUFTARG_NAME(XFS_BUF_TARGET(bp)),
  123. (__uint64_t)blkno, func,
  124. XFS_BUF_GETERROR(bp), XFS_BUF_COUNT(bp));
  125. }
  126. /*
  127. * This isn't an absolute requirement, but it is
  128. * just a good idea to call xfs_read_buf instead of
  129. * directly doing a read_buf call. For one, we shouldn't
  130. * be doing this disk read if we are in SHUTDOWN state anyway,
  131. * so this stops that from happening. Secondly, this does all
  132. * the error checking stuff and the brelse if appropriate for
  133. * the caller, so the code can be a little leaner.
  134. */
  135. int
  136. xfs_read_buf(
  137. struct xfs_mount *mp,
  138. xfs_buftarg_t *target,
  139. xfs_daddr_t blkno,
  140. int len,
  141. uint flags,
  142. xfs_buf_t **bpp)
  143. {
  144. xfs_buf_t *bp;
  145. int error;
  146. if (!flags)
  147. flags = XBF_LOCK | XBF_MAPPED;
  148. bp = xfs_buf_read(target, blkno, len, flags);
  149. if (!bp)
  150. return XFS_ERROR(EIO);
  151. error = XFS_BUF_GETERROR(bp);
  152. if (bp && !error && !XFS_FORCED_SHUTDOWN(mp)) {
  153. *bpp = bp;
  154. } else {
  155. *bpp = NULL;
  156. if (error) {
  157. xfs_ioerror_alert("xfs_read_buf", mp, bp, XFS_BUF_ADDR(bp));
  158. } else {
  159. error = XFS_ERROR(EIO);
  160. }
  161. if (bp) {
  162. XFS_BUF_UNDONE(bp);
  163. XFS_BUF_UNDELAYWRITE(bp);
  164. XFS_BUF_STALE(bp);
  165. /*
  166. * brelse clears B_ERROR and b_error
  167. */
  168. xfs_buf_relse(bp);
  169. }
  170. }
  171. return (error);
  172. }
  173. /*
  174. * helper function to extract extent size hint from inode
  175. */
  176. xfs_extlen_t
  177. xfs_get_extsz_hint(
  178. struct xfs_inode *ip)
  179. {
  180. xfs_extlen_t extsz;
  181. if (unlikely(XFS_IS_REALTIME_INODE(ip))) {
  182. extsz = (ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)
  183. ? ip->i_d.di_extsize
  184. : ip->i_mount->m_sb.sb_rextsize;
  185. ASSERT(extsz);
  186. } else {
  187. extsz = (ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)
  188. ? ip->i_d.di_extsize : 0;
  189. }
  190. return extsz;
  191. }