xfs_rw.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_dinode.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_error.h"
  32. #include "xfs_rw.h"
  33. /*
  34. * Force a shutdown of the filesystem instantly while keeping
  35. * the filesystem consistent. We don't do an unmount here; just shutdown
  36. * the shop, make sure that absolutely nothing persistent happens to
  37. * this filesystem after this point.
  38. */
  39. void
  40. xfs_do_force_shutdown(
  41. xfs_mount_t *mp,
  42. int flags,
  43. char *fname,
  44. int lnnum)
  45. {
  46. int logerror;
  47. logerror = flags & SHUTDOWN_LOG_IO_ERROR;
  48. if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  49. cmn_err(CE_NOTE, "xfs_force_shutdown(%s,0x%x) called from "
  50. "line %d of file %s. Return address = 0x%p",
  51. mp->m_fsname, flags, lnnum, fname, __return_address);
  52. }
  53. /*
  54. * No need to duplicate efforts.
  55. */
  56. if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
  57. return;
  58. /*
  59. * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
  60. * queue up anybody new on the log reservations, and wakes up
  61. * everybody who's sleeping on log reservations to tell them
  62. * the bad news.
  63. */
  64. if (xfs_log_force_umount(mp, logerror))
  65. return;
  66. if (flags & SHUTDOWN_CORRUPT_INCORE) {
  67. xfs_cmn_err(XFS_PTAG_SHUTDOWN_CORRUPT, CE_ALERT, mp,
  68. "Corruption of in-memory data detected. Shutting down filesystem: %s",
  69. mp->m_fsname);
  70. if (XFS_ERRLEVEL_HIGH <= xfs_error_level) {
  71. xfs_stack_trace();
  72. }
  73. } else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  74. if (logerror) {
  75. xfs_cmn_err(XFS_PTAG_SHUTDOWN_LOGERROR, CE_ALERT, mp,
  76. "Log I/O Error Detected. Shutting down filesystem: %s",
  77. mp->m_fsname);
  78. } else if (flags & SHUTDOWN_DEVICE_REQ) {
  79. xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
  80. "All device paths lost. Shutting down filesystem: %s",
  81. mp->m_fsname);
  82. } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
  83. xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
  84. "I/O Error Detected. Shutting down filesystem: %s",
  85. mp->m_fsname);
  86. }
  87. }
  88. if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  89. cmn_err(CE_ALERT, "Please umount the filesystem, "
  90. "and rectify the problem(s)");
  91. }
  92. }
  93. /*
  94. * Prints out an ALERT message about I/O error.
  95. */
  96. void
  97. xfs_ioerror_alert(
  98. char *func,
  99. struct xfs_mount *mp,
  100. xfs_buf_t *bp,
  101. xfs_daddr_t blkno)
  102. {
  103. cmn_err(CE_ALERT,
  104. "I/O error in filesystem (\"%s\") meta-data dev %s block 0x%llx"
  105. " (\"%s\") error %d buf count %zd",
  106. (!mp || !mp->m_fsname) ? "(fs name not set)" : mp->m_fsname,
  107. XFS_BUFTARG_NAME(XFS_BUF_TARGET(bp)),
  108. (__uint64_t)blkno, func,
  109. XFS_BUF_GETERROR(bp), XFS_BUF_COUNT(bp));
  110. }
  111. /*
  112. * This isn't an absolute requirement, but it is
  113. * just a good idea to call xfs_read_buf instead of
  114. * directly doing a read_buf call. For one, we shouldn't
  115. * be doing this disk read if we are in SHUTDOWN state anyway,
  116. * so this stops that from happening. Secondly, this does all
  117. * the error checking stuff and the brelse if appropriate for
  118. * the caller, so the code can be a little leaner.
  119. */
  120. int
  121. xfs_read_buf(
  122. struct xfs_mount *mp,
  123. xfs_buftarg_t *target,
  124. xfs_daddr_t blkno,
  125. int len,
  126. uint flags,
  127. xfs_buf_t **bpp)
  128. {
  129. xfs_buf_t *bp;
  130. int error;
  131. if (!flags)
  132. flags = XBF_LOCK | XBF_MAPPED;
  133. bp = xfs_buf_read(target, blkno, len, flags);
  134. if (!bp)
  135. return XFS_ERROR(EIO);
  136. error = XFS_BUF_GETERROR(bp);
  137. if (bp && !error && !XFS_FORCED_SHUTDOWN(mp)) {
  138. *bpp = bp;
  139. } else {
  140. *bpp = NULL;
  141. if (error) {
  142. xfs_ioerror_alert("xfs_read_buf", mp, bp, XFS_BUF_ADDR(bp));
  143. } else {
  144. error = XFS_ERROR(EIO);
  145. }
  146. if (bp) {
  147. XFS_BUF_UNDONE(bp);
  148. XFS_BUF_UNDELAYWRITE(bp);
  149. XFS_BUF_STALE(bp);
  150. /*
  151. * brelse clears B_ERROR and b_error
  152. */
  153. xfs_buf_relse(bp);
  154. }
  155. }
  156. return (error);
  157. }
  158. /*
  159. * helper function to extract extent size hint from inode
  160. */
  161. xfs_extlen_t
  162. xfs_get_extsz_hint(
  163. struct xfs_inode *ip)
  164. {
  165. xfs_extlen_t extsz;
  166. if (unlikely(XFS_IS_REALTIME_INODE(ip))) {
  167. extsz = (ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)
  168. ? ip->i_d.di_extsize
  169. : ip->i_mount->m_sb.sb_rextsize;
  170. ASSERT(extsz);
  171. } else {
  172. extsz = (ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)
  173. ? ip->i_d.di_extsize : 0;
  174. }
  175. return extsz;
  176. }