blob: 075850c999f4b5e882d2ffa0c245600005979d2c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_trans.h"
39#include "xfs_sb.h"
40#include "xfs_ag.h"
41#include "xfs_dir.h"
42#include "xfs_dir2.h"
43#include "xfs_dmapi.h"
44#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110046#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110048#include "xfs_dir_sf.h"
49#include "xfs_dir2_sf.h"
50#include "xfs_attr_sf.h"
51#include "xfs_dinode.h"
52#include "xfs_inode.h"
53#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "xfs_itable.h"
55#include "xfs_btree.h"
56#include "xfs_alloc.h"
57#include "xfs_ialloc.h"
58#include "xfs_attr.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "xfs_bmap.h"
60#include "xfs_acl.h"
61#include "xfs_mac.h"
62#include "xfs_error.h"
63#include "xfs_buf_item.h"
64#include "xfs_rw.h"
65
66/*
67 * This is a subroutine for xfs_write() and other writers (xfs_ioctl)
68 * which clears the setuid and setgid bits when a file is written.
69 */
70int
71xfs_write_clear_setuid(
72 xfs_inode_t *ip)
73{
74 xfs_mount_t *mp;
75 xfs_trans_t *tp;
76 int error;
77
78 mp = ip->i_mount;
79 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
80 if ((error = xfs_trans_reserve(tp, 0,
81 XFS_WRITEID_LOG_RES(mp),
82 0, 0, 0))) {
83 xfs_trans_cancel(tp, 0);
84 return error;
85 }
86 xfs_ilock(ip, XFS_ILOCK_EXCL);
87 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
88 xfs_trans_ihold(tp, ip);
89 ip->i_d.di_mode &= ~S_ISUID;
90
91 /*
92 * Note that we don't have to worry about mandatory
93 * file locking being disabled here because we only
94 * clear the S_ISGID bit if the Group execute bit is
95 * on, but if it was on then mandatory locking wouldn't
96 * have been enabled.
97 */
98 if (ip->i_d.di_mode & S_IXGRP) {
99 ip->i_d.di_mode &= ~S_ISGID;
100 }
101 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
102 xfs_trans_set_sync(tp);
103 error = xfs_trans_commit(tp, 0, NULL);
104 xfs_iunlock(ip, XFS_ILOCK_EXCL);
105 return 0;
106}
107
108/*
109 * Force a shutdown of the filesystem instantly while keeping
110 * the filesystem consistent. We don't do an unmount here; just shutdown
111 * the shop, make sure that absolutely nothing persistent happens to
112 * this filesystem after this point.
113 */
114
115void
116xfs_do_force_shutdown(
117 bhv_desc_t *bdp,
118 int flags,
119 char *fname,
120 int lnnum)
121{
122 int logerror;
123 xfs_mount_t *mp;
124
125 mp = XFS_BHVTOM(bdp);
126 logerror = flags & XFS_LOG_IO_ERROR;
127
128 if (!(flags & XFS_FORCE_UMOUNT)) {
129 cmn_err(CE_NOTE,
130 "xfs_force_shutdown(%s,0x%x) called from line %d of file %s. Return address = 0x%p",
131 mp->m_fsname,flags,lnnum,fname,__return_address);
132 }
133 /*
134 * No need to duplicate efforts.
135 */
136 if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
137 return;
138
139 /*
140 * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
141 * queue up anybody new on the log reservations, and wakes up
142 * everybody who's sleeping on log reservations and tells
143 * them the bad news.
144 */
145 if (xfs_log_force_umount(mp, logerror))
146 return;
147
148 if (flags & XFS_CORRUPT_INCORE) {
149 xfs_cmn_err(XFS_PTAG_SHUTDOWN_CORRUPT, CE_ALERT, mp,
150 "Corruption of in-memory data detected. Shutting down filesystem: %s",
151 mp->m_fsname);
152 if (XFS_ERRLEVEL_HIGH <= xfs_error_level) {
153 xfs_stack_trace();
154 }
155 } else if (!(flags & XFS_FORCE_UMOUNT)) {
156 if (logerror) {
157 xfs_cmn_err(XFS_PTAG_SHUTDOWN_LOGERROR, CE_ALERT, mp,
158 "Log I/O Error Detected. Shutting down filesystem: %s",
159 mp->m_fsname);
160 } else if (!(flags & XFS_SHUTDOWN_REMOTE_REQ)) {
161 xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
162 "I/O Error Detected. Shutting down filesystem: %s",
163 mp->m_fsname);
164 }
165 }
166 if (!(flags & XFS_FORCE_UMOUNT)) {
167 cmn_err(CE_ALERT,
168 "Please umount the filesystem, and rectify the problem(s)");
169 }
170}
171
172
173/*
174 * Called when we want to stop a buffer from getting written or read.
175 * We attach the EIO error, muck with its flags, and call biodone
176 * so that the proper iodone callbacks get called.
177 */
178int
179xfs_bioerror(
180 xfs_buf_t *bp)
181{
182
183#ifdef XFSERRORDEBUG
184 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
185#endif
186
187 /*
188 * No need to wait until the buffer is unpinned.
189 * We aren't flushing it.
190 */
191 xfs_buftrace("XFS IOERROR", bp);
192 XFS_BUF_ERROR(bp, EIO);
193 /*
194 * We're calling biodone, so delete B_DONE flag. Either way
195 * we have to call the iodone callback, and calling biodone
196 * probably is the best way since it takes care of
197 * GRIO as well.
198 */
199 XFS_BUF_UNREAD(bp);
200 XFS_BUF_UNDELAYWRITE(bp);
201 XFS_BUF_UNDONE(bp);
202 XFS_BUF_STALE(bp);
203
204 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
205 xfs_biodone(bp);
206
207 return (EIO);
208}
209
210/*
211 * Same as xfs_bioerror, except that we are releasing the buffer
212 * here ourselves, and avoiding the biodone call.
213 * This is meant for userdata errors; metadata bufs come with
214 * iodone functions attached, so that we can track down errors.
215 */
216int
217xfs_bioerror_relse(
218 xfs_buf_t *bp)
219{
220 int64_t fl;
221
222 ASSERT(XFS_BUF_IODONE_FUNC(bp) != xfs_buf_iodone_callbacks);
223 ASSERT(XFS_BUF_IODONE_FUNC(bp) != xlog_iodone);
224
225 xfs_buftrace("XFS IOERRELSE", bp);
226 fl = XFS_BUF_BFLAGS(bp);
227 /*
228 * No need to wait until the buffer is unpinned.
229 * We aren't flushing it.
230 *
231 * chunkhold expects B_DONE to be set, whether
232 * we actually finish the I/O or not. We don't want to
233 * change that interface.
234 */
235 XFS_BUF_UNREAD(bp);
236 XFS_BUF_UNDELAYWRITE(bp);
237 XFS_BUF_DONE(bp);
238 XFS_BUF_STALE(bp);
239 XFS_BUF_CLR_IODONE_FUNC(bp);
240 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
241 if (!(fl & XFS_B_ASYNC)) {
242 /*
243 * Mark b_error and B_ERROR _both_.
244 * Lot's of chunkcache code assumes that.
245 * There's no reason to mark error for
246 * ASYNC buffers.
247 */
248 XFS_BUF_ERROR(bp, EIO);
249 XFS_BUF_V_IODONESEMA(bp);
250 } else {
251 xfs_buf_relse(bp);
252 }
253 return (EIO);
254}
255/*
256 * Prints out an ALERT message about I/O error.
257 */
258void
259xfs_ioerror_alert(
260 char *func,
261 struct xfs_mount *mp,
262 xfs_buf_t *bp,
263 xfs_daddr_t blkno)
264{
265 cmn_err(CE_ALERT,
266 "I/O error in filesystem (\"%s\") meta-data dev %s block 0x%llx"
Christoph Hellwigda1650a2005-11-02 10:21:35 +1100267 " (\"%s\") error %d buf count %zd",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 (!mp || !mp->m_fsname) ? "(fs name not set)" : mp->m_fsname,
269 XFS_BUFTARG_NAME(bp->pb_target),
270 (__uint64_t)blkno,
271 func,
272 XFS_BUF_GETERROR(bp),
273 XFS_BUF_COUNT(bp));
274}
275
276/*
277 * This isn't an absolute requirement, but it is
278 * just a good idea to call xfs_read_buf instead of
279 * directly doing a read_buf call. For one, we shouldn't
280 * be doing this disk read if we are in SHUTDOWN state anyway,
281 * so this stops that from happening. Secondly, this does all
282 * the error checking stuff and the brelse if appropriate for
283 * the caller, so the code can be a little leaner.
284 */
285
286int
287xfs_read_buf(
288 struct xfs_mount *mp,
289 xfs_buftarg_t *target,
290 xfs_daddr_t blkno,
291 int len,
292 uint flags,
293 xfs_buf_t **bpp)
294{
295 xfs_buf_t *bp;
296 int error;
297
298 if (flags)
299 bp = xfs_buf_read_flags(target, blkno, len, flags);
300 else
301 bp = xfs_buf_read(target, blkno, len, flags);
302 if (!bp)
303 return XFS_ERROR(EIO);
304 error = XFS_BUF_GETERROR(bp);
305 if (bp && !error && !XFS_FORCED_SHUTDOWN(mp)) {
306 *bpp = bp;
307 } else {
308 *bpp = NULL;
309 if (error) {
310 xfs_ioerror_alert("xfs_read_buf", mp, bp, XFS_BUF_ADDR(bp));
311 } else {
312 error = XFS_ERROR(EIO);
313 }
314 if (bp) {
315 XFS_BUF_UNDONE(bp);
316 XFS_BUF_UNDELAYWRITE(bp);
317 XFS_BUF_STALE(bp);
318 /*
319 * brelse clears B_ERROR and b_error
320 */
321 xfs_buf_relse(bp);
322 }
323 }
324 return (error);
325}
326
327/*
328 * Wrapper around bwrite() so that we can trap
329 * write errors, and act accordingly.
330 */
331int
332xfs_bwrite(
333 struct xfs_mount *mp,
334 struct xfs_buf *bp)
335{
336 int error;
337
338 /*
339 * XXXsup how does this work for quotas.
340 */
341 XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
342 XFS_BUF_SET_FSPRIVATE3(bp, mp);
343 XFS_BUF_WRITE(bp);
344
345 if ((error = XFS_bwrite(bp))) {
346 ASSERT(mp);
347 /*
348 * Cannot put a buftrace here since if the buffer is not
349 * B_HOLD then we will brelse() the buffer before returning
350 * from bwrite and we could be tracing a buffer that has
351 * been reused.
352 */
353 xfs_force_shutdown(mp, XFS_METADATA_IO_ERROR);
354 }
355 return (error);
356}