2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
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.
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.
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
20 #include "xfs_types.h"
24 #include "xfs_trans.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"
46 #include "xfs_error.h"
47 #include "xfs_buf_item.h"
51 * This is a subroutine for xfs_write() and other writers (xfs_ioctl)
52 * which clears the setuid and setgid bits when a file is written.
55 xfs_write_clear_setuid(
63 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
64 if ((error = xfs_trans_reserve(tp, 0,
65 XFS_WRITEID_LOG_RES(mp),
67 xfs_trans_cancel(tp, 0);
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;
76 * Note that we don't have to worry about mandatory
77 * file locking being disabled here because we only
78 * clear the S_ISGID bit if the Group execute bit is
79 * on, but if it was on then mandatory locking wouldn't
82 if (ip->i_d.di_mode & S_IXGRP) {
83 ip->i_d.di_mode &= ~S_ISGID;
85 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
86 xfs_trans_set_sync(tp);
87 error = xfs_trans_commit(tp, 0, NULL);
88 xfs_iunlock(ip, XFS_ILOCK_EXCL);
93 * Handle logging requirements of various synchronous types of write.
96 xfs_write_sync_logforce(
103 * If we're treating this as O_DSYNC and we have not updated the
104 * size, force the log.
106 if (!(mp->m_flags & XFS_MOUNT_OSYNCISOSYNC) &&
107 !(ip->i_update_size)) {
108 xfs_inode_log_item_t *iip = ip->i_itemp;
111 * If an allocation transaction occurred
112 * without extending the size, then we have to force
113 * the log up the proper point to ensure that the
114 * allocation is permanent. We can't count on
115 * the fact that buffered writes lock out direct I/O
116 * writes - the direct I/O write could have extended
117 * the size nontransactionally, then finished before
118 * we started. xfs_write_file will think that the file
119 * didn't grow but the update isn't safe unless the
120 * size change is logged.
122 * Force the log if we've committed a transaction
123 * against the inode or if someone else has and
124 * the commit record hasn't gone to disk (e.g.
125 * the inode is pinned). This guarantees that
126 * all changes affecting the inode are permanent
129 if (iip && iip->ili_last_lsn) {
130 xfs_log_force(mp, iip->ili_last_lsn,
131 XFS_LOG_FORCE | XFS_LOG_SYNC);
132 } else if (xfs_ipincount(ip) > 0) {
133 xfs_log_force(mp, (xfs_lsn_t)0,
134 XFS_LOG_FORCE | XFS_LOG_SYNC);
141 * O_SYNC or O_DSYNC _with_ a size update are handled
144 * If the write was synchronous then we need to make
145 * sure that the inode modification time is permanent.
146 * We'll have updated the timestamp above, so here
147 * we use a synchronous transaction to log the inode.
148 * It's not fast, but it's necessary.
150 * If this a dsync write and the size got changed
151 * non-transactionally, then we need to ensure that
152 * the size change gets logged in a synchronous
155 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITE_SYNC);
156 if ((error = xfs_trans_reserve(tp, 0,
157 XFS_SWRITE_LOG_RES(mp),
159 /* Transaction reserve failed */
160 xfs_trans_cancel(tp, 0);
162 /* Transaction reserve successful */
163 xfs_ilock(ip, XFS_ILOCK_EXCL);
164 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
165 xfs_trans_ihold(tp, ip);
166 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
167 xfs_trans_set_sync(tp);
168 error = xfs_trans_commit(tp, 0, NULL);
169 xfs_iunlock(ip, XFS_ILOCK_EXCL);
177 * Force a shutdown of the filesystem instantly while keeping
178 * the filesystem consistent. We don't do an unmount here; just shutdown
179 * the shop, make sure that absolutely nothing persistent happens to
180 * this filesystem after this point.
184 xfs_do_force_shutdown(
193 mp = XFS_BHVTOM(bdp);
194 logerror = flags & SHUTDOWN_LOG_IO_ERROR;
196 if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
197 cmn_err(CE_NOTE, "xfs_force_shutdown(%s,0x%x) called from "
198 "line %d of file %s. Return address = 0x%p",
199 mp->m_fsname, flags, lnnum, fname, __return_address);
202 * No need to duplicate efforts.
204 if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
208 * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
209 * queue up anybody new on the log reservations, and wakes up
210 * everybody who's sleeping on log reservations to tell them
213 if (xfs_log_force_umount(mp, logerror))
216 if (flags & SHUTDOWN_CORRUPT_INCORE) {
217 xfs_cmn_err(XFS_PTAG_SHUTDOWN_CORRUPT, CE_ALERT, mp,
218 "Corruption of in-memory data detected. Shutting down filesystem: %s",
220 if (XFS_ERRLEVEL_HIGH <= xfs_error_level) {
223 } else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
225 xfs_cmn_err(XFS_PTAG_SHUTDOWN_LOGERROR, CE_ALERT, mp,
226 "Log I/O Error Detected. Shutting down filesystem: %s",
228 } else if (flags & SHUTDOWN_DEVICE_REQ) {
229 xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
230 "All device paths lost. Shutting down filesystem: %s",
232 } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
233 xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
234 "I/O Error Detected. Shutting down filesystem: %s",
238 if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
239 cmn_err(CE_ALERT, "Please umount the filesystem, "
240 "and rectify the problem(s)");
246 * Called when we want to stop a buffer from getting written or read.
247 * We attach the EIO error, muck with its flags, and call biodone
248 * so that the proper iodone callbacks get called.
256 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
260 * No need to wait until the buffer is unpinned.
261 * We aren't flushing it.
263 xfs_buftrace("XFS IOERROR", bp);
264 XFS_BUF_ERROR(bp, EIO);
266 * We're calling biodone, so delete B_DONE flag. Either way
267 * we have to call the iodone callback, and calling biodone
268 * probably is the best way since it takes care of
272 XFS_BUF_UNDELAYWRITE(bp);
276 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
283 * Same as xfs_bioerror, except that we are releasing the buffer
284 * here ourselves, and avoiding the biodone call.
285 * This is meant for userdata errors; metadata bufs come with
286 * iodone functions attached, so that we can track down errors.
294 ASSERT(XFS_BUF_IODONE_FUNC(bp) != xfs_buf_iodone_callbacks);
295 ASSERT(XFS_BUF_IODONE_FUNC(bp) != xlog_iodone);
297 xfs_buftrace("XFS IOERRELSE", bp);
298 fl = XFS_BUF_BFLAGS(bp);
300 * No need to wait until the buffer is unpinned.
301 * We aren't flushing it.
303 * chunkhold expects B_DONE to be set, whether
304 * we actually finish the I/O or not. We don't want to
305 * change that interface.
308 XFS_BUF_UNDELAYWRITE(bp);
311 XFS_BUF_CLR_IODONE_FUNC(bp);
312 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
313 if (!(fl & XFS_B_ASYNC)) {
315 * Mark b_error and B_ERROR _both_.
316 * Lot's of chunkcache code assumes that.
317 * There's no reason to mark error for
320 XFS_BUF_ERROR(bp, EIO);
321 XFS_BUF_V_IODONESEMA(bp);
329 * Prints out an ALERT message about I/O error.
334 struct xfs_mount *mp,
339 "I/O error in filesystem (\"%s\") meta-data dev %s block 0x%llx"
340 " (\"%s\") error %d buf count %zd",
341 (!mp || !mp->m_fsname) ? "(fs name not set)" : mp->m_fsname,
342 XFS_BUFTARG_NAME(XFS_BUF_TARGET(bp)),
343 (__uint64_t)blkno, func,
344 XFS_BUF_GETERROR(bp), XFS_BUF_COUNT(bp));
348 * This isn't an absolute requirement, but it is
349 * just a good idea to call xfs_read_buf instead of
350 * directly doing a read_buf call. For one, we shouldn't
351 * be doing this disk read if we are in SHUTDOWN state anyway,
352 * so this stops that from happening. Secondly, this does all
353 * the error checking stuff and the brelse if appropriate for
354 * the caller, so the code can be a little leaner.
359 struct xfs_mount *mp,
360 xfs_buftarg_t *target,
370 bp = xfs_buf_read_flags(target, blkno, len, flags);
372 bp = xfs_buf_read(target, blkno, len, flags);
374 return XFS_ERROR(EIO);
375 error = XFS_BUF_GETERROR(bp);
376 if (bp && !error && !XFS_FORCED_SHUTDOWN(mp)) {
381 xfs_ioerror_alert("xfs_read_buf", mp, bp, XFS_BUF_ADDR(bp));
383 error = XFS_ERROR(EIO);
387 XFS_BUF_UNDELAYWRITE(bp);
390 * brelse clears B_ERROR and b_error
399 * Wrapper around bwrite() so that we can trap
400 * write errors, and act accordingly.
404 struct xfs_mount *mp,
410 * XXXsup how does this work for quotas.
412 XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
413 XFS_BUF_SET_FSPRIVATE3(bp, mp);
416 if ((error = XFS_bwrite(bp))) {
419 * Cannot put a buftrace here since if the buffer is not
420 * B_HOLD then we will brelse() the buffer before returning
421 * from bwrite and we could be tracing a buffer that has
424 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);