[CONNECTOR]: Replace delayed work with usual work queue.
[linux-2.6/verdex.git] / fs / xfs / linux-2.6 / xfs_lrw.c
blob65e79b471d49570f825033d31843327e2d7c50f2
1 /*
2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_bit.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_trans.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #include "xfs_dir2.h"
27 #include "xfs_alloc.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_quota.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
38 #include "xfs_bmap.h"
39 #include "xfs_btree.h"
40 #include "xfs_ialloc.h"
41 #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"
54 #include <linux/capability.h>
55 #include <linux/writeback.h>
58 #if defined(XFS_RW_TRACE)
59 void
60 xfs_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)
68 xfs_inode_t *ip = XFS_IO_INODE(io);
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)),
84 (void *)((unsigned long)current_pid()),
85 (void *)NULL,
86 (void *)NULL,
87 (void *)NULL,
88 (void *)NULL);
91 void
92 xfs_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)
99 xfs_inode_t *ip = XFS_IO_INODE(io);
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)),
114 (void *)((unsigned long)current_pid()),
115 (void *)NULL,
116 (void *)NULL,
117 (void *)NULL,
118 (void *)NULL,
119 (void *)NULL);
121 #endif
124 * xfs_iozero
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.
133 STATIC int
134 xfs_iozero(
135 struct inode *ip, /* inode */
136 loff_t pos, /* offset in file */
137 size_t count, /* size of data to zero */
138 loff_t end_size) /* max file size to set */
140 unsigned bytes;
141 struct page *page;
142 struct address_space *mapping;
143 char *kaddr;
144 int status;
146 mapping = ip->i_mapping;
147 do {
148 unsigned long index, offset;
150 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
151 index = pos >> PAGE_CACHE_SHIFT;
152 bytes = PAGE_CACHE_SIZE - offset;
153 if (bytes > count)
154 bytes = count;
156 status = -ENOMEM;
157 page = grab_cache_page(mapping, index);
158 if (!page)
159 break;
161 kaddr = kmap(page);
162 status = mapping->a_ops->prepare_write(NULL, page, offset,
163 offset + bytes);
164 if (status) {
165 goto unlock;
168 memset((void *) (kaddr + offset), 0, bytes);
169 flush_dcache_page(page);
170 status = mapping->a_ops->commit_write(NULL, page, offset,
171 offset + bytes);
172 if (!status) {
173 pos += bytes;
174 count -= bytes;
175 if (pos > i_size_read(ip))
176 i_size_write(ip, pos < end_size ? pos : end_size);
179 unlock:
180 kunmap(page);
181 unlock_page(page);
182 page_cache_release(page);
183 if (status)
184 break;
185 } while (count);
187 return (-status);
190 ssize_t /* bytes read, or (-) error */
191 xfs_read(
192 bhv_desc_t *bdp,
193 struct kiocb *iocb,
194 const struct iovec *iovp,
195 unsigned int segs,
196 loff_t *offset,
197 int ioflags,
198 cred_t *credp)
200 struct file *file = iocb->ki_filp;
201 struct inode *inode = file->f_mapping->host;
202 size_t size = 0;
203 ssize_t ret;
204 xfs_fsize_t n;
205 xfs_inode_t *ip;
206 xfs_mount_t *mp;
207 bhv_vnode_t *vp;
208 unsigned long seg;
210 ip = XFS_BHVTOI(bdp);
211 vp = BHV_TO_VNODE(bdp);
212 mp = ip->i_mount;
214 XFS_STATS_INC(xs_read_calls);
216 /* START copy & waste from filemap.c */
217 for (seg = 0; seg < segs; seg++) {
218 const struct iovec *iv = &iovp[seg];
221 * If any segment has a negative length, or the cumulative
222 * length ever wraps negative then return -EINVAL.
224 size += iv->iov_len;
225 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
226 return XFS_ERROR(-EINVAL);
228 /* END copy & waste from filemap.c */
230 if (unlikely(ioflags & IO_ISDIRECT)) {
231 xfs_buftarg_t *target =
232 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
233 mp->m_rtdev_targp : mp->m_ddev_targp;
234 if ((*offset & target->bt_smask) ||
235 (size & target->bt_smask)) {
236 if (*offset == ip->i_d.di_size) {
237 return (0);
239 return -XFS_ERROR(EINVAL);
243 n = XFS_MAXIOFFSET(mp) - *offset;
244 if ((n <= 0) || (size == 0))
245 return 0;
247 if (n < size)
248 size = n;
250 if (XFS_FORCED_SHUTDOWN(mp))
251 return -EIO;
253 if (unlikely(ioflags & IO_ISDIRECT))
254 mutex_lock(&inode->i_mutex);
255 xfs_ilock(ip, XFS_IOLOCK_SHARED);
257 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ) &&
258 !(ioflags & IO_INVIS)) {
259 bhv_vrwlock_t locktype = VRWLOCK_READ;
260 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
262 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ,
263 BHV_TO_VNODE(bdp), *offset, size,
264 dmflags, &locktype);
265 if (ret) {
266 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
267 if (unlikely(ioflags & IO_ISDIRECT))
268 mutex_unlock(&inode->i_mutex);
269 return ret;
273 if (unlikely(ioflags & IO_ISDIRECT)) {
274 if (VN_CACHED(vp))
275 bhv_vop_flushinval_pages(vp, ctooff(offtoct(*offset)),
276 -1, FI_REMAPF_LOCKED);
277 mutex_unlock(&inode->i_mutex);
280 xfs_rw_enter_trace(XFS_READ_ENTER, &ip->i_iocore,
281 (void *)iovp, segs, *offset, ioflags);
283 iocb->ki_pos = *offset;
284 ret = generic_file_aio_read(iocb, iovp, segs, *offset);
285 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
286 ret = wait_on_sync_kiocb(iocb);
287 if (ret > 0)
288 XFS_STATS_ADD(xs_read_bytes, ret);
290 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
291 return ret;
294 ssize_t
295 xfs_sendfile(
296 bhv_desc_t *bdp,
297 struct file *filp,
298 loff_t *offset,
299 int ioflags,
300 size_t count,
301 read_actor_t actor,
302 void *target,
303 cred_t *credp)
305 xfs_inode_t *ip = XFS_BHVTOI(bdp);
306 xfs_mount_t *mp = ip->i_mount;
307 ssize_t ret;
309 XFS_STATS_INC(xs_read_calls);
310 if (XFS_FORCED_SHUTDOWN(mp))
311 return -EIO;
313 xfs_ilock(ip, XFS_IOLOCK_SHARED);
315 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_READ) &&
316 (!(ioflags & IO_INVIS))) {
317 bhv_vrwlock_t locktype = VRWLOCK_READ;
318 int error;
320 error = XFS_SEND_DATA(mp, DM_EVENT_READ, BHV_TO_VNODE(bdp),
321 *offset, count,
322 FILP_DELAY_FLAG(filp), &locktype);
323 if (error) {
324 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
325 return -error;
328 xfs_rw_enter_trace(XFS_SENDFILE_ENTER, &ip->i_iocore,
329 (void *)(unsigned long)target, count, *offset, ioflags);
330 ret = generic_file_sendfile(filp, offset, count, actor, target);
331 if (ret > 0)
332 XFS_STATS_ADD(xs_read_bytes, ret);
334 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
335 return ret;
338 ssize_t
339 xfs_splice_read(
340 bhv_desc_t *bdp,
341 struct file *infilp,
342 loff_t *ppos,
343 struct pipe_inode_info *pipe,
344 size_t count,
345 int flags,
346 int ioflags,
347 cred_t *credp)
349 xfs_inode_t *ip = XFS_BHVTOI(bdp);
350 xfs_mount_t *mp = ip->i_mount;
351 ssize_t ret;
353 XFS_STATS_INC(xs_read_calls);
354 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
355 return -EIO;
357 xfs_ilock(ip, XFS_IOLOCK_SHARED);
359 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_READ) &&
360 (!(ioflags & IO_INVIS))) {
361 bhv_vrwlock_t locktype = VRWLOCK_READ;
362 int error;
364 error = XFS_SEND_DATA(mp, DM_EVENT_READ, BHV_TO_VNODE(bdp),
365 *ppos, count,
366 FILP_DELAY_FLAG(infilp), &locktype);
367 if (error) {
368 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
369 return -error;
372 xfs_rw_enter_trace(XFS_SPLICE_READ_ENTER, &ip->i_iocore,
373 pipe, count, *ppos, ioflags);
374 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
375 if (ret > 0)
376 XFS_STATS_ADD(xs_read_bytes, ret);
378 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
379 return ret;
382 ssize_t
383 xfs_splice_write(
384 bhv_desc_t *bdp,
385 struct pipe_inode_info *pipe,
386 struct file *outfilp,
387 loff_t *ppos,
388 size_t count,
389 int flags,
390 int ioflags,
391 cred_t *credp)
393 xfs_inode_t *ip = XFS_BHVTOI(bdp);
394 xfs_mount_t *mp = ip->i_mount;
395 ssize_t ret;
396 struct inode *inode = outfilp->f_mapping->host;
397 xfs_fsize_t isize;
399 XFS_STATS_INC(xs_write_calls);
400 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
401 return -EIO;
403 xfs_ilock(ip, XFS_IOLOCK_EXCL);
405 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_WRITE) &&
406 (!(ioflags & IO_INVIS))) {
407 bhv_vrwlock_t locktype = VRWLOCK_WRITE;
408 int error;
410 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, BHV_TO_VNODE(bdp),
411 *ppos, count,
412 FILP_DELAY_FLAG(outfilp), &locktype);
413 if (error) {
414 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
415 return -error;
418 xfs_rw_enter_trace(XFS_SPLICE_WRITE_ENTER, &ip->i_iocore,
419 pipe, count, *ppos, ioflags);
420 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
421 if (ret > 0)
422 XFS_STATS_ADD(xs_write_bytes, ret);
424 isize = i_size_read(inode);
425 if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
426 *ppos = isize;
428 if (*ppos > ip->i_d.di_size) {
429 xfs_ilock(ip, XFS_ILOCK_EXCL);
430 if (*ppos > ip->i_d.di_size) {
431 ip->i_d.di_size = *ppos;
432 i_size_write(inode, *ppos);
433 ip->i_update_core = 1;
434 ip->i_update_size = 1;
436 xfs_iunlock(ip, XFS_ILOCK_EXCL);
438 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
439 return ret;
443 * This routine is called to handle zeroing any space in the last
444 * block of the file that is beyond the EOF. We do this since the
445 * size is being increased without writing anything to that block
446 * and we don't want anyone to read the garbage on the disk.
448 STATIC int /* error (positive) */
449 xfs_zero_last_block(
450 struct inode *ip,
451 xfs_iocore_t *io,
452 xfs_fsize_t isize,
453 xfs_fsize_t end_size)
455 xfs_fileoff_t last_fsb;
456 xfs_mount_t *mp = io->io_mount;
457 int nimaps;
458 int zero_offset;
459 int zero_len;
460 int error = 0;
461 xfs_bmbt_irec_t imap;
462 loff_t loff;
464 ASSERT(ismrlocked(io->io_lock, MR_UPDATE) != 0);
466 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
467 if (zero_offset == 0) {
469 * There are no extra bytes in the last block on disk to
470 * zero, so return.
472 return 0;
475 last_fsb = XFS_B_TO_FSBT(mp, isize);
476 nimaps = 1;
477 error = XFS_BMAPI(mp, NULL, io, last_fsb, 1, 0, NULL, 0, &imap,
478 &nimaps, NULL, NULL);
479 if (error) {
480 return error;
482 ASSERT(nimaps > 0);
484 * If the block underlying isize is just a hole, then there
485 * is nothing to zero.
487 if (imap.br_startblock == HOLESTARTBLOCK) {
488 return 0;
491 * Zero the part of the last block beyond the EOF, and write it
492 * out sync. We need to drop the ilock while we do this so we
493 * don't deadlock when the buffer cache calls back to us.
495 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
497 loff = XFS_FSB_TO_B(mp, last_fsb);
498 zero_len = mp->m_sb.sb_blocksize - zero_offset;
499 error = xfs_iozero(ip, loff + zero_offset, zero_len, end_size);
501 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
502 ASSERT(error >= 0);
503 return error;
507 * Zero any on disk space between the current EOF and the new,
508 * larger EOF. This handles the normal case of zeroing the remainder
509 * of the last block in the file and the unusual case of zeroing blocks
510 * out beyond the size of the file. This second case only happens
511 * with fixed size extents and when the system crashes before the inode
512 * size was updated but after blocks were allocated. If fill is set,
513 * then any holes in the range are filled and zeroed. If not, the holes
514 * are left alone as holes.
517 int /* error (positive) */
518 xfs_zero_eof(
519 bhv_vnode_t *vp,
520 xfs_iocore_t *io,
521 xfs_off_t offset, /* starting I/O offset */
522 xfs_fsize_t isize, /* current inode size */
523 xfs_fsize_t end_size) /* terminal inode size */
525 struct inode *ip = vn_to_inode(vp);
526 xfs_fileoff_t start_zero_fsb;
527 xfs_fileoff_t end_zero_fsb;
528 xfs_fileoff_t zero_count_fsb;
529 xfs_fileoff_t last_fsb;
530 xfs_mount_t *mp = io->io_mount;
531 int nimaps;
532 int error = 0;
533 xfs_bmbt_irec_t imap;
535 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
536 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
537 ASSERT(offset > isize);
540 * First handle zeroing the block on which isize resides.
541 * We only zero a part of that block so it is handled specially.
543 error = xfs_zero_last_block(ip, io, isize, end_size);
544 if (error) {
545 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
546 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
547 return error;
551 * Calculate the range between the new size and the old
552 * where blocks needing to be zeroed may exist. To get the
553 * block where the last byte in the file currently resides,
554 * we need to subtract one from the size and truncate back
555 * to a block boundary. We subtract 1 in case the size is
556 * exactly on a block boundary.
558 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
559 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
560 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
561 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
562 if (last_fsb == end_zero_fsb) {
564 * The size was only incremented on its last block.
565 * We took care of that above, so just return.
567 return 0;
570 ASSERT(start_zero_fsb <= end_zero_fsb);
571 while (start_zero_fsb <= end_zero_fsb) {
572 nimaps = 1;
573 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
574 error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb,
575 0, NULL, 0, &imap, &nimaps, NULL, NULL);
576 if (error) {
577 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
578 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
579 return error;
581 ASSERT(nimaps > 0);
583 if (imap.br_state == XFS_EXT_UNWRITTEN ||
584 imap.br_startblock == HOLESTARTBLOCK) {
586 * This loop handles initializing pages that were
587 * partially initialized by the code below this
588 * loop. It basically zeroes the part of the page
589 * that sits on a hole and sets the page as P_HOLE
590 * and calls remapf if it is a mapped file.
592 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
593 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
594 continue;
598 * There are blocks we need to zero.
599 * Drop the inode lock while we're doing the I/O.
600 * We'll still have the iolock to protect us.
602 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
604 error = xfs_iozero(ip,
605 XFS_FSB_TO_B(mp, start_zero_fsb),
606 XFS_FSB_TO_B(mp, imap.br_blockcount),
607 end_size);
608 if (error) {
609 goto out_lock;
612 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
613 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
615 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
618 return 0;
620 out_lock:
622 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
623 ASSERT(error >= 0);
624 return error;
627 ssize_t /* bytes written, or (-) error */
628 xfs_write(
629 bhv_desc_t *bdp,
630 struct kiocb *iocb,
631 const struct iovec *iovp,
632 unsigned int nsegs,
633 loff_t *offset,
634 int ioflags,
635 cred_t *credp)
637 struct file *file = iocb->ki_filp;
638 struct address_space *mapping = file->f_mapping;
639 struct inode *inode = mapping->host;
640 unsigned long segs = nsegs;
641 xfs_inode_t *xip;
642 xfs_mount_t *mp;
643 ssize_t ret = 0, error = 0;
644 xfs_fsize_t isize, new_size;
645 xfs_iocore_t *io;
646 bhv_vnode_t *vp;
647 unsigned long seg;
648 int iolock;
649 int eventsent = 0;
650 bhv_vrwlock_t locktype;
651 size_t ocount = 0, count;
652 loff_t pos;
653 int need_i_mutex = 1, need_flush = 0;
655 XFS_STATS_INC(xs_write_calls);
657 vp = BHV_TO_VNODE(bdp);
658 xip = XFS_BHVTOI(bdp);
660 for (seg = 0; seg < segs; seg++) {
661 const struct iovec *iv = &iovp[seg];
664 * If any segment has a negative length, or the cumulative
665 * length ever wraps negative then return -EINVAL.
667 ocount += iv->iov_len;
668 if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
669 return -EINVAL;
670 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
671 continue;
672 if (seg == 0)
673 return -EFAULT;
674 segs = seg;
675 ocount -= iv->iov_len; /* This segment is no good */
676 break;
679 count = ocount;
680 pos = *offset;
682 if (count == 0)
683 return 0;
685 io = &xip->i_iocore;
686 mp = io->io_mount;
688 vfs_wait_for_freeze(vp->v_vfsp, SB_FREEZE_WRITE);
690 if (XFS_FORCED_SHUTDOWN(mp))
691 return -EIO;
693 if (ioflags & IO_ISDIRECT) {
694 xfs_buftarg_t *target =
695 (xip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
696 mp->m_rtdev_targp : mp->m_ddev_targp;
698 if ((pos & target->bt_smask) || (count & target->bt_smask))
699 return XFS_ERROR(-EINVAL);
701 if (!VN_CACHED(vp) && pos < i_size_read(inode))
702 need_i_mutex = 0;
704 if (VN_CACHED(vp))
705 need_flush = 1;
708 relock:
709 if (need_i_mutex) {
710 iolock = XFS_IOLOCK_EXCL;
711 locktype = VRWLOCK_WRITE;
713 mutex_lock(&inode->i_mutex);
714 } else {
715 iolock = XFS_IOLOCK_SHARED;
716 locktype = VRWLOCK_WRITE_DIRECT;
719 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
721 isize = i_size_read(inode);
723 if (file->f_flags & O_APPEND)
724 *offset = isize;
726 start:
727 error = -generic_write_checks(file, &pos, &count,
728 S_ISBLK(inode->i_mode));
729 if (error) {
730 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
731 goto out_unlock_mutex;
734 new_size = pos + count;
735 if (new_size > isize)
736 io->io_new_size = new_size;
738 if ((DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_WRITE) &&
739 !(ioflags & IO_INVIS) && !eventsent)) {
740 loff_t savedsize = pos;
741 int dmflags = FILP_DELAY_FLAG(file);
743 if (need_i_mutex)
744 dmflags |= DM_FLAGS_IMUX;
746 xfs_iunlock(xip, XFS_ILOCK_EXCL);
747 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,
748 pos, count,
749 dmflags, &locktype);
750 if (error) {
751 xfs_iunlock(xip, iolock);
752 goto out_unlock_mutex;
754 xfs_ilock(xip, XFS_ILOCK_EXCL);
755 eventsent = 1;
758 * The iolock was dropped and reacquired in XFS_SEND_DATA
759 * so we have to recheck the size when appending.
760 * We will only "goto start;" once, since having sent the
761 * event prevents another call to XFS_SEND_DATA, which is
762 * what allows the size to change in the first place.
764 if ((file->f_flags & O_APPEND) && savedsize != isize) {
765 pos = isize = xip->i_d.di_size;
766 goto start;
770 if (likely(!(ioflags & IO_INVIS))) {
771 file_update_time(file);
772 xfs_ichgtime_fast(xip, inode,
773 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
777 * If the offset is beyond the size of the file, we have a couple
778 * of things to do. First, if there is already space allocated
779 * we need to either create holes or zero the disk or ...
781 * If there is a page where the previous size lands, we need
782 * to zero it out up to the new size.
785 if (pos > isize) {
786 error = xfs_zero_eof(BHV_TO_VNODE(bdp), io, pos,
787 isize, pos + count);
788 if (error) {
789 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
790 goto out_unlock_mutex;
793 xfs_iunlock(xip, XFS_ILOCK_EXCL);
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.
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))
808 error = -remove_suid(file->f_path.dentry);
809 if (unlikely(error)) {
810 xfs_iunlock(xip, iolock);
811 goto out_unlock_mutex;
815 retry:
816 /* We can write back this queue in page reclaim */
817 current->backing_dev_info = mapping->backing_dev_info;
819 if ((ioflags & IO_ISDIRECT)) {
820 if (need_flush) {
821 xfs_inval_cached_trace(io, pos, -1,
822 ctooff(offtoct(pos)), -1);
823 bhv_vop_flushinval_pages(vp, ctooff(offtoct(pos)),
824 -1, FI_REMAPF_LOCKED);
827 if (need_i_mutex) {
828 /* demote the lock now the cached pages are gone */
829 XFS_ILOCK_DEMOTE(mp, io, XFS_IOLOCK_EXCL);
830 mutex_unlock(&inode->i_mutex);
832 iolock = XFS_IOLOCK_SHARED;
833 locktype = VRWLOCK_WRITE_DIRECT;
834 need_i_mutex = 0;
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);
843 * direct-io write to a hole: fall through to buffered I/O
844 * for completing the rest of the request.
846 if (ret >= 0 && ret != count) {
847 XFS_STATS_ADD(xs_write_bytes, ret);
849 pos += ret;
850 count -= ret;
852 need_i_mutex = 1;
853 ioflags &= ~IO_ISDIRECT;
854 xfs_iunlock(xip, iolock);
855 goto relock;
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);
864 current->backing_dev_info = NULL;
866 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
867 ret = wait_on_sync_kiocb(iocb);
869 if ((ret == -ENOSPC) &&
870 DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_NOSPACE) &&
871 !(ioflags & IO_INVIS)) {
873 xfs_rwunlock(bdp, locktype);
874 if (need_i_mutex)
875 mutex_unlock(&inode->i_mutex);
876 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)
880 goto out_nounlocks;
881 if (need_i_mutex)
882 mutex_lock(&inode->i_mutex);
883 xfs_rwlock(bdp, locktype);
884 pos = xip->i_d.di_size;
885 ret = 0;
886 goto retry;
889 isize = i_size_read(inode);
890 if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
891 *offset = isize;
893 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;
901 xfs_iunlock(xip, XFS_ILOCK_EXCL);
904 error = -ret;
905 if (ret <= 0)
906 goto out_unlock_internal;
908 XFS_STATS_ADD(xs_write_bytes, ret);
910 /* Handle various SYNC-type writes */
911 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
912 error = xfs_write_sync_logforce(mp, xip);
913 if (error)
914 goto out_unlock_internal;
916 xfs_rwunlock(bdp, locktype);
917 if (need_i_mutex)
918 mutex_unlock(&inode->i_mutex);
920 error = sync_page_range(inode, mapping, pos, ret);
921 if (!error)
922 error = ret;
923 return error;
926 out_unlock_internal:
927 xfs_rwunlock(bdp, locktype);
928 out_unlock_mutex:
929 if (need_i_mutex)
930 mutex_unlock(&inode->i_mutex);
931 out_nounlocks:
932 return -error;
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.
942 xfs_bdstrat_cb(struct xfs_buf *bp)
944 xfs_mount_t *mp;
946 mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
947 if (!XFS_FORCED_SHUTDOWN(mp)) {
948 xfs_buf_iorequest(bp);
949 return 0;
950 } else {
951 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
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.
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));
967 xfs_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)
974 xfs_inode_t *ip = XFS_BHVTOI(bdp);
975 xfs_iocore_t *io = &ip->i_iocore;
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));
981 return xfs_iomap(io, offset, count, flags, iomapp, niomaps);
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.
991 xfsbdstrat(
992 struct xfs_mount *mp,
993 struct xfs_buf *bp)
995 ASSERT(mp);
996 if (!XFS_FORCED_SHUTDOWN(mp)) {
997 /* Grio redirection would go here
998 * if (XFS_BUF_IS_GRIO(bp)) {
1001 xfs_buf_iorequest(bp);
1002 return 0;
1005 xfs_buftrace("XFSBDSTRAT IOERROR", bp);
1006 return (xfs_bioerror_relse(bp));
1010 * If the underlying (data/log/rt) device is readonly, there are some
1011 * operations that cannot proceed.
1014 xfs_dev_is_read_only(
1015 xfs_mount_t *mp,
1016 char *message)
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;
1027 return 0;