blob: 94941da6c4da3469bf4385859f5eaaed452f587c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * 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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_quota.h"
30#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dinode.h"
37#include "xfs_inode.h"
38#include "xfs_bmap.h"
Nathan Scotta844f452005-11-02 14:38:42 +110039#include "xfs_btree.h"
40#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_rtalloc.h"
42#include "xfs_error.h"
43#include "xfs_itable.h"
44#include "xfs_rw.h"
45#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_attr.h"
47#include "xfs_inode_item.h"
48#include "xfs_buf_item.h"
49#include "xfs_utils.h"
50#include "xfs_iomap.h"
51
52#include <linux/capability.h>
53#include <linux/writeback.h>
54
55
56#if defined(XFS_RW_TRACE)
57void
58xfs_rw_enter_trace(
59 int tag,
60 xfs_iocore_t *io,
61 void *data,
62 size_t segs,
63 loff_t offset,
64 int ioflags)
65{
66 xfs_inode_t *ip = XFS_IO_INODE(io);
67
68 if (ip->i_rwtrace == NULL)
69 return;
70 ktrace_enter(ip->i_rwtrace,
71 (void *)(unsigned long)tag,
72 (void *)ip,
73 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
74 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
75 (void *)data,
76 (void *)((unsigned long)segs),
77 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
78 (void *)((unsigned long)(offset & 0xffffffff)),
79 (void *)((unsigned long)ioflags),
80 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
81 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
Yingping Luf1fdc842006-03-22 12:44:15 +110082 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 (void *)NULL,
84 (void *)NULL,
85 (void *)NULL,
86 (void *)NULL);
87}
88
89void
90xfs_inval_cached_trace(
91 xfs_iocore_t *io,
92 xfs_off_t offset,
93 xfs_off_t len,
94 xfs_off_t first,
95 xfs_off_t last)
96{
97 xfs_inode_t *ip = XFS_IO_INODE(io);
98
99 if (ip->i_rwtrace == NULL)
100 return;
101 ktrace_enter(ip->i_rwtrace,
102 (void *)(__psint_t)XFS_INVAL_CACHED,
103 (void *)ip,
104 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
105 (void *)((unsigned long)(offset & 0xffffffff)),
106 (void *)((unsigned long)((len >> 32) & 0xffffffff)),
107 (void *)((unsigned long)(len & 0xffffffff)),
108 (void *)((unsigned long)((first >> 32) & 0xffffffff)),
109 (void *)((unsigned long)(first & 0xffffffff)),
110 (void *)((unsigned long)((last >> 32) & 0xffffffff)),
111 (void *)((unsigned long)(last & 0xffffffff)),
Yingping Luf1fdc842006-03-22 12:44:15 +1100112 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 (void *)NULL,
114 (void *)NULL,
115 (void *)NULL,
116 (void *)NULL,
117 (void *)NULL);
118}
119#endif
120
121/*
122 * xfs_iozero
123 *
124 * xfs_iozero clears the specified range of buffer supplied,
125 * and marks all the affected blocks as valid and modified. If
126 * an affected block is not allocated, it will be allocated. If
127 * an affected block is not completely overwritten, and is not
128 * valid before the operation, it will be read from disk before
129 * being partially zeroed.
130 */
131STATIC int
132xfs_iozero(
133 struct inode *ip, /* inode */
134 loff_t pos, /* offset in file */
Lachlan McIlroy68160162007-02-10 18:36:47 +1100135 size_t count) /* size of data to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 unsigned bytes;
138 struct page *page;
139 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int status;
141
142 mapping = ip->i_mapping;
143 do {
144 unsigned long index, offset;
145
146 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
147 index = pos >> PAGE_CACHE_SHIFT;
148 bytes = PAGE_CACHE_SIZE - offset;
149 if (bytes > count)
150 bytes = count;
151
152 status = -ENOMEM;
153 page = grab_cache_page(mapping, index);
154 if (!page)
155 break;
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 status = mapping->a_ops->prepare_write(NULL, page, offset,
158 offset + bytes);
David Chinnere7ff6ae2007-02-10 18:37:46 +1100159 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Christoph Hellwig700716c2007-05-24 15:27:17 +1000162 zero_user_page(page, offset, bytes, KM_USER0);
David Chinnere7ff6ae2007-02-10 18:37:46 +1100163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 status = mapping->a_ops->commit_write(NULL, page, offset,
165 offset + bytes);
166 if (!status) {
167 pos += bytes;
168 count -= bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170
171unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unlock_page(page);
173 page_cache_release(page);
174 if (status)
175 break;
176 } while (count);
177
178 return (-status);
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181ssize_t /* bytes read, or (-) error */
182xfs_read(
183 bhv_desc_t *bdp,
184 struct kiocb *iocb,
185 const struct iovec *iovp,
186 unsigned int segs,
187 loff_t *offset,
188 int ioflags,
189 cred_t *credp)
190{
191 struct file *file = iocb->ki_filp;
192 struct inode *inode = file->f_mapping->host;
193 size_t size = 0;
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000194 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 xfs_fsize_t n;
196 xfs_inode_t *ip;
197 xfs_mount_t *mp;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000198 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 unsigned long seg;
200
201 ip = XFS_BHVTOI(bdp);
202 vp = BHV_TO_VNODE(bdp);
203 mp = ip->i_mount;
204
205 XFS_STATS_INC(xs_read_calls);
206
207 /* START copy & waste from filemap.c */
208 for (seg = 0; seg < segs; seg++) {
209 const struct iovec *iv = &iovp[seg];
210
211 /*
212 * If any segment has a negative length, or the cumulative
213 * length ever wraps negative then return -EINVAL.
214 */
215 size += iv->iov_len;
216 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
217 return XFS_ERROR(-EINVAL);
218 }
219 /* END copy & waste from filemap.c */
220
221 if (unlikely(ioflags & IO_ISDIRECT)) {
222 xfs_buftarg_t *target =
223 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
224 mp->m_rtdev_targp : mp->m_ddev_targp;
Nathan Scottce8e9222006-01-11 15:39:08 +1100225 if ((*offset & target->bt_smask) ||
226 (size & target->bt_smask)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000227 if (*offset == ip->i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return (0);
229 }
230 return -XFS_ERROR(EINVAL);
231 }
232 }
233
234 n = XFS_MAXIOFFSET(mp) - *offset;
235 if ((n <= 0) || (size == 0))
236 return 0;
237
238 if (n < size)
239 size = n;
240
Nathan Scotta13828b2006-03-17 17:26:14 +1100241 if (XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 if (unlikely(ioflags & IO_ISDIRECT))
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800245 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 xfs_ilock(ip, XFS_IOLOCK_SHARED);
247
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000248 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000249 bhv_vrwlock_t locktype = VRWLOCK_READ;
Dean Roehriche1a40fa2005-06-22 10:20:44 +1000250 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ,
253 BHV_TO_VNODE(bdp), *offset, size,
Dean Roehriche1a40fa2005-06-22 10:20:44 +1000254 dmflags, &locktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (ret) {
256 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000257 if (unlikely(ioflags & IO_ISDIRECT))
258 mutex_unlock(&inode->i_mutex);
259 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 }
262
Nathan Scottedcd4bc2006-09-28 11:05:33 +1000263 if (unlikely(ioflags & IO_ISDIRECT)) {
264 if (VN_CACHED(vp))
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000265 ret = bhv_vop_flushinval_pages(vp, ctooff(offtoct(*offset)),
Nathan Scottedcd4bc2006-09-28 11:05:33 +1000266 -1, FI_REMAPF_LOCKED);
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000267 mutex_unlock(&inode->i_mutex);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000268 if (ret) {
269 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
270 return ret;
271 }
Nathan Scottedcd4bc2006-09-28 11:05:33 +1000272 }
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 xfs_rw_enter_trace(XFS_READ_ENTER, &ip->i_iocore,
275 (void *)iovp, segs, *offset, ioflags);
Badari Pulavarty543ade12006-09-30 23:28:48 -0700276
277 iocb->ki_pos = *offset;
278 ret = generic_file_aio_read(iocb, iovp, segs, *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
280 ret = wait_on_sync_kiocb(iocb);
281 if (ret > 0)
282 XFS_STATS_ADD(xs_read_bytes, ret);
283
284 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return ret;
286}
287
288ssize_t
Nathan Scott1b895842006-03-31 13:08:59 +1000289xfs_splice_read(
290 bhv_desc_t *bdp,
291 struct file *infilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200292 loff_t *ppos,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200293 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000294 size_t count,
295 int flags,
296 int ioflags,
297 cred_t *credp)
298{
299 xfs_inode_t *ip = XFS_BHVTOI(bdp);
300 xfs_mount_t *mp = ip->i_mount;
301 ssize_t ret;
302
303 XFS_STATS_INC(xs_read_calls);
304 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
305 return -EIO;
306
307 xfs_ilock(ip, XFS_IOLOCK_SHARED);
308
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000309 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000310 bhv_vrwlock_t locktype = VRWLOCK_READ;
Nathan Scott1b895842006-03-31 13:08:59 +1000311 int error;
312
313 error = XFS_SEND_DATA(mp, DM_EVENT_READ, BHV_TO_VNODE(bdp),
Jens Axboecbb7e572006-04-11 14:57:50 +0200314 *ppos, count,
Nathan Scott1b895842006-03-31 13:08:59 +1000315 FILP_DELAY_FLAG(infilp), &locktype);
316 if (error) {
317 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
318 return -error;
319 }
320 }
321 xfs_rw_enter_trace(XFS_SPLICE_READ_ENTER, &ip->i_iocore,
Jens Axboecbb7e572006-04-11 14:57:50 +0200322 pipe, count, *ppos, ioflags);
323 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
Nathan Scott1b895842006-03-31 13:08:59 +1000324 if (ret > 0)
325 XFS_STATS_ADD(xs_read_bytes, ret);
326
327 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
328 return ret;
329}
330
331ssize_t
332xfs_splice_write(
333 bhv_desc_t *bdp,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200334 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000335 struct file *outfilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200336 loff_t *ppos,
Nathan Scott1b895842006-03-31 13:08:59 +1000337 size_t count,
338 int flags,
339 int ioflags,
340 cred_t *credp)
341{
342 xfs_inode_t *ip = XFS_BHVTOI(bdp);
343 xfs_mount_t *mp = ip->i_mount;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000344 xfs_iocore_t *io = &ip->i_iocore;
Nathan Scott1b895842006-03-31 13:08:59 +1000345 ssize_t ret;
David Chinner0a8d17d2006-09-07 14:27:15 +1000346 struct inode *inode = outfilp->f_mapping->host;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000347 xfs_fsize_t isize, new_size;
Nathan Scott1b895842006-03-31 13:08:59 +1000348
349 XFS_STATS_INC(xs_write_calls);
350 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
351 return -EIO;
352
353 xfs_ilock(ip, XFS_IOLOCK_EXCL);
354
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000355 if (DM_EVENT_ENABLED(ip, DM_EVENT_WRITE) && !(ioflags & IO_INVIS)) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000356 bhv_vrwlock_t locktype = VRWLOCK_WRITE;
Nathan Scott1b895842006-03-31 13:08:59 +1000357 int error;
358
359 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, BHV_TO_VNODE(bdp),
Jens Axboecbb7e572006-04-11 14:57:50 +0200360 *ppos, count,
Nathan Scott1b895842006-03-31 13:08:59 +1000361 FILP_DELAY_FLAG(outfilp), &locktype);
362 if (error) {
363 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
364 return -error;
365 }
366 }
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000367
368 new_size = *ppos + count;
369
370 xfs_ilock(ip, XFS_ILOCK_EXCL);
371 if (new_size > ip->i_size)
372 io->io_new_size = new_size;
373 xfs_iunlock(ip, XFS_ILOCK_EXCL);
374
Nathan Scott1b895842006-03-31 13:08:59 +1000375 xfs_rw_enter_trace(XFS_SPLICE_WRITE_ENTER, &ip->i_iocore,
Jens Axboecbb7e572006-04-11 14:57:50 +0200376 pipe, count, *ppos, ioflags);
377 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
Nathan Scott1b895842006-03-31 13:08:59 +1000378 if (ret > 0)
379 XFS_STATS_ADD(xs_write_bytes, ret);
380
David Chinner0a8d17d2006-09-07 14:27:15 +1000381 isize = i_size_read(inode);
382 if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
383 *ppos = isize;
384
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000385 if (*ppos > ip->i_size) {
David Chinner0a8d17d2006-09-07 14:27:15 +1000386 xfs_ilock(ip, XFS_ILOCK_EXCL);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000387 if (*ppos > ip->i_size)
388 ip->i_size = *ppos;
389 xfs_iunlock(ip, XFS_ILOCK_EXCL);
390 }
391
392 if (io->io_new_size) {
393 xfs_ilock(ip, XFS_ILOCK_EXCL);
394 io->io_new_size = 0;
395 if (ip->i_d.di_size > ip->i_size)
396 ip->i_d.di_size = ip->i_size;
David Chinner0a8d17d2006-09-07 14:27:15 +1000397 xfs_iunlock(ip, XFS_ILOCK_EXCL);
398 }
Nathan Scott1b895842006-03-31 13:08:59 +1000399 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return ret;
401}
402
403/*
404 * This routine is called to handle zeroing any space in the last
405 * block of the file that is beyond the EOF. We do this since the
406 * size is being increased without writing anything to that block
407 * and we don't want anyone to read the garbage on the disk.
408 */
409STATIC int /* error (positive) */
410xfs_zero_last_block(
411 struct inode *ip,
412 xfs_iocore_t *io,
Lachlan McIlroy68160162007-02-10 18:36:47 +1100413 xfs_fsize_t offset,
414 xfs_fsize_t isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 xfs_fileoff_t last_fsb;
Nathan Scott1b895842006-03-31 13:08:59 +1000417 xfs_mount_t *mp = io->io_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 int nimaps;
419 int zero_offset;
420 int zero_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 int error = 0;
422 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 ASSERT(ismrlocked(io->io_lock, MR_UPDATE) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Eric Sandeen24ee8082006-01-11 15:34:32 +1100426 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
427 if (zero_offset == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /*
429 * There are no extra bytes in the last block on disk to
430 * zero, so return.
431 */
432 return 0;
433 }
434
435 last_fsb = XFS_B_TO_FSBT(mp, isize);
436 nimaps = 1;
437 error = XFS_BMAPI(mp, NULL, io, last_fsb, 1, 0, NULL, 0, &imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000438 &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (error) {
440 return error;
441 }
442 ASSERT(nimaps > 0);
443 /*
444 * If the block underlying isize is just a hole, then there
445 * is nothing to zero.
446 */
447 if (imap.br_startblock == HOLESTARTBLOCK) {
448 return 0;
449 }
450 /*
451 * Zero the part of the last block beyond the EOF, and write it
452 * out sync. We need to drop the ilock while we do this so we
453 * don't deadlock when the buffer cache calls back to us.
454 */
455 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
Nathan Scott1b895842006-03-31 13:08:59 +1000456
Eric Sandeen24ee8082006-01-11 15:34:32 +1100457 zero_len = mp->m_sb.sb_blocksize - zero_offset;
Lachlan McIlroy68160162007-02-10 18:36:47 +1100458 if (isize + zero_len > offset)
459 zero_len = offset - isize;
460 error = xfs_iozero(ip, isize, zero_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
463 ASSERT(error >= 0);
464 return error;
465}
466
467/*
468 * Zero any on disk space between the current EOF and the new,
469 * larger EOF. This handles the normal case of zeroing the remainder
470 * of the last block in the file and the unusual case of zeroing blocks
471 * out beyond the size of the file. This second case only happens
472 * with fixed size extents and when the system crashes before the inode
473 * size was updated but after blocks were allocated. If fill is set,
474 * then any holes in the range are filled and zeroed. If not, the holes
475 * are left alone as holes.
476 */
477
478int /* error (positive) */
479xfs_zero_eof(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000480 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 xfs_iocore_t *io,
482 xfs_off_t offset, /* starting I/O offset */
Lachlan McIlroy68160162007-02-10 18:36:47 +1100483 xfs_fsize_t isize) /* current inode size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Nathan Scottec86dc02006-03-17 17:25:36 +1100485 struct inode *ip = vn_to_inode(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 xfs_fileoff_t start_zero_fsb;
487 xfs_fileoff_t end_zero_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 xfs_fileoff_t zero_count_fsb;
489 xfs_fileoff_t last_fsb;
Lachlan McIlroy68160162007-02-10 18:36:47 +1100490 xfs_fileoff_t zero_off;
491 xfs_fsize_t zero_len;
Nathan Scott1b895842006-03-31 13:08:59 +1000492 xfs_mount_t *mp = io->io_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 int nimaps;
494 int error = 0;
495 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
498 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
Eric Sandeen24ee8082006-01-11 15:34:32 +1100499 ASSERT(offset > isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /*
502 * First handle zeroing the block on which isize resides.
503 * We only zero a part of that block so it is handled specially.
504 */
Lachlan McIlroy68160162007-02-10 18:36:47 +1100505 error = xfs_zero_last_block(ip, io, offset, isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (error) {
507 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
508 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
509 return error;
510 }
511
512 /*
513 * Calculate the range between the new size and the old
514 * where blocks needing to be zeroed may exist. To get the
515 * block where the last byte in the file currently resides,
516 * we need to subtract one from the size and truncate back
517 * to a block boundary. We subtract 1 in case the size is
518 * exactly on a block boundary.
519 */
520 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
521 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
522 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
523 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
524 if (last_fsb == end_zero_fsb) {
525 /*
526 * The size was only incremented on its last block.
527 * We took care of that above, so just return.
528 */
529 return 0;
530 }
531
532 ASSERT(start_zero_fsb <= end_zero_fsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 while (start_zero_fsb <= end_zero_fsb) {
534 nimaps = 1;
535 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
536 error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000537 0, NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (error) {
539 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
540 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
541 return error;
542 }
543 ASSERT(nimaps > 0);
544
545 if (imap.br_state == XFS_EXT_UNWRITTEN ||
546 imap.br_startblock == HOLESTARTBLOCK) {
547 /*
548 * This loop handles initializing pages that were
549 * partially initialized by the code below this
550 * loop. It basically zeroes the part of the page
551 * that sits on a hole and sets the page as P_HOLE
552 * and calls remapf if it is a mapped file.
553 */
Eric Sandeen24ee8082006-01-11 15:34:32 +1100554 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
556 continue;
557 }
558
559 /*
Nathan Scott3d80ede2006-06-09 14:57:30 +1000560 * There are blocks we need to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 * Drop the inode lock while we're doing the I/O.
562 * We'll still have the iolock to protect us.
563 */
564 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
565
Lachlan McIlroy68160162007-02-10 18:36:47 +1100566 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
567 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
568
569 if ((zero_off + zero_len) > offset)
570 zero_len = offset - zero_off;
571
572 error = xfs_iozero(ip, zero_off, zero_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (error) {
574 goto out_lock;
575 }
576
Nathan Scott3d80ede2006-06-09 14:57:30 +1000577 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
579
580 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
581 }
582
583 return 0;
584
585out_lock:
586
587 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
588 ASSERT(error >= 0);
589 return error;
590}
591
592ssize_t /* bytes written, or (-) error */
593xfs_write(
594 bhv_desc_t *bdp,
595 struct kiocb *iocb,
596 const struct iovec *iovp,
597 unsigned int nsegs,
598 loff_t *offset,
599 int ioflags,
600 cred_t *credp)
601{
602 struct file *file = iocb->ki_filp;
603 struct address_space *mapping = file->f_mapping;
604 struct inode *inode = mapping->host;
605 unsigned long segs = nsegs;
606 xfs_inode_t *xip;
607 xfs_mount_t *mp;
608 ssize_t ret = 0, error = 0;
609 xfs_fsize_t isize, new_size;
610 xfs_iocore_t *io;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000611 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 int iolock;
613 int eventsent = 0;
Nathan Scott8285fb52006-06-09 17:07:12 +1000614 bhv_vrwlock_t locktype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 size_t ocount = 0, count;
616 loff_t pos;
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000617 int need_i_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 XFS_STATS_INC(xs_write_calls);
620
621 vp = BHV_TO_VNODE(bdp);
622 xip = XFS_BHVTOI(bdp);
623
Dmitriy Monakhov0ceb3312007-05-08 00:23:02 -0700624 error = generic_segment_checks(iovp, &segs, &ocount, VERIFY_READ);
625 if (error)
626 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 count = ocount;
629 pos = *offset;
630
631 if (count == 0)
632 return 0;
633
634 io = &xip->i_iocore;
635 mp = io->io_mount;
636
Nathan Scott34327e12006-06-09 17:11:55 +1000637 vfs_wait_for_freeze(vp->v_vfsp, SB_FREEZE_WRITE);
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (XFS_FORCED_SHUTDOWN(mp))
640 return -EIO;
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642relock:
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000643 if (ioflags & IO_ISDIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 iolock = XFS_IOLOCK_SHARED;
645 locktype = VRWLOCK_WRITE_DIRECT;
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000646 need_i_mutex = 0;
647 } else {
648 iolock = XFS_IOLOCK_EXCL;
649 locktype = VRWLOCK_WRITE;
650 need_i_mutex = 1;
651 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
654 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656start:
657 error = -generic_write_checks(file, &pos, &count,
658 S_ISBLK(inode->i_mode));
659 if (error) {
660 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100661 goto out_unlock_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000664 if ((DM_EVENT_ENABLED(xip, DM_EVENT_WRITE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 !(ioflags & IO_INVIS) && !eventsent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 int dmflags = FILP_DELAY_FLAG(file);
667
Nathan Scotta13828b2006-03-17 17:26:14 +1100668 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800669 dmflags |= DM_FLAGS_IMUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 xfs_iunlock(xip, XFS_ILOCK_EXCL);
672 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,
673 pos, count,
674 dmflags, &locktype);
675 if (error) {
Lachlan McIlroy71dfd5a2007-05-08 13:50:12 +1000676 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678 xfs_ilock(xip, XFS_ILOCK_EXCL);
679 eventsent = 1;
680
681 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000682 * The iolock was dropped and reacquired in XFS_SEND_DATA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 * so we have to recheck the size when appending.
684 * We will only "goto start;" once, since having sent the
685 * event prevents another call to XFS_SEND_DATA, which is
686 * what allows the size to change in the first place.
687 */
Lachlan McIlroy71dfd5a2007-05-08 13:50:12 +1000688 if ((file->f_flags & O_APPEND) && pos != xip->i_size)
689 goto start;
690 }
691
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000692 if (ioflags & IO_ISDIRECT) {
693 xfs_buftarg_t *target =
694 (xip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
695 mp->m_rtdev_targp : mp->m_ddev_targp;
696
697 if ((pos & target->bt_smask) || (count & target->bt_smask)) {
698 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
699 return XFS_ERROR(-EINVAL);
700 }
701
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000702 if (!need_i_mutex && (VN_CACHED(vp) || pos > xip->i_size)) {
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000703 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
704 iolock = XFS_IOLOCK_EXCL;
705 locktype = VRWLOCK_WRITE;
706 need_i_mutex = 1;
707 mutex_lock(&inode->i_mutex);
708 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 goto start;
710 }
711 }
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 new_size = pos + count;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000714 if (new_size > xip->i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 io->io_new_size = new_size;
716
Nathan Scott4aeb6642005-11-02 11:43:58 +1100717 if (likely(!(ioflags & IO_INVIS))) {
Christoph Hellwig870f4812006-01-09 20:52:01 -0800718 file_update_time(file);
Nathan Scott4aeb6642005-11-02 11:43:58 +1100719 xfs_ichgtime_fast(xip, inode,
720 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722
723 /*
724 * If the offset is beyond the size of the file, we have a couple
725 * of things to do. First, if there is already space allocated
726 * we need to either create holes or zero the disk or ...
727 *
728 * If there is a page where the previous size lands, we need
729 * to zero it out up to the new size.
730 */
731
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000732 if (pos > xip->i_size) {
733 error = xfs_zero_eof(BHV_TO_VNODE(bdp), io, pos, xip->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (error) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000735 xfs_iunlock(xip, XFS_ILOCK_EXCL);
736 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738 }
739 xfs_iunlock(xip, XFS_ILOCK_EXCL);
740
741 /*
742 * If we're writing the file then make sure to clear the
743 * setuid and setgid bits if the process is not being run
744 * by root. This keeps people from modifying setuid and
745 * setgid binaries.
746 */
747
748 if (((xip->i_d.di_mode & S_ISUID) ||
749 ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
750 (S_ISGID | S_IXGRP))) &&
751 !capable(CAP_FSETID)) {
752 error = xfs_write_clear_setuid(xip);
753 if (likely(!error))
Josef "Jeff" Sipeke678fb02006-12-08 02:36:49 -0800754 error = -remove_suid(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (unlikely(error)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000756 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758 }
759
760retry:
761 /* We can write back this queue in page reclaim */
762 current->backing_dev_info = mapping->backing_dev_info;
763
764 if ((ioflags & IO_ISDIRECT)) {
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000765 if (VN_CACHED(vp)) {
766 WARN_ON(need_i_mutex == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 xfs_inval_cached_trace(io, pos, -1,
768 ctooff(offtoct(pos)), -1);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000769 error = bhv_vop_flushinval_pages(vp, ctooff(offtoct(pos)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000771 if (error)
772 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
Nathan Scotta13828b2006-03-17 17:26:14 +1100775 if (need_i_mutex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /* demote the lock now the cached pages are gone */
777 XFS_ILOCK_DEMOTE(mp, io, XFS_IOLOCK_EXCL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800778 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 iolock = XFS_IOLOCK_SHARED;
781 locktype = VRWLOCK_WRITE_DIRECT;
Nathan Scotta13828b2006-03-17 17:26:14 +1100782 need_i_mutex = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
785 xfs_rw_enter_trace(XFS_DIOWR_ENTER, io, (void *)iovp, segs,
786 *offset, ioflags);
787 ret = generic_file_direct_write(iocb, iovp,
788 &segs, pos, offset, count, ocount);
789
790 /*
791 * direct-io write to a hole: fall through to buffered I/O
792 * for completing the rest of the request.
793 */
794 if (ret >= 0 && ret != count) {
795 XFS_STATS_ADD(xs_write_bytes, ret);
796
797 pos += ret;
798 count -= ret;
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 ioflags &= ~IO_ISDIRECT;
801 xfs_iunlock(xip, iolock);
802 goto relock;
803 }
804 } else {
805 xfs_rw_enter_trace(XFS_WRITE_ENTER, io, (void *)iovp, segs,
806 *offset, ioflags);
807 ret = generic_file_buffered_write(iocb, iovp, segs,
808 pos, offset, count, ret);
809 }
810
811 current->backing_dev_info = NULL;
812
813 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
814 ret = wait_on_sync_kiocb(iocb);
815
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000816 if (ret == -ENOSPC &&
817 DM_EVENT_ENABLED(xip, DM_EVENT_NOSPACE) && !(ioflags & IO_INVIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 xfs_rwunlock(bdp, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100819 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800820 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp,
822 DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL,
823 0, 0, 0); /* Delay flag intentionally unused */
Nathan Scotta13828b2006-03-17 17:26:14 +1100824 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800825 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 xfs_rwlock(bdp, locktype);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000827 if (error)
828 goto out_unlock_internal;
829 pos = xip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 ret = 0;
831 goto retry;
832 }
833
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100834 isize = i_size_read(inode);
835 if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
836 *offset = isize;
837
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000838 if (*offset > xip->i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 xfs_ilock(xip, XFS_ILOCK_EXCL);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000840 if (*offset > xip->i_size)
841 xip->i_size = *offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 xfs_iunlock(xip, XFS_ILOCK_EXCL);
843 }
844
845 error = -ret;
846 if (ret <= 0)
847 goto out_unlock_internal;
848
849 XFS_STATS_ADD(xs_write_bytes, ret);
850
851 /* Handle various SYNC-type writes */
852 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
Nathan Scott1e69dd02006-06-19 08:39:53 +1000853 error = xfs_write_sync_logforce(mp, xip);
854 if (error)
855 goto out_unlock_internal;
Nathan Scotta13828b2006-03-17 17:26:14 +1100856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 xfs_rwunlock(bdp, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100858 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800859 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 error = sync_page_range(inode, mapping, pos, ret);
862 if (!error)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000863 error = -ret;
864 if (need_i_mutex)
865 mutex_lock(&inode->i_mutex);
866 xfs_rwlock(bdp, locktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
869 out_unlock_internal:
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000870 if (io->io_new_size) {
871 xfs_ilock(xip, XFS_ILOCK_EXCL);
872 io->io_new_size = 0;
873 /*
874 * If this was a direct or synchronous I/O that failed (such
875 * as ENOSPC) then part of the I/O may have been written to
876 * disk before the error occured. In this case the on-disk
877 * file size may have been adjusted beyond the in-memory file
878 * size and now needs to be truncated back.
879 */
880 if (xip->i_d.di_size > xip->i_size)
881 xip->i_d.di_size = xip->i_size;
882 xfs_iunlock(xip, XFS_ILOCK_EXCL);
883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 xfs_rwunlock(bdp, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100885 out_unlock_mutex:
886 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800887 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return -error;
889}
890
891/*
892 * All xfs metadata buffers except log state machine buffers
893 * get this attached as their b_bdstrat callback function.
894 * This is so that we can catch a buffer
895 * after prematurely unpinning it to forcibly shutdown the filesystem.
896 */
897int
898xfs_bdstrat_cb(struct xfs_buf *bp)
899{
900 xfs_mount_t *mp;
901
902 mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
903 if (!XFS_FORCED_SHUTDOWN(mp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100904 xfs_buf_iorequest(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return 0;
906 } else {
907 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
908 /*
909 * Metadata write that didn't get logged but
910 * written delayed anyway. These aren't associated
911 * with a transaction, and can be ignored.
912 */
913 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
914 (XFS_BUF_ISREAD(bp)) == 0)
915 return (xfs_bioerror_relse(bp));
916 else
917 return (xfs_bioerror(bp));
918 }
919}
920
921
922int
923xfs_bmap(bhv_desc_t *bdp,
924 xfs_off_t offset,
925 ssize_t count,
926 int flags,
927 xfs_iomap_t *iomapp,
928 int *niomaps)
929{
930 xfs_inode_t *ip = XFS_BHVTOI(bdp);
931 xfs_iocore_t *io = &ip->i_iocore;
932
933 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
934 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
935 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
936
937 return xfs_iomap(io, offset, count, flags, iomapp, niomaps);
938}
939
940/*
941 * Wrapper around bdstrat so that we can stop data
942 * from going to disk in case we are shutting down the filesystem.
943 * Typically user data goes thru this path; one of the exceptions
944 * is the superblock.
945 */
946int
947xfsbdstrat(
948 struct xfs_mount *mp,
949 struct xfs_buf *bp)
950{
951 ASSERT(mp);
952 if (!XFS_FORCED_SHUTDOWN(mp)) {
953 /* Grio redirection would go here
954 * if (XFS_BUF_IS_GRIO(bp)) {
955 */
956
Nathan Scottce8e9222006-01-11 15:39:08 +1100957 xfs_buf_iorequest(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return 0;
959 }
960
961 xfs_buftrace("XFSBDSTRAT IOERROR", bp);
962 return (xfs_bioerror_relse(bp));
963}
964
965/*
966 * If the underlying (data/log/rt) device is readonly, there are some
967 * operations that cannot proceed.
968 */
969int
970xfs_dev_is_read_only(
971 xfs_mount_t *mp,
972 char *message)
973{
974 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
975 xfs_readonly_buftarg(mp->m_logdev_targp) ||
976 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
977 cmn_err(CE_NOTE,
978 "XFS: %s required on read-only device.", message);
979 cmn_err(CE_NOTE,
980 "XFS: write access unavailable, cannot proceed.");
981 return EROFS;
982 }
983 return 0;
984}