xfs_rw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. /*
  48. * This is a subroutine for xfs_write() and other writers (xfs_ioctl)
  49. * which clears the setuid and setgid bits when a file is written.
  50. */
  51. int
  52. xfs_write_clear_setuid(
  53. xfs_inode_t *ip)
  54. {
  55. xfs_mount_t *mp;
  56. xfs_trans_t *tp;
  57. int error;
  58. mp = ip->i_mount;
  59. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  60. if ((error = xfs_trans_reserve(tp, 0,
  61. XFS_WRITEID_LOG_RES(mp),
  62. 0, 0, 0))) {
  63. xfs_trans_cancel(tp, 0);
  64. return error;
  65. }
  66. xfs_ilock(ip, XFS_ILOCK_EXCL);
  67. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  68. xfs_trans_ihold(tp, ip);
  69. ip->i_d.di_mode &= ~S_ISUID;
  70. /*
  71. * Note that we don't have to worry about mandatory
  72. * file locking being disabled here because we only
  73. * clear the S_ISGID bit if the Group execute bit is
  74. * on, but if it was on then mandatory locking wouldn't
  75. * have been enabled.
  76. */
  77. if (ip->i_d.di_mode & S_IXGRP) {
  78. ip->i_d.di_mode &= ~S_ISGID;
  79. }
  80. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  81. xfs_trans_set_sync(tp);
  82. error = xfs_trans_commit(tp, 0);
  83. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  84. return 0;
  85. }
  86. /*
  87. * Handle logging requirements of various synchronous types of write.
  88. */
  89. int
  90. xfs_write_sync_logforce(
  91. xfs_mount_t *mp,
  92. xfs_inode_t *ip)
  93. {
  94. int error = 0;
  95. /*
  96. * If we're treating this as O_DSYNC and we have not updated the
  97. * size, force the log.
  98. */
  99. if (!(mp->m_flags & XFS_MOUNT_OSYNCISOSYNC) &&
  100. !(ip->i_update_size)) {
  101. xfs_inode_log_item_t *iip = ip->i_itemp;
  102. /*
  103. * If an allocation transaction occurred
  104. * without extending the size, then we have to force
  105. * the log up the proper point to ensure that the
  106. * allocation is permanent. We can't count on
  107. * the fact that buffered writes lock out direct I/O
  108. * writes - the direct I/O write could have extended
  109. * the size nontransactionally, then finished before
  110. * we started. xfs_write_file will think that the file
  111. * didn't grow but the update isn't safe unless the
  112. * size change is logged.
  113. *
  114. * Force the log if we've committed a transaction
  115. * against the inode or if someone else has and
  116. * the commit record hasn't gone to disk (e.g.
  117. * the inode is pinned). This guarantees that
  118. * all changes affecting the inode are permanent
  119. * when we return.
  120. */
  121. if (iip && iip->ili_last_lsn) {
  122. error = _xfs_log_force(mp, iip->ili_last_lsn,
  123. XFS_LOG_FORCE | XFS_LOG_SYNC, NULL);
  124. } else if (xfs_ipincount(ip) > 0) {
  125. error = _xfs_log_force(mp, (xfs_lsn_t)0,
  126. XFS_LOG_FORCE | XFS_LOG_SYNC, NULL);
  127. }
  128. } else {
  129. xfs_trans_t *tp;
  130. /*
  131. * O_SYNC or O_DSYNC _with_ a size update are handled
  132. * the same way.
  133. *
  134. * If the write was synchronous then we need to make
  135. * sure that the inode modification time is permanent.
  136. * We'll have updated the timestamp above, so here
  137. * we use a synchronous transaction to log the inode.
  138. * It's not fast, but it's necessary.
  139. *
  140. * If this a dsync write and the size got changed
  141. * non-transactionally, then we need to ensure that
  142. * the size change gets logged in a synchronous
  143. * transaction.
  144. */
  145. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITE_SYNC);
  146. if ((error = xfs_trans_reserve(tp, 0,
  147. XFS_SWRITE_LOG_RES(mp),
  148. 0, 0, 0))) {
  149. /* Transaction reserve failed */
  150. xfs_trans_cancel(tp, 0);
  151. } else {
  152. /* Transaction reserve successful */
  153. xfs_ilock(ip, XFS_ILOCK_EXCL);
  154. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  155. xfs_trans_ihold(tp, ip);
  156. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  157. xfs_trans_set_sync(tp);
  158. error = xfs_trans_commit(tp, 0);
  159. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  160. }
  161. }
  162. return error;
  163. }
  164. /*
  165. * Force a shutdown of the filesystem instantly while keeping
  166. * the filesystem consistent. We don't do an unmount here; just shutdown
  167. * the shop, make sure that absolutely nothing persistent happens to
  168. * this filesystem after this point.
  169. */
  170. void
  171. xfs_do_force_shutdown(
  172. xfs_mount_t *mp,
  173. int flags,
  174. char *fname,
  175. int lnnum)
  176. {
  177. int logerror;
  178. logerror = flags & SHUTDOWN_LOG_IO_ERROR;
  179. if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  180. cmn_err(CE_NOTE, "xfs_force_shutdown(%s,0x%x) called from "
  181. "line %d of file %s. Return address = 0x%p",
  182. mp->m_fsname, flags, lnnum, fname, __return_address);
  183. }
  184. /*
  185. * No need to duplicate efforts.
  186. */
  187. if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
  188. return;
  189. /*
  190. * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
  191. * queue up anybody new on the log reservations, and wakes up
  192. * everybody who's sleeping on log reservations to tell them
  193. * the bad news.
  194. */
  195. if (xfs_log_force_umount(mp, logerror))
  196. return;
  197. if (flags & SHUTDOWN_CORRUPT_INCORE) {
  198. xfs_cmn_err(XFS_PTAG_SHUTDOWN_CORRUPT, CE_ALERT, mp,
  199. "Corruption of in-memory data detected. Shutting down filesystem: %s",
  200. mp->m_fsname);
  201. if (XFS_ERRLEVEL_HIGH <= xfs_error_level) {
  202. xfs_stack_trace();
  203. }
  204. } else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  205. if (logerror) {
  206. xfs_cmn_err(XFS_PTAG_SHUTDOWN_LOGERROR, CE_ALERT, mp,
  207. "Log I/O Error Detected. Shutting down filesystem: %s",
  208. mp->m_fsname);
  209. } else if (flags & SHUTDOWN_DEVICE_REQ) {
  210. xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
  211. "All device paths lost. Shutting down filesystem: %s",
  212. mp->m_fsname);
  213. } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
  214. xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
  215. "I/O Error Detected. Shutting down filesystem: %s",
  216. mp->m_fsname);
  217. }
  218. }
  219. if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  220. cmn_err(CE_ALERT, "Please umount the filesystem, "
  221. "and rectify the problem(s)");
  222. }
  223. }
  224. /*
  225. * Called when we want to stop a buffer from getting written or read.
  226. * We attach the EIO error, muck with its flags, and call biodone
  227. * so that the proper iodone callbacks get called.
  228. */
  229. int
  230. xfs_bioerror(
  231. xfs_buf_t *bp)
  232. {
  233. #ifdef XFSERRORDEBUG
  234. ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
  235. #endif
  236. /*
  237. * No need to wait until the buffer is unpinned.
  238. * We aren't flushing it.
  239. */
  240. xfs_buftrace("XFS IOERROR", bp);
  241. XFS_BUF_ERROR(bp, EIO);
  242. /*
  243. * We're calling biodone, so delete B_DONE flag. Either way
  244. * we have to call the iodone callback, and calling biodone
  245. * probably is the best way since it takes care of
  246. * GRIO as well.
  247. */
  248. XFS_BUF_UNREAD(bp);
  249. XFS_BUF_UNDELAYWRITE(bp);
  250. XFS_BUF_UNDONE(bp);
  251. XFS_BUF_STALE(bp);
  252. XFS_BUF_CLR_BDSTRAT_FUNC(bp);
  253. xfs_biodone(bp);
  254. return (EIO);
  255. }
  256. /*
  257. * Same as xfs_bioerror, except that we are releasing the buffer
  258. * here ourselves, and avoiding the biodone call.
  259. * This is meant for userdata errors; metadata bufs come with
  260. * iodone functions attached, so that we can track down errors.
  261. */
  262. int
  263. xfs_bioerror_relse(
  264. xfs_buf_t *bp)
  265. {
  266. int64_t fl;
  267. ASSERT(XFS_BUF_IODONE_FUNC(bp) != xfs_buf_iodone_callbacks);
  268. ASSERT(XFS_BUF_IODONE_FUNC(bp) != xlog_iodone);
  269. xfs_buftrace("XFS IOERRELSE", bp);
  270. fl = XFS_BUF_BFLAGS(bp);
  271. /*
  272. * No need to wait until the buffer is unpinned.
  273. * We aren't flushing it.
  274. *
  275. * chunkhold expects B_DONE to be set, whether
  276. * we actually finish the I/O or not. We don't want to
  277. * change that interface.
  278. */
  279. XFS_BUF_UNREAD(bp);
  280. XFS_BUF_UNDELAYWRITE(bp);
  281. XFS_BUF_DONE(bp);
  282. XFS_BUF_STALE(bp);
  283. XFS_BUF_CLR_IODONE_FUNC(bp);
  284. XFS_BUF_CLR_BDSTRAT_FUNC(bp);
  285. if (!(fl & XFS_B_ASYNC)) {
  286. /*
  287. * Mark b_error and B_ERROR _both_.
  288. * Lot's of chunkcache code assumes that.
  289. * There's no reason to mark error for
  290. * ASYNC buffers.
  291. */
  292. XFS_BUF_ERROR(bp, EIO);
  293. XFS_BUF_FINISH_IOWAIT(bp);
  294. } else {
  295. xfs_buf_relse(bp);
  296. }
  297. return (EIO);
  298. }
  299. /*
  300. * Prints out an ALERT message about I/O error.
  301. */
  302. void
  303. xfs_ioerror_alert(
  304. char *func,
  305. struct xfs_mount *mp,
  306. xfs_buf_t *bp,
  307. xfs_daddr_t blkno)
  308. {
  309. cmn_err(CE_ALERT,
  310. "I/O error in filesystem (\"%s\") meta-data dev %s block 0x%llx"
  311. " (\"%s\") error %d buf count %zd",
  312. (!mp || !mp->m_fsname) ? "(fs name not set)" : mp->m_fsname,
  313. XFS_BUFTARG_NAME(XFS_BUF_TARGET(bp)),
  314. (__uint64_t)blkno, func,
  315. XFS_BUF_GETERROR(bp), XFS_BUF_COUNT(bp));
  316. }
  317. /*
  318. * This isn't an absolute requirement, but it is
  319. * just a good idea to call xfs_read_buf instead of
  320. * directly doing a read_buf call. For one, we shouldn't
  321. * be doing this disk read if we are in SHUTDOWN state anyway,
  322. * so this stops that from happening. Secondly, this does all
  323. * the error checking stuff and the brelse if appropriate for
  324. * the caller, so the code can be a little leaner.
  325. */
  326. int
  327. xfs_read_buf(
  328. struct xfs_mount *mp,
  329. xfs_buftarg_t *target,
  330. xfs_daddr_t blkno,
  331. int len,
  332. uint flags,
  333. xfs_buf_t **bpp)
  334. {
  335. xfs_buf_t *bp;
  336. int error;
  337. if (flags)
  338. bp = xfs_buf_read_flags(target, blkno, len, flags);
  339. else
  340. bp = xfs_buf_read(target, blkno, len, flags);
  341. if (!bp)
  342. return XFS_ERROR(EIO);
  343. error = XFS_BUF_GETERROR(bp);
  344. if (bp && !error && !XFS_FORCED_SHUTDOWN(mp)) {
  345. *bpp = bp;
  346. } else {
  347. *bpp = NULL;
  348. if (error) {
  349. xfs_ioerror_alert("xfs_read_buf", mp, bp, XFS_BUF_ADDR(bp));
  350. } else {
  351. error = XFS_ERROR(EIO);
  352. }
  353. if (bp) {
  354. XFS_BUF_UNDONE(bp);
  355. XFS_BUF_UNDELAYWRITE(bp);
  356. XFS_BUF_STALE(bp);
  357. /*
  358. * brelse clears B_ERROR and b_error
  359. */
  360. xfs_buf_relse(bp);
  361. }
  362. }
  363. return (error);
  364. }
  365. /*
  366. * Wrapper around bwrite() so that we can trap
  367. * write errors, and act accordingly.
  368. */
  369. int
  370. xfs_bwrite(
  371. struct xfs_mount *mp,
  372. struct xfs_buf *bp)
  373. {
  374. int error;
  375. /*
  376. * XXXsup how does this work for quotas.
  377. */
  378. XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
  379. bp->b_mount = mp;
  380. XFS_BUF_WRITE(bp);
  381. if ((error = XFS_bwrite(bp))) {
  382. ASSERT(mp);
  383. /*
  384. * Cannot put a buftrace here since if the buffer is not
  385. * B_HOLD then we will brelse() the buffer before returning
  386. * from bwrite and we could be tracing a buffer that has
  387. * been reused.
  388. */
  389. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  390. }
  391. return (error);
  392. }