2 * Copyright (c) 2000-2003,2005 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"
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"
48 #include "xfs_error.h"
49 #include "xfs_buf_item.h"
53 * This is a subroutine for xfs_write() and other writers (xfs_ioctl)
54 * which clears the setuid and setgid bits when a file is written.
57 xfs_write_clear_setuid(
65 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
66 if ((error = xfs_trans_reserve(tp, 0,
67 XFS_WRITEID_LOG_RES(mp),
69 xfs_trans_cancel(tp, 0);
72 xfs_ilock(ip, XFS_ILOCK_EXCL);
73 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
74 xfs_trans_ihold(tp, ip);
75 ip->i_d.di_mode &= ~S_ISUID;
78 * Note that we don't have to worry about mandatory
79 * file locking being disabled here because we only
80 * clear the S_ISGID bit if the Group execute bit is
81 * on, but if it was on then mandatory locking wouldn't
84 if (ip->i_d.di_mode & S_IXGRP) {
85 ip->i_d.di_mode &= ~S_ISGID;
87 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
88 xfs_trans_set_sync(tp);
89 error = xfs_trans_commit(tp, 0, NULL);
90 xfs_iunlock(ip, XFS_ILOCK_EXCL);
95 * Force a shutdown of the filesystem instantly while keeping
96 * the filesystem consistent. We don't do an unmount here; just shutdown
97 * the shop, make sure that absolutely nothing persistent happens to
98 * this filesystem after this point.
102 xfs_do_force_shutdown(
111 mp = XFS_BHVTOM(bdp);
112 logerror = flags & SHUTDOWN_LOG_IO_ERROR;
114 if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
115 cmn_err(CE_NOTE, "xfs_force_shutdown(%s,0x%x) called from "
116 "line %d of file %s. Return address = 0x%p",
117 mp->m_fsname, flags, lnnum, fname, __return_address);
120 * No need to duplicate efforts.
122 if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
126 * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
127 * queue up anybody new on the log reservations, and wakes up
128 * everybody who's sleeping on log reservations to tell them
131 if (xfs_log_force_umount(mp, logerror))
134 if (flags & SHUTDOWN_CORRUPT_INCORE) {
135 xfs_cmn_err(XFS_PTAG_SHUTDOWN_CORRUPT, CE_ALERT, mp,
136 "Corruption of in-memory data detected. Shutting down filesystem: %s",
138 if (XFS_ERRLEVEL_HIGH <= xfs_error_level) {
141 } else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
143 xfs_cmn_err(XFS_PTAG_SHUTDOWN_LOGERROR, CE_ALERT, mp,
144 "Log I/O Error Detected. Shutting down filesystem: %s",
146 } else if (flags & SHUTDOWN_DEVICE_REQ) {
147 xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
148 "All device paths lost. Shutting down filesystem: %s",
150 } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
151 xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
152 "I/O Error Detected. Shutting down filesystem: %s",
156 if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
157 cmn_err(CE_ALERT, "Please umount the filesystem, "
158 "and rectify the problem(s)");
164 * Called when we want to stop a buffer from getting written or read.
165 * We attach the EIO error, muck with its flags, and call biodone
166 * so that the proper iodone callbacks get called.
174 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
178 * No need to wait until the buffer is unpinned.
179 * We aren't flushing it.
181 xfs_buftrace("XFS IOERROR", bp);
182 XFS_BUF_ERROR(bp, EIO);
184 * We're calling biodone, so delete B_DONE flag. Either way
185 * we have to call the iodone callback, and calling biodone
186 * probably is the best way since it takes care of
190 XFS_BUF_UNDELAYWRITE(bp);
194 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
201 * Same as xfs_bioerror, except that we are releasing the buffer
202 * here ourselves, and avoiding the biodone call.
203 * This is meant for userdata errors; metadata bufs come with
204 * iodone functions attached, so that we can track down errors.
212 ASSERT(XFS_BUF_IODONE_FUNC(bp) != xfs_buf_iodone_callbacks);
213 ASSERT(XFS_BUF_IODONE_FUNC(bp) != xlog_iodone);
215 xfs_buftrace("XFS IOERRELSE", bp);
216 fl = XFS_BUF_BFLAGS(bp);
218 * No need to wait until the buffer is unpinned.
219 * We aren't flushing it.
221 * chunkhold expects B_DONE to be set, whether
222 * we actually finish the I/O or not. We don't want to
223 * change that interface.
226 XFS_BUF_UNDELAYWRITE(bp);
229 XFS_BUF_CLR_IODONE_FUNC(bp);
230 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
231 if (!(fl & XFS_B_ASYNC)) {
233 * Mark b_error and B_ERROR _both_.
234 * Lot's of chunkcache code assumes that.
235 * There's no reason to mark error for
238 XFS_BUF_ERROR(bp, EIO);
239 XFS_BUF_V_IODONESEMA(bp);
247 * Prints out an ALERT message about I/O error.
252 struct xfs_mount *mp,
257 "I/O error in filesystem (\"%s\") meta-data dev %s block 0x%llx"
258 " (\"%s\") error %d buf count %zd",
259 (!mp || !mp->m_fsname) ? "(fs name not set)" : mp->m_fsname,
260 XFS_BUFTARG_NAME(XFS_BUF_TARGET(bp)),
261 (__uint64_t)blkno, func,
262 XFS_BUF_GETERROR(bp), XFS_BUF_COUNT(bp));
266 * This isn't an absolute requirement, but it is
267 * just a good idea to call xfs_read_buf instead of
268 * directly doing a read_buf call. For one, we shouldn't
269 * be doing this disk read if we are in SHUTDOWN state anyway,
270 * so this stops that from happening. Secondly, this does all
271 * the error checking stuff and the brelse if appropriate for
272 * the caller, so the code can be a little leaner.
277 struct xfs_mount *mp,
278 xfs_buftarg_t *target,
288 bp = xfs_buf_read_flags(target, blkno, len, flags);
290 bp = xfs_buf_read(target, blkno, len, flags);
292 return XFS_ERROR(EIO);
293 error = XFS_BUF_GETERROR(bp);
294 if (bp && !error && !XFS_FORCED_SHUTDOWN(mp)) {
299 xfs_ioerror_alert("xfs_read_buf", mp, bp, XFS_BUF_ADDR(bp));
301 error = XFS_ERROR(EIO);
305 XFS_BUF_UNDELAYWRITE(bp);
308 * brelse clears B_ERROR and b_error
317 * Wrapper around bwrite() so that we can trap
318 * write errors, and act accordingly.
322 struct xfs_mount *mp,
328 * XXXsup how does this work for quotas.
330 XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
331 XFS_BUF_SET_FSPRIVATE3(bp, mp);
334 if ((error = XFS_bwrite(bp))) {
337 * Cannot put a buftrace here since if the buffer is not
338 * B_HOLD then we will brelse() the buffer before returning
339 * from bwrite and we could be tracing a buffer that has
342 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);