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