xfs_rw.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. xfs_notice(mp,
  50. "%s(0x%x) called from line %d of file %s. Return address = 0x%p",
  51. __func__, 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_alert_tag(mp, XFS_PTAG_SHUTDOWN_CORRUPT,
  68. "Corruption of in-memory data detected. Shutting down filesystem");
  69. if (XFS_ERRLEVEL_HIGH <= xfs_error_level)
  70. xfs_stack_trace();
  71. } else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  72. if (logerror) {
  73. xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_LOGERROR,
  74. "Log I/O Error Detected. Shutting down filesystem");
  75. } else if (flags & SHUTDOWN_DEVICE_REQ) {
  76. xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR,
  77. "All device paths lost. Shutting down filesystem");
  78. } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
  79. xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR,
  80. "I/O Error Detected. Shutting down filesystem");
  81. }
  82. }
  83. if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  84. xfs_alert(mp,
  85. "Please umount the filesystem and rectify the problem(s)");
  86. }
  87. }
  88. /*
  89. * Prints out an ALERT message about I/O error.
  90. */
  91. void
  92. xfs_ioerror_alert(
  93. char *func,
  94. struct xfs_mount *mp,
  95. xfs_buf_t *bp,
  96. xfs_daddr_t blkno)
  97. {
  98. xfs_alert(mp,
  99. "I/O error occurred: meta-data dev %s block 0x%llx"
  100. " (\"%s\") error %d buf count %zd",
  101. XFS_BUFTARG_NAME(XFS_BUF_TARGET(bp)),
  102. (__uint64_t)blkno, func,
  103. XFS_BUF_GETERROR(bp), XFS_BUF_COUNT(bp));
  104. }
  105. /*
  106. * This isn't an absolute requirement, but it is
  107. * just a good idea to call xfs_read_buf instead of
  108. * directly doing a read_buf call. For one, we shouldn't
  109. * be doing this disk read if we are in SHUTDOWN state anyway,
  110. * so this stops that from happening. Secondly, this does all
  111. * the error checking stuff and the brelse if appropriate for
  112. * the caller, so the code can be a little leaner.
  113. */
  114. int
  115. xfs_read_buf(
  116. struct xfs_mount *mp,
  117. xfs_buftarg_t *target,
  118. xfs_daddr_t blkno,
  119. int len,
  120. uint flags,
  121. xfs_buf_t **bpp)
  122. {
  123. xfs_buf_t *bp;
  124. int error;
  125. if (!flags)
  126. flags = XBF_LOCK | XBF_MAPPED;
  127. bp = xfs_buf_read(target, blkno, len, flags);
  128. if (!bp)
  129. return XFS_ERROR(EIO);
  130. error = XFS_BUF_GETERROR(bp);
  131. if (bp && !error && !XFS_FORCED_SHUTDOWN(mp)) {
  132. *bpp = bp;
  133. } else {
  134. *bpp = NULL;
  135. if (error) {
  136. xfs_ioerror_alert("xfs_read_buf", mp, bp, XFS_BUF_ADDR(bp));
  137. } else {
  138. error = XFS_ERROR(EIO);
  139. }
  140. if (bp) {
  141. XFS_BUF_UNDONE(bp);
  142. XFS_BUF_UNDELAYWRITE(bp);
  143. XFS_BUF_STALE(bp);
  144. /*
  145. * brelse clears B_ERROR and b_error
  146. */
  147. xfs_buf_relse(bp);
  148. }
  149. }
  150. return (error);
  151. }
  152. /*
  153. * helper function to extract extent size hint from inode
  154. */
  155. xfs_extlen_t
  156. xfs_get_extsz_hint(
  157. struct xfs_inode *ip)
  158. {
  159. if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
  160. return ip->i_d.di_extsize;
  161. if (XFS_IS_REALTIME_INODE(ip))
  162. return ip->i_mount->m_sb.sb_rextsize;
  163. return 0;
  164. }