blob: 56861d5daaef54432a94a640a7b1e65812030ef7 [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_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_dinode.h"
30#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_rw.h"
33
34/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * Force a shutdown of the filesystem instantly while keeping
36 * the filesystem consistent. We don't do an unmount here; just shutdown
37 * the shop, make sure that absolutely nothing persistent happens to
38 * this filesystem after this point.
39 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040void
41xfs_do_force_shutdown(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +100042 xfs_mount_t *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 int flags,
44 char *fname,
45 int lnnum)
46{
47 int logerror;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Nathan Scott7d04a332006-06-09 14:58:38 +100049 logerror = flags & SHUTDOWN_LOG_IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Nathan Scott7d04a332006-06-09 14:58:38 +100051 if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
52 cmn_err(CE_NOTE, "xfs_force_shutdown(%s,0x%x) called from "
53 "line %d of file %s. Return address = 0x%p",
54 mp->m_fsname, flags, lnnum, fname, __return_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 }
56 /*
57 * No need to duplicate efforts.
58 */
59 if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
60 return;
61
62 /*
63 * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
64 * queue up anybody new on the log reservations, and wakes up
Nathan Scott7d04a332006-06-09 14:58:38 +100065 * everybody who's sleeping on log reservations to tell them
66 * the bad news.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 */
68 if (xfs_log_force_umount(mp, logerror))
69 return;
70
Nathan Scott7d04a332006-06-09 14:58:38 +100071 if (flags & SHUTDOWN_CORRUPT_INCORE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 xfs_cmn_err(XFS_PTAG_SHUTDOWN_CORRUPT, CE_ALERT, mp,
73 "Corruption of in-memory data detected. Shutting down filesystem: %s",
74 mp->m_fsname);
75 if (XFS_ERRLEVEL_HIGH <= xfs_error_level) {
76 xfs_stack_trace();
77 }
Nathan Scott7d04a332006-06-09 14:58:38 +100078 } else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (logerror) {
80 xfs_cmn_err(XFS_PTAG_SHUTDOWN_LOGERROR, CE_ALERT, mp,
Nathan Scott7d04a332006-06-09 14:58:38 +100081 "Log I/O Error Detected. Shutting down filesystem: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +100083 } else if (flags & SHUTDOWN_DEVICE_REQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
Nathan Scott7d04a332006-06-09 14:58:38 +100085 "All device paths lost. Shutting down filesystem: %s",
86 mp->m_fsname);
87 } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
88 xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
89 "I/O Error Detected. Shutting down filesystem: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 mp->m_fsname);
91 }
92 }
Nathan Scott7d04a332006-06-09 14:58:38 +100093 if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
94 cmn_err(CE_ALERT, "Please umount the filesystem, "
95 "and rectify the problem(s)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
97}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
100 * Prints out an ALERT message about I/O error.
101 */
102void
103xfs_ioerror_alert(
104 char *func,
105 struct xfs_mount *mp,
106 xfs_buf_t *bp,
107 xfs_daddr_t blkno)
108{
109 cmn_err(CE_ALERT,
110 "I/O error in filesystem (\"%s\") meta-data dev %s block 0x%llx"
Christoph Hellwigda1650a2005-11-02 10:21:35 +1100111 " (\"%s\") error %d buf count %zd",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 (!mp || !mp->m_fsname) ? "(fs name not set)" : mp->m_fsname,
Nathan Scottce8e9222006-01-11 15:39:08 +1100113 XFS_BUFTARG_NAME(XFS_BUF_TARGET(bp)),
114 (__uint64_t)blkno, func,
115 XFS_BUF_GETERROR(bp), XFS_BUF_COUNT(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118/*
119 * This isn't an absolute requirement, but it is
120 * just a good idea to call xfs_read_buf instead of
121 * directly doing a read_buf call. For one, we shouldn't
122 * be doing this disk read if we are in SHUTDOWN state anyway,
123 * so this stops that from happening. Secondly, this does all
124 * the error checking stuff and the brelse if appropriate for
125 * the caller, so the code can be a little leaner.
126 */
127
128int
129xfs_read_buf(
130 struct xfs_mount *mp,
131 xfs_buftarg_t *target,
132 xfs_daddr_t blkno,
133 int len,
134 uint flags,
135 xfs_buf_t **bpp)
136{
137 xfs_buf_t *bp;
138 int error;
139
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000140 if (!flags)
141 flags = XBF_LOCK | XBF_MAPPED;
142
143 bp = xfs_buf_read(target, blkno, len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (!bp)
145 return XFS_ERROR(EIO);
146 error = XFS_BUF_GETERROR(bp);
147 if (bp && !error && !XFS_FORCED_SHUTDOWN(mp)) {
148 *bpp = bp;
149 } else {
150 *bpp = NULL;
151 if (error) {
152 xfs_ioerror_alert("xfs_read_buf", mp, bp, XFS_BUF_ADDR(bp));
153 } else {
154 error = XFS_ERROR(EIO);
155 }
156 if (bp) {
157 XFS_BUF_UNDONE(bp);
158 XFS_BUF_UNDELAYWRITE(bp);
159 XFS_BUF_STALE(bp);
160 /*
161 * brelse clears B_ERROR and b_error
162 */
163 xfs_buf_relse(bp);
164 }
165 }
166 return (error);
167}
168
169/*
Christoph Hellwig5683f532009-11-14 16:17:21 +0000170 * helper function to extract extent size hint from inode
171 */
172xfs_extlen_t
173xfs_get_extsz_hint(
174 struct xfs_inode *ip)
175{
176 xfs_extlen_t extsz;
177
178 if (unlikely(XFS_IS_REALTIME_INODE(ip))) {
179 extsz = (ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)
180 ? ip->i_d.di_extsize
181 : ip->i_mount->m_sb.sb_rextsize;
182 ASSERT(extsz);
183 } else {
184 extsz = (ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)
185 ? ip->i_d.di_extsize : 0;
186 }
187
188 return extsz;
189}