blob: c4b20872f07ddea19bf44f8b46c42b72c7980926 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_itable.h"
41#include "xfs_btree.h"
42#include "xfs_alloc.h"
43#include "xfs_ialloc.h"
44#include "xfs_attr.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#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/*
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.
55 */
56int
57xfs_write_clear_setuid(
58 xfs_inode_t *ip)
59{
60 xfs_mount_t *mp;
61 xfs_trans_t *tp;
62 int error;
63
64 mp = ip->i_mount;
65 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
66 if ((error = xfs_trans_reserve(tp, 0,
67 XFS_WRITEID_LOG_RES(mp),
68 0, 0, 0))) {
69 xfs_trans_cancel(tp, 0);
70 return error;
71 }
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;
76
77 /*
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
82 * have been enabled.
83 */
84 if (ip->i_d.di_mode & S_IXGRP) {
85 ip->i_d.di_mode &= ~S_ISGID;
86 }
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);
91 return 0;
92}
93
94/*
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.
99 */
100
101void
102xfs_do_force_shutdown(
103 bhv_desc_t *bdp,
104 int flags,
105 char *fname,
106 int lnnum)
107{
108 int logerror;
109 xfs_mount_t *mp;
110
111 mp = XFS_BHVTOM(bdp);
112 logerror = flags & XFS_LOG_IO_ERROR;
113
114 if (!(flags & XFS_FORCE_UMOUNT)) {
115 cmn_err(CE_NOTE,
116 "xfs_force_shutdown(%s,0x%x) called from line %d of file %s. Return address = 0x%p",
117 mp->m_fsname,flags,lnnum,fname,__return_address);
118 }
119 /*
120 * No need to duplicate efforts.
121 */
122 if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
123 return;
124
125 /*
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 and tells
129 * them the bad news.
130 */
131 if (xfs_log_force_umount(mp, logerror))
132 return;
133
134 if (flags & XFS_CORRUPT_INCORE) {
135 xfs_cmn_err(XFS_PTAG_SHUTDOWN_CORRUPT, CE_ALERT, mp,
136 "Corruption of in-memory data detected. Shutting down filesystem: %s",
137 mp->m_fsname);
138 if (XFS_ERRLEVEL_HIGH <= xfs_error_level) {
139 xfs_stack_trace();
140 }
141 } else if (!(flags & XFS_FORCE_UMOUNT)) {
142 if (logerror) {
143 xfs_cmn_err(XFS_PTAG_SHUTDOWN_LOGERROR, CE_ALERT, mp,
144 "Log I/O Error Detected. Shutting down filesystem: %s",
145 mp->m_fsname);
146 } else if (!(flags & XFS_SHUTDOWN_REMOTE_REQ)) {
147 xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
148 "I/O Error Detected. Shutting down filesystem: %s",
149 mp->m_fsname);
150 }
151 }
152 if (!(flags & XFS_FORCE_UMOUNT)) {
153 cmn_err(CE_ALERT,
154 "Please umount the filesystem, and rectify the problem(s)");
155 }
156}
157
158
159/*
160 * Called when we want to stop a buffer from getting written or read.
161 * We attach the EIO error, muck with its flags, and call biodone
162 * so that the proper iodone callbacks get called.
163 */
164int
165xfs_bioerror(
166 xfs_buf_t *bp)
167{
168
169#ifdef XFSERRORDEBUG
170 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
171#endif
172
173 /*
174 * No need to wait until the buffer is unpinned.
175 * We aren't flushing it.
176 */
177 xfs_buftrace("XFS IOERROR", bp);
178 XFS_BUF_ERROR(bp, EIO);
179 /*
180 * We're calling biodone, so delete B_DONE flag. Either way
181 * we have to call the iodone callback, and calling biodone
182 * probably is the best way since it takes care of
183 * GRIO as well.
184 */
185 XFS_BUF_UNREAD(bp);
186 XFS_BUF_UNDELAYWRITE(bp);
187 XFS_BUF_UNDONE(bp);
188 XFS_BUF_STALE(bp);
189
190 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
191 xfs_biodone(bp);
192
193 return (EIO);
194}
195
196/*
197 * Same as xfs_bioerror, except that we are releasing the buffer
198 * here ourselves, and avoiding the biodone call.
199 * This is meant for userdata errors; metadata bufs come with
200 * iodone functions attached, so that we can track down errors.
201 */
202int
203xfs_bioerror_relse(
204 xfs_buf_t *bp)
205{
206 int64_t fl;
207
208 ASSERT(XFS_BUF_IODONE_FUNC(bp) != xfs_buf_iodone_callbacks);
209 ASSERT(XFS_BUF_IODONE_FUNC(bp) != xlog_iodone);
210
211 xfs_buftrace("XFS IOERRELSE", bp);
212 fl = XFS_BUF_BFLAGS(bp);
213 /*
214 * No need to wait until the buffer is unpinned.
215 * We aren't flushing it.
216 *
217 * chunkhold expects B_DONE to be set, whether
218 * we actually finish the I/O or not. We don't want to
219 * change that interface.
220 */
221 XFS_BUF_UNREAD(bp);
222 XFS_BUF_UNDELAYWRITE(bp);
223 XFS_BUF_DONE(bp);
224 XFS_BUF_STALE(bp);
225 XFS_BUF_CLR_IODONE_FUNC(bp);
226 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
227 if (!(fl & XFS_B_ASYNC)) {
228 /*
229 * Mark b_error and B_ERROR _both_.
230 * Lot's of chunkcache code assumes that.
231 * There's no reason to mark error for
232 * ASYNC buffers.
233 */
234 XFS_BUF_ERROR(bp, EIO);
235 XFS_BUF_V_IODONESEMA(bp);
236 } else {
237 xfs_buf_relse(bp);
238 }
239 return (EIO);
240}
241/*
242 * Prints out an ALERT message about I/O error.
243 */
244void
245xfs_ioerror_alert(
246 char *func,
247 struct xfs_mount *mp,
248 xfs_buf_t *bp,
249 xfs_daddr_t blkno)
250{
251 cmn_err(CE_ALERT,
252 "I/O error in filesystem (\"%s\") meta-data dev %s block 0x%llx"
Christoph Hellwigda1650a2005-11-02 10:21:35 +1100253 " (\"%s\") error %d buf count %zd",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 (!mp || !mp->m_fsname) ? "(fs name not set)" : mp->m_fsname,
255 XFS_BUFTARG_NAME(bp->pb_target),
256 (__uint64_t)blkno,
257 func,
258 XFS_BUF_GETERROR(bp),
259 XFS_BUF_COUNT(bp));
260}
261
262/*
263 * This isn't an absolute requirement, but it is
264 * just a good idea to call xfs_read_buf instead of
265 * directly doing a read_buf call. For one, we shouldn't
266 * be doing this disk read if we are in SHUTDOWN state anyway,
267 * so this stops that from happening. Secondly, this does all
268 * the error checking stuff and the brelse if appropriate for
269 * the caller, so the code can be a little leaner.
270 */
271
272int
273xfs_read_buf(
274 struct xfs_mount *mp,
275 xfs_buftarg_t *target,
276 xfs_daddr_t blkno,
277 int len,
278 uint flags,
279 xfs_buf_t **bpp)
280{
281 xfs_buf_t *bp;
282 int error;
283
284 if (flags)
285 bp = xfs_buf_read_flags(target, blkno, len, flags);
286 else
287 bp = xfs_buf_read(target, blkno, len, flags);
288 if (!bp)
289 return XFS_ERROR(EIO);
290 error = XFS_BUF_GETERROR(bp);
291 if (bp && !error && !XFS_FORCED_SHUTDOWN(mp)) {
292 *bpp = bp;
293 } else {
294 *bpp = NULL;
295 if (error) {
296 xfs_ioerror_alert("xfs_read_buf", mp, bp, XFS_BUF_ADDR(bp));
297 } else {
298 error = XFS_ERROR(EIO);
299 }
300 if (bp) {
301 XFS_BUF_UNDONE(bp);
302 XFS_BUF_UNDELAYWRITE(bp);
303 XFS_BUF_STALE(bp);
304 /*
305 * brelse clears B_ERROR and b_error
306 */
307 xfs_buf_relse(bp);
308 }
309 }
310 return (error);
311}
312
313/*
314 * Wrapper around bwrite() so that we can trap
315 * write errors, and act accordingly.
316 */
317int
318xfs_bwrite(
319 struct xfs_mount *mp,
320 struct xfs_buf *bp)
321{
322 int error;
323
324 /*
325 * XXXsup how does this work for quotas.
326 */
327 XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
328 XFS_BUF_SET_FSPRIVATE3(bp, mp);
329 XFS_BUF_WRITE(bp);
330
331 if ((error = XFS_bwrite(bp))) {
332 ASSERT(mp);
333 /*
334 * Cannot put a buftrace here since if the buffer is not
335 * B_HOLD then we will brelse() the buffer before returning
336 * from bwrite and we could be tracing a buffer that has
337 * been reused.
338 */
339 xfs_force_shutdown(mp, XFS_METADATA_IO_ERROR);
340 }
341 return (error);
342}