| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Nathan Scott | 1e69dd0 | 2006-06-19 08:39:53 +1000 | [diff] [blame] | 2 |  * Copyright (c) 2000-2006 Silicon Graphics, Inc. | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 |  * All Rights Reserved. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 |  * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 |  * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 |  * published by the Free Software Foundation. | 
 | 8 |  * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 |  * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 |  * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 |  * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_bit.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_inum.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_trans.h" | 
 | 25 | #include "xfs_sb.h" | 
 | 26 | #include "xfs_ag.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_mount.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_bmap_btree.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 29 | #include "xfs_dinode.h" | 
 | 30 | #include "xfs_inode.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_error.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_rw.h" | 
 | 33 |  | 
 | 34 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 |  * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | void | 
 | 41 | xfs_do_force_shutdown( | 
| Christoph Hellwig | 48c872a9f | 2007-08-30 17:20:31 +1000 | [diff] [blame] | 42 | 	xfs_mount_t	*mp, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | 	int		flags, | 
 | 44 | 	char		*fname, | 
 | 45 | 	int		lnnum) | 
 | 46 | { | 
 | 47 | 	int		logerror; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 |  | 
| Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 49 | 	logerror = flags & SHUTDOWN_LOG_IO_ERROR; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 |  | 
| Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 51 | 	if (!(flags & SHUTDOWN_FORCE_UMOUNT)) { | 
| Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 52 | 		xfs_notice(mp, | 
 | 53 | 	"%s(0x%x) called from line %d of file %s.  Return address = 0x%p", | 
 | 54 | 			__func__, flags, lnnum, fname, __return_address); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | 	} | 
 | 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 Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 65 | 	 * everybody who's sleeping on log reservations to tell them | 
 | 66 | 	 * the bad news. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | 	 */ | 
 | 68 | 	if (xfs_log_force_umount(mp, logerror)) | 
 | 69 | 		return; | 
 | 70 |  | 
| Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 71 | 	if (flags & SHUTDOWN_CORRUPT_INCORE) { | 
| Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 72 | 		xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_CORRUPT, | 
 | 73 |     "Corruption of in-memory data detected.  Shutting down filesystem"); | 
 | 74 | 		if (XFS_ERRLEVEL_HIGH <= xfs_error_level) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | 			xfs_stack_trace(); | 
| Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 76 | 	} else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | 		if (logerror) { | 
| Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 78 | 			xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_LOGERROR, | 
 | 79 | 		"Log I/O Error Detected.  Shutting down filesystem"); | 
| Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 80 | 		} else if (flags & SHUTDOWN_DEVICE_REQ) { | 
| Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 81 | 			xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR, | 
 | 82 | 		"All device paths lost.  Shutting down filesystem"); | 
| Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 83 | 		} else if (!(flags & SHUTDOWN_REMOTE_REQ)) { | 
| Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 84 | 			xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR, | 
 | 85 | 		"I/O Error Detected. Shutting down filesystem"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | 		} | 
 | 87 | 	} | 
| Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 88 | 	if (!(flags & SHUTDOWN_FORCE_UMOUNT)) { | 
| Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 89 | 		xfs_alert(mp, | 
 | 90 | 	"Please umount the filesystem and rectify the problem(s)"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | 	} | 
 | 92 | } | 
 | 93 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 |  * This isn't an absolute requirement, but it is | 
 | 96 |  * just a good idea to call xfs_read_buf instead of | 
 | 97 |  * directly doing a read_buf call. For one, we shouldn't | 
 | 98 |  * be doing this disk read if we are in SHUTDOWN state anyway, | 
 | 99 |  * so this stops that from happening. Secondly, this does all | 
 | 100 |  * the error checking stuff and the brelse if appropriate for | 
 | 101 |  * the caller, so the code can be a little leaner. | 
 | 102 |  */ | 
 | 103 |  | 
 | 104 | int | 
 | 105 | xfs_read_buf( | 
 | 106 | 	struct xfs_mount *mp, | 
 | 107 | 	xfs_buftarg_t	 *target, | 
 | 108 | 	xfs_daddr_t	 blkno, | 
 | 109 | 	int              len, | 
 | 110 | 	uint             flags, | 
 | 111 | 	xfs_buf_t	 **bpp) | 
 | 112 | { | 
 | 113 | 	xfs_buf_t	 *bp; | 
 | 114 | 	int		 error; | 
 | 115 |  | 
| Christoph Hellwig | 6ad112b | 2009-11-24 18:02:23 +0000 | [diff] [blame] | 116 | 	if (!flags) | 
 | 117 | 		flags = XBF_LOCK | XBF_MAPPED; | 
 | 118 |  | 
 | 119 | 	bp = xfs_buf_read(target, blkno, len, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | 	if (!bp) | 
 | 121 | 		return XFS_ERROR(EIO); | 
| Chandra Seetharaman | 5a52c2a58 | 2011-07-22 23:39:51 +0000 | [diff] [blame] | 122 | 	error = bp->b_error; | 
 | 123 | 	if (!error && !XFS_FORCED_SHUTDOWN(mp)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | 		*bpp = bp; | 
 | 125 | 	} else { | 
 | 126 | 		*bpp = NULL; | 
 | 127 | 		if (error) { | 
| Christoph Hellwig | 901796a | 2011-10-10 16:52:49 +0000 | [diff] [blame] | 128 | 			xfs_buf_ioerror_alert(bp, __func__); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | 		} else { | 
 | 130 | 			error = XFS_ERROR(EIO); | 
 | 131 | 		} | 
 | 132 | 		if (bp) { | 
 | 133 | 			XFS_BUF_UNDONE(bp); | 
| Christoph Hellwig | c867cb6 | 2011-10-10 16:52:46 +0000 | [diff] [blame] | 134 | 			xfs_buf_stale(bp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | 			/* | 
 | 136 | 			 * brelse clears B_ERROR and b_error | 
 | 137 | 			 */ | 
 | 138 | 			xfs_buf_relse(bp); | 
 | 139 | 		} | 
 | 140 | 	} | 
 | 141 | 	return (error); | 
 | 142 | } | 
 | 143 |  | 
 | 144 | /* | 
| Christoph Hellwig | 5683f53 | 2009-11-14 16:17:21 +0000 | [diff] [blame] | 145 |  * helper function to extract extent size hint from inode | 
 | 146 |  */ | 
 | 147 | xfs_extlen_t | 
 | 148 | xfs_get_extsz_hint( | 
 | 149 | 	struct xfs_inode	*ip) | 
 | 150 | { | 
| Christoph Hellwig | 0d8b30a | 2011-01-25 09:06:20 +0000 | [diff] [blame] | 151 | 	if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize) | 
 | 152 | 		return ip->i_d.di_extsize; | 
 | 153 | 	if (XFS_IS_REALTIME_INODE(ip)) | 
 | 154 | 		return ip->i_mount->m_sb.sb_rextsize; | 
 | 155 | 	return 0; | 
| Christoph Hellwig | 5683f53 | 2009-11-14 16:17:21 +0000 | [diff] [blame] | 156 | } |