blob: 3e76c5c9dda1ad1cc1a508ca7c738e6aca9fa89a [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"
26#include "xfs_dir.h"
27#include "xfs_dir2.h"
28#include "xfs_alloc.h"
29#include "xfs_dmapi.h"
30#include "xfs_quota.h"
31#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dir_sf.h"
36#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_dinode.h"
39#include "xfs_inode.h"
40#include "xfs_bmap.h"
Nathan Scotta844f452005-11-02 14:38:42 +110041#include "xfs_btree.h"
42#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_rtalloc.h"
44#include "xfs_error.h"
45#include "xfs_itable.h"
46#include "xfs_rw.h"
47#include "xfs_acl.h"
48#include "xfs_cap.h"
49#include "xfs_mac.h"
50#include "xfs_attr.h"
51#include "xfs_inode_item.h"
52#include "xfs_buf_item.h"
53#include "xfs_utils.h"
54#include "xfs_iomap.h"
55
56#include <linux/capability.h>
57#include <linux/writeback.h>
58
59
60#if defined(XFS_RW_TRACE)
61void
62xfs_rw_enter_trace(
63 int tag,
64 xfs_iocore_t *io,
65 void *data,
66 size_t segs,
67 loff_t offset,
68 int ioflags)
69{
70 xfs_inode_t *ip = XFS_IO_INODE(io);
71
72 if (ip->i_rwtrace == NULL)
73 return;
74 ktrace_enter(ip->i_rwtrace,
75 (void *)(unsigned long)tag,
76 (void *)ip,
77 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
78 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
79 (void *)data,
80 (void *)((unsigned long)segs),
81 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
82 (void *)((unsigned long)(offset & 0xffffffff)),
83 (void *)((unsigned long)ioflags),
84 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
85 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
Yingping Luf1fdc842006-03-22 12:44:15 +110086 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 (void *)NULL,
88 (void *)NULL,
89 (void *)NULL,
90 (void *)NULL);
91}
92
93void
94xfs_inval_cached_trace(
95 xfs_iocore_t *io,
96 xfs_off_t offset,
97 xfs_off_t len,
98 xfs_off_t first,
99 xfs_off_t last)
100{
101 xfs_inode_t *ip = XFS_IO_INODE(io);
102
103 if (ip->i_rwtrace == NULL)
104 return;
105 ktrace_enter(ip->i_rwtrace,
106 (void *)(__psint_t)XFS_INVAL_CACHED,
107 (void *)ip,
108 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
109 (void *)((unsigned long)(offset & 0xffffffff)),
110 (void *)((unsigned long)((len >> 32) & 0xffffffff)),
111 (void *)((unsigned long)(len & 0xffffffff)),
112 (void *)((unsigned long)((first >> 32) & 0xffffffff)),
113 (void *)((unsigned long)(first & 0xffffffff)),
114 (void *)((unsigned long)((last >> 32) & 0xffffffff)),
115 (void *)((unsigned long)(last & 0xffffffff)),
Yingping Luf1fdc842006-03-22 12:44:15 +1100116 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 (void *)NULL,
118 (void *)NULL,
119 (void *)NULL,
120 (void *)NULL,
121 (void *)NULL);
122}
123#endif
124
125/*
126 * xfs_iozero
127 *
128 * xfs_iozero clears the specified range of buffer supplied,
129 * and marks all the affected blocks as valid and modified. If
130 * an affected block is not allocated, it will be allocated. If
131 * an affected block is not completely overwritten, and is not
132 * valid before the operation, it will be read from disk before
133 * being partially zeroed.
134 */
135STATIC int
136xfs_iozero(
137 struct inode *ip, /* inode */
138 loff_t pos, /* offset in file */
139 size_t count, /* size of data to zero */
140 loff_t end_size) /* max file size to set */
141{
142 unsigned bytes;
143 struct page *page;
144 struct address_space *mapping;
145 char *kaddr;
146 int status;
147
148 mapping = ip->i_mapping;
149 do {
150 unsigned long index, offset;
151
152 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
153 index = pos >> PAGE_CACHE_SHIFT;
154 bytes = PAGE_CACHE_SIZE - offset;
155 if (bytes > count)
156 bytes = count;
157
158 status = -ENOMEM;
159 page = grab_cache_page(mapping, index);
160 if (!page)
161 break;
162
163 kaddr = kmap(page);
164 status = mapping->a_ops->prepare_write(NULL, page, offset,
165 offset + bytes);
166 if (status) {
167 goto unlock;
168 }
169
170 memset((void *) (kaddr + offset), 0, bytes);
171 flush_dcache_page(page);
172 status = mapping->a_ops->commit_write(NULL, page, offset,
173 offset + bytes);
174 if (!status) {
175 pos += bytes;
176 count -= bytes;
177 if (pos > i_size_read(ip))
178 i_size_write(ip, pos < end_size ? pos : end_size);
179 }
180
181unlock:
182 kunmap(page);
183 unlock_page(page);
184 page_cache_release(page);
185 if (status)
186 break;
187 } while (count);
188
189 return (-status);
190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192ssize_t /* bytes read, or (-) error */
193xfs_read(
194 bhv_desc_t *bdp,
195 struct kiocb *iocb,
196 const struct iovec *iovp,
197 unsigned int segs,
198 loff_t *offset,
199 int ioflags,
200 cred_t *credp)
201{
202 struct file *file = iocb->ki_filp;
203 struct inode *inode = file->f_mapping->host;
204 size_t size = 0;
205 ssize_t ret;
206 xfs_fsize_t n;
207 xfs_inode_t *ip;
208 xfs_mount_t *mp;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000209 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 unsigned long seg;
211
212 ip = XFS_BHVTOI(bdp);
213 vp = BHV_TO_VNODE(bdp);
214 mp = ip->i_mount;
215
216 XFS_STATS_INC(xs_read_calls);
217
218 /* START copy & waste from filemap.c */
219 for (seg = 0; seg < segs; seg++) {
220 const struct iovec *iv = &iovp[seg];
221
222 /*
223 * If any segment has a negative length, or the cumulative
224 * length ever wraps negative then return -EINVAL.
225 */
226 size += iv->iov_len;
227 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
228 return XFS_ERROR(-EINVAL);
229 }
230 /* END copy & waste from filemap.c */
231
232 if (unlikely(ioflags & IO_ISDIRECT)) {
233 xfs_buftarg_t *target =
234 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
235 mp->m_rtdev_targp : mp->m_ddev_targp;
Nathan Scottce8e9222006-01-11 15:39:08 +1100236 if ((*offset & target->bt_smask) ||
237 (size & target->bt_smask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (*offset == ip->i_d.di_size) {
239 return (0);
240 }
241 return -XFS_ERROR(EINVAL);
242 }
243 }
244
245 n = XFS_MAXIOFFSET(mp) - *offset;
246 if ((n <= 0) || (size == 0))
247 return 0;
248
249 if (n < size)
250 size = n;
251
Nathan Scotta13828b2006-03-17 17:26:14 +1100252 if (XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 if (unlikely(ioflags & IO_ISDIRECT))
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800256 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 xfs_ilock(ip, XFS_IOLOCK_SHARED);
258
259 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ) &&
260 !(ioflags & IO_INVIS)) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000261 bhv_vrwlock_t locktype = VRWLOCK_READ;
Dean Roehriche1a40fa2005-06-22 10:20:44 +1000262 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ,
265 BHV_TO_VNODE(bdp), *offset, size,
Dean Roehriche1a40fa2005-06-22 10:20:44 +1000266 dmflags, &locktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (ret) {
268 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Nathan Scotta13828b2006-03-17 17:26:14 +1100269 goto unlock_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271 }
272
Nathan Scott9cea2362006-03-17 17:26:41 +1100273 if (unlikely((ioflags & IO_ISDIRECT) && VN_CACHED(vp)))
Nathan Scott67fcaa72006-06-09 17:00:52 +1000274 bhv_vop_flushinval_pages(vp, ctooff(offtoct(*offset)),
Nathan Scott9cea2362006-03-17 17:26:41 +1100275 -1, FI_REMAPF_LOCKED);
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 xfs_rw_enter_trace(XFS_READ_ENTER, &ip->i_iocore,
278 (void *)iovp, segs, *offset, ioflags);
279 ret = __generic_file_aio_read(iocb, iovp, segs, offset);
280 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
281 ret = wait_on_sync_kiocb(iocb);
282 if (ret > 0)
283 XFS_STATS_ADD(xs_read_bytes, ret);
284
285 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
286
Nathan Scotta13828b2006-03-17 17:26:14 +1100287unlock_mutex:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (unlikely(ioflags & IO_ISDIRECT))
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800289 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return ret;
291}
292
293ssize_t
294xfs_sendfile(
295 bhv_desc_t *bdp,
296 struct file *filp,
297 loff_t *offset,
298 int ioflags,
299 size_t count,
300 read_actor_t actor,
301 void *target,
302 cred_t *credp)
303{
Nathan Scott1b895842006-03-31 13:08:59 +1000304 xfs_inode_t *ip = XFS_BHVTOI(bdp);
305 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 XFS_STATS_INC(xs_read_calls);
Nathan Scott1b895842006-03-31 13:08:59 +1000309 if (XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return -EIO;
311
312 xfs_ilock(ip, XFS_IOLOCK_SHARED);
313
Nathan Scott1b895842006-03-31 13:08:59 +1000314 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_READ) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 (!(ioflags & IO_INVIS))) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000316 bhv_vrwlock_t locktype = VRWLOCK_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 int error;
318
Nathan Scott1b895842006-03-31 13:08:59 +1000319 error = XFS_SEND_DATA(mp, DM_EVENT_READ, BHV_TO_VNODE(bdp),
320 *offset, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 FILP_DELAY_FLAG(filp), &locktype);
322 if (error) {
323 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
324 return -error;
325 }
326 }
327 xfs_rw_enter_trace(XFS_SENDFILE_ENTER, &ip->i_iocore,
328 (void *)(unsigned long)target, count, *offset, ioflags);
329 ret = generic_file_sendfile(filp, offset, count, actor, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (ret > 0)
331 XFS_STATS_ADD(xs_read_bytes, ret);
332
Nathan Scott1b895842006-03-31 13:08:59 +1000333 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
334 return ret;
335}
336
337ssize_t
338xfs_splice_read(
339 bhv_desc_t *bdp,
340 struct file *infilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200341 loff_t *ppos,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200342 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000343 size_t count,
344 int flags,
345 int ioflags,
346 cred_t *credp)
347{
348 xfs_inode_t *ip = XFS_BHVTOI(bdp);
349 xfs_mount_t *mp = ip->i_mount;
350 ssize_t ret;
351
352 XFS_STATS_INC(xs_read_calls);
353 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
354 return -EIO;
355
356 xfs_ilock(ip, XFS_IOLOCK_SHARED);
357
358 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_READ) &&
359 (!(ioflags & IO_INVIS))) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000360 bhv_vrwlock_t locktype = VRWLOCK_READ;
Nathan Scott1b895842006-03-31 13:08:59 +1000361 int error;
362
363 error = XFS_SEND_DATA(mp, DM_EVENT_READ, BHV_TO_VNODE(bdp),
Jens Axboecbb7e572006-04-11 14:57:50 +0200364 *ppos, count,
Nathan Scott1b895842006-03-31 13:08:59 +1000365 FILP_DELAY_FLAG(infilp), &locktype);
366 if (error) {
367 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
368 return -error;
369 }
370 }
371 xfs_rw_enter_trace(XFS_SPLICE_READ_ENTER, &ip->i_iocore,
Jens Axboecbb7e572006-04-11 14:57:50 +0200372 pipe, count, *ppos, ioflags);
373 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
Nathan Scott1b895842006-03-31 13:08:59 +1000374 if (ret > 0)
375 XFS_STATS_ADD(xs_read_bytes, ret);
376
377 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
378 return ret;
379}
380
381ssize_t
382xfs_splice_write(
383 bhv_desc_t *bdp,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200384 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000385 struct file *outfilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200386 loff_t *ppos,
Nathan Scott1b895842006-03-31 13:08:59 +1000387 size_t count,
388 int flags,
389 int ioflags,
390 cred_t *credp)
391{
392 xfs_inode_t *ip = XFS_BHVTOI(bdp);
393 xfs_mount_t *mp = ip->i_mount;
394 ssize_t ret;
395
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
421 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return ret;
423}
424
425/*
426 * This routine is called to handle zeroing any space in the last
427 * block of the file that is beyond the EOF. We do this since the
428 * size is being increased without writing anything to that block
429 * and we don't want anyone to read the garbage on the disk.
430 */
431STATIC int /* error (positive) */
432xfs_zero_last_block(
433 struct inode *ip,
434 xfs_iocore_t *io,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 xfs_fsize_t isize,
436 xfs_fsize_t end_size)
437{
438 xfs_fileoff_t last_fsb;
Nathan Scott1b895842006-03-31 13:08:59 +1000439 xfs_mount_t *mp = io->io_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 int nimaps;
441 int zero_offset;
442 int zero_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 int error = 0;
444 xfs_bmbt_irec_t imap;
445 loff_t loff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 ASSERT(ismrlocked(io->io_lock, MR_UPDATE) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Eric Sandeen24ee8082006-01-11 15:34:32 +1100449 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
450 if (zero_offset == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /*
452 * There are no extra bytes in the last block on disk to
453 * zero, so return.
454 */
455 return 0;
456 }
457
458 last_fsb = XFS_B_TO_FSBT(mp, isize);
459 nimaps = 1;
460 error = XFS_BMAPI(mp, NULL, io, last_fsb, 1, 0, NULL, 0, &imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000461 &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (error) {
463 return error;
464 }
465 ASSERT(nimaps > 0);
466 /*
467 * If the block underlying isize is just a hole, then there
468 * is nothing to zero.
469 */
470 if (imap.br_startblock == HOLESTARTBLOCK) {
471 return 0;
472 }
473 /*
474 * Zero the part of the last block beyond the EOF, and write it
475 * out sync. We need to drop the ilock while we do this so we
476 * don't deadlock when the buffer cache calls back to us.
477 */
478 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
Nathan Scott1b895842006-03-31 13:08:59 +1000479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 loff = XFS_FSB_TO_B(mp, last_fsb);
Eric Sandeen24ee8082006-01-11 15:34:32 +1100481 zero_len = mp->m_sb.sb_blocksize - zero_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 error = xfs_iozero(ip, loff + zero_offset, zero_len, end_size);
483
484 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
485 ASSERT(error >= 0);
486 return error;
487}
488
489/*
490 * Zero any on disk space between the current EOF and the new,
491 * larger EOF. This handles the normal case of zeroing the remainder
492 * of the last block in the file and the unusual case of zeroing blocks
493 * out beyond the size of the file. This second case only happens
494 * with fixed size extents and when the system crashes before the inode
495 * size was updated but after blocks were allocated. If fill is set,
496 * then any holes in the range are filled and zeroed. If not, the holes
497 * are left alone as holes.
498 */
499
500int /* error (positive) */
501xfs_zero_eof(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000502 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 xfs_iocore_t *io,
504 xfs_off_t offset, /* starting I/O offset */
505 xfs_fsize_t isize, /* current inode size */
506 xfs_fsize_t end_size) /* terminal inode size */
507{
Nathan Scottec86dc02006-03-17 17:25:36 +1100508 struct inode *ip = vn_to_inode(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 xfs_fileoff_t start_zero_fsb;
510 xfs_fileoff_t end_zero_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 xfs_fileoff_t zero_count_fsb;
512 xfs_fileoff_t last_fsb;
Nathan Scott1b895842006-03-31 13:08:59 +1000513 xfs_mount_t *mp = io->io_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 int nimaps;
515 int error = 0;
516 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
519 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
Eric Sandeen24ee8082006-01-11 15:34:32 +1100520 ASSERT(offset > isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /*
523 * First handle zeroing the block on which isize resides.
524 * We only zero a part of that block so it is handled specially.
525 */
Eric Sandeen24ee8082006-01-11 15:34:32 +1100526 error = xfs_zero_last_block(ip, io, isize, end_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (error) {
528 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
529 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
530 return error;
531 }
532
533 /*
534 * Calculate the range between the new size and the old
535 * where blocks needing to be zeroed may exist. To get the
536 * block where the last byte in the file currently resides,
537 * we need to subtract one from the size and truncate back
538 * to a block boundary. We subtract 1 in case the size is
539 * exactly on a block boundary.
540 */
541 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
542 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
543 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
544 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
545 if (last_fsb == end_zero_fsb) {
546 /*
547 * The size was only incremented on its last block.
548 * We took care of that above, so just return.
549 */
550 return 0;
551 }
552
553 ASSERT(start_zero_fsb <= end_zero_fsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 while (start_zero_fsb <= end_zero_fsb) {
555 nimaps = 1;
556 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
557 error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000558 0, NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (error) {
560 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
561 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
562 return error;
563 }
564 ASSERT(nimaps > 0);
565
566 if (imap.br_state == XFS_EXT_UNWRITTEN ||
567 imap.br_startblock == HOLESTARTBLOCK) {
568 /*
569 * This loop handles initializing pages that were
570 * partially initialized by the code below this
571 * loop. It basically zeroes the part of the page
572 * that sits on a hole and sets the page as P_HOLE
573 * and calls remapf if it is a mapped file.
574 */
Eric Sandeen24ee8082006-01-11 15:34:32 +1100575 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
577 continue;
578 }
579
580 /*
Nathan Scott3d80ede2006-06-09 14:57:30 +1000581 * There are blocks we need to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 * Drop the inode lock while we're doing the I/O.
583 * We'll still have the iolock to protect us.
584 */
585 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
586
Eric Sandeen24ee8082006-01-11 15:34:32 +1100587 error = xfs_iozero(ip,
588 XFS_FSB_TO_B(mp, start_zero_fsb),
Nathan Scott3d80ede2006-06-09 14:57:30 +1000589 XFS_FSB_TO_B(mp, imap.br_blockcount),
Eric Sandeen24ee8082006-01-11 15:34:32 +1100590 end_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (error) {
592 goto out_lock;
593 }
594
Nathan Scott3d80ede2006-06-09 14:57:30 +1000595 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
597
598 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
599 }
600
601 return 0;
602
603out_lock:
604
605 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
606 ASSERT(error >= 0);
607 return error;
608}
609
610ssize_t /* bytes written, or (-) error */
611xfs_write(
612 bhv_desc_t *bdp,
613 struct kiocb *iocb,
614 const struct iovec *iovp,
615 unsigned int nsegs,
616 loff_t *offset,
617 int ioflags,
618 cred_t *credp)
619{
620 struct file *file = iocb->ki_filp;
621 struct address_space *mapping = file->f_mapping;
622 struct inode *inode = mapping->host;
623 unsigned long segs = nsegs;
624 xfs_inode_t *xip;
625 xfs_mount_t *mp;
626 ssize_t ret = 0, error = 0;
627 xfs_fsize_t isize, new_size;
628 xfs_iocore_t *io;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000629 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 unsigned long seg;
631 int iolock;
632 int eventsent = 0;
Nathan Scott8285fb52006-06-09 17:07:12 +1000633 bhv_vrwlock_t locktype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 size_t ocount = 0, count;
635 loff_t pos;
Nathan Scotta13828b2006-03-17 17:26:14 +1100636 int need_i_mutex = 1, need_flush = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 XFS_STATS_INC(xs_write_calls);
639
640 vp = BHV_TO_VNODE(bdp);
641 xip = XFS_BHVTOI(bdp);
642
643 for (seg = 0; seg < segs; seg++) {
644 const struct iovec *iv = &iovp[seg];
645
646 /*
647 * If any segment has a negative length, or the cumulative
648 * length ever wraps negative then return -EINVAL.
649 */
650 ocount += iv->iov_len;
651 if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
652 return -EINVAL;
653 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
654 continue;
655 if (seg == 0)
656 return -EFAULT;
657 segs = seg;
658 ocount -= iv->iov_len; /* This segment is no good */
659 break;
660 }
661
662 count = ocount;
663 pos = *offset;
664
665 if (count == 0)
666 return 0;
667
668 io = &xip->i_iocore;
669 mp = io->io_mount;
670
Nathan Scott34327e12006-06-09 17:11:55 +1000671 vfs_wait_for_freeze(vp->v_vfsp, SB_FREEZE_WRITE);
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (XFS_FORCED_SHUTDOWN(mp))
674 return -EIO;
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (ioflags & IO_ISDIRECT) {
677 xfs_buftarg_t *target =
678 (xip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
679 mp->m_rtdev_targp : mp->m_ddev_targp;
680
Nathan Scottce8e9222006-01-11 15:39:08 +1100681 if ((pos & target->bt_smask) || (count & target->bt_smask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return XFS_ERROR(-EINVAL);
683
684 if (!VN_CACHED(vp) && pos < i_size_read(inode))
Nathan Scotta13828b2006-03-17 17:26:14 +1100685 need_i_mutex = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 if (VN_CACHED(vp))
688 need_flush = 1;
689 }
690
691relock:
Nathan Scotta13828b2006-03-17 17:26:14 +1100692 if (need_i_mutex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 iolock = XFS_IOLOCK_EXCL;
694 locktype = VRWLOCK_WRITE;
695
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800696 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 } else {
698 iolock = XFS_IOLOCK_SHARED;
699 locktype = VRWLOCK_WRITE_DIRECT;
700 }
701
702 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
703
704 isize = i_size_read(inode);
705
706 if (file->f_flags & O_APPEND)
707 *offset = isize;
708
709start:
710 error = -generic_write_checks(file, &pos, &count,
711 S_ISBLK(inode->i_mode));
712 if (error) {
713 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100714 goto out_unlock_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716
717 new_size = pos + count;
718 if (new_size > isize)
719 io->io_new_size = new_size;
720
721 if ((DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_WRITE) &&
722 !(ioflags & IO_INVIS) && !eventsent)) {
723 loff_t savedsize = pos;
724 int dmflags = FILP_DELAY_FLAG(file);
725
Nathan Scotta13828b2006-03-17 17:26:14 +1100726 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800727 dmflags |= DM_FLAGS_IMUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 xfs_iunlock(xip, XFS_ILOCK_EXCL);
730 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,
731 pos, count,
732 dmflags, &locktype);
733 if (error) {
734 xfs_iunlock(xip, iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100735 goto out_unlock_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737 xfs_ilock(xip, XFS_ILOCK_EXCL);
738 eventsent = 1;
739
740 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000741 * The iolock was dropped and reacquired in XFS_SEND_DATA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 * so we have to recheck the size when appending.
743 * We will only "goto start;" once, since having sent the
744 * event prevents another call to XFS_SEND_DATA, which is
745 * what allows the size to change in the first place.
746 */
747 if ((file->f_flags & O_APPEND) && savedsize != isize) {
748 pos = isize = xip->i_d.di_size;
749 goto start;
750 }
751 }
752
Nathan Scott4aeb6642005-11-02 11:43:58 +1100753 if (likely(!(ioflags & IO_INVIS))) {
Christoph Hellwig870f4812006-01-09 20:52:01 -0800754 file_update_time(file);
Nathan Scott4aeb6642005-11-02 11:43:58 +1100755 xfs_ichgtime_fast(xip, inode,
756 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
759 /*
760 * If the offset is beyond the size of the file, we have a couple
761 * of things to do. First, if there is already space allocated
762 * we need to either create holes or zero the disk or ...
763 *
764 * If there is a page where the previous size lands, we need
765 * to zero it out up to the new size.
766 */
767
768 if (pos > isize) {
769 error = xfs_zero_eof(BHV_TO_VNODE(bdp), io, pos,
770 isize, pos + count);
771 if (error) {
772 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100773 goto out_unlock_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775 }
776 xfs_iunlock(xip, XFS_ILOCK_EXCL);
777
778 /*
779 * If we're writing the file then make sure to clear the
780 * setuid and setgid bits if the process is not being run
781 * by root. This keeps people from modifying setuid and
782 * setgid binaries.
783 */
784
785 if (((xip->i_d.di_mode & S_ISUID) ||
786 ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
787 (S_ISGID | S_IXGRP))) &&
788 !capable(CAP_FSETID)) {
789 error = xfs_write_clear_setuid(xip);
790 if (likely(!error))
791 error = -remove_suid(file->f_dentry);
792 if (unlikely(error)) {
793 xfs_iunlock(xip, iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100794 goto out_unlock_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796 }
797
798retry:
799 /* We can write back this queue in page reclaim */
800 current->backing_dev_info = mapping->backing_dev_info;
801
802 if ((ioflags & IO_ISDIRECT)) {
803 if (need_flush) {
804 xfs_inval_cached_trace(io, pos, -1,
805 ctooff(offtoct(pos)), -1);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000806 bhv_vop_flushinval_pages(vp, ctooff(offtoct(pos)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 -1, FI_REMAPF_LOCKED);
808 }
809
Nathan Scotta13828b2006-03-17 17:26:14 +1100810 if (need_i_mutex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /* demote the lock now the cached pages are gone */
812 XFS_ILOCK_DEMOTE(mp, io, XFS_IOLOCK_EXCL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800813 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 iolock = XFS_IOLOCK_SHARED;
816 locktype = VRWLOCK_WRITE_DIRECT;
Nathan Scotta13828b2006-03-17 17:26:14 +1100817 need_i_mutex = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819
820 xfs_rw_enter_trace(XFS_DIOWR_ENTER, io, (void *)iovp, segs,
821 *offset, ioflags);
822 ret = generic_file_direct_write(iocb, iovp,
823 &segs, pos, offset, count, ocount);
824
825 /*
826 * direct-io write to a hole: fall through to buffered I/O
827 * for completing the rest of the request.
828 */
829 if (ret >= 0 && ret != count) {
830 XFS_STATS_ADD(xs_write_bytes, ret);
831
832 pos += ret;
833 count -= ret;
834
Nathan Scotta13828b2006-03-17 17:26:14 +1100835 need_i_mutex = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 ioflags &= ~IO_ISDIRECT;
837 xfs_iunlock(xip, iolock);
838 goto relock;
839 }
840 } else {
841 xfs_rw_enter_trace(XFS_WRITE_ENTER, io, (void *)iovp, segs,
842 *offset, ioflags);
843 ret = generic_file_buffered_write(iocb, iovp, segs,
844 pos, offset, count, ret);
845 }
846
847 current->backing_dev_info = NULL;
848
849 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
850 ret = wait_on_sync_kiocb(iocb);
851
852 if ((ret == -ENOSPC) &&
853 DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_NOSPACE) &&
854 !(ioflags & IO_INVIS)) {
855
856 xfs_rwunlock(bdp, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100857 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800858 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp,
860 DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL,
861 0, 0, 0); /* Delay flag intentionally unused */
862 if (error)
Dean Roehriche1a40fa2005-06-22 10:20:44 +1000863 goto out_nounlocks;
Nathan Scotta13828b2006-03-17 17:26:14 +1100864 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800865 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 xfs_rwlock(bdp, locktype);
867 pos = xip->i_d.di_size;
868 ret = 0;
869 goto retry;
870 }
871
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100872 isize = i_size_read(inode);
873 if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
874 *offset = isize;
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (*offset > xip->i_d.di_size) {
877 xfs_ilock(xip, XFS_ILOCK_EXCL);
878 if (*offset > xip->i_d.di_size) {
879 xip->i_d.di_size = *offset;
880 i_size_write(inode, *offset);
881 xip->i_update_core = 1;
882 xip->i_update_size = 1;
883 }
884 xfs_iunlock(xip, XFS_ILOCK_EXCL);
885 }
886
887 error = -ret;
888 if (ret <= 0)
889 goto out_unlock_internal;
890
891 XFS_STATS_ADD(xs_write_bytes, ret);
892
893 /* Handle various SYNC-type writes */
894 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
895 /*
896 * If we're treating this as O_DSYNC and we have not updated the
897 * size, force the log.
898 */
899 if (!(mp->m_flags & XFS_MOUNT_OSYNCISOSYNC) &&
900 !(xip->i_update_size)) {
901 xfs_inode_log_item_t *iip = xip->i_itemp;
902
903 /*
904 * If an allocation transaction occurred
905 * without extending the size, then we have to force
906 * the log up the proper point to ensure that the
907 * allocation is permanent. We can't count on
908 * the fact that buffered writes lock out direct I/O
909 * writes - the direct I/O write could have extended
910 * the size nontransactionally, then finished before
911 * we started. xfs_write_file will think that the file
912 * didn't grow but the update isn't safe unless the
913 * size change is logged.
914 *
915 * Force the log if we've committed a transaction
916 * against the inode or if someone else has and
917 * the commit record hasn't gone to disk (e.g.
918 * the inode is pinned). This guarantees that
919 * all changes affecting the inode are permanent
920 * when we return.
921 */
922 if (iip && iip->ili_last_lsn) {
923 xfs_log_force(mp, iip->ili_last_lsn,
924 XFS_LOG_FORCE | XFS_LOG_SYNC);
925 } else if (xfs_ipincount(xip) > 0) {
926 xfs_log_force(mp, (xfs_lsn_t)0,
927 XFS_LOG_FORCE | XFS_LOG_SYNC);
928 }
929
930 } else {
931 xfs_trans_t *tp;
932
933 /*
934 * O_SYNC or O_DSYNC _with_ a size update are handled
935 * the same way.
936 *
937 * If the write was synchronous then we need to make
938 * sure that the inode modification time is permanent.
939 * We'll have updated the timestamp above, so here
940 * we use a synchronous transaction to log the inode.
941 * It's not fast, but it's necessary.
942 *
943 * If this a dsync write and the size got changed
944 * non-transactionally, then we need to ensure that
945 * the size change gets logged in a synchronous
946 * transaction.
947 */
948
949 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITE_SYNC);
950 if ((error = xfs_trans_reserve(tp, 0,
951 XFS_SWRITE_LOG_RES(mp),
952 0, 0, 0))) {
953 /* Transaction reserve failed */
954 xfs_trans_cancel(tp, 0);
955 } else {
956 /* Transaction reserve successful */
957 xfs_ilock(xip, XFS_ILOCK_EXCL);
958 xfs_trans_ijoin(tp, xip, XFS_ILOCK_EXCL);
959 xfs_trans_ihold(tp, xip);
960 xfs_trans_log_inode(tp, xip, XFS_ILOG_CORE);
961 xfs_trans_set_sync(tp);
962 error = xfs_trans_commit(tp, 0, NULL);
963 xfs_iunlock(xip, XFS_ILOCK_EXCL);
964 }
965 if (error)
966 goto out_unlock_internal;
967 }
Nathan Scotta13828b2006-03-17 17:26:14 +1100968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 xfs_rwunlock(bdp, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100970 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800971 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 error = sync_page_range(inode, mapping, pos, ret);
974 if (!error)
975 error = ret;
976 return error;
977 }
978
979 out_unlock_internal:
980 xfs_rwunlock(bdp, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100981 out_unlock_mutex:
982 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800983 mutex_unlock(&inode->i_mutex);
Dean Roehriche1a40fa2005-06-22 10:20:44 +1000984 out_nounlocks:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return -error;
986}
987
988/*
989 * All xfs metadata buffers except log state machine buffers
990 * get this attached as their b_bdstrat callback function.
991 * This is so that we can catch a buffer
992 * after prematurely unpinning it to forcibly shutdown the filesystem.
993 */
994int
995xfs_bdstrat_cb(struct xfs_buf *bp)
996{
997 xfs_mount_t *mp;
998
999 mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
1000 if (!XFS_FORCED_SHUTDOWN(mp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001001 xfs_buf_iorequest(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return 0;
1003 } else {
1004 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
1005 /*
1006 * Metadata write that didn't get logged but
1007 * written delayed anyway. These aren't associated
1008 * with a transaction, and can be ignored.
1009 */
1010 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
1011 (XFS_BUF_ISREAD(bp)) == 0)
1012 return (xfs_bioerror_relse(bp));
1013 else
1014 return (xfs_bioerror(bp));
1015 }
1016}
1017
1018
1019int
1020xfs_bmap(bhv_desc_t *bdp,
1021 xfs_off_t offset,
1022 ssize_t count,
1023 int flags,
1024 xfs_iomap_t *iomapp,
1025 int *niomaps)
1026{
1027 xfs_inode_t *ip = XFS_BHVTOI(bdp);
1028 xfs_iocore_t *io = &ip->i_iocore;
1029
1030 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
1031 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
1032 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
1033
1034 return xfs_iomap(io, offset, count, flags, iomapp, niomaps);
1035}
1036
1037/*
1038 * Wrapper around bdstrat so that we can stop data
1039 * from going to disk in case we are shutting down the filesystem.
1040 * Typically user data goes thru this path; one of the exceptions
1041 * is the superblock.
1042 */
1043int
1044xfsbdstrat(
1045 struct xfs_mount *mp,
1046 struct xfs_buf *bp)
1047{
1048 ASSERT(mp);
1049 if (!XFS_FORCED_SHUTDOWN(mp)) {
1050 /* Grio redirection would go here
1051 * if (XFS_BUF_IS_GRIO(bp)) {
1052 */
1053
Nathan Scottce8e9222006-01-11 15:39:08 +11001054 xfs_buf_iorequest(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return 0;
1056 }
1057
1058 xfs_buftrace("XFSBDSTRAT IOERROR", bp);
1059 return (xfs_bioerror_relse(bp));
1060}
1061
1062/*
1063 * If the underlying (data/log/rt) device is readonly, there are some
1064 * operations that cannot proceed.
1065 */
1066int
1067xfs_dev_is_read_only(
1068 xfs_mount_t *mp,
1069 char *message)
1070{
1071 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
1072 xfs_readonly_buftarg(mp->m_logdev_targp) ||
1073 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
1074 cmn_err(CE_NOTE,
1075 "XFS: %s required on read-only device.", message);
1076 cmn_err(CE_NOTE,
1077 "XFS: write access unavailable, cannot proceed.");
1078 return EROFS;
1079 }
1080 return 0;
1081}