xfs_rw.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #include "xfs.h"
  33. #include "xfs_macros.h"
  34. #include "xfs_types.h"
  35. #include "xfs_inum.h"
  36. #include "xfs_log.h"
  37. #include "xfs_trans.h"
  38. #include "xfs_sb.h"
  39. #include "xfs_ag.h"
  40. #include "xfs_dir.h"
  41. #include "xfs_dir2.h"
  42. #include "xfs_dmapi.h"
  43. #include "xfs_mount.h"
  44. #include "xfs_alloc_btree.h"
  45. #include "xfs_bmap_btree.h"
  46. #include "xfs_ialloc_btree.h"
  47. #include "xfs_itable.h"
  48. #include "xfs_btree.h"
  49. #include "xfs_alloc.h"
  50. #include "xfs_ialloc.h"
  51. #include "xfs_attr.h"
  52. #include "xfs_attr_sf.h"
  53. #include "xfs_dir_sf.h"
  54. #include "xfs_dir2_sf.h"
  55. #include "xfs_dinode.h"
  56. #include "xfs_inode_item.h"
  57. #include "xfs_inode.h"
  58. #include "xfs_bmap.h"
  59. #include "xfs_acl.h"
  60. #include "xfs_mac.h"
  61. #include "xfs_error.h"
  62. #include "xfs_buf_item.h"
  63. #include "xfs_rw.h"
  64. /*
  65. * This is a subroutine for xfs_write() and other writers (xfs_ioctl)
  66. * which clears the setuid and setgid bits when a file is written.
  67. */
  68. int
  69. xfs_write_clear_setuid(
  70. xfs_inode_t *ip)
  71. {
  72. xfs_mount_t *mp;
  73. xfs_trans_t *tp;
  74. int error;
  75. mp = ip->i_mount;
  76. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  77. if ((error = xfs_trans_reserve(tp, 0,
  78. XFS_WRITEID_LOG_RES(mp),
  79. 0, 0, 0))) {
  80. xfs_trans_cancel(tp, 0);
  81. return error;
  82. }
  83. xfs_ilock(ip, XFS_ILOCK_EXCL);
  84. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  85. xfs_trans_ihold(tp, ip);
  86. ip->i_d.di_mode &= ~S_ISUID;
  87. /*
  88. * Note that we don't have to worry about mandatory
  89. * file locking being disabled here because we only
  90. * clear the S_ISGID bit if the Group execute bit is
  91. * on, but if it was on then mandatory locking wouldn't
  92. * have been enabled.
  93. */
  94. if (ip->i_d.di_mode & S_IXGRP) {
  95. ip->i_d.di_mode &= ~S_ISGID;
  96. }
  97. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  98. xfs_trans_set_sync(tp);
  99. error = xfs_trans_commit(tp, 0, NULL);
  100. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  101. return 0;
  102. }
  103. /*
  104. * Force a shutdown of the filesystem instantly while keeping
  105. * the filesystem consistent. We don't do an unmount here; just shutdown
  106. * the shop, make sure that absolutely nothing persistent happens to
  107. * this filesystem after this point.
  108. */
  109. void
  110. xfs_do_force_shutdown(
  111. bhv_desc_t *bdp,
  112. int flags,
  113. char *fname,
  114. int lnnum)
  115. {
  116. int logerror;
  117. xfs_mount_t *mp;
  118. mp = XFS_BHVTOM(bdp);
  119. logerror = flags & XFS_LOG_IO_ERROR;
  120. if (!(flags & XFS_FORCE_UMOUNT)) {
  121. cmn_err(CE_NOTE,
  122. "xfs_force_shutdown(%s,0x%x) called from line %d of file %s. Return address = 0x%p",
  123. mp->m_fsname,flags,lnnum,fname,__return_address);
  124. }
  125. /*
  126. * No need to duplicate efforts.
  127. */
  128. if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
  129. return;
  130. /*
  131. * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
  132. * queue up anybody new on the log reservations, and wakes up
  133. * everybody who's sleeping on log reservations and tells
  134. * them the bad news.
  135. */
  136. if (xfs_log_force_umount(mp, logerror))
  137. return;
  138. if (flags & XFS_CORRUPT_INCORE) {
  139. xfs_cmn_err(XFS_PTAG_SHUTDOWN_CORRUPT, CE_ALERT, mp,
  140. "Corruption of in-memory data detected. Shutting down filesystem: %s",
  141. mp->m_fsname);
  142. if (XFS_ERRLEVEL_HIGH <= xfs_error_level) {
  143. xfs_stack_trace();
  144. }
  145. } else if (!(flags & XFS_FORCE_UMOUNT)) {
  146. if (logerror) {
  147. xfs_cmn_err(XFS_PTAG_SHUTDOWN_LOGERROR, CE_ALERT, mp,
  148. "Log I/O Error Detected. Shutting down filesystem: %s",
  149. mp->m_fsname);
  150. } else if (!(flags & XFS_SHUTDOWN_REMOTE_REQ)) {
  151. xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
  152. "I/O Error Detected. Shutting down filesystem: %s",
  153. mp->m_fsname);
  154. }
  155. }
  156. if (!(flags & XFS_FORCE_UMOUNT)) {
  157. cmn_err(CE_ALERT,
  158. "Please umount the filesystem, and rectify the problem(s)");
  159. }
  160. }
  161. /*
  162. * Called when we want to stop a buffer from getting written or read.
  163. * We attach the EIO error, muck with its flags, and call biodone
  164. * so that the proper iodone callbacks get called.
  165. */
  166. int
  167. xfs_bioerror(
  168. xfs_buf_t *bp)
  169. {
  170. #ifdef XFSERRORDEBUG
  171. ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
  172. #endif
  173. /*
  174. * No need to wait until the buffer is unpinned.
  175. * We aren't flushing it.
  176. */
  177. xfs_buftrace("XFS IOERROR", bp);
  178. XFS_BUF_ERROR(bp, EIO);
  179. /*
  180. * We're calling biodone, so delete B_DONE flag. Either way
  181. * we have to call the iodone callback, and calling biodone
  182. * probably is the best way since it takes care of
  183. * GRIO as well.
  184. */
  185. XFS_BUF_UNREAD(bp);
  186. XFS_BUF_UNDELAYWRITE(bp);
  187. XFS_BUF_UNDONE(bp);
  188. XFS_BUF_STALE(bp);
  189. XFS_BUF_CLR_BDSTRAT_FUNC(bp);
  190. xfs_biodone(bp);
  191. return (EIO);
  192. }
  193. /*
  194. * Same as xfs_bioerror, except that we are releasing the buffer
  195. * here ourselves, and avoiding the biodone call.
  196. * This is meant for userdata errors; metadata bufs come with
  197. * iodone functions attached, so that we can track down errors.
  198. */
  199. int
  200. xfs_bioerror_relse(
  201. xfs_buf_t *bp)
  202. {
  203. int64_t fl;
  204. ASSERT(XFS_BUF_IODONE_FUNC(bp) != xfs_buf_iodone_callbacks);
  205. ASSERT(XFS_BUF_IODONE_FUNC(bp) != xlog_iodone);
  206. xfs_buftrace("XFS IOERRELSE", bp);
  207. fl = XFS_BUF_BFLAGS(bp);
  208. /*
  209. * No need to wait until the buffer is unpinned.
  210. * We aren't flushing it.
  211. *
  212. * chunkhold expects B_DONE to be set, whether
  213. * we actually finish the I/O or not. We don't want to
  214. * change that interface.
  215. */
  216. XFS_BUF_UNREAD(bp);
  217. XFS_BUF_UNDELAYWRITE(bp);
  218. XFS_BUF_DONE(bp);
  219. XFS_BUF_STALE(bp);
  220. XFS_BUF_CLR_IODONE_FUNC(bp);
  221. XFS_BUF_CLR_BDSTRAT_FUNC(bp);
  222. if (!(fl & XFS_B_ASYNC)) {
  223. /*
  224. * Mark b_error and B_ERROR _both_.
  225. * Lot's of chunkcache code assumes that.
  226. * There's no reason to mark error for
  227. * ASYNC buffers.
  228. */
  229. XFS_BUF_ERROR(bp, EIO);
  230. XFS_BUF_V_IODONESEMA(bp);
  231. } else {
  232. xfs_buf_relse(bp);
  233. }
  234. return (EIO);
  235. }
  236. /*
  237. * Prints out an ALERT message about I/O error.
  238. */
  239. void
  240. xfs_ioerror_alert(
  241. char *func,
  242. struct xfs_mount *mp,
  243. xfs_buf_t *bp,
  244. xfs_daddr_t blkno)
  245. {
  246. cmn_err(CE_ALERT,
  247. "I/O error in filesystem (\"%s\") meta-data dev %s block 0x%llx"
  248. " (\"%s\") error %d buf count %u",
  249. (!mp || !mp->m_fsname) ? "(fs name not set)" : mp->m_fsname,
  250. XFS_BUFTARG_NAME(bp->pb_target),
  251. (__uint64_t)blkno,
  252. func,
  253. XFS_BUF_GETERROR(bp),
  254. XFS_BUF_COUNT(bp));
  255. }
  256. /*
  257. * This isn't an absolute requirement, but it is
  258. * just a good idea to call xfs_read_buf instead of
  259. * directly doing a read_buf call. For one, we shouldn't
  260. * be doing this disk read if we are in SHUTDOWN state anyway,
  261. * so this stops that from happening. Secondly, this does all
  262. * the error checking stuff and the brelse if appropriate for
  263. * the caller, so the code can be a little leaner.
  264. */
  265. int
  266. xfs_read_buf(
  267. struct xfs_mount *mp,
  268. xfs_buftarg_t *target,
  269. xfs_daddr_t blkno,
  270. int len,
  271. uint flags,
  272. xfs_buf_t **bpp)
  273. {
  274. xfs_buf_t *bp;
  275. int error;
  276. if (flags)
  277. bp = xfs_buf_read_flags(target, blkno, len, flags);
  278. else
  279. bp = xfs_buf_read(target, blkno, len, flags);
  280. if (!bp)
  281. return XFS_ERROR(EIO);
  282. error = XFS_BUF_GETERROR(bp);
  283. if (bp && !error && !XFS_FORCED_SHUTDOWN(mp)) {
  284. *bpp = bp;
  285. } else {
  286. *bpp = NULL;
  287. if (error) {
  288. xfs_ioerror_alert("xfs_read_buf", mp, bp, XFS_BUF_ADDR(bp));
  289. } else {
  290. error = XFS_ERROR(EIO);
  291. }
  292. if (bp) {
  293. XFS_BUF_UNDONE(bp);
  294. XFS_BUF_UNDELAYWRITE(bp);
  295. XFS_BUF_STALE(bp);
  296. /*
  297. * brelse clears B_ERROR and b_error
  298. */
  299. xfs_buf_relse(bp);
  300. }
  301. }
  302. return (error);
  303. }
  304. /*
  305. * Wrapper around bwrite() so that we can trap
  306. * write errors, and act accordingly.
  307. */
  308. int
  309. xfs_bwrite(
  310. struct xfs_mount *mp,
  311. struct xfs_buf *bp)
  312. {
  313. int error;
  314. /*
  315. * XXXsup how does this work for quotas.
  316. */
  317. XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
  318. XFS_BUF_SET_FSPRIVATE3(bp, mp);
  319. XFS_BUF_WRITE(bp);
  320. if ((error = XFS_bwrite(bp))) {
  321. ASSERT(mp);
  322. /*
  323. * Cannot put a buftrace here since if the buffer is not
  324. * B_HOLD then we will brelse() the buffer before returning
  325. * from bwrite and we could be tracing a buffer that has
  326. * been reused.
  327. */
  328. xfs_force_shutdown(mp, XFS_METADATA_IO_ERROR);
  329. }
  330. return (error);
  331. }