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
;
143 mapping
= ip
->i_mapping
;
145 unsigned long index
, offset
;
147 offset
= (pos
& (PAGE_CACHE_SIZE
-1)); /* Within page */
148 index
= pos
>> PAGE_CACHE_SHIFT
;
149 bytes
= PAGE_CACHE_SIZE
- offset
;
154 page
= grab_cache_page(mapping
, index
);
159 status
= mapping
->a_ops
->prepare_write(NULL
, page
, offset
,
165 memset((void *) (kaddr
+ offset
), 0, bytes
);
166 flush_dcache_page(page
);
167 status
= mapping
->a_ops
->commit_write(NULL
, page
, offset
,
177 page_cache_release(page
);
185 ssize_t
/* bytes read, or (-) error */
189 const struct iovec
*iovp
,
195 struct file
*file
= iocb
->ki_filp
;
196 struct inode
*inode
= file
->f_mapping
->host
;
205 ip
= XFS_BHVTOI(bdp
);
206 vp
= BHV_TO_VNODE(bdp
);
209 XFS_STATS_INC(xs_read_calls
);
211 /* START copy & waste from filemap.c */
212 for (seg
= 0; seg
< segs
; seg
++) {
213 const struct iovec
*iv
= &iovp
[seg
];
216 * If any segment has a negative length, or the cumulative
217 * length ever wraps negative then return -EINVAL.
220 if (unlikely((ssize_t
)(size
|iv
->iov_len
) < 0))
221 return XFS_ERROR(-EINVAL
);
223 /* END copy & waste from filemap.c */
225 if (unlikely(ioflags
& IO_ISDIRECT
)) {
226 xfs_buftarg_t
*target
=
227 (ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
) ?
228 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
229 if ((*offset
& target
->bt_smask
) ||
230 (size
& target
->bt_smask
)) {
231 if (*offset
== ip
->i_d
.di_size
) {
234 return -XFS_ERROR(EINVAL
);
238 n
= XFS_MAXIOFFSET(mp
) - *offset
;
239 if ((n
<= 0) || (size
== 0))
245 if (XFS_FORCED_SHUTDOWN(mp
))
248 if (unlikely(ioflags
& IO_ISDIRECT
))
249 mutex_lock(&inode
->i_mutex
);
250 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
252 if (DM_EVENT_ENABLED(vp
->v_vfsp
, ip
, DM_EVENT_READ
) &&
253 !(ioflags
& IO_INVIS
)) {
254 bhv_vrwlock_t locktype
= VRWLOCK_READ
;
255 int dmflags
= FILP_DELAY_FLAG(file
) | DM_SEM_FLAG_RD(ioflags
);
257 ret
= -XFS_SEND_DATA(mp
, DM_EVENT_READ
,
258 BHV_TO_VNODE(bdp
), *offset
, size
,
261 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
262 if (unlikely(ioflags
& IO_ISDIRECT
))
263 mutex_unlock(&inode
->i_mutex
);
268 if (unlikely(ioflags
& IO_ISDIRECT
)) {
270 bhv_vop_flushinval_pages(vp
, ctooff(offtoct(*offset
)),
271 -1, FI_REMAPF_LOCKED
);
272 mutex_unlock(&inode
->i_mutex
);
275 xfs_rw_enter_trace(XFS_READ_ENTER
, &ip
->i_iocore
,
276 (void *)iovp
, segs
, *offset
, ioflags
);
278 iocb
->ki_pos
= *offset
;
279 ret
= generic_file_aio_read(iocb
, iovp
, segs
, *offset
);
280 if (ret
== -EIOCBQUEUED
&& !(ioflags
& IO_ISAIO
))
281 ret
= wait_on_sync_kiocb(iocb
);
283 XFS_STATS_ADD(xs_read_bytes
, ret
);
285 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
300 xfs_inode_t
*ip
= XFS_BHVTOI(bdp
);
301 xfs_mount_t
*mp
= ip
->i_mount
;
304 XFS_STATS_INC(xs_read_calls
);
305 if (XFS_FORCED_SHUTDOWN(mp
))
308 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
310 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp
)->v_vfsp
, ip
, DM_EVENT_READ
) &&
311 (!(ioflags
& IO_INVIS
))) {
312 bhv_vrwlock_t locktype
= VRWLOCK_READ
;
315 error
= XFS_SEND_DATA(mp
, DM_EVENT_READ
, BHV_TO_VNODE(bdp
),
317 FILP_DELAY_FLAG(filp
), &locktype
);
319 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
323 xfs_rw_enter_trace(XFS_SENDFILE_ENTER
, &ip
->i_iocore
,
324 (void *)(unsigned long)target
, count
, *offset
, ioflags
);
325 ret
= generic_file_sendfile(filp
, offset
, count
, actor
, target
);
327 XFS_STATS_ADD(xs_read_bytes
, ret
);
329 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
338 struct pipe_inode_info
*pipe
,
344 xfs_inode_t
*ip
= XFS_BHVTOI(bdp
);
345 xfs_mount_t
*mp
= ip
->i_mount
;
348 XFS_STATS_INC(xs_read_calls
);
349 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
352 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
354 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp
)->v_vfsp
, ip
, DM_EVENT_READ
) &&
355 (!(ioflags
& IO_INVIS
))) {
356 bhv_vrwlock_t locktype
= VRWLOCK_READ
;
359 error
= XFS_SEND_DATA(mp
, DM_EVENT_READ
, BHV_TO_VNODE(bdp
),
361 FILP_DELAY_FLAG(infilp
), &locktype
);
363 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
367 xfs_rw_enter_trace(XFS_SPLICE_READ_ENTER
, &ip
->i_iocore
,
368 pipe
, count
, *ppos
, ioflags
);
369 ret
= generic_file_splice_read(infilp
, ppos
, pipe
, count
, flags
);
371 XFS_STATS_ADD(xs_read_bytes
, ret
);
373 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
380 struct pipe_inode_info
*pipe
,
381 struct file
*outfilp
,
388 xfs_inode_t
*ip
= XFS_BHVTOI(bdp
);
389 xfs_mount_t
*mp
= ip
->i_mount
;
391 struct inode
*inode
= outfilp
->f_mapping
->host
;
394 XFS_STATS_INC(xs_write_calls
);
395 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
398 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
400 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp
)->v_vfsp
, ip
, DM_EVENT_WRITE
) &&
401 (!(ioflags
& IO_INVIS
))) {
402 bhv_vrwlock_t locktype
= VRWLOCK_WRITE
;
405 error
= XFS_SEND_DATA(mp
, DM_EVENT_WRITE
, BHV_TO_VNODE(bdp
),
407 FILP_DELAY_FLAG(outfilp
), &locktype
);
409 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
413 xfs_rw_enter_trace(XFS_SPLICE_WRITE_ENTER
, &ip
->i_iocore
,
414 pipe
, count
, *ppos
, ioflags
);
415 ret
= generic_file_splice_write(pipe
, outfilp
, ppos
, count
, flags
);
417 XFS_STATS_ADD(xs_write_bytes
, ret
);
419 isize
= i_size_read(inode
);
420 if (unlikely(ret
< 0 && ret
!= -EFAULT
&& *ppos
> isize
))
423 if (*ppos
> ip
->i_d
.di_size
) {
424 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
425 if (*ppos
> ip
->i_d
.di_size
) {
426 ip
->i_d
.di_size
= *ppos
;
427 i_size_write(inode
, *ppos
);
428 ip
->i_update_core
= 1;
429 ip
->i_update_size
= 1;
431 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
433 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
438 * This routine is called to handle zeroing any space in the last
439 * block of the file that is beyond the EOF. We do this since the
440 * size is being increased without writing anything to that block
441 * and we don't want anyone to read the garbage on the disk.
443 STATIC
int /* error (positive) */
450 xfs_fileoff_t last_fsb
;
451 xfs_mount_t
*mp
= io
->io_mount
;
456 xfs_bmbt_irec_t imap
;
458 ASSERT(ismrlocked(io
->io_lock
, MR_UPDATE
) != 0);
460 zero_offset
= XFS_B_FSB_OFFSET(mp
, isize
);
461 if (zero_offset
== 0) {
463 * There are no extra bytes in the last block on disk to
469 last_fsb
= XFS_B_TO_FSBT(mp
, isize
);
471 error
= XFS_BMAPI(mp
, NULL
, io
, last_fsb
, 1, 0, NULL
, 0, &imap
,
472 &nimaps
, NULL
, NULL
);
478 * If the block underlying isize is just a hole, then there
479 * is nothing to zero.
481 if (imap
.br_startblock
== HOLESTARTBLOCK
) {
485 * Zero the part of the last block beyond the EOF, and write it
486 * out sync. We need to drop the ilock while we do this so we
487 * don't deadlock when the buffer cache calls back to us.
489 XFS_IUNLOCK(mp
, io
, XFS_ILOCK_EXCL
| XFS_EXTSIZE_RD
);
491 zero_len
= mp
->m_sb
.sb_blocksize
- zero_offset
;
492 if (isize
+ zero_len
> offset
)
493 zero_len
= offset
- isize
;
494 error
= xfs_iozero(ip
, isize
, zero_len
);
496 XFS_ILOCK(mp
, io
, XFS_ILOCK_EXCL
|XFS_EXTSIZE_RD
);
502 * Zero any on disk space between the current EOF and the new,
503 * larger EOF. This handles the normal case of zeroing the remainder
504 * of the last block in the file and the unusual case of zeroing blocks
505 * out beyond the size of the file. This second case only happens
506 * with fixed size extents and when the system crashes before the inode
507 * size was updated but after blocks were allocated. If fill is set,
508 * then any holes in the range are filled and zeroed. If not, the holes
509 * are left alone as holes.
512 int /* error (positive) */
516 xfs_off_t offset
, /* starting I/O offset */
517 xfs_fsize_t isize
) /* current inode size */
519 struct inode
*ip
= vn_to_inode(vp
);
520 xfs_fileoff_t start_zero_fsb
;
521 xfs_fileoff_t end_zero_fsb
;
522 xfs_fileoff_t zero_count_fsb
;
523 xfs_fileoff_t last_fsb
;
524 xfs_fileoff_t zero_off
;
525 xfs_fsize_t zero_len
;
526 xfs_mount_t
*mp
= io
->io_mount
;
529 xfs_bmbt_irec_t imap
;
531 ASSERT(ismrlocked(io
->io_lock
, MR_UPDATE
));
532 ASSERT(ismrlocked(io
->io_iolock
, MR_UPDATE
));
533 ASSERT(offset
> isize
);
536 * First handle zeroing the block on which isize resides.
537 * We only zero a part of that block so it is handled specially.
539 error
= xfs_zero_last_block(ip
, io
, offset
, isize
);
541 ASSERT(ismrlocked(io
->io_lock
, MR_UPDATE
));
542 ASSERT(ismrlocked(io
->io_iolock
, MR_UPDATE
));
547 * Calculate the range between the new size and the old
548 * where blocks needing to be zeroed may exist. To get the
549 * block where the last byte in the file currently resides,
550 * we need to subtract one from the size and truncate back
551 * to a block boundary. We subtract 1 in case the size is
552 * exactly on a block boundary.
554 last_fsb
= isize
? XFS_B_TO_FSBT(mp
, isize
- 1) : (xfs_fileoff_t
)-1;
555 start_zero_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)isize
);
556 end_zero_fsb
= XFS_B_TO_FSBT(mp
, offset
- 1);
557 ASSERT((xfs_sfiloff_t
)last_fsb
< (xfs_sfiloff_t
)start_zero_fsb
);
558 if (last_fsb
== end_zero_fsb
) {
560 * The size was only incremented on its last block.
561 * We took care of that above, so just return.
566 ASSERT(start_zero_fsb
<= end_zero_fsb
);
567 while (start_zero_fsb
<= end_zero_fsb
) {
569 zero_count_fsb
= end_zero_fsb
- start_zero_fsb
+ 1;
570 error
= XFS_BMAPI(mp
, NULL
, io
, start_zero_fsb
, zero_count_fsb
,
571 0, NULL
, 0, &imap
, &nimaps
, NULL
, NULL
);
573 ASSERT(ismrlocked(io
->io_lock
, MR_UPDATE
));
574 ASSERT(ismrlocked(io
->io_iolock
, MR_UPDATE
));
579 if (imap
.br_state
== XFS_EXT_UNWRITTEN
||
580 imap
.br_startblock
== HOLESTARTBLOCK
) {
582 * This loop handles initializing pages that were
583 * partially initialized by the code below this
584 * loop. It basically zeroes the part of the page
585 * that sits on a hole and sets the page as P_HOLE
586 * and calls remapf if it is a mapped file.
588 start_zero_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
589 ASSERT(start_zero_fsb
<= (end_zero_fsb
+ 1));
594 * There are blocks we need to zero.
595 * Drop the inode lock while we're doing the I/O.
596 * We'll still have the iolock to protect us.
598 XFS_IUNLOCK(mp
, io
, XFS_ILOCK_EXCL
|XFS_EXTSIZE_RD
);
600 zero_off
= XFS_FSB_TO_B(mp
, start_zero_fsb
);
601 zero_len
= XFS_FSB_TO_B(mp
, imap
.br_blockcount
);
603 if ((zero_off
+ zero_len
) > offset
)
604 zero_len
= offset
- zero_off
;
606 error
= xfs_iozero(ip
, zero_off
, zero_len
);
611 start_zero_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
612 ASSERT(start_zero_fsb
<= (end_zero_fsb
+ 1));
614 XFS_ILOCK(mp
, io
, XFS_ILOCK_EXCL
|XFS_EXTSIZE_RD
);
621 XFS_ILOCK(mp
, io
, XFS_ILOCK_EXCL
|XFS_EXTSIZE_RD
);
626 ssize_t
/* bytes written, or (-) error */
630 const struct iovec
*iovp
,
636 struct file
*file
= iocb
->ki_filp
;
637 struct address_space
*mapping
= file
->f_mapping
;
638 struct inode
*inode
= mapping
->host
;
639 unsigned long segs
= nsegs
;
642 ssize_t ret
= 0, error
= 0;
643 xfs_fsize_t isize
, new_size
;
649 bhv_vrwlock_t locktype
;
650 size_t ocount
= 0, count
;
652 int need_i_mutex
= 1, need_flush
= 0;
654 XFS_STATS_INC(xs_write_calls
);
656 vp
= BHV_TO_VNODE(bdp
);
657 xip
= XFS_BHVTOI(bdp
);
659 for (seg
= 0; seg
< segs
; seg
++) {
660 const struct iovec
*iv
= &iovp
[seg
];
663 * If any segment has a negative length, or the cumulative
664 * length ever wraps negative then return -EINVAL.
666 ocount
+= iv
->iov_len
;
667 if (unlikely((ssize_t
)(ocount
|iv
->iov_len
) < 0))
669 if (access_ok(VERIFY_READ
, iv
->iov_base
, iv
->iov_len
))
674 ocount
-= iv
->iov_len
; /* This segment is no good */
687 vfs_wait_for_freeze(vp
->v_vfsp
, SB_FREEZE_WRITE
);
689 if (XFS_FORCED_SHUTDOWN(mp
))
692 if (ioflags
& IO_ISDIRECT
) {
693 xfs_buftarg_t
*target
=
694 (xip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
) ?
695 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
697 if ((pos
& target
->bt_smask
) || (count
& target
->bt_smask
))
698 return XFS_ERROR(-EINVAL
);
700 if (!VN_CACHED(vp
) && pos
< i_size_read(inode
))
709 iolock
= XFS_IOLOCK_EXCL
;
710 locktype
= VRWLOCK_WRITE
;
712 mutex_lock(&inode
->i_mutex
);
714 iolock
= XFS_IOLOCK_SHARED
;
715 locktype
= VRWLOCK_WRITE_DIRECT
;
718 xfs_ilock(xip
, XFS_ILOCK_EXCL
|iolock
);
720 isize
= i_size_read(inode
);
722 if (file
->f_flags
& O_APPEND
)
726 error
= -generic_write_checks(file
, &pos
, &count
,
727 S_ISBLK(inode
->i_mode
));
729 xfs_iunlock(xip
, XFS_ILOCK_EXCL
|iolock
);
730 goto out_unlock_mutex
;
733 new_size
= pos
+ count
;
734 if (new_size
> isize
)
735 io
->io_new_size
= new_size
;
737 if ((DM_EVENT_ENABLED(vp
->v_vfsp
, xip
, DM_EVENT_WRITE
) &&
738 !(ioflags
& IO_INVIS
) && !eventsent
)) {
739 loff_t savedsize
= pos
;
740 int dmflags
= FILP_DELAY_FLAG(file
);
743 dmflags
|= DM_FLAGS_IMUX
;
745 xfs_iunlock(xip
, XFS_ILOCK_EXCL
);
746 error
= XFS_SEND_DATA(xip
->i_mount
, DM_EVENT_WRITE
, vp
,
750 xfs_iunlock(xip
, iolock
);
751 goto out_unlock_mutex
;
753 xfs_ilock(xip
, XFS_ILOCK_EXCL
);
757 * The iolock was dropped and reacquired in XFS_SEND_DATA
758 * so we have to recheck the size when appending.
759 * We will only "goto start;" once, since having sent the
760 * event prevents another call to XFS_SEND_DATA, which is
761 * what allows the size to change in the first place.
763 if ((file
->f_flags
& O_APPEND
) && savedsize
!= isize
) {
764 pos
= isize
= xip
->i_d
.di_size
;
769 if (likely(!(ioflags
& IO_INVIS
))) {
770 file_update_time(file
);
771 xfs_ichgtime_fast(xip
, inode
,
772 XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
776 * If the offset is beyond the size of the file, we have a couple
777 * of things to do. First, if there is already space allocated
778 * we need to either create holes or zero the disk or ...
780 * If there is a page where the previous size lands, we need
781 * to zero it out up to the new size.
785 error
= xfs_zero_eof(BHV_TO_VNODE(bdp
), io
, pos
, isize
);
787 xfs_iunlock(xip
, XFS_ILOCK_EXCL
|iolock
);
788 goto out_unlock_mutex
;
791 xfs_iunlock(xip
, XFS_ILOCK_EXCL
);
794 * If we're writing the file then make sure to clear the
795 * setuid and setgid bits if the process is not being run
796 * by root. This keeps people from modifying setuid and
800 if (((xip
->i_d
.di_mode
& S_ISUID
) ||
801 ((xip
->i_d
.di_mode
& (S_ISGID
| S_IXGRP
)) ==
802 (S_ISGID
| S_IXGRP
))) &&
803 !capable(CAP_FSETID
)) {
804 error
= xfs_write_clear_setuid(xip
);
806 error
= -remove_suid(file
->f_path
.dentry
);
807 if (unlikely(error
)) {
808 xfs_iunlock(xip
, iolock
);
809 goto out_unlock_mutex
;
814 /* We can write back this queue in page reclaim */
815 current
->backing_dev_info
= mapping
->backing_dev_info
;
817 if ((ioflags
& IO_ISDIRECT
)) {
819 xfs_inval_cached_trace(io
, pos
, -1,
820 ctooff(offtoct(pos
)), -1);
821 bhv_vop_flushinval_pages(vp
, ctooff(offtoct(pos
)),
822 -1, FI_REMAPF_LOCKED
);
826 /* demote the lock now the cached pages are gone */
827 XFS_ILOCK_DEMOTE(mp
, io
, XFS_IOLOCK_EXCL
);
828 mutex_unlock(&inode
->i_mutex
);
830 iolock
= XFS_IOLOCK_SHARED
;
831 locktype
= VRWLOCK_WRITE_DIRECT
;
835 xfs_rw_enter_trace(XFS_DIOWR_ENTER
, io
, (void *)iovp
, segs
,
837 ret
= generic_file_direct_write(iocb
, iovp
,
838 &segs
, pos
, offset
, count
, ocount
);
841 * direct-io write to a hole: fall through to buffered I/O
842 * for completing the rest of the request.
844 if (ret
>= 0 && ret
!= count
) {
845 XFS_STATS_ADD(xs_write_bytes
, ret
);
851 ioflags
&= ~IO_ISDIRECT
;
852 xfs_iunlock(xip
, iolock
);
856 xfs_rw_enter_trace(XFS_WRITE_ENTER
, io
, (void *)iovp
, segs
,
858 ret
= generic_file_buffered_write(iocb
, iovp
, segs
,
859 pos
, offset
, count
, ret
);
862 current
->backing_dev_info
= NULL
;
864 if (ret
== -EIOCBQUEUED
&& !(ioflags
& IO_ISAIO
))
865 ret
= wait_on_sync_kiocb(iocb
);
867 if ((ret
== -ENOSPC
) &&
868 DM_EVENT_ENABLED(vp
->v_vfsp
, xip
, DM_EVENT_NOSPACE
) &&
869 !(ioflags
& IO_INVIS
)) {
871 xfs_rwunlock(bdp
, locktype
);
873 mutex_unlock(&inode
->i_mutex
);
874 error
= XFS_SEND_NAMESP(xip
->i_mount
, DM_EVENT_NOSPACE
, vp
,
875 DM_RIGHT_NULL
, vp
, DM_RIGHT_NULL
, NULL
, NULL
,
876 0, 0, 0); /* Delay flag intentionally unused */
880 mutex_lock(&inode
->i_mutex
);
881 xfs_rwlock(bdp
, locktype
);
882 pos
= xip
->i_d
.di_size
;
887 isize
= i_size_read(inode
);
888 if (unlikely(ret
< 0 && ret
!= -EFAULT
&& *offset
> isize
))
891 if (*offset
> xip
->i_d
.di_size
) {
892 xfs_ilock(xip
, XFS_ILOCK_EXCL
);
893 if (*offset
> xip
->i_d
.di_size
) {
894 xip
->i_d
.di_size
= *offset
;
895 i_size_write(inode
, *offset
);
896 xip
->i_update_core
= 1;
897 xip
->i_update_size
= 1;
899 xfs_iunlock(xip
, XFS_ILOCK_EXCL
);
904 goto out_unlock_internal
;
906 XFS_STATS_ADD(xs_write_bytes
, ret
);
908 /* Handle various SYNC-type writes */
909 if ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
)) {
910 error
= xfs_write_sync_logforce(mp
, xip
);
912 goto out_unlock_internal
;
914 xfs_rwunlock(bdp
, locktype
);
916 mutex_unlock(&inode
->i_mutex
);
918 error
= sync_page_range(inode
, mapping
, pos
, ret
);
925 xfs_rwunlock(bdp
, locktype
);
928 mutex_unlock(&inode
->i_mutex
);
934 * All xfs metadata buffers except log state machine buffers
935 * get this attached as their b_bdstrat callback function.
936 * This is so that we can catch a buffer
937 * after prematurely unpinning it to forcibly shutdown the filesystem.
940 xfs_bdstrat_cb(struct xfs_buf
*bp
)
944 mp
= XFS_BUF_FSPRIVATE3(bp
, xfs_mount_t
*);
945 if (!XFS_FORCED_SHUTDOWN(mp
)) {
946 xfs_buf_iorequest(bp
);
949 xfs_buftrace("XFS__BDSTRAT IOERROR", bp
);
951 * Metadata write that didn't get logged but
952 * written delayed anyway. These aren't associated
953 * with a transaction, and can be ignored.
955 if (XFS_BUF_IODONE_FUNC(bp
) == NULL
&&
956 (XFS_BUF_ISREAD(bp
)) == 0)
957 return (xfs_bioerror_relse(bp
));
959 return (xfs_bioerror(bp
));
965 xfs_bmap(bhv_desc_t
*bdp
,
972 xfs_inode_t
*ip
= XFS_BHVTOI(bdp
);
973 xfs_iocore_t
*io
= &ip
->i_iocore
;
975 ASSERT((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
);
976 ASSERT(((ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
) != 0) ==
977 ((ip
->i_iocore
.io_flags
& XFS_IOCORE_RT
) != 0));
979 return xfs_iomap(io
, offset
, count
, flags
, iomapp
, niomaps
);
983 * Wrapper around bdstrat so that we can stop data
984 * from going to disk in case we are shutting down the filesystem.
985 * Typically user data goes thru this path; one of the exceptions
990 struct xfs_mount
*mp
,
994 if (!XFS_FORCED_SHUTDOWN(mp
)) {
995 /* Grio redirection would go here
996 * if (XFS_BUF_IS_GRIO(bp)) {
999 xfs_buf_iorequest(bp
);
1003 xfs_buftrace("XFSBDSTRAT IOERROR", bp
);
1004 return (xfs_bioerror_relse(bp
));
1008 * If the underlying (data/log/rt) device is readonly, there are some
1009 * operations that cannot proceed.
1012 xfs_dev_is_read_only(
1016 if (xfs_readonly_buftarg(mp
->m_ddev_targp
) ||
1017 xfs_readonly_buftarg(mp
->m_logdev_targp
) ||
1018 (mp
->m_rtdev_targp
&& xfs_readonly_buftarg(mp
->m_rtdev_targp
))) {
1020 "XFS: %s required on read-only device.", message
);
1022 "XFS: write access unavailable, cannot proceed.");