m68k: Atari SCSI revival
[linux/fpc-iii.git] / fs / xfs / xfs_rw.c
blob1ea7c0ca6ae0f8d5ec63d8ab6aca7706b13e1626
1 /*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
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.
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.
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
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_acl.h"
45 #include "xfs_error.h"
46 #include "xfs_buf_item.h"
47 #include "xfs_rw.h"
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.
53 int
54 xfs_write_clear_setuid(
55 xfs_inode_t *ip)
57 xfs_mount_t *mp;
58 xfs_trans_t *tp;
59 int error;
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;
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;
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.
81 if (ip->i_d.di_mode & S_IXGRP) {
82 ip->i_d.di_mode &= ~S_ISGID;
84 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
85 xfs_trans_set_sync(tp);
86 error = xfs_trans_commit(tp, 0, NULL);
87 xfs_iunlock(ip, XFS_ILOCK_EXCL);
88 return 0;
92 * Handle logging requirements of various synchronous types of write.
94 int
95 xfs_write_sync_logforce(
96 xfs_mount_t *mp,
97 xfs_inode_t *ip)
99 int error = 0;
102 * If we're treating this as O_DSYNC and we have not updated the
103 * size, force the log.
105 if (!(mp->m_flags & XFS_MOUNT_OSYNCISOSYNC) &&
106 !(ip->i_update_size)) {
107 xfs_inode_log_item_t *iip = ip->i_itemp;
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.
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.
128 if (iip && iip->ili_last_lsn) {
129 xfs_log_force(mp, iip->ili_last_lsn,
130 XFS_LOG_FORCE | XFS_LOG_SYNC);
131 } else if (xfs_ipincount(ip) > 0) {
132 xfs_log_force(mp, (xfs_lsn_t)0,
133 XFS_LOG_FORCE | XFS_LOG_SYNC);
136 } else {
137 xfs_trans_t *tp;
140 * O_SYNC or O_DSYNC _with_ a size update are handled
141 * the same way.
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.
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.
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);
167 error = xfs_trans_commit(tp, 0, NULL);
168 xfs_iunlock(ip, XFS_ILOCK_EXCL);
172 return error;
176 * 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.
182 void
183 xfs_do_force_shutdown(
184 bhv_desc_t *bdp,
185 int flags,
186 char *fname,
187 int lnnum)
189 int logerror;
190 xfs_mount_t *mp;
192 mp = XFS_BHVTOM(bdp);
193 logerror = flags & SHUTDOWN_LOG_IO_ERROR;
195 if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
196 cmn_err(CE_NOTE, "xfs_force_shutdown(%s,0x%x) called from "
197 "line %d of file %s. Return address = 0x%p",
198 mp->m_fsname, flags, lnnum, fname, __return_address);
201 * No need to duplicate efforts.
203 if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
204 return;
207 * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
208 * queue up anybody new on the log reservations, and wakes up
209 * everybody who's sleeping on log reservations to tell them
210 * the bad news.
212 if (xfs_log_force_umount(mp, logerror))
213 return;
215 if (flags & SHUTDOWN_CORRUPT_INCORE) {
216 xfs_cmn_err(XFS_PTAG_SHUTDOWN_CORRUPT, CE_ALERT, mp,
217 "Corruption of in-memory data detected. Shutting down filesystem: %s",
218 mp->m_fsname);
219 if (XFS_ERRLEVEL_HIGH <= xfs_error_level) {
220 xfs_stack_trace();
222 } else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
223 if (logerror) {
224 xfs_cmn_err(XFS_PTAG_SHUTDOWN_LOGERROR, CE_ALERT, mp,
225 "Log I/O Error Detected. Shutting down filesystem: %s",
226 mp->m_fsname);
227 } else if (flags & SHUTDOWN_DEVICE_REQ) {
228 xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
229 "All device paths lost. Shutting down filesystem: %s",
230 mp->m_fsname);
231 } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
232 xfs_cmn_err(XFS_PTAG_SHUTDOWN_IOERROR, CE_ALERT, mp,
233 "I/O Error Detected. Shutting down filesystem: %s",
234 mp->m_fsname);
237 if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
238 cmn_err(CE_ALERT, "Please umount the filesystem, "
239 "and rectify the problem(s)");
245 * Called when we want to stop a buffer from getting written or read.
246 * We attach the EIO error, muck with its flags, and call biodone
247 * so that the proper iodone callbacks get called.
250 xfs_bioerror(
251 xfs_buf_t *bp)
254 #ifdef XFSERRORDEBUG
255 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
256 #endif
259 * No need to wait until the buffer is unpinned.
260 * We aren't flushing it.
262 xfs_buftrace("XFS IOERROR", bp);
263 XFS_BUF_ERROR(bp, EIO);
265 * We're calling biodone, so delete B_DONE flag. Either way
266 * we have to call the iodone callback, and calling biodone
267 * probably is the best way since it takes care of
268 * GRIO as well.
270 XFS_BUF_UNREAD(bp);
271 XFS_BUF_UNDELAYWRITE(bp);
272 XFS_BUF_UNDONE(bp);
273 XFS_BUF_STALE(bp);
275 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
276 xfs_biodone(bp);
278 return (EIO);
282 * Same as xfs_bioerror, except that we are releasing the buffer
283 * here ourselves, and avoiding the biodone call.
284 * This is meant for userdata errors; metadata bufs come with
285 * iodone functions attached, so that we can track down errors.
288 xfs_bioerror_relse(
289 xfs_buf_t *bp)
291 int64_t fl;
293 ASSERT(XFS_BUF_IODONE_FUNC(bp) != xfs_buf_iodone_callbacks);
294 ASSERT(XFS_BUF_IODONE_FUNC(bp) != xlog_iodone);
296 xfs_buftrace("XFS IOERRELSE", bp);
297 fl = XFS_BUF_BFLAGS(bp);
299 * No need to wait until the buffer is unpinned.
300 * We aren't flushing it.
302 * chunkhold expects B_DONE to be set, whether
303 * we actually finish the I/O or not. We don't want to
304 * change that interface.
306 XFS_BUF_UNREAD(bp);
307 XFS_BUF_UNDELAYWRITE(bp);
308 XFS_BUF_DONE(bp);
309 XFS_BUF_STALE(bp);
310 XFS_BUF_CLR_IODONE_FUNC(bp);
311 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
312 if (!(fl & XFS_B_ASYNC)) {
314 * Mark b_error and B_ERROR _both_.
315 * Lot's of chunkcache code assumes that.
316 * There's no reason to mark error for
317 * ASYNC buffers.
319 XFS_BUF_ERROR(bp, EIO);
320 XFS_BUF_V_IODONESEMA(bp);
321 } else {
322 xfs_buf_relse(bp);
324 return (EIO);
328 * Prints out an ALERT message about I/O error.
330 void
331 xfs_ioerror_alert(
332 char *func,
333 struct xfs_mount *mp,
334 xfs_buf_t *bp,
335 xfs_daddr_t blkno)
337 cmn_err(CE_ALERT,
338 "I/O error in filesystem (\"%s\") meta-data dev %s block 0x%llx"
339 " (\"%s\") error %d buf count %zd",
340 (!mp || !mp->m_fsname) ? "(fs name not set)" : mp->m_fsname,
341 XFS_BUFTARG_NAME(XFS_BUF_TARGET(bp)),
342 (__uint64_t)blkno, func,
343 XFS_BUF_GETERROR(bp), XFS_BUF_COUNT(bp));
347 * This isn't an absolute requirement, but it is
348 * just a good idea to call xfs_read_buf instead of
349 * directly doing a read_buf call. For one, we shouldn't
350 * be doing this disk read if we are in SHUTDOWN state anyway,
351 * so this stops that from happening. Secondly, this does all
352 * the error checking stuff and the brelse if appropriate for
353 * the caller, so the code can be a little leaner.
357 xfs_read_buf(
358 struct xfs_mount *mp,
359 xfs_buftarg_t *target,
360 xfs_daddr_t blkno,
361 int len,
362 uint flags,
363 xfs_buf_t **bpp)
365 xfs_buf_t *bp;
366 int error;
368 if (flags)
369 bp = xfs_buf_read_flags(target, blkno, len, flags);
370 else
371 bp = xfs_buf_read(target, blkno, len, flags);
372 if (!bp)
373 return XFS_ERROR(EIO);
374 error = XFS_BUF_GETERROR(bp);
375 if (bp && !error && !XFS_FORCED_SHUTDOWN(mp)) {
376 *bpp = bp;
377 } else {
378 *bpp = NULL;
379 if (error) {
380 xfs_ioerror_alert("xfs_read_buf", mp, bp, XFS_BUF_ADDR(bp));
381 } else {
382 error = XFS_ERROR(EIO);
384 if (bp) {
385 XFS_BUF_UNDONE(bp);
386 XFS_BUF_UNDELAYWRITE(bp);
387 XFS_BUF_STALE(bp);
389 * brelse clears B_ERROR and b_error
391 xfs_buf_relse(bp);
394 return (error);
398 * Wrapper around bwrite() so that we can trap
399 * write errors, and act accordingly.
402 xfs_bwrite(
403 struct xfs_mount *mp,
404 struct xfs_buf *bp)
406 int error;
409 * XXXsup how does this work for quotas.
411 XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
412 XFS_BUF_SET_FSPRIVATE3(bp, mp);
413 XFS_BUF_WRITE(bp);
415 if ((error = XFS_bwrite(bp))) {
416 ASSERT(mp);
418 * Cannot put a buftrace here since if the buffer is not
419 * B_HOLD then we will brelse() the buffer before returning
420 * from bwrite and we could be tracing a buffer that has
421 * been reused.
423 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
425 return (error);