blob: 36f3a21c54d295aa835ebabe3f13a390696482c7 [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"
44#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_error.h"
46#include "xfs_buf_item.h"
47#include "xfs_rw.h"
48
49/*
50 * This is a subroutine for xfs_write() and other writers (xfs_ioctl)
51 * which clears the setuid and setgid bits when a file is written.
52 */
53int
54xfs_write_clear_setuid(
55 xfs_inode_t *ip)
56{
57 xfs_mount_t *mp;
58 xfs_trans_t *tp;
59 int error;
60
61 mp = ip->i_mount;
62 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
63 if ((error = xfs_trans_reserve(tp, 0,
64 XFS_WRITEID_LOG_RES(mp),
65 0, 0, 0))) {
66 xfs_trans_cancel(tp, 0);
67 return error;
68 }
69 xfs_ilock(ip, XFS_ILOCK_EXCL);
70 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
71 xfs_trans_ihold(tp, ip);
72 ip->i_d.di_mode &= ~S_ISUID;
73
74 /*
75 * Note that we don't have to worry about mandatory
76 * file locking being disabled here because we only
77 * clear the S_ISGID bit if the Group execute bit is
78 * on, but if it was on then mandatory locking wouldn't
79 * have been enabled.
80 */
81 if (ip->i_d.di_mode & S_IXGRP) {
82 ip->i_d.di_mode &= ~S_ISGID;
83 }
84 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
85 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +100086 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 xfs_iunlock(ip, XFS_ILOCK_EXCL);
88 return 0;
89}
90
91/*
Nathan Scott1e69dd02006-06-19 08:39:53 +100092 * Handle logging requirements of various synchronous types of write.
93 */
94int
95xfs_write_sync_logforce(
96 xfs_mount_t *mp,
97 xfs_inode_t *ip)
98{
99 int error = 0;
100
101 /*
102 * If we're treating this as O_DSYNC and we have not updated the
103 * size, force the log.
104 */
105 if (!(mp->m_flags & XFS_MOUNT_OSYNCISOSYNC) &&
106 !(ip->i_update_size)) {
107 xfs_inode_log_item_t *iip = ip->i_itemp;
108
109 /*
110 * If an allocation transaction occurred
111 * without extending the size, then we have to force
112 * the log up the proper point to ensure that the
113 * allocation is permanent. We can't count on
114 * the fact that buffered writes lock out direct I/O
115 * writes - the direct I/O write could have extended
116 * the size nontransactionally, then finished before
117 * we started. xfs_write_file will think that the file
118 * didn't grow but the update isn't safe unless the
119 * size change is logged.
120 *
121 * Force the log if we've committed a transaction
122 * against the inode or if someone else has and
123 * the commit record hasn't gone to disk (e.g.
124 * the inode is pinned). This guarantees that
125 * all changes affecting the inode are permanent
126 * when we return.
127 */
128 if (iip && iip->ili_last_lsn) {
David Chinnerb911ca02008-04-10 12:24:30 +1000129 error = _xfs_log_force(mp, iip->ili_last_lsn,
130 XFS_LOG_FORCE | XFS_LOG_SYNC, NULL);
Nathan Scott1e69dd02006-06-19 08:39:53 +1000131 } else if (xfs_ipincount(ip) > 0) {
David Chinnerb911ca02008-04-10 12:24:30 +1000132 error = _xfs_log_force(mp, (xfs_lsn_t)0,
133 XFS_LOG_FORCE | XFS_LOG_SYNC, NULL);
Nathan Scott1e69dd02006-06-19 08:39:53 +1000134 }
135
136 } else {
137 xfs_trans_t *tp;
138
139 /*
140 * O_SYNC or O_DSYNC _with_ a size update are handled
141 * the same way.
142 *
143 * If the write was synchronous then we need to make
144 * sure that the inode modification time is permanent.
145 * We'll have updated the timestamp above, so here
146 * we use a synchronous transaction to log the inode.
147 * It's not fast, but it's necessary.
148 *
149 * If this a dsync write and the size got changed
150 * non-transactionally, then we need to ensure that
151 * the size change gets logged in a synchronous
152 * transaction.
153 */
154 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITE_SYNC);
155 if ((error = xfs_trans_reserve(tp, 0,
156 XFS_SWRITE_LOG_RES(mp),
157 0, 0, 0))) {
158 /* Transaction reserve failed */
159 xfs_trans_cancel(tp, 0);
160 } else {
161 /* Transaction reserve successful */
162 xfs_ilock(ip, XFS_ILOCK_EXCL);
163 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
164 xfs_trans_ihold(tp, ip);
165 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
166 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000167 error = xfs_trans_commit(tp, 0);
Nathan Scott1e69dd02006-06-19 08:39:53 +1000168 xfs_iunlock(ip, XFS_ILOCK_EXCL);
169 }
170 }
171
172 return error;
173}
174
175/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * Force a shutdown of the filesystem instantly while keeping
177 * the filesystem consistent. We don't do an unmount here; just shutdown
178 * the shop, make sure that absolutely nothing persistent happens to
179 * this filesystem after this point.
180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181void
182xfs_do_force_shutdown(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000183 xfs_mount_t *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int flags,
185 char *fname,
186 int lnnum)
187{
188 int logerror;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Nathan Scott7d04a332006-06-09 14:58:38 +1000190 logerror = flags & SHUTDOWN_LOG_IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Nathan Scott7d04a332006-06-09 14:58:38 +1000192 if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
193 cmn_err(CE_NOTE, "xfs_force_shutdown(%s,0x%x) called from "
194 "line %d of file %s. Return address = 0x%p",
195 mp->m_fsname, flags, lnnum, fname, __return_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197 /*
198 * No need to duplicate efforts.
199 */
200 if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
201 return;
202
203 /*
204 * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
205 * queue up anybody new on the log reservations, and wakes up
Nathan Scott7d04a332006-06-09 14:58:38 +1000206 * everybody who's sleeping on log reservations to tell them
207 * the bad news.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 */
209 if (xfs_log_force_umount(mp, logerror))
210 return;
211
Nathan Scott7d04a332006-06-09 14:58:38 +1000212 if (flags & SHUTDOWN_CORRUPT_INCORE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 xfs_cmn_err(XFS_PTAG_SHUTDOWN_CORRUPT, CE_ALERT, mp,
214 "Corruption of in-memory data detected. Shutting down filesystem: %s",
215 mp->m_fsname);
216 if (XFS_ERRLEVEL_HIGH <= xfs_error_level) {
217 xfs_stack_trace();
218 }
Nathan Scott7d04a332006-06-09 14:58:38 +1000219 } else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (logerror) {
221 xfs_cmn_err(XFS_PTAG_SHUTDOWN_LOGERROR, CE_ALERT, mp,
Nathan Scott7d04a332006-06-09 14:58:38 +1000222 "Log I/O Error Detected. Shutting down filesystem: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +1000224 } else if (flags & SHUTDOWN_DEVICE_REQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
Nathan Scott7d04a332006-06-09 14:58:38 +1000226 "All device paths lost. Shutting down filesystem: %s",
227 mp->m_fsname);
228 } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
229 xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
230 "I/O Error Detected. Shutting down filesystem: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 mp->m_fsname);
232 }
233 }
Nathan Scott7d04a332006-06-09 14:58:38 +1000234 if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
235 cmn_err(CE_ALERT, "Please umount the filesystem, "
236 "and rectify the problem(s)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238}
239
240
241/*
242 * Called when we want to stop a buffer from getting written or read.
243 * We attach the EIO error, muck with its flags, and call biodone
244 * so that the proper iodone callbacks get called.
245 */
246int
247xfs_bioerror(
248 xfs_buf_t *bp)
249{
250
251#ifdef XFSERRORDEBUG
252 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
253#endif
254
255 /*
256 * No need to wait until the buffer is unpinned.
257 * We aren't flushing it.
258 */
259 xfs_buftrace("XFS IOERROR", bp);
260 XFS_BUF_ERROR(bp, EIO);
261 /*
262 * We're calling biodone, so delete B_DONE flag. Either way
263 * we have to call the iodone callback, and calling biodone
264 * probably is the best way since it takes care of
265 * GRIO as well.
266 */
267 XFS_BUF_UNREAD(bp);
268 XFS_BUF_UNDELAYWRITE(bp);
269 XFS_BUF_UNDONE(bp);
270 XFS_BUF_STALE(bp);
271
272 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
273 xfs_biodone(bp);
274
275 return (EIO);
276}
277
278/*
279 * Same as xfs_bioerror, except that we are releasing the buffer
280 * here ourselves, and avoiding the biodone call.
281 * This is meant for userdata errors; metadata bufs come with
282 * iodone functions attached, so that we can track down errors.
283 */
284int
285xfs_bioerror_relse(
286 xfs_buf_t *bp)
287{
288 int64_t fl;
289
290 ASSERT(XFS_BUF_IODONE_FUNC(bp) != xfs_buf_iodone_callbacks);
291 ASSERT(XFS_BUF_IODONE_FUNC(bp) != xlog_iodone);
292
293 xfs_buftrace("XFS IOERRELSE", bp);
294 fl = XFS_BUF_BFLAGS(bp);
295 /*
296 * No need to wait until the buffer is unpinned.
297 * We aren't flushing it.
298 *
299 * chunkhold expects B_DONE to be set, whether
300 * we actually finish the I/O or not. We don't want to
301 * change that interface.
302 */
303 XFS_BUF_UNREAD(bp);
304 XFS_BUF_UNDELAYWRITE(bp);
305 XFS_BUF_DONE(bp);
306 XFS_BUF_STALE(bp);
307 XFS_BUF_CLR_IODONE_FUNC(bp);
308 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
309 if (!(fl & XFS_B_ASYNC)) {
310 /*
311 * Mark b_error and B_ERROR _both_.
312 * Lot's of chunkcache code assumes that.
313 * There's no reason to mark error for
314 * ASYNC buffers.
315 */
316 XFS_BUF_ERROR(bp, EIO);
David Chinnerb4dd3302008-08-13 16:36:11 +1000317 XFS_BUF_FINISH_IOWAIT(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 } else {
319 xfs_buf_relse(bp);
320 }
321 return (EIO);
322}
Nathan Scottce8e9222006-01-11 15:39:08 +1100323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/*
325 * Prints out an ALERT message about I/O error.
326 */
327void
328xfs_ioerror_alert(
329 char *func,
330 struct xfs_mount *mp,
331 xfs_buf_t *bp,
332 xfs_daddr_t blkno)
333{
334 cmn_err(CE_ALERT,
335 "I/O error in filesystem (\"%s\") meta-data dev %s block 0x%llx"
Christoph Hellwigda1650a2005-11-02 10:21:35 +1100336 " (\"%s\") error %d buf count %zd",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 (!mp || !mp->m_fsname) ? "(fs name not set)" : mp->m_fsname,
Nathan Scottce8e9222006-01-11 15:39:08 +1100338 XFS_BUFTARG_NAME(XFS_BUF_TARGET(bp)),
339 (__uint64_t)blkno, func,
340 XFS_BUF_GETERROR(bp), XFS_BUF_COUNT(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343/*
344 * This isn't an absolute requirement, but it is
345 * just a good idea to call xfs_read_buf instead of
346 * directly doing a read_buf call. For one, we shouldn't
347 * be doing this disk read if we are in SHUTDOWN state anyway,
348 * so this stops that from happening. Secondly, this does all
349 * the error checking stuff and the brelse if appropriate for
350 * the caller, so the code can be a little leaner.
351 */
352
353int
354xfs_read_buf(
355 struct xfs_mount *mp,
356 xfs_buftarg_t *target,
357 xfs_daddr_t blkno,
358 int len,
359 uint flags,
360 xfs_buf_t **bpp)
361{
362 xfs_buf_t *bp;
363 int error;
364
365 if (flags)
366 bp = xfs_buf_read_flags(target, blkno, len, flags);
367 else
368 bp = xfs_buf_read(target, blkno, len, flags);
369 if (!bp)
370 return XFS_ERROR(EIO);
371 error = XFS_BUF_GETERROR(bp);
372 if (bp && !error && !XFS_FORCED_SHUTDOWN(mp)) {
373 *bpp = bp;
374 } else {
375 *bpp = NULL;
376 if (error) {
377 xfs_ioerror_alert("xfs_read_buf", mp, bp, XFS_BUF_ADDR(bp));
378 } else {
379 error = XFS_ERROR(EIO);
380 }
381 if (bp) {
382 XFS_BUF_UNDONE(bp);
383 XFS_BUF_UNDELAYWRITE(bp);
384 XFS_BUF_STALE(bp);
385 /*
386 * brelse clears B_ERROR and b_error
387 */
388 xfs_buf_relse(bp);
389 }
390 }
391 return (error);
392}
393
394/*
395 * Wrapper around bwrite() so that we can trap
396 * write errors, and act accordingly.
397 */
398int
399xfs_bwrite(
400 struct xfs_mount *mp,
401 struct xfs_buf *bp)
402{
403 int error;
404
405 /*
406 * XXXsup how does this work for quotas.
407 */
408 XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
Christoph Hellwig15ac08a2008-12-09 04:47:30 -0500409 bp->b_mount = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 XFS_BUF_WRITE(bp);
411
412 if ((error = XFS_bwrite(bp))) {
413 ASSERT(mp);
414 /*
415 * Cannot put a buftrace here since if the buffer is not
416 * B_HOLD then we will brelse() the buffer before returning
417 * from bwrite and we could be tracing a buffer that has
418 * been reused.
419 */
Nathan Scott7d04a332006-06-09 14:58:38 +1000420 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422 return (error);
423}