blob: c55e28189abacc991f0c2aa6c7b433cd1e468ceb [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"
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/*
Nathan Scott1e69dd02006-06-19 08:39:53 +100095 * Handle logging requirements of various synchronous types of write.
96 */
97int
98xfs_write_sync_logforce(
99 xfs_mount_t *mp,
100 xfs_inode_t *ip)
101{
102 int error = 0;
103
104 /*
105 * If we're treating this as O_DSYNC and we have not updated the
106 * size, force the log.
107 */
108 if (!(mp->m_flags & XFS_MOUNT_OSYNCISOSYNC) &&
109 !(ip->i_update_size)) {
110 xfs_inode_log_item_t *iip = ip->i_itemp;
111
112 /*
113 * If an allocation transaction occurred
114 * without extending the size, then we have to force
115 * the log up the proper point to ensure that the
116 * allocation is permanent. We can't count on
117 * the fact that buffered writes lock out direct I/O
118 * writes - the direct I/O write could have extended
119 * the size nontransactionally, then finished before
120 * we started. xfs_write_file will think that the file
121 * didn't grow but the update isn't safe unless the
122 * size change is logged.
123 *
124 * Force the log if we've committed a transaction
125 * against the inode or if someone else has and
126 * the commit record hasn't gone to disk (e.g.
127 * the inode is pinned). This guarantees that
128 * all changes affecting the inode are permanent
129 * when we return.
130 */
131 if (iip && iip->ili_last_lsn) {
132 xfs_log_force(mp, iip->ili_last_lsn,
133 XFS_LOG_FORCE | XFS_LOG_SYNC);
134 } else if (xfs_ipincount(ip) > 0) {
135 xfs_log_force(mp, (xfs_lsn_t)0,
136 XFS_LOG_FORCE | XFS_LOG_SYNC);
137 }
138
139 } else {
140 xfs_trans_t *tp;
141
142 /*
143 * O_SYNC or O_DSYNC _with_ a size update are handled
144 * the same way.
145 *
146 * If the write was synchronous then we need to make
147 * sure that the inode modification time is permanent.
148 * We'll have updated the timestamp above, so here
149 * we use a synchronous transaction to log the inode.
150 * It's not fast, but it's necessary.
151 *
152 * If this a dsync write and the size got changed
153 * non-transactionally, then we need to ensure that
154 * the size change gets logged in a synchronous
155 * transaction.
156 */
157 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITE_SYNC);
158 if ((error = xfs_trans_reserve(tp, 0,
159 XFS_SWRITE_LOG_RES(mp),
160 0, 0, 0))) {
161 /* Transaction reserve failed */
162 xfs_trans_cancel(tp, 0);
163 } else {
164 /* Transaction reserve successful */
165 xfs_ilock(ip, XFS_ILOCK_EXCL);
166 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
167 xfs_trans_ihold(tp, ip);
168 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
169 xfs_trans_set_sync(tp);
170 error = xfs_trans_commit(tp, 0, NULL);
171 xfs_iunlock(ip, XFS_ILOCK_EXCL);
172 }
173 }
174
175 return error;
176}
177
178/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * Force a shutdown of the filesystem instantly while keeping
180 * the filesystem consistent. We don't do an unmount here; just shutdown
181 * the shop, make sure that absolutely nothing persistent happens to
182 * this filesystem after this point.
183 */
184
185void
186xfs_do_force_shutdown(
187 bhv_desc_t *bdp,
188 int flags,
189 char *fname,
190 int lnnum)
191{
192 int logerror;
193 xfs_mount_t *mp;
194
195 mp = XFS_BHVTOM(bdp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000196 logerror = flags & SHUTDOWN_LOG_IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Nathan Scott7d04a332006-06-09 14:58:38 +1000198 if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
199 cmn_err(CE_NOTE, "xfs_force_shutdown(%s,0x%x) called from "
200 "line %d of file %s. Return address = 0x%p",
201 mp->m_fsname, flags, lnnum, fname, __return_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203 /*
204 * No need to duplicate efforts.
205 */
206 if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
207 return;
208
209 /*
210 * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
211 * queue up anybody new on the log reservations, and wakes up
Nathan Scott7d04a332006-06-09 14:58:38 +1000212 * everybody who's sleeping on log reservations to tell them
213 * the bad news.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
215 if (xfs_log_force_umount(mp, logerror))
216 return;
217
Nathan Scott7d04a332006-06-09 14:58:38 +1000218 if (flags & SHUTDOWN_CORRUPT_INCORE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 xfs_cmn_err(XFS_PTAG_SHUTDOWN_CORRUPT, CE_ALERT, mp,
220 "Corruption of in-memory data detected. Shutting down filesystem: %s",
221 mp->m_fsname);
222 if (XFS_ERRLEVEL_HIGH <= xfs_error_level) {
223 xfs_stack_trace();
224 }
Nathan Scott7d04a332006-06-09 14:58:38 +1000225 } else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (logerror) {
227 xfs_cmn_err(XFS_PTAG_SHUTDOWN_LOGERROR, CE_ALERT, mp,
Nathan Scott7d04a332006-06-09 14:58:38 +1000228 "Log I/O Error Detected. Shutting down filesystem: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +1000230 } else if (flags & SHUTDOWN_DEVICE_REQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
Nathan Scott7d04a332006-06-09 14:58:38 +1000232 "All device paths lost. Shutting down filesystem: %s",
233 mp->m_fsname);
234 } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
235 xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
236 "I/O Error Detected. Shutting down filesystem: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 mp->m_fsname);
238 }
239 }
Nathan Scott7d04a332006-06-09 14:58:38 +1000240 if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
241 cmn_err(CE_ALERT, "Please umount the filesystem, "
242 "and rectify the problem(s)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244}
245
246
247/*
248 * Called when we want to stop a buffer from getting written or read.
249 * We attach the EIO error, muck with its flags, and call biodone
250 * so that the proper iodone callbacks get called.
251 */
252int
253xfs_bioerror(
254 xfs_buf_t *bp)
255{
256
257#ifdef XFSERRORDEBUG
258 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
259#endif
260
261 /*
262 * No need to wait until the buffer is unpinned.
263 * We aren't flushing it.
264 */
265 xfs_buftrace("XFS IOERROR", bp);
266 XFS_BUF_ERROR(bp, EIO);
267 /*
268 * We're calling biodone, so delete B_DONE flag. Either way
269 * we have to call the iodone callback, and calling biodone
270 * probably is the best way since it takes care of
271 * GRIO as well.
272 */
273 XFS_BUF_UNREAD(bp);
274 XFS_BUF_UNDELAYWRITE(bp);
275 XFS_BUF_UNDONE(bp);
276 XFS_BUF_STALE(bp);
277
278 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
279 xfs_biodone(bp);
280
281 return (EIO);
282}
283
284/*
285 * Same as xfs_bioerror, except that we are releasing the buffer
286 * here ourselves, and avoiding the biodone call.
287 * This is meant for userdata errors; metadata bufs come with
288 * iodone functions attached, so that we can track down errors.
289 */
290int
291xfs_bioerror_relse(
292 xfs_buf_t *bp)
293{
294 int64_t fl;
295
296 ASSERT(XFS_BUF_IODONE_FUNC(bp) != xfs_buf_iodone_callbacks);
297 ASSERT(XFS_BUF_IODONE_FUNC(bp) != xlog_iodone);
298
299 xfs_buftrace("XFS IOERRELSE", bp);
300 fl = XFS_BUF_BFLAGS(bp);
301 /*
302 * No need to wait until the buffer is unpinned.
303 * We aren't flushing it.
304 *
305 * chunkhold expects B_DONE to be set, whether
306 * we actually finish the I/O or not. We don't want to
307 * change that interface.
308 */
309 XFS_BUF_UNREAD(bp);
310 XFS_BUF_UNDELAYWRITE(bp);
311 XFS_BUF_DONE(bp);
312 XFS_BUF_STALE(bp);
313 XFS_BUF_CLR_IODONE_FUNC(bp);
314 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
315 if (!(fl & XFS_B_ASYNC)) {
316 /*
317 * Mark b_error and B_ERROR _both_.
318 * Lot's of chunkcache code assumes that.
319 * There's no reason to mark error for
320 * ASYNC buffers.
321 */
322 XFS_BUF_ERROR(bp, EIO);
323 XFS_BUF_V_IODONESEMA(bp);
324 } else {
325 xfs_buf_relse(bp);
326 }
327 return (EIO);
328}
Nathan Scottce8e9222006-01-11 15:39:08 +1100329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330/*
331 * Prints out an ALERT message about I/O error.
332 */
333void
334xfs_ioerror_alert(
335 char *func,
336 struct xfs_mount *mp,
337 xfs_buf_t *bp,
338 xfs_daddr_t blkno)
339{
340 cmn_err(CE_ALERT,
341 "I/O error in filesystem (\"%s\") meta-data dev %s block 0x%llx"
Christoph Hellwigda1650a2005-11-02 10:21:35 +1100342 " (\"%s\") error %d buf count %zd",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 (!mp || !mp->m_fsname) ? "(fs name not set)" : mp->m_fsname,
Nathan Scottce8e9222006-01-11 15:39:08 +1100344 XFS_BUFTARG_NAME(XFS_BUF_TARGET(bp)),
345 (__uint64_t)blkno, func,
346 XFS_BUF_GETERROR(bp), XFS_BUF_COUNT(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349/*
350 * This isn't an absolute requirement, but it is
351 * just a good idea to call xfs_read_buf instead of
352 * directly doing a read_buf call. For one, we shouldn't
353 * be doing this disk read if we are in SHUTDOWN state anyway,
354 * so this stops that from happening. Secondly, this does all
355 * the error checking stuff and the brelse if appropriate for
356 * the caller, so the code can be a little leaner.
357 */
358
359int
360xfs_read_buf(
361 struct xfs_mount *mp,
362 xfs_buftarg_t *target,
363 xfs_daddr_t blkno,
364 int len,
365 uint flags,
366 xfs_buf_t **bpp)
367{
368 xfs_buf_t *bp;
369 int error;
370
371 if (flags)
372 bp = xfs_buf_read_flags(target, blkno, len, flags);
373 else
374 bp = xfs_buf_read(target, blkno, len, flags);
375 if (!bp)
376 return XFS_ERROR(EIO);
377 error = XFS_BUF_GETERROR(bp);
378 if (bp && !error && !XFS_FORCED_SHUTDOWN(mp)) {
379 *bpp = bp;
380 } else {
381 *bpp = NULL;
382 if (error) {
383 xfs_ioerror_alert("xfs_read_buf", mp, bp, XFS_BUF_ADDR(bp));
384 } else {
385 error = XFS_ERROR(EIO);
386 }
387 if (bp) {
388 XFS_BUF_UNDONE(bp);
389 XFS_BUF_UNDELAYWRITE(bp);
390 XFS_BUF_STALE(bp);
391 /*
392 * brelse clears B_ERROR and b_error
393 */
394 xfs_buf_relse(bp);
395 }
396 }
397 return (error);
398}
399
400/*
401 * Wrapper around bwrite() so that we can trap
402 * write errors, and act accordingly.
403 */
404int
405xfs_bwrite(
406 struct xfs_mount *mp,
407 struct xfs_buf *bp)
408{
409 int error;
410
411 /*
412 * XXXsup how does this work for quotas.
413 */
414 XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
415 XFS_BUF_SET_FSPRIVATE3(bp, mp);
416 XFS_BUF_WRITE(bp);
417
418 if ((error = XFS_bwrite(bp))) {
419 ASSERT(mp);
420 /*
421 * Cannot put a buftrace here since if the buffer is not
422 * B_HOLD then we will brelse() the buffer before returning
423 * from bwrite and we could be tracing a buffer that has
424 * been reused.
425 */
Nathan Scott7d04a332006-06-09 14:58:38 +1000426 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428 return (error);
429}