xfs: Sort delayed write buffers before dispatch
[safe/jmp/linux-2.6] / fs / xfs / xfs_rw.c
1 /*
2  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
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
7  * published by the Free Software Foundation.
8  *
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.
13  *
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
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #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"
38 #include "xfs_itable.h"
39 #include "xfs_btree.h"
40 #include "xfs_alloc.h"
41 #include "xfs_ialloc.h"
42 #include "xfs_attr.h"
43 #include "xfs_bmap.h"
44 #include "xfs_error.h"
45 #include "xfs_buf_item.h"
46 #include "xfs_rw.h"
47 #include "xfs_trace.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  */
53 int
54 xfs_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);
86         error = xfs_trans_commit(tp, 0);
87         xfs_iunlock(ip, XFS_ILOCK_EXCL);
88         return 0;
89 }
90
91 /*
92  * Force a shutdown of the filesystem instantly while keeping
93  * the filesystem consistent. We don't do an unmount here; just shutdown
94  * the shop, make sure that absolutely nothing persistent happens to
95  * this filesystem after this point.
96  */
97 void
98 xfs_do_force_shutdown(
99         xfs_mount_t     *mp,
100         int             flags,
101         char            *fname,
102         int             lnnum)
103 {
104         int             logerror;
105
106         logerror = flags & SHUTDOWN_LOG_IO_ERROR;
107
108         if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
109                 cmn_err(CE_NOTE, "xfs_force_shutdown(%s,0x%x) called from "
110                                  "line %d of file %s.  Return address = 0x%p",
111                         mp->m_fsname, flags, lnnum, fname, __return_address);
112         }
113         /*
114          * No need to duplicate efforts.
115          */
116         if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
117                 return;
118
119         /*
120          * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
121          * queue up anybody new on the log reservations, and wakes up
122          * everybody who's sleeping on log reservations to tell them
123          * the bad news.
124          */
125         if (xfs_log_force_umount(mp, logerror))
126                 return;
127
128         if (flags & SHUTDOWN_CORRUPT_INCORE) {
129                 xfs_cmn_err(XFS_PTAG_SHUTDOWN_CORRUPT, CE_ALERT, mp,
130     "Corruption of in-memory data detected.  Shutting down filesystem: %s",
131                         mp->m_fsname);
132                 if (XFS_ERRLEVEL_HIGH <= xfs_error_level) {
133                         xfs_stack_trace();
134                 }
135         } else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
136                 if (logerror) {
137                         xfs_cmn_err(XFS_PTAG_SHUTDOWN_LOGERROR, CE_ALERT, mp,
138                 "Log I/O Error Detected.  Shutting down filesystem: %s",
139                                 mp->m_fsname);
140                 } else if (flags & SHUTDOWN_DEVICE_REQ) {
141                         xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
142                 "All device paths lost.  Shutting down filesystem: %s",
143                                 mp->m_fsname);
144                 } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
145                         xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
146                 "I/O Error Detected.  Shutting down filesystem: %s",
147                                 mp->m_fsname);
148                 }
149         }
150         if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
151                 cmn_err(CE_ALERT, "Please umount the filesystem, "
152                                   "and rectify the problem(s)");
153         }
154 }
155
156 /*
157  * Prints out an ALERT message about I/O error.
158  */
159 void
160 xfs_ioerror_alert(
161         char                    *func,
162         struct xfs_mount        *mp,
163         xfs_buf_t               *bp,
164         xfs_daddr_t             blkno)
165 {
166         cmn_err(CE_ALERT,
167  "I/O error in filesystem (\"%s\") meta-data dev %s block 0x%llx"
168  "       (\"%s\") error %d buf count %zd",
169                 (!mp || !mp->m_fsname) ? "(fs name not set)" : mp->m_fsname,
170                 XFS_BUFTARG_NAME(XFS_BUF_TARGET(bp)),
171                 (__uint64_t)blkno, func,
172                 XFS_BUF_GETERROR(bp), XFS_BUF_COUNT(bp));
173 }
174
175 /*
176  * This isn't an absolute requirement, but it is
177  * just a good idea to call xfs_read_buf instead of
178  * directly doing a read_buf call. For one, we shouldn't
179  * be doing this disk read if we are in SHUTDOWN state anyway,
180  * so this stops that from happening. Secondly, this does all
181  * the error checking stuff and the brelse if appropriate for
182  * the caller, so the code can be a little leaner.
183  */
184
185 int
186 xfs_read_buf(
187         struct xfs_mount *mp,
188         xfs_buftarg_t    *target,
189         xfs_daddr_t      blkno,
190         int              len,
191         uint             flags,
192         xfs_buf_t        **bpp)
193 {
194         xfs_buf_t        *bp;
195         int              error;
196
197         if (!flags)
198                 flags = XBF_LOCK | XBF_MAPPED;
199
200         bp = xfs_buf_read(target, blkno, len, flags);
201         if (!bp)
202                 return XFS_ERROR(EIO);
203         error = XFS_BUF_GETERROR(bp);
204         if (bp && !error && !XFS_FORCED_SHUTDOWN(mp)) {
205                 *bpp = bp;
206         } else {
207                 *bpp = NULL;
208                 if (error) {
209                         xfs_ioerror_alert("xfs_read_buf", mp, bp, XFS_BUF_ADDR(bp));
210                 } else {
211                         error = XFS_ERROR(EIO);
212                 }
213                 if (bp) {
214                         XFS_BUF_UNDONE(bp);
215                         XFS_BUF_UNDELAYWRITE(bp);
216                         XFS_BUF_STALE(bp);
217                         /*
218                          * brelse clears B_ERROR and b_error
219                          */
220                         xfs_buf_relse(bp);
221                 }
222         }
223         return (error);
224 }
225
226 /*
227  * helper function to extract extent size hint from inode
228  */
229 xfs_extlen_t
230 xfs_get_extsz_hint(
231         struct xfs_inode        *ip)
232 {
233         xfs_extlen_t            extsz;
234
235         if (unlikely(XFS_IS_REALTIME_INODE(ip))) {
236                 extsz = (ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)
237                                 ? ip->i_d.di_extsize
238                                 : ip->i_mount->m_sb.sb_rextsize;
239                 ASSERT(extsz);
240         } else {
241                 extsz = (ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)
242                                 ? ip->i_d.di_extsize : 0;
243         }
244
245         return extsz;
246 }