blob: 50ba4794f19fe0120921668f28b094e10f830079 [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"
46#include "xfs_cap.h"
47#include "xfs_mac.h"
48#include "xfs_attr.h"
49#include "xfs_inode_item.h"
50#include "xfs_buf_item.h"
51#include "xfs_utils.h"
52#include "xfs_iomap.h"
53
54#include <linux/capability.h>
55#include <linux/writeback.h>
56
57
58#if defined(XFS_RW_TRACE)
59void
60xfs_rw_enter_trace(
61 int tag,
62 xfs_iocore_t *io,
63 void *data,
64 size_t segs,
65 loff_t offset,
66 int ioflags)
67{
68 xfs_inode_t *ip = XFS_IO_INODE(io);
69
70 if (ip->i_rwtrace == NULL)
71 return;
72 ktrace_enter(ip->i_rwtrace,
73 (void *)(unsigned long)tag,
74 (void *)ip,
75 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
76 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
77 (void *)data,
78 (void *)((unsigned long)segs),
79 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
80 (void *)((unsigned long)(offset & 0xffffffff)),
81 (void *)((unsigned long)ioflags),
82 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
83 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
Yingping Luf1fdc842006-03-22 12:44:15 +110084 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 (void *)NULL,
86 (void *)NULL,
87 (void *)NULL,
88 (void *)NULL);
89}
90
91void
92xfs_inval_cached_trace(
93 xfs_iocore_t *io,
94 xfs_off_t offset,
95 xfs_off_t len,
96 xfs_off_t first,
97 xfs_off_t last)
98{
99 xfs_inode_t *ip = XFS_IO_INODE(io);
100
101 if (ip->i_rwtrace == NULL)
102 return;
103 ktrace_enter(ip->i_rwtrace,
104 (void *)(__psint_t)XFS_INVAL_CACHED,
105 (void *)ip,
106 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
107 (void *)((unsigned long)(offset & 0xffffffff)),
108 (void *)((unsigned long)((len >> 32) & 0xffffffff)),
109 (void *)((unsigned long)(len & 0xffffffff)),
110 (void *)((unsigned long)((first >> 32) & 0xffffffff)),
111 (void *)((unsigned long)(first & 0xffffffff)),
112 (void *)((unsigned long)((last >> 32) & 0xffffffff)),
113 (void *)((unsigned long)(last & 0xffffffff)),
Yingping Luf1fdc842006-03-22 12:44:15 +1100114 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 (void *)NULL,
116 (void *)NULL,
117 (void *)NULL,
118 (void *)NULL,
119 (void *)NULL);
120}
121#endif
122
123/*
124 * xfs_iozero
125 *
126 * xfs_iozero clears the specified range of buffer supplied,
127 * and marks all the affected blocks as valid and modified. If
128 * an affected block is not allocated, it will be allocated. If
129 * an affected block is not completely overwritten, and is not
130 * valid before the operation, it will be read from disk before
131 * being partially zeroed.
132 */
133STATIC int
134xfs_iozero(
135 struct inode *ip, /* inode */
136 loff_t pos, /* offset in file */
Lachlan McIlroy68160162007-02-10 18:36:47 +1100137 size_t count) /* size of data to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 unsigned bytes;
140 struct page *page;
141 struct address_space *mapping;
142 char *kaddr;
143 int status;
144
145 mapping = ip->i_mapping;
146 do {
147 unsigned long index, offset;
148
149 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
150 index = pos >> PAGE_CACHE_SHIFT;
151 bytes = PAGE_CACHE_SIZE - offset;
152 if (bytes > count)
153 bytes = count;
154
155 status = -ENOMEM;
156 page = grab_cache_page(mapping, index);
157 if (!page)
158 break;
159
160 kaddr = kmap(page);
161 status = mapping->a_ops->prepare_write(NULL, page, offset,
162 offset + bytes);
163 if (status) {
164 goto unlock;
165 }
166
167 memset((void *) (kaddr + offset), 0, bytes);
168 flush_dcache_page(page);
169 status = mapping->a_ops->commit_write(NULL, page, offset,
170 offset + bytes);
171 if (!status) {
172 pos += bytes;
173 count -= bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175
176unlock:
177 kunmap(page);
178 unlock_page(page);
179 page_cache_release(page);
180 if (status)
181 break;
182 } while (count);
183
184 return (-status);
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187ssize_t /* bytes read, or (-) error */
188xfs_read(
189 bhv_desc_t *bdp,
190 struct kiocb *iocb,
191 const struct iovec *iovp,
192 unsigned int segs,
193 loff_t *offset,
194 int ioflags,
195 cred_t *credp)
196{
197 struct file *file = iocb->ki_filp;
198 struct inode *inode = file->f_mapping->host;
199 size_t size = 0;
200 ssize_t ret;
201 xfs_fsize_t n;
202 xfs_inode_t *ip;
203 xfs_mount_t *mp;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000204 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 unsigned long seg;
206
207 ip = XFS_BHVTOI(bdp);
208 vp = BHV_TO_VNODE(bdp);
209 mp = ip->i_mount;
210
211 XFS_STATS_INC(xs_read_calls);
212
213 /* START copy & waste from filemap.c */
214 for (seg = 0; seg < segs; seg++) {
215 const struct iovec *iv = &iovp[seg];
216
217 /*
218 * If any segment has a negative length, or the cumulative
219 * length ever wraps negative then return -EINVAL.
220 */
221 size += iv->iov_len;
222 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
223 return XFS_ERROR(-EINVAL);
224 }
225 /* END copy & waste from filemap.c */
226
227 if (unlikely(ioflags & IO_ISDIRECT)) {
228 xfs_buftarg_t *target =
229 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
230 mp->m_rtdev_targp : mp->m_ddev_targp;
Nathan Scottce8e9222006-01-11 15:39:08 +1100231 if ((*offset & target->bt_smask) ||
232 (size & target->bt_smask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (*offset == ip->i_d.di_size) {
234 return (0);
235 }
236 return -XFS_ERROR(EINVAL);
237 }
238 }
239
240 n = XFS_MAXIOFFSET(mp) - *offset;
241 if ((n <= 0) || (size == 0))
242 return 0;
243
244 if (n < size)
245 size = n;
246
Nathan Scotta13828b2006-03-17 17:26:14 +1100247 if (XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 if (unlikely(ioflags & IO_ISDIRECT))
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800251 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 xfs_ilock(ip, XFS_IOLOCK_SHARED);
253
254 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ) &&
255 !(ioflags & IO_INVIS)) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000256 bhv_vrwlock_t locktype = VRWLOCK_READ;
Dean Roehriche1a40fa2005-06-22 10:20:44 +1000257 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ,
260 BHV_TO_VNODE(bdp), *offset, size,
Dean Roehriche1a40fa2005-06-22 10:20:44 +1000261 dmflags, &locktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (ret) {
263 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000264 if (unlikely(ioflags & IO_ISDIRECT))
265 mutex_unlock(&inode->i_mutex);
266 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268 }
269
Nathan Scottedcd4bc2006-09-28 11:05:33 +1000270 if (unlikely(ioflags & IO_ISDIRECT)) {
271 if (VN_CACHED(vp))
272 bhv_vop_flushinval_pages(vp, ctooff(offtoct(*offset)),
273 -1, FI_REMAPF_LOCKED);
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000274 mutex_unlock(&inode->i_mutex);
Nathan Scottedcd4bc2006-09-28 11:05:33 +1000275 }
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 xfs_rw_enter_trace(XFS_READ_ENTER, &ip->i_iocore,
278 (void *)iovp, segs, *offset, ioflags);
Badari Pulavarty543ade12006-09-30 23:28:48 -0700279
280 iocb->ki_pos = *offset;
281 ret = generic_file_aio_read(iocb, iovp, segs, *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
283 ret = wait_on_sync_kiocb(iocb);
284 if (ret > 0)
285 XFS_STATS_ADD(xs_read_bytes, ret);
286
287 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return ret;
289}
290
291ssize_t
292xfs_sendfile(
293 bhv_desc_t *bdp,
294 struct file *filp,
295 loff_t *offset,
296 int ioflags,
297 size_t count,
298 read_actor_t actor,
299 void *target,
300 cred_t *credp)
301{
Nathan Scott1b895842006-03-31 13:08:59 +1000302 xfs_inode_t *ip = XFS_BHVTOI(bdp);
303 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 XFS_STATS_INC(xs_read_calls);
Nathan Scott1b895842006-03-31 13:08:59 +1000307 if (XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return -EIO;
309
310 xfs_ilock(ip, XFS_IOLOCK_SHARED);
311
Nathan Scott1b895842006-03-31 13:08:59 +1000312 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_READ) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 (!(ioflags & IO_INVIS))) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000314 bhv_vrwlock_t locktype = VRWLOCK_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 int error;
316
Nathan Scott1b895842006-03-31 13:08:59 +1000317 error = XFS_SEND_DATA(mp, DM_EVENT_READ, BHV_TO_VNODE(bdp),
318 *offset, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 FILP_DELAY_FLAG(filp), &locktype);
320 if (error) {
321 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
322 return -error;
323 }
324 }
325 xfs_rw_enter_trace(XFS_SENDFILE_ENTER, &ip->i_iocore,
326 (void *)(unsigned long)target, count, *offset, ioflags);
327 ret = generic_file_sendfile(filp, offset, count, actor, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (ret > 0)
329 XFS_STATS_ADD(xs_read_bytes, ret);
330
Nathan Scott1b895842006-03-31 13:08:59 +1000331 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
332 return ret;
333}
334
335ssize_t
336xfs_splice_read(
337 bhv_desc_t *bdp,
338 struct file *infilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200339 loff_t *ppos,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200340 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000341 size_t count,
342 int flags,
343 int ioflags,
344 cred_t *credp)
345{
346 xfs_inode_t *ip = XFS_BHVTOI(bdp);
347 xfs_mount_t *mp = ip->i_mount;
348 ssize_t ret;
349
350 XFS_STATS_INC(xs_read_calls);
351 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
352 return -EIO;
353
354 xfs_ilock(ip, XFS_IOLOCK_SHARED);
355
356 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_READ) &&
357 (!(ioflags & IO_INVIS))) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000358 bhv_vrwlock_t locktype = VRWLOCK_READ;
Nathan Scott1b895842006-03-31 13:08:59 +1000359 int error;
360
361 error = XFS_SEND_DATA(mp, DM_EVENT_READ, BHV_TO_VNODE(bdp),
Jens Axboecbb7e572006-04-11 14:57:50 +0200362 *ppos, count,
Nathan Scott1b895842006-03-31 13:08:59 +1000363 FILP_DELAY_FLAG(infilp), &locktype);
364 if (error) {
365 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
366 return -error;
367 }
368 }
369 xfs_rw_enter_trace(XFS_SPLICE_READ_ENTER, &ip->i_iocore,
Jens Axboecbb7e572006-04-11 14:57:50 +0200370 pipe, count, *ppos, ioflags);
371 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
Nathan Scott1b895842006-03-31 13:08:59 +1000372 if (ret > 0)
373 XFS_STATS_ADD(xs_read_bytes, ret);
374
375 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
376 return ret;
377}
378
379ssize_t
380xfs_splice_write(
381 bhv_desc_t *bdp,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200382 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000383 struct file *outfilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200384 loff_t *ppos,
Nathan Scott1b895842006-03-31 13:08:59 +1000385 size_t count,
386 int flags,
387 int ioflags,
388 cred_t *credp)
389{
390 xfs_inode_t *ip = XFS_BHVTOI(bdp);
391 xfs_mount_t *mp = ip->i_mount;
392 ssize_t ret;
David Chinner0a8d17d2006-09-07 14:27:15 +1000393 struct inode *inode = outfilp->f_mapping->host;
394 xfs_fsize_t isize;
Nathan Scott1b895842006-03-31 13:08:59 +1000395
396 XFS_STATS_INC(xs_write_calls);
397 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
398 return -EIO;
399
400 xfs_ilock(ip, XFS_IOLOCK_EXCL);
401
402 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_WRITE) &&
403 (!(ioflags & IO_INVIS))) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000404 bhv_vrwlock_t locktype = VRWLOCK_WRITE;
Nathan Scott1b895842006-03-31 13:08:59 +1000405 int error;
406
407 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, BHV_TO_VNODE(bdp),
Jens Axboecbb7e572006-04-11 14:57:50 +0200408 *ppos, count,
Nathan Scott1b895842006-03-31 13:08:59 +1000409 FILP_DELAY_FLAG(outfilp), &locktype);
410 if (error) {
411 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
412 return -error;
413 }
414 }
415 xfs_rw_enter_trace(XFS_SPLICE_WRITE_ENTER, &ip->i_iocore,
Jens Axboecbb7e572006-04-11 14:57:50 +0200416 pipe, count, *ppos, ioflags);
417 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
Nathan Scott1b895842006-03-31 13:08:59 +1000418 if (ret > 0)
419 XFS_STATS_ADD(xs_write_bytes, ret);
420
David Chinner0a8d17d2006-09-07 14:27:15 +1000421 isize = i_size_read(inode);
422 if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
423 *ppos = isize;
424
425 if (*ppos > ip->i_d.di_size) {
426 xfs_ilock(ip, XFS_ILOCK_EXCL);
427 if (*ppos > ip->i_d.di_size) {
428 ip->i_d.di_size = *ppos;
429 i_size_write(inode, *ppos);
430 ip->i_update_core = 1;
431 ip->i_update_size = 1;
432 }
433 xfs_iunlock(ip, XFS_ILOCK_EXCL);
434 }
Nathan Scott1b895842006-03-31 13:08:59 +1000435 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return ret;
437}
438
439/*
440 * This routine is called to handle zeroing any space in the last
441 * block of the file that is beyond the EOF. We do this since the
442 * size is being increased without writing anything to that block
443 * and we don't want anyone to read the garbage on the disk.
444 */
445STATIC int /* error (positive) */
446xfs_zero_last_block(
447 struct inode *ip,
448 xfs_iocore_t *io,
Lachlan McIlroy68160162007-02-10 18:36:47 +1100449 xfs_fsize_t offset,
450 xfs_fsize_t isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 xfs_fileoff_t last_fsb;
Nathan Scott1b895842006-03-31 13:08:59 +1000453 xfs_mount_t *mp = io->io_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 int nimaps;
455 int zero_offset;
456 int zero_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 int error = 0;
458 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 ASSERT(ismrlocked(io->io_lock, MR_UPDATE) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Eric Sandeen24ee8082006-01-11 15:34:32 +1100462 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
463 if (zero_offset == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /*
465 * There are no extra bytes in the last block on disk to
466 * zero, so return.
467 */
468 return 0;
469 }
470
471 last_fsb = XFS_B_TO_FSBT(mp, isize);
472 nimaps = 1;
473 error = XFS_BMAPI(mp, NULL, io, last_fsb, 1, 0, NULL, 0, &imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000474 &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (error) {
476 return error;
477 }
478 ASSERT(nimaps > 0);
479 /*
480 * If the block underlying isize is just a hole, then there
481 * is nothing to zero.
482 */
483 if (imap.br_startblock == HOLESTARTBLOCK) {
484 return 0;
485 }
486 /*
487 * Zero the part of the last block beyond the EOF, and write it
488 * out sync. We need to drop the ilock while we do this so we
489 * don't deadlock when the buffer cache calls back to us.
490 */
491 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
Nathan Scott1b895842006-03-31 13:08:59 +1000492
Eric Sandeen24ee8082006-01-11 15:34:32 +1100493 zero_len = mp->m_sb.sb_blocksize - zero_offset;
Lachlan McIlroy68160162007-02-10 18:36:47 +1100494 if (isize + zero_len > offset)
495 zero_len = offset - isize;
496 error = xfs_iozero(ip, isize, zero_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
499 ASSERT(error >= 0);
500 return error;
501}
502
503/*
504 * Zero any on disk space between the current EOF and the new,
505 * larger EOF. This handles the normal case of zeroing the remainder
506 * of the last block in the file and the unusual case of zeroing blocks
507 * out beyond the size of the file. This second case only happens
508 * with fixed size extents and when the system crashes before the inode
509 * size was updated but after blocks were allocated. If fill is set,
510 * then any holes in the range are filled and zeroed. If not, the holes
511 * are left alone as holes.
512 */
513
514int /* error (positive) */
515xfs_zero_eof(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000516 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 xfs_iocore_t *io,
518 xfs_off_t offset, /* starting I/O offset */
Lachlan McIlroy68160162007-02-10 18:36:47 +1100519 xfs_fsize_t isize) /* current inode size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Nathan Scottec86dc02006-03-17 17:25:36 +1100521 struct inode *ip = vn_to_inode(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 xfs_fileoff_t start_zero_fsb;
523 xfs_fileoff_t end_zero_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 xfs_fileoff_t zero_count_fsb;
525 xfs_fileoff_t last_fsb;
Lachlan McIlroy68160162007-02-10 18:36:47 +1100526 xfs_fileoff_t zero_off;
527 xfs_fsize_t zero_len;
Nathan Scott1b895842006-03-31 13:08:59 +1000528 xfs_mount_t *mp = io->io_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 int nimaps;
530 int error = 0;
531 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
534 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
Eric Sandeen24ee8082006-01-11 15:34:32 +1100535 ASSERT(offset > isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /*
538 * First handle zeroing the block on which isize resides.
539 * We only zero a part of that block so it is handled specially.
540 */
Lachlan McIlroy68160162007-02-10 18:36:47 +1100541 error = xfs_zero_last_block(ip, io, offset, isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (error) {
543 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
544 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
545 return error;
546 }
547
548 /*
549 * Calculate the range between the new size and the old
550 * where blocks needing to be zeroed may exist. To get the
551 * block where the last byte in the file currently resides,
552 * we need to subtract one from the size and truncate back
553 * to a block boundary. We subtract 1 in case the size is
554 * exactly on a block boundary.
555 */
556 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
557 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
558 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
559 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
560 if (last_fsb == end_zero_fsb) {
561 /*
562 * The size was only incremented on its last block.
563 * We took care of that above, so just return.
564 */
565 return 0;
566 }
567
568 ASSERT(start_zero_fsb <= end_zero_fsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 while (start_zero_fsb <= end_zero_fsb) {
570 nimaps = 1;
571 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
572 error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000573 0, NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (error) {
575 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
576 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
577 return error;
578 }
579 ASSERT(nimaps > 0);
580
581 if (imap.br_state == XFS_EXT_UNWRITTEN ||
582 imap.br_startblock == HOLESTARTBLOCK) {
583 /*
584 * This loop handles initializing pages that were
585 * partially initialized by the code below this
586 * loop. It basically zeroes the part of the page
587 * that sits on a hole and sets the page as P_HOLE
588 * and calls remapf if it is a mapped file.
589 */
Eric Sandeen24ee8082006-01-11 15:34:32 +1100590 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
592 continue;
593 }
594
595 /*
Nathan Scott3d80ede2006-06-09 14:57:30 +1000596 * There are blocks we need to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 * Drop the inode lock while we're doing the I/O.
598 * We'll still have the iolock to protect us.
599 */
600 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
601
Lachlan McIlroy68160162007-02-10 18:36:47 +1100602 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
603 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
604
605 if ((zero_off + zero_len) > offset)
606 zero_len = offset - zero_off;
607
608 error = xfs_iozero(ip, zero_off, zero_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (error) {
610 goto out_lock;
611 }
612
Nathan Scott3d80ede2006-06-09 14:57:30 +1000613 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
615
616 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
617 }
618
619 return 0;
620
621out_lock:
622
623 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
624 ASSERT(error >= 0);
625 return error;
626}
627
628ssize_t /* bytes written, or (-) error */
629xfs_write(
630 bhv_desc_t *bdp,
631 struct kiocb *iocb,
632 const struct iovec *iovp,
633 unsigned int nsegs,
634 loff_t *offset,
635 int ioflags,
636 cred_t *credp)
637{
638 struct file *file = iocb->ki_filp;
639 struct address_space *mapping = file->f_mapping;
640 struct inode *inode = mapping->host;
641 unsigned long segs = nsegs;
642 xfs_inode_t *xip;
643 xfs_mount_t *mp;
644 ssize_t ret = 0, error = 0;
645 xfs_fsize_t isize, new_size;
646 xfs_iocore_t *io;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000647 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 unsigned long seg;
649 int iolock;
650 int eventsent = 0;
Nathan Scott8285fb52006-06-09 17:07:12 +1000651 bhv_vrwlock_t locktype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 size_t ocount = 0, count;
653 loff_t pos;
Nathan Scotta13828b2006-03-17 17:26:14 +1100654 int need_i_mutex = 1, need_flush = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 XFS_STATS_INC(xs_write_calls);
657
658 vp = BHV_TO_VNODE(bdp);
659 xip = XFS_BHVTOI(bdp);
660
661 for (seg = 0; seg < segs; seg++) {
662 const struct iovec *iv = &iovp[seg];
663
664 /*
665 * If any segment has a negative length, or the cumulative
666 * length ever wraps negative then return -EINVAL.
667 */
668 ocount += iv->iov_len;
669 if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
670 return -EINVAL;
671 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
672 continue;
673 if (seg == 0)
674 return -EFAULT;
675 segs = seg;
676 ocount -= iv->iov_len; /* This segment is no good */
677 break;
678 }
679
680 count = ocount;
681 pos = *offset;
682
683 if (count == 0)
684 return 0;
685
686 io = &xip->i_iocore;
687 mp = io->io_mount;
688
Nathan Scott34327e12006-06-09 17:11:55 +1000689 vfs_wait_for_freeze(vp->v_vfsp, SB_FREEZE_WRITE);
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (XFS_FORCED_SHUTDOWN(mp))
692 return -EIO;
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (ioflags & IO_ISDIRECT) {
695 xfs_buftarg_t *target =
696 (xip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
697 mp->m_rtdev_targp : mp->m_ddev_targp;
698
Nathan Scottce8e9222006-01-11 15:39:08 +1100699 if ((pos & target->bt_smask) || (count & target->bt_smask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return XFS_ERROR(-EINVAL);
701
702 if (!VN_CACHED(vp) && pos < i_size_read(inode))
Nathan Scotta13828b2006-03-17 17:26:14 +1100703 need_i_mutex = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (VN_CACHED(vp))
706 need_flush = 1;
707 }
708
709relock:
Nathan Scotta13828b2006-03-17 17:26:14 +1100710 if (need_i_mutex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 iolock = XFS_IOLOCK_EXCL;
712 locktype = VRWLOCK_WRITE;
713
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800714 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 } else {
716 iolock = XFS_IOLOCK_SHARED;
717 locktype = VRWLOCK_WRITE_DIRECT;
718 }
719
720 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
721
722 isize = i_size_read(inode);
723
724 if (file->f_flags & O_APPEND)
725 *offset = isize;
726
727start:
728 error = -generic_write_checks(file, &pos, &count,
729 S_ISBLK(inode->i_mode));
730 if (error) {
731 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100732 goto out_unlock_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
735 new_size = pos + count;
736 if (new_size > isize)
737 io->io_new_size = new_size;
738
739 if ((DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_WRITE) &&
740 !(ioflags & IO_INVIS) && !eventsent)) {
741 loff_t savedsize = pos;
742 int dmflags = FILP_DELAY_FLAG(file);
743
Nathan Scotta13828b2006-03-17 17:26:14 +1100744 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800745 dmflags |= DM_FLAGS_IMUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 xfs_iunlock(xip, XFS_ILOCK_EXCL);
748 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,
749 pos, count,
750 dmflags, &locktype);
751 if (error) {
752 xfs_iunlock(xip, iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100753 goto out_unlock_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755 xfs_ilock(xip, XFS_ILOCK_EXCL);
756 eventsent = 1;
757
758 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000759 * The iolock was dropped and reacquired in XFS_SEND_DATA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 * so we have to recheck the size when appending.
761 * We will only "goto start;" once, since having sent the
762 * event prevents another call to XFS_SEND_DATA, which is
763 * what allows the size to change in the first place.
764 */
765 if ((file->f_flags & O_APPEND) && savedsize != isize) {
766 pos = isize = xip->i_d.di_size;
767 goto start;
768 }
769 }
770
Nathan Scott4aeb6642005-11-02 11:43:58 +1100771 if (likely(!(ioflags & IO_INVIS))) {
Christoph Hellwig870f4812006-01-09 20:52:01 -0800772 file_update_time(file);
Nathan Scott4aeb6642005-11-02 11:43:58 +1100773 xfs_ichgtime_fast(xip, inode,
774 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776
777 /*
778 * If the offset is beyond the size of the file, we have a couple
779 * of things to do. First, if there is already space allocated
780 * we need to either create holes or zero the disk or ...
781 *
782 * If there is a page where the previous size lands, we need
783 * to zero it out up to the new size.
784 */
785
786 if (pos > isize) {
Lachlan McIlroy68160162007-02-10 18:36:47 +1100787 error = xfs_zero_eof(BHV_TO_VNODE(bdp), io, pos, isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (error) {
789 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100790 goto out_unlock_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792 }
793 xfs_iunlock(xip, XFS_ILOCK_EXCL);
794
795 /*
796 * If we're writing the file then make sure to clear the
797 * setuid and setgid bits if the process is not being run
798 * by root. This keeps people from modifying setuid and
799 * setgid binaries.
800 */
801
802 if (((xip->i_d.di_mode & S_ISUID) ||
803 ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
804 (S_ISGID | S_IXGRP))) &&
805 !capable(CAP_FSETID)) {
806 error = xfs_write_clear_setuid(xip);
807 if (likely(!error))
Josef "Jeff" Sipeke678fb02006-12-08 02:36:49 -0800808 error = -remove_suid(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (unlikely(error)) {
810 xfs_iunlock(xip, iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100811 goto out_unlock_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813 }
814
815retry:
816 /* We can write back this queue in page reclaim */
817 current->backing_dev_info = mapping->backing_dev_info;
818
819 if ((ioflags & IO_ISDIRECT)) {
820 if (need_flush) {
821 xfs_inval_cached_trace(io, pos, -1,
822 ctooff(offtoct(pos)), -1);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000823 bhv_vop_flushinval_pages(vp, ctooff(offtoct(pos)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 -1, FI_REMAPF_LOCKED);
825 }
826
Nathan Scotta13828b2006-03-17 17:26:14 +1100827 if (need_i_mutex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 /* demote the lock now the cached pages are gone */
829 XFS_ILOCK_DEMOTE(mp, io, XFS_IOLOCK_EXCL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800830 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 iolock = XFS_IOLOCK_SHARED;
833 locktype = VRWLOCK_WRITE_DIRECT;
Nathan Scotta13828b2006-03-17 17:26:14 +1100834 need_i_mutex = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
837 xfs_rw_enter_trace(XFS_DIOWR_ENTER, io, (void *)iovp, segs,
838 *offset, ioflags);
839 ret = generic_file_direct_write(iocb, iovp,
840 &segs, pos, offset, count, ocount);
841
842 /*
843 * direct-io write to a hole: fall through to buffered I/O
844 * for completing the rest of the request.
845 */
846 if (ret >= 0 && ret != count) {
847 XFS_STATS_ADD(xs_write_bytes, ret);
848
849 pos += ret;
850 count -= ret;
851
Nathan Scotta13828b2006-03-17 17:26:14 +1100852 need_i_mutex = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 ioflags &= ~IO_ISDIRECT;
854 xfs_iunlock(xip, iolock);
855 goto relock;
856 }
857 } else {
858 xfs_rw_enter_trace(XFS_WRITE_ENTER, io, (void *)iovp, segs,
859 *offset, ioflags);
860 ret = generic_file_buffered_write(iocb, iovp, segs,
861 pos, offset, count, ret);
862 }
863
864 current->backing_dev_info = NULL;
865
866 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
867 ret = wait_on_sync_kiocb(iocb);
868
869 if ((ret == -ENOSPC) &&
870 DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_NOSPACE) &&
871 !(ioflags & IO_INVIS)) {
872
873 xfs_rwunlock(bdp, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100874 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800875 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp,
877 DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL,
878 0, 0, 0); /* Delay flag intentionally unused */
879 if (error)
Dean Roehriche1a40fa2005-06-22 10:20:44 +1000880 goto out_nounlocks;
Nathan Scotta13828b2006-03-17 17:26:14 +1100881 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800882 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 xfs_rwlock(bdp, locktype);
884 pos = xip->i_d.di_size;
885 ret = 0;
886 goto retry;
887 }
888
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100889 isize = i_size_read(inode);
890 if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
891 *offset = isize;
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (*offset > xip->i_d.di_size) {
894 xfs_ilock(xip, XFS_ILOCK_EXCL);
895 if (*offset > xip->i_d.di_size) {
896 xip->i_d.di_size = *offset;
897 i_size_write(inode, *offset);
898 xip->i_update_core = 1;
899 xip->i_update_size = 1;
900 }
901 xfs_iunlock(xip, XFS_ILOCK_EXCL);
902 }
903
904 error = -ret;
905 if (ret <= 0)
906 goto out_unlock_internal;
907
908 XFS_STATS_ADD(xs_write_bytes, ret);
909
910 /* Handle various SYNC-type writes */
911 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
Nathan Scott1e69dd02006-06-19 08:39:53 +1000912 error = xfs_write_sync_logforce(mp, xip);
913 if (error)
914 goto out_unlock_internal;
Nathan Scotta13828b2006-03-17 17:26:14 +1100915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 xfs_rwunlock(bdp, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100917 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800918 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 error = sync_page_range(inode, mapping, pos, ret);
921 if (!error)
922 error = ret;
923 return error;
924 }
925
926 out_unlock_internal:
927 xfs_rwunlock(bdp, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100928 out_unlock_mutex:
929 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800930 mutex_unlock(&inode->i_mutex);
Dean Roehriche1a40fa2005-06-22 10:20:44 +1000931 out_nounlocks:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return -error;
933}
934
935/*
936 * All xfs metadata buffers except log state machine buffers
937 * get this attached as their b_bdstrat callback function.
938 * This is so that we can catch a buffer
939 * after prematurely unpinning it to forcibly shutdown the filesystem.
940 */
941int
942xfs_bdstrat_cb(struct xfs_buf *bp)
943{
944 xfs_mount_t *mp;
945
946 mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
947 if (!XFS_FORCED_SHUTDOWN(mp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100948 xfs_buf_iorequest(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return 0;
950 } else {
951 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
952 /*
953 * Metadata write that didn't get logged but
954 * written delayed anyway. These aren't associated
955 * with a transaction, and can be ignored.
956 */
957 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
958 (XFS_BUF_ISREAD(bp)) == 0)
959 return (xfs_bioerror_relse(bp));
960 else
961 return (xfs_bioerror(bp));
962 }
963}
964
965
966int
967xfs_bmap(bhv_desc_t *bdp,
968 xfs_off_t offset,
969 ssize_t count,
970 int flags,
971 xfs_iomap_t *iomapp,
972 int *niomaps)
973{
974 xfs_inode_t *ip = XFS_BHVTOI(bdp);
975 xfs_iocore_t *io = &ip->i_iocore;
976
977 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
978 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
979 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
980
981 return xfs_iomap(io, offset, count, flags, iomapp, niomaps);
982}
983
984/*
985 * Wrapper around bdstrat so that we can stop data
986 * from going to disk in case we are shutting down the filesystem.
987 * Typically user data goes thru this path; one of the exceptions
988 * is the superblock.
989 */
990int
991xfsbdstrat(
992 struct xfs_mount *mp,
993 struct xfs_buf *bp)
994{
995 ASSERT(mp);
996 if (!XFS_FORCED_SHUTDOWN(mp)) {
997 /* Grio redirection would go here
998 * if (XFS_BUF_IS_GRIO(bp)) {
999 */
1000
Nathan Scottce8e9222006-01-11 15:39:08 +11001001 xfs_buf_iorequest(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return 0;
1003 }
1004
1005 xfs_buftrace("XFSBDSTRAT IOERROR", bp);
1006 return (xfs_bioerror_relse(bp));
1007}
1008
1009/*
1010 * If the underlying (data/log/rt) device is readonly, there are some
1011 * operations that cannot proceed.
1012 */
1013int
1014xfs_dev_is_read_only(
1015 xfs_mount_t *mp,
1016 char *message)
1017{
1018 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
1019 xfs_readonly_buftarg(mp->m_logdev_targp) ||
1020 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
1021 cmn_err(CE_NOTE,
1022 "XFS: %s required on read-only device.", message);
1023 cmn_err(CE_NOTE,
1024 "XFS: write access unavailable, cannot proceed.");
1025 return EROFS;
1026 }
1027 return 0;
1028}