xfs_rw.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. * This is a subroutine for xfs_write() and other writers (xfs_ioctl)
  50. * which clears the setuid and setgid bits when a file is written.
  51. */
  52. int
  53. xfs_write_clear_setuid(
  54. xfs_inode_t *ip)
  55. {
  56. xfs_mount_t *mp;
  57. xfs_trans_t *tp;
  58. int error;
  59. mp = ip->i_mount;
  60. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  61. if ((error = xfs_trans_reserve(tp, 0,
  62. XFS_WRITEID_LOG_RES(mp),
  63. 0, 0, 0))) {
  64. xfs_trans_cancel(tp, 0);
  65. return error;
  66. }
  67. xfs_ilock(ip, XFS_ILOCK_EXCL);
  68. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  69. xfs_trans_ihold(tp, ip);
  70. ip->i_d.di_mode &= ~S_ISUID;
  71. /*
  72. * Note that we don't have to worry about mandatory
  73. * file locking being disabled here because we only
  74. * clear the S_ISGID bit if the Group execute bit is
  75. * on, but if it was on then mandatory locking wouldn't
  76. * have been enabled.
  77. */
  78. if (ip->i_d.di_mode & S_IXGRP) {
  79. ip->i_d.di_mode &= ~S_ISGID;
  80. }
  81. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  82. xfs_trans_set_sync(tp);
  83. error = xfs_trans_commit(tp, 0);
  84. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  85. return 0;
  86. }
  87. /*
  88. * Force a shutdown of the filesystem instantly while keeping
  89. * the filesystem consistent. We don't do an unmount here; just shutdown
  90. * the shop, make sure that absolutely nothing persistent happens to
  91. * this filesystem after this point.
  92. */
  93. void
  94. xfs_do_force_shutdown(
  95. xfs_mount_t *mp,
  96. int flags,
  97. char *fname,
  98. int lnnum)
  99. {
  100. int logerror;
  101. logerror = flags & SHUTDOWN_LOG_IO_ERROR;
  102. if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  103. cmn_err(CE_NOTE, "xfs_force_shutdown(%s,0x%x) called from "
  104. "line %d of file %s. Return address = 0x%p",
  105. mp->m_fsname, flags, lnnum, fname, __return_address);
  106. }
  107. /*
  108. * No need to duplicate efforts.
  109. */
  110. if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
  111. return;
  112. /*
  113. * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
  114. * queue up anybody new on the log reservations, and wakes up
  115. * everybody who's sleeping on log reservations to tell them
  116. * the bad news.
  117. */
  118. if (xfs_log_force_umount(mp, logerror))
  119. return;
  120. if (flags & SHUTDOWN_CORRUPT_INCORE) {
  121. xfs_cmn_err(XFS_PTAG_SHUTDOWN_CORRUPT, CE_ALERT, mp,
  122. "Corruption of in-memory data detected. Shutting down filesystem: %s",
  123. mp->m_fsname);
  124. if (XFS_ERRLEVEL_HIGH <= xfs_error_level) {
  125. xfs_stack_trace();
  126. }
  127. } else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  128. if (logerror) {
  129. xfs_cmn_err(XFS_PTAG_SHUTDOWN_LOGERROR, CE_ALERT, mp,
  130. "Log I/O Error Detected. Shutting down filesystem: %s",
  131. mp->m_fsname);
  132. } else if (flags & SHUTDOWN_DEVICE_REQ) {
  133. xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
  134. "All device paths lost. Shutting down filesystem: %s",
  135. mp->m_fsname);
  136. } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
  137. xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
  138. "I/O Error Detected. Shutting down filesystem: %s",
  139. mp->m_fsname);
  140. }
  141. }
  142. if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  143. cmn_err(CE_ALERT, "Please umount the filesystem, "
  144. "and rectify the problem(s)");
  145. }
  146. }
  147. /*
  148. * Called when we want to stop a buffer from getting written or read.
  149. * We attach the EIO error, muck with its flags, and call biodone
  150. * so that the proper iodone callbacks get called.
  151. */
  152. int
  153. xfs_bioerror(
  154. xfs_buf_t *bp)
  155. {
  156. #ifdef XFSERRORDEBUG
  157. ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
  158. #endif
  159. /*
  160. * No need to wait until the buffer is unpinned.
  161. * We aren't flushing it.
  162. */
  163. XFS_BUF_ERROR(bp, EIO);
  164. /*
  165. * We're calling biodone, so delete B_DONE flag. Either way
  166. * we have to call the iodone callback, and calling biodone
  167. * probably is the best way since it takes care of
  168. * GRIO as well.
  169. */
  170. XFS_BUF_UNREAD(bp);
  171. XFS_BUF_UNDELAYWRITE(bp);
  172. XFS_BUF_UNDONE(bp);
  173. XFS_BUF_STALE(bp);
  174. XFS_BUF_CLR_BDSTRAT_FUNC(bp);
  175. xfs_biodone(bp);
  176. return (EIO);
  177. }
  178. /*
  179. * Same as xfs_bioerror, except that we are releasing the buffer
  180. * here ourselves, and avoiding the biodone call.
  181. * This is meant for userdata errors; metadata bufs come with
  182. * iodone functions attached, so that we can track down errors.
  183. */
  184. int
  185. xfs_bioerror_relse(
  186. xfs_buf_t *bp)
  187. {
  188. int64_t fl;
  189. ASSERT(XFS_BUF_IODONE_FUNC(bp) != xfs_buf_iodone_callbacks);
  190. ASSERT(XFS_BUF_IODONE_FUNC(bp) != xlog_iodone);
  191. fl = XFS_BUF_BFLAGS(bp);
  192. /*
  193. * No need to wait until the buffer is unpinned.
  194. * We aren't flushing it.
  195. *
  196. * chunkhold expects B_DONE to be set, whether
  197. * we actually finish the I/O or not. We don't want to
  198. * change that interface.
  199. */
  200. XFS_BUF_UNREAD(bp);
  201. XFS_BUF_UNDELAYWRITE(bp);
  202. XFS_BUF_DONE(bp);
  203. XFS_BUF_STALE(bp);
  204. XFS_BUF_CLR_IODONE_FUNC(bp);
  205. XFS_BUF_CLR_BDSTRAT_FUNC(bp);
  206. if (!(fl & XFS_B_ASYNC)) {
  207. /*
  208. * Mark b_error and B_ERROR _both_.
  209. * Lot's of chunkcache code assumes that.
  210. * There's no reason to mark error for
  211. * ASYNC buffers.
  212. */
  213. XFS_BUF_ERROR(bp, EIO);
  214. XFS_BUF_FINISH_IOWAIT(bp);
  215. } else {
  216. xfs_buf_relse(bp);
  217. }
  218. return (EIO);
  219. }
  220. /*
  221. * Prints out an ALERT message about I/O error.
  222. */
  223. void
  224. xfs_ioerror_alert(
  225. char *func,
  226. struct xfs_mount *mp,
  227. xfs_buf_t *bp,
  228. xfs_daddr_t blkno)
  229. {
  230. cmn_err(CE_ALERT,
  231. "I/O error in filesystem (\"%s\") meta-data dev %s block 0x%llx"
  232. " (\"%s\") error %d buf count %zd",
  233. (!mp || !mp->m_fsname) ? "(fs name not set)" : mp->m_fsname,
  234. XFS_BUFTARG_NAME(XFS_BUF_TARGET(bp)),
  235. (__uint64_t)blkno, func,
  236. XFS_BUF_GETERROR(bp), XFS_BUF_COUNT(bp));
  237. }
  238. /*
  239. * This isn't an absolute requirement, but it is
  240. * just a good idea to call xfs_read_buf instead of
  241. * directly doing a read_buf call. For one, we shouldn't
  242. * be doing this disk read if we are in SHUTDOWN state anyway,
  243. * so this stops that from happening. Secondly, this does all
  244. * the error checking stuff and the brelse if appropriate for
  245. * the caller, so the code can be a little leaner.
  246. */
  247. int
  248. xfs_read_buf(
  249. struct xfs_mount *mp,
  250. xfs_buftarg_t *target,
  251. xfs_daddr_t blkno,
  252. int len,
  253. uint flags,
  254. xfs_buf_t **bpp)
  255. {
  256. xfs_buf_t *bp;
  257. int error;
  258. if (!flags)
  259. flags = XBF_LOCK | XBF_MAPPED;
  260. bp = xfs_buf_read(target, blkno, len, flags);
  261. if (!bp)
  262. return XFS_ERROR(EIO);
  263. error = XFS_BUF_GETERROR(bp);
  264. if (bp && !error && !XFS_FORCED_SHUTDOWN(mp)) {
  265. *bpp = bp;
  266. } else {
  267. *bpp = NULL;
  268. if (error) {
  269. xfs_ioerror_alert("xfs_read_buf", mp, bp, XFS_BUF_ADDR(bp));
  270. } else {
  271. error = XFS_ERROR(EIO);
  272. }
  273. if (bp) {
  274. XFS_BUF_UNDONE(bp);
  275. XFS_BUF_UNDELAYWRITE(bp);
  276. XFS_BUF_STALE(bp);
  277. /*
  278. * brelse clears B_ERROR and b_error
  279. */
  280. xfs_buf_relse(bp);
  281. }
  282. }
  283. return (error);
  284. }
  285. /*
  286. * Wrapper around bwrite() so that we can trap
  287. * write errors, and act accordingly.
  288. */
  289. int
  290. xfs_bwrite(
  291. struct xfs_mount *mp,
  292. struct xfs_buf *bp)
  293. {
  294. int error;
  295. /*
  296. * XXXsup how does this work for quotas.
  297. */
  298. XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
  299. bp->b_mount = mp;
  300. XFS_BUF_WRITE(bp);
  301. if ((error = XFS_bwrite(bp))) {
  302. ASSERT(mp);
  303. /*
  304. * Cannot put a buftrace here since if the buffer is not
  305. * B_HOLD then we will brelse() the buffer before returning
  306. * from bwrite and we could be tracing a buffer that has
  307. * been reused.
  308. */
  309. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  310. }
  311. return (error);
  312. }
  313. /*
  314. * helper function to extract extent size hint from inode
  315. */
  316. xfs_extlen_t
  317. xfs_get_extsz_hint(
  318. struct xfs_inode *ip)
  319. {
  320. xfs_extlen_t extsz;
  321. if (unlikely(XFS_IS_REALTIME_INODE(ip))) {
  322. extsz = (ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)
  323. ? ip->i_d.di_extsize
  324. : ip->i_mount->m_sb.sb_rextsize;
  325. ASSERT(extsz);
  326. } else {
  327. extsz = (ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)
  328. ? ip->i_d.di_extsize : 0;
  329. }
  330. return extsz;
  331. }