xfs_rw.c 11 KB

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