blob: 3f816ad7ff19e7f0a93458333ac7b98d3842d3ec [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott1e69dd02006-06-19 08:39:53 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * 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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_itable.h"
39#include "xfs_btree.h"
40#include "xfs_alloc.h"
41#include "xfs_ialloc.h"
42#include "xfs_attr.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_error.h"
45#include "xfs_buf_item.h"
46#include "xfs_rw.h"
47
48/*
49 * This is a subroutine for xfs_write() and other writers (xfs_ioctl)
50 * which clears the setuid and setgid bits when a file is written.
51 */
52int
53xfs_write_clear_setuid(
54 xfs_inode_t *ip)
55{
56 xfs_mount_t *mp;
57 xfs_trans_t *tp;
58 int error;
59
60 mp = ip->i_mount;
61 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
62 if ((error = xfs_trans_reserve(tp, 0,
63 XFS_WRITEID_LOG_RES(mp),
64 0, 0, 0))) {
65 xfs_trans_cancel(tp, 0);
66 return error;
67 }
68 xfs_ilock(ip, XFS_ILOCK_EXCL);
69 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
70 xfs_trans_ihold(tp, ip);
71 ip->i_d.di_mode &= ~S_ISUID;
72
73 /*
74 * Note that we don't have to worry about mandatory
75 * file locking being disabled here because we only
76 * clear the S_ISGID bit if the Group execute bit is
77 * on, but if it was on then mandatory locking wouldn't
78 * have been enabled.
79 */
80 if (ip->i_d.di_mode & S_IXGRP) {
81 ip->i_d.di_mode &= ~S_ISGID;
82 }
83 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
84 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +100085 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 xfs_iunlock(ip, XFS_ILOCK_EXCL);
87 return 0;
88}
89
90/*
91 * Force a shutdown of the filesystem instantly while keeping
92 * the filesystem consistent. We don't do an unmount here; just shutdown
93 * the shop, make sure that absolutely nothing persistent happens to
94 * this filesystem after this point.
95 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096void
97xfs_do_force_shutdown(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +100098 xfs_mount_t *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 int flags,
100 char *fname,
101 int lnnum)
102{
103 int logerror;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Nathan Scott7d04a332006-06-09 14:58:38 +1000105 logerror = flags & SHUTDOWN_LOG_IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Nathan Scott7d04a332006-06-09 14:58:38 +1000107 if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
108 cmn_err(CE_NOTE, "xfs_force_shutdown(%s,0x%x) called from "
109 "line %d of file %s. Return address = 0x%p",
110 mp->m_fsname, flags, lnnum, fname, __return_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112 /*
113 * No need to duplicate efforts.
114 */
115 if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
116 return;
117
118 /*
119 * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
120 * queue up anybody new on the log reservations, and wakes up
Nathan Scott7d04a332006-06-09 14:58:38 +1000121 * everybody who's sleeping on log reservations to tell them
122 * the bad news.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 */
124 if (xfs_log_force_umount(mp, logerror))
125 return;
126
Nathan Scott7d04a332006-06-09 14:58:38 +1000127 if (flags & SHUTDOWN_CORRUPT_INCORE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 xfs_cmn_err(XFS_PTAG_SHUTDOWN_CORRUPT, CE_ALERT, mp,
129 "Corruption of in-memory data detected. Shutting down filesystem: %s",
130 mp->m_fsname);
131 if (XFS_ERRLEVEL_HIGH <= xfs_error_level) {
132 xfs_stack_trace();
133 }
Nathan Scott7d04a332006-06-09 14:58:38 +1000134 } else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (logerror) {
136 xfs_cmn_err(XFS_PTAG_SHUTDOWN_LOGERROR, CE_ALERT, mp,
Nathan Scott7d04a332006-06-09 14:58:38 +1000137 "Log I/O Error Detected. Shutting down filesystem: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +1000139 } else if (flags & SHUTDOWN_DEVICE_REQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
Nathan Scott7d04a332006-06-09 14:58:38 +1000141 "All device paths lost. Shutting down filesystem: %s",
142 mp->m_fsname);
143 } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
144 xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
145 "I/O Error Detected. Shutting down filesystem: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 mp->m_fsname);
147 }
148 }
Nathan Scott7d04a332006-06-09 14:58:38 +1000149 if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
150 cmn_err(CE_ALERT, "Please umount the filesystem, "
151 "and rectify the problem(s)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153}
154
155
156/*
157 * Called when we want to stop a buffer from getting written or read.
158 * We attach the EIO error, muck with its flags, and call biodone
159 * so that the proper iodone callbacks get called.
160 */
161int
162xfs_bioerror(
163 xfs_buf_t *bp)
164{
165
166#ifdef XFSERRORDEBUG
167 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
168#endif
169
170 /*
171 * No need to wait until the buffer is unpinned.
172 * We aren't flushing it.
173 */
174 xfs_buftrace("XFS IOERROR", bp);
175 XFS_BUF_ERROR(bp, EIO);
176 /*
177 * We're calling biodone, so delete B_DONE flag. Either way
178 * we have to call the iodone callback, and calling biodone
179 * probably is the best way since it takes care of
180 * GRIO as well.
181 */
182 XFS_BUF_UNREAD(bp);
183 XFS_BUF_UNDELAYWRITE(bp);
184 XFS_BUF_UNDONE(bp);
185 XFS_BUF_STALE(bp);
186
187 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
188 xfs_biodone(bp);
189
190 return (EIO);
191}
192
193/*
194 * Same as xfs_bioerror, except that we are releasing the buffer
195 * here ourselves, and avoiding the biodone call.
196 * This is meant for userdata errors; metadata bufs come with
197 * iodone functions attached, so that we can track down errors.
198 */
199int
200xfs_bioerror_relse(
201 xfs_buf_t *bp)
202{
203 int64_t fl;
204
205 ASSERT(XFS_BUF_IODONE_FUNC(bp) != xfs_buf_iodone_callbacks);
206 ASSERT(XFS_BUF_IODONE_FUNC(bp) != xlog_iodone);
207
208 xfs_buftrace("XFS IOERRELSE", bp);
209 fl = XFS_BUF_BFLAGS(bp);
210 /*
211 * No need to wait until the buffer is unpinned.
212 * We aren't flushing it.
213 *
214 * chunkhold expects B_DONE to be set, whether
215 * we actually finish the I/O or not. We don't want to
216 * change that interface.
217 */
218 XFS_BUF_UNREAD(bp);
219 XFS_BUF_UNDELAYWRITE(bp);
220 XFS_BUF_DONE(bp);
221 XFS_BUF_STALE(bp);
222 XFS_BUF_CLR_IODONE_FUNC(bp);
223 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
224 if (!(fl & XFS_B_ASYNC)) {
225 /*
226 * Mark b_error and B_ERROR _both_.
227 * Lot's of chunkcache code assumes that.
228 * There's no reason to mark error for
229 * ASYNC buffers.
230 */
231 XFS_BUF_ERROR(bp, EIO);
David Chinnerb4dd3302008-08-13 16:36:11 +1000232 XFS_BUF_FINISH_IOWAIT(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 } else {
234 xfs_buf_relse(bp);
235 }
236 return (EIO);
237}
Nathan Scottce8e9222006-01-11 15:39:08 +1100238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/*
240 * Prints out an ALERT message about I/O error.
241 */
242void
243xfs_ioerror_alert(
244 char *func,
245 struct xfs_mount *mp,
246 xfs_buf_t *bp,
247 xfs_daddr_t blkno)
248{
249 cmn_err(CE_ALERT,
250 "I/O error in filesystem (\"%s\") meta-data dev %s block 0x%llx"
Christoph Hellwigda1650a2005-11-02 10:21:35 +1100251 " (\"%s\") error %d buf count %zd",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 (!mp || !mp->m_fsname) ? "(fs name not set)" : mp->m_fsname,
Nathan Scottce8e9222006-01-11 15:39:08 +1100253 XFS_BUFTARG_NAME(XFS_BUF_TARGET(bp)),
254 (__uint64_t)blkno, func,
255 XFS_BUF_GETERROR(bp), XFS_BUF_COUNT(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258/*
259 * This isn't an absolute requirement, but it is
260 * just a good idea to call xfs_read_buf instead of
261 * directly doing a read_buf call. For one, we shouldn't
262 * be doing this disk read if we are in SHUTDOWN state anyway,
263 * so this stops that from happening. Secondly, this does all
264 * the error checking stuff and the brelse if appropriate for
265 * the caller, so the code can be a little leaner.
266 */
267
268int
269xfs_read_buf(
270 struct xfs_mount *mp,
271 xfs_buftarg_t *target,
272 xfs_daddr_t blkno,
273 int len,
274 uint flags,
275 xfs_buf_t **bpp)
276{
277 xfs_buf_t *bp;
278 int error;
279
280 if (flags)
281 bp = xfs_buf_read_flags(target, blkno, len, flags);
282 else
283 bp = xfs_buf_read(target, blkno, len, flags);
284 if (!bp)
285 return XFS_ERROR(EIO);
286 error = XFS_BUF_GETERROR(bp);
287 if (bp && !error && !XFS_FORCED_SHUTDOWN(mp)) {
288 *bpp = bp;
289 } else {
290 *bpp = NULL;
291 if (error) {
292 xfs_ioerror_alert("xfs_read_buf", mp, bp, XFS_BUF_ADDR(bp));
293 } else {
294 error = XFS_ERROR(EIO);
295 }
296 if (bp) {
297 XFS_BUF_UNDONE(bp);
298 XFS_BUF_UNDELAYWRITE(bp);
299 XFS_BUF_STALE(bp);
300 /*
301 * brelse clears B_ERROR and b_error
302 */
303 xfs_buf_relse(bp);
304 }
305 }
306 return (error);
307}
308
309/*
310 * Wrapper around bwrite() so that we can trap
311 * write errors, and act accordingly.
312 */
313int
314xfs_bwrite(
315 struct xfs_mount *mp,
316 struct xfs_buf *bp)
317{
318 int error;
319
320 /*
321 * XXXsup how does this work for quotas.
322 */
323 XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
Christoph Hellwig15ac08a2008-12-09 04:47:30 -0500324 bp->b_mount = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 XFS_BUF_WRITE(bp);
326
327 if ((error = XFS_bwrite(bp))) {
328 ASSERT(mp);
329 /*
330 * Cannot put a buftrace here since if the buffer is not
331 * B_HOLD then we will brelse() the buffer before returning
332 * from bwrite and we could be tracing a buffer that has
333 * been reused.
334 */
Nathan Scott7d04a332006-06-09 14:58:38 +1000335 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337 return (error);
338}