2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
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
23 #include "xfs_trans.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"
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"
47 #include "xfs_inode_item.h"
48 #include "xfs_buf_item.h"
49 #include "xfs_utils.h"
50 #include "xfs_iomap.h"
52 #include <linux/capability.h>
53 #include <linux/writeback.h>
56 #if defined(XFS_RW_TRACE)
66 xfs_inode_t
*ip
= XFS_IO_INODE(io
);
68 if (ip
->i_rwtrace
== NULL
)
70 ktrace_enter(ip
->i_rwtrace
,
71 (void *)(unsigned long)tag
,
73 (void *)((unsigned long)((ip
->i_d
.di_size
>> 32) & 0xffffffff)),
74 (void *)((unsigned long)(ip
->i_d
.di_size
& 0xffffffff)),
76 (void *)((unsigned long)segs
),
77 (void *)((unsigned long)((offset
>> 32) & 0xffffffff)),
78 (void *)((unsigned long)(offset
& 0xffffffff)),
79 (void *)((unsigned long)ioflags
),
80 (void *)((unsigned long)((io
->io_new_size
>> 32) & 0xffffffff)),
81 (void *)((unsigned long)(io
->io_new_size
& 0xffffffff)),
82 (void *)((unsigned long)current_pid()),
90 xfs_inval_cached_trace(
97 xfs_inode_t
*ip
= XFS_IO_INODE(io
);
99 if (ip
->i_rwtrace
== NULL
)
101 ktrace_enter(ip
->i_rwtrace
,
102 (void *)(__psint_t
)XFS_INVAL_CACHED
,
104 (void *)((unsigned long)((offset
>> 32) & 0xffffffff)),
105 (void *)((unsigned long)(offset
& 0xffffffff)),
106 (void *)((unsigned long)((len
>> 32) & 0xffffffff)),
107 (void *)((unsigned long)(len
& 0xffffffff)),
108 (void *)((unsigned long)((first
>> 32) & 0xffffffff)),
109 (void *)((unsigned long)(first
& 0xffffffff)),
110 (void *)((unsigned long)((last
>> 32) & 0xffffffff)),
111 (void *)((unsigned long)(last
& 0xffffffff)),
112 (void *)((unsigned long)current_pid()),
124 * xfs_iozero clears the specified range of buffer supplied,
125 * and marks all the affected blocks as valid and modified. If
126 * an affected block is not allocated, it will be allocated. If
127 * an affected block is not completely overwritten, and is not
128 * valid before the operation, it will be read from disk before
129 * being partially zeroed.
133 struct inode
*ip
, /* inode */
134 loff_t pos
, /* offset in file */
135 size_t count
) /* size of data to zero */
139 struct address_space
*mapping
;
142 mapping
= ip
->i_mapping
;
144 unsigned long index
, offset
;
146 offset
= (pos
& (PAGE_CACHE_SIZE
-1)); /* Within page */
147 index
= pos
>> PAGE_CACHE_SHIFT
;
148 bytes
= PAGE_CACHE_SIZE
- offset
;
153 page
= grab_cache_page(mapping
, index
);
157 status
= mapping
->a_ops
->prepare_write(NULL
, page
, offset
,
162 memclear_highpage_flush(page
, offset
, bytes
);
164 status
= mapping
->a_ops
->commit_write(NULL
, page
, offset
,
173 page_cache_release(page
);
181 ssize_t
/* bytes read, or (-) error */
185 const struct iovec
*iovp
,
191 struct file
*file
= iocb
->ki_filp
;
192 struct inode
*inode
= file
->f_mapping
->host
;
201 ip
= XFS_BHVTOI(bdp
);
202 vp
= BHV_TO_VNODE(bdp
);
205 XFS_STATS_INC(xs_read_calls
);
207 /* START copy & waste from filemap.c */
208 for (seg
= 0; seg
< segs
; seg
++) {
209 const struct iovec
*iv
= &iovp
[seg
];
212 * If any segment has a negative length, or the cumulative
213 * length ever wraps negative then return -EINVAL.
216 if (unlikely((ssize_t
)(size
|iv
->iov_len
) < 0))
217 return XFS_ERROR(-EINVAL
);
219 /* END copy & waste from filemap.c */
221 if (unlikely(ioflags
& IO_ISDIRECT
)) {
222 xfs_buftarg_t
*target
=
223 (ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
) ?
224 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
225 if ((*offset
& target
->bt_smask
) ||
226 (size
& target
->bt_smask
)) {
227 if (*offset
== ip
->i_d
.di_size
) {
230 return -XFS_ERROR(EINVAL
);
234 n
= XFS_MAXIOFFSET(mp
) - *offset
;
235 if ((n
<= 0) || (size
== 0))
241 if (XFS_FORCED_SHUTDOWN(mp
))
244 if (unlikely(ioflags
& IO_ISDIRECT
))
245 mutex_lock(&inode
->i_mutex
);
246 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
248 if (DM_EVENT_ENABLED(vp
->v_vfsp
, ip
, DM_EVENT_READ
) &&
249 !(ioflags
& IO_INVIS
)) {
250 bhv_vrwlock_t locktype
= VRWLOCK_READ
;
251 int dmflags
= FILP_DELAY_FLAG(file
) | DM_SEM_FLAG_RD(ioflags
);
253 ret
= -XFS_SEND_DATA(mp
, DM_EVENT_READ
,
254 BHV_TO_VNODE(bdp
), *offset
, size
,
257 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
258 if (unlikely(ioflags
& IO_ISDIRECT
))
259 mutex_unlock(&inode
->i_mutex
);
264 if (unlikely(ioflags
& IO_ISDIRECT
)) {
266 bhv_vop_flushinval_pages(vp
, ctooff(offtoct(*offset
)),
267 -1, FI_REMAPF_LOCKED
);
268 mutex_unlock(&inode
->i_mutex
);
271 xfs_rw_enter_trace(XFS_READ_ENTER
, &ip
->i_iocore
,
272 (void *)iovp
, segs
, *offset
, ioflags
);
274 iocb
->ki_pos
= *offset
;
275 ret
= generic_file_aio_read(iocb
, iovp
, segs
, *offset
);
276 if (ret
== -EIOCBQUEUED
&& !(ioflags
& IO_ISAIO
))
277 ret
= wait_on_sync_kiocb(iocb
);
279 XFS_STATS_ADD(xs_read_bytes
, ret
);
281 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
296 xfs_inode_t
*ip
= XFS_BHVTOI(bdp
);
297 xfs_mount_t
*mp
= ip
->i_mount
;
300 XFS_STATS_INC(xs_read_calls
);
301 if (XFS_FORCED_SHUTDOWN(mp
))
304 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
306 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp
)->v_vfsp
, ip
, DM_EVENT_READ
) &&
307 (!(ioflags
& IO_INVIS
))) {
308 bhv_vrwlock_t locktype
= VRWLOCK_READ
;
311 error
= XFS_SEND_DATA(mp
, DM_EVENT_READ
, BHV_TO_VNODE(bdp
),
313 FILP_DELAY_FLAG(filp
), &locktype
);
315 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
319 xfs_rw_enter_trace(XFS_SENDFILE_ENTER
, &ip
->i_iocore
,
320 (void *)(unsigned long)target
, count
, *offset
, ioflags
);
321 ret
= generic_file_sendfile(filp
, offset
, count
, actor
, target
);
323 XFS_STATS_ADD(xs_read_bytes
, ret
);
325 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
334 struct pipe_inode_info
*pipe
,
340 xfs_inode_t
*ip
= XFS_BHVTOI(bdp
);
341 xfs_mount_t
*mp
= ip
->i_mount
;
344 XFS_STATS_INC(xs_read_calls
);
345 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
348 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
350 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp
)->v_vfsp
, ip
, DM_EVENT_READ
) &&
351 (!(ioflags
& IO_INVIS
))) {
352 bhv_vrwlock_t locktype
= VRWLOCK_READ
;
355 error
= XFS_SEND_DATA(mp
, DM_EVENT_READ
, BHV_TO_VNODE(bdp
),
357 FILP_DELAY_FLAG(infilp
), &locktype
);
359 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
363 xfs_rw_enter_trace(XFS_SPLICE_READ_ENTER
, &ip
->i_iocore
,
364 pipe
, count
, *ppos
, ioflags
);
365 ret
= generic_file_splice_read(infilp
, ppos
, pipe
, count
, flags
);
367 XFS_STATS_ADD(xs_read_bytes
, ret
);
369 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
376 struct pipe_inode_info
*pipe
,
377 struct file
*outfilp
,
384 xfs_inode_t
*ip
= XFS_BHVTOI(bdp
);
385 xfs_mount_t
*mp
= ip
->i_mount
;
387 struct inode
*inode
= outfilp
->f_mapping
->host
;
390 XFS_STATS_INC(xs_write_calls
);
391 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
394 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
396 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp
)->v_vfsp
, ip
, DM_EVENT_WRITE
) &&
397 (!(ioflags
& IO_INVIS
))) {
398 bhv_vrwlock_t locktype
= VRWLOCK_WRITE
;
401 error
= XFS_SEND_DATA(mp
, DM_EVENT_WRITE
, BHV_TO_VNODE(bdp
),
403 FILP_DELAY_FLAG(outfilp
), &locktype
);
405 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
409 xfs_rw_enter_trace(XFS_SPLICE_WRITE_ENTER
, &ip
->i_iocore
,
410 pipe
, count
, *ppos
, ioflags
);
411 ret
= generic_file_splice_write(pipe
, outfilp
, ppos
, count
, flags
);
413 XFS_STATS_ADD(xs_write_bytes
, ret
);
415 isize
= i_size_read(inode
);
416 if (unlikely(ret
< 0 && ret
!= -EFAULT
&& *ppos
> isize
))
419 if (*ppos
> ip
->i_d
.di_size
) {
420 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
421 if (*ppos
> ip
->i_d
.di_size
) {
422 ip
->i_d
.di_size
= *ppos
;
423 i_size_write(inode
, *ppos
);
424 ip
->i_update_core
= 1;
425 ip
->i_update_size
= 1;
427 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
429 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
434 * This routine is called to handle zeroing any space in the last
435 * block of the file that is beyond the EOF. We do this since the
436 * size is being increased without writing anything to that block
437 * and we don't want anyone to read the garbage on the disk.
439 STATIC
int /* error (positive) */
446 xfs_fileoff_t last_fsb
;
447 xfs_mount_t
*mp
= io
->io_mount
;
452 xfs_bmbt_irec_t imap
;
454 ASSERT(ismrlocked(io
->io_lock
, MR_UPDATE
) != 0);
456 zero_offset
= XFS_B_FSB_OFFSET(mp
, isize
);
457 if (zero_offset
== 0) {
459 * There are no extra bytes in the last block on disk to
465 last_fsb
= XFS_B_TO_FSBT(mp
, isize
);
467 error
= XFS_BMAPI(mp
, NULL
, io
, last_fsb
, 1, 0, NULL
, 0, &imap
,
468 &nimaps
, NULL
, NULL
);
474 * If the block underlying isize is just a hole, then there
475 * is nothing to zero.
477 if (imap
.br_startblock
== HOLESTARTBLOCK
) {
481 * Zero the part of the last block beyond the EOF, and write it
482 * out sync. We need to drop the ilock while we do this so we
483 * don't deadlock when the buffer cache calls back to us.
485 XFS_IUNLOCK(mp
, io
, XFS_ILOCK_EXCL
| XFS_EXTSIZE_RD
);
487 zero_len
= mp
->m_sb
.sb_blocksize
- zero_offset
;
488 if (isize
+ zero_len
> offset
)
489 zero_len
= offset
- isize
;
490 error
= xfs_iozero(ip
, isize
, zero_len
);
492 XFS_ILOCK(mp
, io
, XFS_ILOCK_EXCL
|XFS_EXTSIZE_RD
);
498 * Zero any on disk space between the current EOF and the new,
499 * larger EOF. This handles the normal case of zeroing the remainder
500 * of the last block in the file and the unusual case of zeroing blocks
501 * out beyond the size of the file. This second case only happens
502 * with fixed size extents and when the system crashes before the inode
503 * size was updated but after blocks were allocated. If fill is set,
504 * then any holes in the range are filled and zeroed. If not, the holes
505 * are left alone as holes.
508 int /* error (positive) */
512 xfs_off_t offset
, /* starting I/O offset */
513 xfs_fsize_t isize
) /* current inode size */
515 struct inode
*ip
= vn_to_inode(vp
);
516 xfs_fileoff_t start_zero_fsb
;
517 xfs_fileoff_t end_zero_fsb
;
518 xfs_fileoff_t zero_count_fsb
;
519 xfs_fileoff_t last_fsb
;
520 xfs_fileoff_t zero_off
;
521 xfs_fsize_t zero_len
;
522 xfs_mount_t
*mp
= io
->io_mount
;
525 xfs_bmbt_irec_t imap
;
527 ASSERT(ismrlocked(io
->io_lock
, MR_UPDATE
));
528 ASSERT(ismrlocked(io
->io_iolock
, MR_UPDATE
));
529 ASSERT(offset
> isize
);
532 * First handle zeroing the block on which isize resides.
533 * We only zero a part of that block so it is handled specially.
535 error
= xfs_zero_last_block(ip
, io
, offset
, isize
);
537 ASSERT(ismrlocked(io
->io_lock
, MR_UPDATE
));
538 ASSERT(ismrlocked(io
->io_iolock
, MR_UPDATE
));
543 * Calculate the range between the new size and the old
544 * where blocks needing to be zeroed may exist. To get the
545 * block where the last byte in the file currently resides,
546 * we need to subtract one from the size and truncate back
547 * to a block boundary. We subtract 1 in case the size is
548 * exactly on a block boundary.
550 last_fsb
= isize
? XFS_B_TO_FSBT(mp
, isize
- 1) : (xfs_fileoff_t
)-1;
551 start_zero_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)isize
);
552 end_zero_fsb
= XFS_B_TO_FSBT(mp
, offset
- 1);
553 ASSERT((xfs_sfiloff_t
)last_fsb
< (xfs_sfiloff_t
)start_zero_fsb
);
554 if (last_fsb
== end_zero_fsb
) {
556 * The size was only incremented on its last block.
557 * We took care of that above, so just return.
562 ASSERT(start_zero_fsb
<= end_zero_fsb
);
563 while (start_zero_fsb
<= end_zero_fsb
) {
565 zero_count_fsb
= end_zero_fsb
- start_zero_fsb
+ 1;
566 error
= XFS_BMAPI(mp
, NULL
, io
, start_zero_fsb
, zero_count_fsb
,
567 0, NULL
, 0, &imap
, &nimaps
, NULL
, NULL
);
569 ASSERT(ismrlocked(io
->io_lock
, MR_UPDATE
));
570 ASSERT(ismrlocked(io
->io_iolock
, MR_UPDATE
));
575 if (imap
.br_state
== XFS_EXT_UNWRITTEN
||
576 imap
.br_startblock
== HOLESTARTBLOCK
) {
578 * This loop handles initializing pages that were
579 * partially initialized by the code below this
580 * loop. It basically zeroes the part of the page
581 * that sits on a hole and sets the page as P_HOLE
582 * and calls remapf if it is a mapped file.
584 start_zero_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
585 ASSERT(start_zero_fsb
<= (end_zero_fsb
+ 1));
590 * There are blocks we need to zero.
591 * Drop the inode lock while we're doing the I/O.
592 * We'll still have the iolock to protect us.
594 XFS_IUNLOCK(mp
, io
, XFS_ILOCK_EXCL
|XFS_EXTSIZE_RD
);
596 zero_off
= XFS_FSB_TO_B(mp
, start_zero_fsb
);
597 zero_len
= XFS_FSB_TO_B(mp
, imap
.br_blockcount
);
599 if ((zero_off
+ zero_len
) > offset
)
600 zero_len
= offset
- zero_off
;
602 error
= xfs_iozero(ip
, zero_off
, zero_len
);
607 start_zero_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
608 ASSERT(start_zero_fsb
<= (end_zero_fsb
+ 1));
610 XFS_ILOCK(mp
, io
, XFS_ILOCK_EXCL
|XFS_EXTSIZE_RD
);
617 XFS_ILOCK(mp
, io
, XFS_ILOCK_EXCL
|XFS_EXTSIZE_RD
);
622 ssize_t
/* bytes written, or (-) error */
626 const struct iovec
*iovp
,
632 struct file
*file
= iocb
->ki_filp
;
633 struct address_space
*mapping
= file
->f_mapping
;
634 struct inode
*inode
= mapping
->host
;
635 unsigned long segs
= nsegs
;
638 ssize_t ret
= 0, error
= 0;
639 xfs_fsize_t isize
, new_size
;
645 bhv_vrwlock_t locktype
;
646 size_t ocount
= 0, count
;
648 int need_i_mutex
= 1, need_flush
= 0;
650 XFS_STATS_INC(xs_write_calls
);
652 vp
= BHV_TO_VNODE(bdp
);
653 xip
= XFS_BHVTOI(bdp
);
655 for (seg
= 0; seg
< segs
; seg
++) {
656 const struct iovec
*iv
= &iovp
[seg
];
659 * If any segment has a negative length, or the cumulative
660 * length ever wraps negative then return -EINVAL.
662 ocount
+= iv
->iov_len
;
663 if (unlikely((ssize_t
)(ocount
|iv
->iov_len
) < 0))
665 if (access_ok(VERIFY_READ
, iv
->iov_base
, iv
->iov_len
))
670 ocount
-= iv
->iov_len
; /* This segment is no good */
683 vfs_wait_for_freeze(vp
->v_vfsp
, SB_FREEZE_WRITE
);
685 if (XFS_FORCED_SHUTDOWN(mp
))
688 if (ioflags
& IO_ISDIRECT
) {
689 xfs_buftarg_t
*target
=
690 (xip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
) ?
691 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
693 if ((pos
& target
->bt_smask
) || (count
& target
->bt_smask
))
694 return XFS_ERROR(-EINVAL
);
696 if (!VN_CACHED(vp
) && pos
< i_size_read(inode
))
705 iolock
= XFS_IOLOCK_EXCL
;
706 locktype
= VRWLOCK_WRITE
;
708 mutex_lock(&inode
->i_mutex
);
710 iolock
= XFS_IOLOCK_SHARED
;
711 locktype
= VRWLOCK_WRITE_DIRECT
;
714 xfs_ilock(xip
, XFS_ILOCK_EXCL
|iolock
);
716 isize
= i_size_read(inode
);
718 if (file
->f_flags
& O_APPEND
)
722 error
= -generic_write_checks(file
, &pos
, &count
,
723 S_ISBLK(inode
->i_mode
));
725 xfs_iunlock(xip
, XFS_ILOCK_EXCL
|iolock
);
726 goto out_unlock_mutex
;
729 new_size
= pos
+ count
;
730 if (new_size
> isize
)
731 io
->io_new_size
= new_size
;
733 if ((DM_EVENT_ENABLED(vp
->v_vfsp
, xip
, DM_EVENT_WRITE
) &&
734 !(ioflags
& IO_INVIS
) && !eventsent
)) {
735 loff_t savedsize
= pos
;
736 int dmflags
= FILP_DELAY_FLAG(file
);
739 dmflags
|= DM_FLAGS_IMUX
;
741 xfs_iunlock(xip
, XFS_ILOCK_EXCL
);
742 error
= XFS_SEND_DATA(xip
->i_mount
, DM_EVENT_WRITE
, vp
,
746 xfs_iunlock(xip
, iolock
);
747 goto out_unlock_mutex
;
749 xfs_ilock(xip
, XFS_ILOCK_EXCL
);
753 * The iolock was dropped and reacquired in XFS_SEND_DATA
754 * so we have to recheck the size when appending.
755 * We will only "goto start;" once, since having sent the
756 * event prevents another call to XFS_SEND_DATA, which is
757 * what allows the size to change in the first place.
759 if ((file
->f_flags
& O_APPEND
) && savedsize
!= isize
) {
760 pos
= isize
= xip
->i_d
.di_size
;
765 if (likely(!(ioflags
& IO_INVIS
))) {
766 file_update_time(file
);
767 xfs_ichgtime_fast(xip
, inode
,
768 XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
772 * If the offset is beyond the size of the file, we have a couple
773 * of things to do. First, if there is already space allocated
774 * we need to either create holes or zero the disk or ...
776 * If there is a page where the previous size lands, we need
777 * to zero it out up to the new size.
781 error
= xfs_zero_eof(BHV_TO_VNODE(bdp
), io
, pos
, isize
);
783 xfs_iunlock(xip
, XFS_ILOCK_EXCL
|iolock
);
784 goto out_unlock_mutex
;
787 xfs_iunlock(xip
, XFS_ILOCK_EXCL
);
790 * If we're writing the file then make sure to clear the
791 * setuid and setgid bits if the process is not being run
792 * by root. This keeps people from modifying setuid and
796 if (((xip
->i_d
.di_mode
& S_ISUID
) ||
797 ((xip
->i_d
.di_mode
& (S_ISGID
| S_IXGRP
)) ==
798 (S_ISGID
| S_IXGRP
))) &&
799 !capable(CAP_FSETID
)) {
800 error
= xfs_write_clear_setuid(xip
);
802 error
= -remove_suid(file
->f_path
.dentry
);
803 if (unlikely(error
)) {
804 xfs_iunlock(xip
, iolock
);
805 goto out_unlock_mutex
;
810 /* We can write back this queue in page reclaim */
811 current
->backing_dev_info
= mapping
->backing_dev_info
;
813 if ((ioflags
& IO_ISDIRECT
)) {
815 xfs_inval_cached_trace(io
, pos
, -1,
816 ctooff(offtoct(pos
)), -1);
817 bhv_vop_flushinval_pages(vp
, ctooff(offtoct(pos
)),
818 -1, FI_REMAPF_LOCKED
);
822 /* demote the lock now the cached pages are gone */
823 XFS_ILOCK_DEMOTE(mp
, io
, XFS_IOLOCK_EXCL
);
824 mutex_unlock(&inode
->i_mutex
);
826 iolock
= XFS_IOLOCK_SHARED
;
827 locktype
= VRWLOCK_WRITE_DIRECT
;
831 xfs_rw_enter_trace(XFS_DIOWR_ENTER
, io
, (void *)iovp
, segs
,
833 ret
= generic_file_direct_write(iocb
, iovp
,
834 &segs
, pos
, offset
, count
, ocount
);
837 * direct-io write to a hole: fall through to buffered I/O
838 * for completing the rest of the request.
840 if (ret
>= 0 && ret
!= count
) {
841 XFS_STATS_ADD(xs_write_bytes
, ret
);
847 ioflags
&= ~IO_ISDIRECT
;
848 xfs_iunlock(xip
, iolock
);
852 xfs_rw_enter_trace(XFS_WRITE_ENTER
, io
, (void *)iovp
, segs
,
854 ret
= generic_file_buffered_write(iocb
, iovp
, segs
,
855 pos
, offset
, count
, ret
);
858 current
->backing_dev_info
= NULL
;
860 if (ret
== -EIOCBQUEUED
&& !(ioflags
& IO_ISAIO
))
861 ret
= wait_on_sync_kiocb(iocb
);
863 if ((ret
== -ENOSPC
) &&
864 DM_EVENT_ENABLED(vp
->v_vfsp
, xip
, DM_EVENT_NOSPACE
) &&
865 !(ioflags
& IO_INVIS
)) {
867 xfs_rwunlock(bdp
, locktype
);
869 mutex_unlock(&inode
->i_mutex
);
870 error
= XFS_SEND_NAMESP(xip
->i_mount
, DM_EVENT_NOSPACE
, vp
,
871 DM_RIGHT_NULL
, vp
, DM_RIGHT_NULL
, NULL
, NULL
,
872 0, 0, 0); /* Delay flag intentionally unused */
876 mutex_lock(&inode
->i_mutex
);
877 xfs_rwlock(bdp
, locktype
);
878 pos
= xip
->i_d
.di_size
;
883 isize
= i_size_read(inode
);
884 if (unlikely(ret
< 0 && ret
!= -EFAULT
&& *offset
> isize
))
887 if (*offset
> xip
->i_d
.di_size
) {
888 xfs_ilock(xip
, XFS_ILOCK_EXCL
);
889 if (*offset
> xip
->i_d
.di_size
) {
890 xip
->i_d
.di_size
= *offset
;
891 i_size_write(inode
, *offset
);
892 xip
->i_update_core
= 1;
893 xip
->i_update_size
= 1;
895 xfs_iunlock(xip
, XFS_ILOCK_EXCL
);
900 goto out_unlock_internal
;
902 XFS_STATS_ADD(xs_write_bytes
, ret
);
904 /* Handle various SYNC-type writes */
905 if ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
)) {
906 error
= xfs_write_sync_logforce(mp
, xip
);
908 goto out_unlock_internal
;
910 xfs_rwunlock(bdp
, locktype
);
912 mutex_unlock(&inode
->i_mutex
);
914 error
= sync_page_range(inode
, mapping
, pos
, ret
);
921 xfs_rwunlock(bdp
, locktype
);
924 mutex_unlock(&inode
->i_mutex
);
930 * All xfs metadata buffers except log state machine buffers
931 * get this attached as their b_bdstrat callback function.
932 * This is so that we can catch a buffer
933 * after prematurely unpinning it to forcibly shutdown the filesystem.
936 xfs_bdstrat_cb(struct xfs_buf
*bp
)
940 mp
= XFS_BUF_FSPRIVATE3(bp
, xfs_mount_t
*);
941 if (!XFS_FORCED_SHUTDOWN(mp
)) {
942 xfs_buf_iorequest(bp
);
945 xfs_buftrace("XFS__BDSTRAT IOERROR", bp
);
947 * Metadata write that didn't get logged but
948 * written delayed anyway. These aren't associated
949 * with a transaction, and can be ignored.
951 if (XFS_BUF_IODONE_FUNC(bp
) == NULL
&&
952 (XFS_BUF_ISREAD(bp
)) == 0)
953 return (xfs_bioerror_relse(bp
));
955 return (xfs_bioerror(bp
));
961 xfs_bmap(bhv_desc_t
*bdp
,
968 xfs_inode_t
*ip
= XFS_BHVTOI(bdp
);
969 xfs_iocore_t
*io
= &ip
->i_iocore
;
971 ASSERT((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
);
972 ASSERT(((ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
) != 0) ==
973 ((ip
->i_iocore
.io_flags
& XFS_IOCORE_RT
) != 0));
975 return xfs_iomap(io
, offset
, count
, flags
, iomapp
, niomaps
);
979 * Wrapper around bdstrat so that we can stop data
980 * from going to disk in case we are shutting down the filesystem.
981 * Typically user data goes thru this path; one of the exceptions
986 struct xfs_mount
*mp
,
990 if (!XFS_FORCED_SHUTDOWN(mp
)) {
991 /* Grio redirection would go here
992 * if (XFS_BUF_IS_GRIO(bp)) {
995 xfs_buf_iorequest(bp
);
999 xfs_buftrace("XFSBDSTRAT IOERROR", bp
);
1000 return (xfs_bioerror_relse(bp
));
1004 * If the underlying (data/log/rt) device is readonly, there are some
1005 * operations that cannot proceed.
1008 xfs_dev_is_read_only(
1012 if (xfs_readonly_buftarg(mp
->m_ddev_targp
) ||
1013 xfs_readonly_buftarg(mp
->m_logdev_targp
) ||
1014 (mp
->m_rtdev_targp
&& xfs_readonly_buftarg(mp
->m_rtdev_targp
))) {
1016 "XFS: %s required on read-only device.", message
);
1018 "XFS: write access unavailable, cannot proceed.");