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"
28 #include "xfs_alloc.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_quota.h"
31 #include "xfs_mount.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir_sf.h"
36 #include "xfs_dir2_sf.h"
37 #include "xfs_attr_sf.h"
38 #include "xfs_dinode.h"
39 #include "xfs_inode.h"
41 #include "xfs_btree.h"
42 #include "xfs_ialloc.h"
43 #include "xfs_rtalloc.h"
44 #include "xfs_error.h"
45 #include "xfs_itable.h"
51 #include "xfs_inode_item.h"
52 #include "xfs_buf_item.h"
53 #include "xfs_utils.h"
54 #include "xfs_iomap.h"
56 #include <linux/capability.h>
57 #include <linux/writeback.h>
60 #if defined(XFS_RW_TRACE)
70 xfs_inode_t
*ip
= XFS_IO_INODE(io
);
72 if (ip
->i_rwtrace
== NULL
)
74 ktrace_enter(ip
->i_rwtrace
,
75 (void *)(unsigned long)tag
,
77 (void *)((unsigned long)((ip
->i_d
.di_size
>> 32) & 0xffffffff)),
78 (void *)((unsigned long)(ip
->i_d
.di_size
& 0xffffffff)),
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)),
86 (void *)((unsigned long)current_pid()),
94 xfs_inval_cached_trace(
101 xfs_inode_t
*ip
= XFS_IO_INODE(io
);
103 if (ip
->i_rwtrace
== NULL
)
105 ktrace_enter(ip
->i_rwtrace
,
106 (void *)(__psint_t
)XFS_INVAL_CACHED
,
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)),
116 (void *)((unsigned long)current_pid()),
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.
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 */
144 struct address_space
*mapping
;
148 mapping
= ip
->i_mapping
;
150 unsigned long index
, offset
;
152 offset
= (pos
& (PAGE_CACHE_SIZE
-1)); /* Within page */
153 index
= pos
>> PAGE_CACHE_SHIFT
;
154 bytes
= PAGE_CACHE_SIZE
- offset
;
159 page
= grab_cache_page(mapping
, index
);
164 status
= mapping
->a_ops
->prepare_write(NULL
, page
, offset
,
170 memset((void *) (kaddr
+ offset
), 0, bytes
);
171 flush_dcache_page(page
);
172 status
= mapping
->a_ops
->commit_write(NULL
, page
, offset
,
177 if (pos
> i_size_read(ip
))
178 i_size_write(ip
, pos
< end_size
? pos
: end_size
);
184 page_cache_release(page
);
192 ssize_t
/* bytes read, or (-) error */
196 const struct iovec
*iovp
,
202 struct file
*file
= iocb
->ki_filp
;
203 struct inode
*inode
= file
->f_mapping
->host
;
212 ip
= XFS_BHVTOI(bdp
);
213 vp
= BHV_TO_VNODE(bdp
);
216 XFS_STATS_INC(xs_read_calls
);
218 /* START copy & waste from filemap.c */
219 for (seg
= 0; seg
< segs
; seg
++) {
220 const struct iovec
*iv
= &iovp
[seg
];
223 * If any segment has a negative length, or the cumulative
224 * length ever wraps negative then return -EINVAL.
227 if (unlikely((ssize_t
)(size
|iv
->iov_len
) < 0))
228 return XFS_ERROR(-EINVAL
);
230 /* END copy & waste from filemap.c */
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
;
236 if ((*offset
& target
->bt_smask
) ||
237 (size
& target
->bt_smask
)) {
238 if (*offset
== ip
->i_d
.di_size
) {
241 return -XFS_ERROR(EINVAL
);
245 n
= XFS_MAXIOFFSET(mp
) - *offset
;
246 if ((n
<= 0) || (size
== 0))
252 if (XFS_FORCED_SHUTDOWN(mp
))
255 if (unlikely(ioflags
& IO_ISDIRECT
))
256 mutex_lock(&inode
->i_mutex
);
257 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
259 if (DM_EVENT_ENABLED(vp
->v_vfsp
, ip
, DM_EVENT_READ
) &&
260 !(ioflags
& IO_INVIS
)) {
261 vrwlock_t locktype
= VRWLOCK_READ
;
262 int dmflags
= FILP_DELAY_FLAG(file
) | DM_SEM_FLAG_RD(ioflags
);
264 ret
= -XFS_SEND_DATA(mp
, DM_EVENT_READ
,
265 BHV_TO_VNODE(bdp
), *offset
, size
,
268 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
273 if (unlikely((ioflags
& IO_ISDIRECT
) && VN_CACHED(vp
)))
274 VOP_FLUSHINVAL_PAGES(vp
, ctooff(offtoct(*offset
)),
275 -1, FI_REMAPF_LOCKED
);
277 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
);
283 XFS_STATS_ADD(xs_read_bytes
, ret
);
285 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
288 if (unlikely(ioflags
& IO_ISDIRECT
))
289 mutex_unlock(&inode
->i_mutex
);
310 ip
= XFS_BHVTOI(bdp
);
311 vp
= BHV_TO_VNODE(bdp
);
314 XFS_STATS_INC(xs_read_calls
);
316 n
= XFS_MAXIOFFSET(mp
) - *offset
;
317 if ((n
<= 0) || (count
== 0))
323 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
326 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
328 if (DM_EVENT_ENABLED(vp
->v_vfsp
, ip
, DM_EVENT_READ
) &&
329 (!(ioflags
& IO_INVIS
))) {
330 vrwlock_t locktype
= VRWLOCK_READ
;
333 error
= XFS_SEND_DATA(mp
, DM_EVENT_READ
, BHV_TO_VNODE(bdp
), *offset
, count
,
334 FILP_DELAY_FLAG(filp
), &locktype
);
336 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
340 xfs_rw_enter_trace(XFS_SENDFILE_ENTER
, &ip
->i_iocore
,
341 (void *)(unsigned long)target
, count
, *offset
, ioflags
);
342 ret
= generic_file_sendfile(filp
, offset
, count
, actor
, target
);
344 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
347 XFS_STATS_ADD(xs_read_bytes
, ret
);
353 * This routine is called to handle zeroing any space in the last
354 * block of the file that is beyond the EOF. We do this since the
355 * size is being increased without writing anything to that block
356 * and we don't want anyone to read the garbage on the disk.
358 STATIC
int /* error (positive) */
363 xfs_fsize_t end_size
)
365 xfs_fileoff_t last_fsb
;
371 xfs_bmbt_irec_t imap
;
374 ASSERT(ismrlocked(io
->io_lock
, MR_UPDATE
) != 0);
378 zero_offset
= XFS_B_FSB_OFFSET(mp
, isize
);
379 if (zero_offset
== 0) {
381 * There are no extra bytes in the last block on disk to
387 last_fsb
= XFS_B_TO_FSBT(mp
, isize
);
389 error
= XFS_BMAPI(mp
, NULL
, io
, last_fsb
, 1, 0, NULL
, 0, &imap
,
396 * If the block underlying isize is just a hole, then there
397 * is nothing to zero.
399 if (imap
.br_startblock
== HOLESTARTBLOCK
) {
403 * Zero the part of the last block beyond the EOF, and write it
404 * out sync. We need to drop the ilock while we do this so we
405 * don't deadlock when the buffer cache calls back to us.
407 XFS_IUNLOCK(mp
, io
, XFS_ILOCK_EXCL
| XFS_EXTSIZE_RD
);
408 loff
= XFS_FSB_TO_B(mp
, last_fsb
);
410 zero_len
= mp
->m_sb
.sb_blocksize
- zero_offset
;
412 error
= xfs_iozero(ip
, loff
+ zero_offset
, zero_len
, end_size
);
414 XFS_ILOCK(mp
, io
, XFS_ILOCK_EXCL
|XFS_EXTSIZE_RD
);
420 * Zero any on disk space between the current EOF and the new,
421 * larger EOF. This handles the normal case of zeroing the remainder
422 * of the last block in the file and the unusual case of zeroing blocks
423 * out beyond the size of the file. This second case only happens
424 * with fixed size extents and when the system crashes before the inode
425 * size was updated but after blocks were allocated. If fill is set,
426 * then any holes in the range are filled and zeroed. If not, the holes
427 * are left alone as holes.
430 int /* error (positive) */
434 xfs_off_t offset
, /* starting I/O offset */
435 xfs_fsize_t isize
, /* current inode size */
436 xfs_fsize_t end_size
) /* terminal inode size */
438 struct inode
*ip
= vn_to_inode(vp
);
439 xfs_fileoff_t start_zero_fsb
;
440 xfs_fileoff_t end_zero_fsb
;
441 xfs_fileoff_t zero_count_fsb
;
442 xfs_fileoff_t last_fsb
;
443 xfs_extlen_t buf_len_fsb
;
447 xfs_bmbt_irec_t imap
;
449 ASSERT(ismrlocked(io
->io_lock
, MR_UPDATE
));
450 ASSERT(ismrlocked(io
->io_iolock
, MR_UPDATE
));
451 ASSERT(offset
> isize
);
456 * First handle zeroing the block on which isize resides.
457 * We only zero a part of that block so it is handled specially.
459 error
= xfs_zero_last_block(ip
, io
, isize
, end_size
);
461 ASSERT(ismrlocked(io
->io_lock
, MR_UPDATE
));
462 ASSERT(ismrlocked(io
->io_iolock
, MR_UPDATE
));
467 * Calculate the range between the new size and the old
468 * where blocks needing to be zeroed may exist. To get the
469 * block where the last byte in the file currently resides,
470 * we need to subtract one from the size and truncate back
471 * to a block boundary. We subtract 1 in case the size is
472 * exactly on a block boundary.
474 last_fsb
= isize
? XFS_B_TO_FSBT(mp
, isize
- 1) : (xfs_fileoff_t
)-1;
475 start_zero_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)isize
);
476 end_zero_fsb
= XFS_B_TO_FSBT(mp
, offset
- 1);
477 ASSERT((xfs_sfiloff_t
)last_fsb
< (xfs_sfiloff_t
)start_zero_fsb
);
478 if (last_fsb
== end_zero_fsb
) {
480 * The size was only incremented on its last block.
481 * We took care of that above, so just return.
486 ASSERT(start_zero_fsb
<= end_zero_fsb
);
487 while (start_zero_fsb
<= end_zero_fsb
) {
489 zero_count_fsb
= end_zero_fsb
- start_zero_fsb
+ 1;
490 error
= XFS_BMAPI(mp
, NULL
, io
, start_zero_fsb
, zero_count_fsb
,
491 0, NULL
, 0, &imap
, &nimaps
, NULL
);
493 ASSERT(ismrlocked(io
->io_lock
, MR_UPDATE
));
494 ASSERT(ismrlocked(io
->io_iolock
, MR_UPDATE
));
499 if (imap
.br_state
== XFS_EXT_UNWRITTEN
||
500 imap
.br_startblock
== HOLESTARTBLOCK
) {
502 * This loop handles initializing pages that were
503 * partially initialized by the code below this
504 * loop. It basically zeroes the part of the page
505 * that sits on a hole and sets the page as P_HOLE
506 * and calls remapf if it is a mapped file.
508 start_zero_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
509 ASSERT(start_zero_fsb
<= (end_zero_fsb
+ 1));
514 * There are blocks in the range requested.
515 * Zero them a single write at a time. We actually
516 * don't zero the entire range returned if it is
517 * too big and simply loop around to get the rest.
518 * That is not the most efficient thing to do, but it
519 * is simple and this path should not be exercised often.
521 buf_len_fsb
= XFS_FILBLKS_MIN(imap
.br_blockcount
,
522 mp
->m_writeio_blocks
<< 8);
524 * Drop the inode lock while we're doing the I/O.
525 * We'll still have the iolock to protect us.
527 XFS_IUNLOCK(mp
, io
, XFS_ILOCK_EXCL
|XFS_EXTSIZE_RD
);
529 error
= xfs_iozero(ip
,
530 XFS_FSB_TO_B(mp
, start_zero_fsb
),
531 XFS_FSB_TO_B(mp
, buf_len_fsb
),
538 start_zero_fsb
= imap
.br_startoff
+ buf_len_fsb
;
539 ASSERT(start_zero_fsb
<= (end_zero_fsb
+ 1));
541 XFS_ILOCK(mp
, io
, XFS_ILOCK_EXCL
|XFS_EXTSIZE_RD
);
548 XFS_ILOCK(mp
, io
, XFS_ILOCK_EXCL
|XFS_EXTSIZE_RD
);
553 ssize_t
/* bytes written, or (-) error */
557 const struct iovec
*iovp
,
563 struct file
*file
= iocb
->ki_filp
;
564 struct address_space
*mapping
= file
->f_mapping
;
565 struct inode
*inode
= mapping
->host
;
566 unsigned long segs
= nsegs
;
569 ssize_t ret
= 0, error
= 0;
570 xfs_fsize_t isize
, new_size
;
577 size_t ocount
= 0, count
;
579 int need_i_mutex
= 1, need_flush
= 0;
581 XFS_STATS_INC(xs_write_calls
);
583 vp
= BHV_TO_VNODE(bdp
);
584 xip
= XFS_BHVTOI(bdp
);
586 for (seg
= 0; seg
< segs
; seg
++) {
587 const struct iovec
*iv
= &iovp
[seg
];
590 * If any segment has a negative length, or the cumulative
591 * length ever wraps negative then return -EINVAL.
593 ocount
+= iv
->iov_len
;
594 if (unlikely((ssize_t
)(ocount
|iv
->iov_len
) < 0))
596 if (access_ok(VERIFY_READ
, iv
->iov_base
, iv
->iov_len
))
601 ocount
-= iv
->iov_len
; /* This segment is no good */
614 if (XFS_FORCED_SHUTDOWN(mp
))
617 fs_check_frozen(vp
->v_vfsp
, SB_FREEZE_WRITE
);
619 if (ioflags
& IO_ISDIRECT
) {
620 xfs_buftarg_t
*target
=
621 (xip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
) ?
622 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
624 if ((pos
& target
->bt_smask
) || (count
& target
->bt_smask
))
625 return XFS_ERROR(-EINVAL
);
627 if (!VN_CACHED(vp
) && pos
< i_size_read(inode
))
636 iolock
= XFS_IOLOCK_EXCL
;
637 locktype
= VRWLOCK_WRITE
;
639 mutex_lock(&inode
->i_mutex
);
641 iolock
= XFS_IOLOCK_SHARED
;
642 locktype
= VRWLOCK_WRITE_DIRECT
;
645 xfs_ilock(xip
, XFS_ILOCK_EXCL
|iolock
);
647 isize
= i_size_read(inode
);
649 if (file
->f_flags
& O_APPEND
)
653 error
= -generic_write_checks(file
, &pos
, &count
,
654 S_ISBLK(inode
->i_mode
));
656 xfs_iunlock(xip
, XFS_ILOCK_EXCL
|iolock
);
657 goto out_unlock_mutex
;
660 new_size
= pos
+ count
;
661 if (new_size
> isize
)
662 io
->io_new_size
= new_size
;
664 if ((DM_EVENT_ENABLED(vp
->v_vfsp
, xip
, DM_EVENT_WRITE
) &&
665 !(ioflags
& IO_INVIS
) && !eventsent
)) {
666 loff_t savedsize
= pos
;
667 int dmflags
= FILP_DELAY_FLAG(file
);
670 dmflags
|= DM_FLAGS_IMUX
;
672 xfs_iunlock(xip
, XFS_ILOCK_EXCL
);
673 error
= XFS_SEND_DATA(xip
->i_mount
, DM_EVENT_WRITE
, vp
,
677 xfs_iunlock(xip
, iolock
);
678 goto out_unlock_mutex
;
680 xfs_ilock(xip
, XFS_ILOCK_EXCL
);
684 * The iolock was dropped and reacquired in XFS_SEND_DATA
685 * so we have to recheck the size when appending.
686 * We will only "goto start;" once, since having sent the
687 * event prevents another call to XFS_SEND_DATA, which is
688 * what allows the size to change in the first place.
690 if ((file
->f_flags
& O_APPEND
) && savedsize
!= isize
) {
691 pos
= isize
= xip
->i_d
.di_size
;
696 if (likely(!(ioflags
& IO_INVIS
))) {
697 file_update_time(file
);
698 xfs_ichgtime_fast(xip
, inode
,
699 XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
703 * If the offset is beyond the size of the file, we have a couple
704 * of things to do. First, if there is already space allocated
705 * we need to either create holes or zero the disk or ...
707 * If there is a page where the previous size lands, we need
708 * to zero it out up to the new size.
712 error
= xfs_zero_eof(BHV_TO_VNODE(bdp
), io
, pos
,
715 xfs_iunlock(xip
, XFS_ILOCK_EXCL
|iolock
);
716 goto out_unlock_mutex
;
719 xfs_iunlock(xip
, XFS_ILOCK_EXCL
);
722 * If we're writing the file then make sure to clear the
723 * setuid and setgid bits if the process is not being run
724 * by root. This keeps people from modifying setuid and
728 if (((xip
->i_d
.di_mode
& S_ISUID
) ||
729 ((xip
->i_d
.di_mode
& (S_ISGID
| S_IXGRP
)) ==
730 (S_ISGID
| S_IXGRP
))) &&
731 !capable(CAP_FSETID
)) {
732 error
= xfs_write_clear_setuid(xip
);
734 error
= -remove_suid(file
->f_dentry
);
735 if (unlikely(error
)) {
736 xfs_iunlock(xip
, iolock
);
737 goto out_unlock_mutex
;
742 /* We can write back this queue in page reclaim */
743 current
->backing_dev_info
= mapping
->backing_dev_info
;
745 if ((ioflags
& IO_ISDIRECT
)) {
747 xfs_inval_cached_trace(io
, pos
, -1,
748 ctooff(offtoct(pos
)), -1);
749 VOP_FLUSHINVAL_PAGES(vp
, ctooff(offtoct(pos
)),
750 -1, FI_REMAPF_LOCKED
);
754 /* demote the lock now the cached pages are gone */
755 XFS_ILOCK_DEMOTE(mp
, io
, XFS_IOLOCK_EXCL
);
756 mutex_unlock(&inode
->i_mutex
);
758 iolock
= XFS_IOLOCK_SHARED
;
759 locktype
= VRWLOCK_WRITE_DIRECT
;
763 xfs_rw_enter_trace(XFS_DIOWR_ENTER
, io
, (void *)iovp
, segs
,
765 ret
= generic_file_direct_write(iocb
, iovp
,
766 &segs
, pos
, offset
, count
, ocount
);
769 * direct-io write to a hole: fall through to buffered I/O
770 * for completing the rest of the request.
772 if (ret
>= 0 && ret
!= count
) {
773 XFS_STATS_ADD(xs_write_bytes
, ret
);
779 ioflags
&= ~IO_ISDIRECT
;
780 xfs_iunlock(xip
, iolock
);
784 xfs_rw_enter_trace(XFS_WRITE_ENTER
, io
, (void *)iovp
, segs
,
786 ret
= generic_file_buffered_write(iocb
, iovp
, segs
,
787 pos
, offset
, count
, ret
);
790 current
->backing_dev_info
= NULL
;
792 if (ret
== -EIOCBQUEUED
&& !(ioflags
& IO_ISAIO
))
793 ret
= wait_on_sync_kiocb(iocb
);
795 if ((ret
== -ENOSPC
) &&
796 DM_EVENT_ENABLED(vp
->v_vfsp
, xip
, DM_EVENT_NOSPACE
) &&
797 !(ioflags
& IO_INVIS
)) {
799 xfs_rwunlock(bdp
, locktype
);
801 mutex_unlock(&inode
->i_mutex
);
802 error
= XFS_SEND_NAMESP(xip
->i_mount
, DM_EVENT_NOSPACE
, vp
,
803 DM_RIGHT_NULL
, vp
, DM_RIGHT_NULL
, NULL
, NULL
,
804 0, 0, 0); /* Delay flag intentionally unused */
808 mutex_lock(&inode
->i_mutex
);
809 xfs_rwlock(bdp
, locktype
);
810 pos
= xip
->i_d
.di_size
;
815 isize
= i_size_read(inode
);
816 if (unlikely(ret
< 0 && ret
!= -EFAULT
&& *offset
> isize
))
819 if (*offset
> xip
->i_d
.di_size
) {
820 xfs_ilock(xip
, XFS_ILOCK_EXCL
);
821 if (*offset
> xip
->i_d
.di_size
) {
822 xip
->i_d
.di_size
= *offset
;
823 i_size_write(inode
, *offset
);
824 xip
->i_update_core
= 1;
825 xip
->i_update_size
= 1;
827 xfs_iunlock(xip
, XFS_ILOCK_EXCL
);
832 goto out_unlock_internal
;
834 XFS_STATS_ADD(xs_write_bytes
, ret
);
836 /* Handle various SYNC-type writes */
837 if ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
)) {
839 * If we're treating this as O_DSYNC and we have not updated the
840 * size, force the log.
842 if (!(mp
->m_flags
& XFS_MOUNT_OSYNCISOSYNC
) &&
843 !(xip
->i_update_size
)) {
844 xfs_inode_log_item_t
*iip
= xip
->i_itemp
;
847 * If an allocation transaction occurred
848 * without extending the size, then we have to force
849 * the log up the proper point to ensure that the
850 * allocation is permanent. We can't count on
851 * the fact that buffered writes lock out direct I/O
852 * writes - the direct I/O write could have extended
853 * the size nontransactionally, then finished before
854 * we started. xfs_write_file will think that the file
855 * didn't grow but the update isn't safe unless the
856 * size change is logged.
858 * Force the log if we've committed a transaction
859 * against the inode or if someone else has and
860 * the commit record hasn't gone to disk (e.g.
861 * the inode is pinned). This guarantees that
862 * all changes affecting the inode are permanent
865 if (iip
&& iip
->ili_last_lsn
) {
866 xfs_log_force(mp
, iip
->ili_last_lsn
,
867 XFS_LOG_FORCE
| XFS_LOG_SYNC
);
868 } else if (xfs_ipincount(xip
) > 0) {
869 xfs_log_force(mp
, (xfs_lsn_t
)0,
870 XFS_LOG_FORCE
| XFS_LOG_SYNC
);
877 * O_SYNC or O_DSYNC _with_ a size update are handled
880 * If the write was synchronous then we need to make
881 * sure that the inode modification time is permanent.
882 * We'll have updated the timestamp above, so here
883 * we use a synchronous transaction to log the inode.
884 * It's not fast, but it's necessary.
886 * If this a dsync write and the size got changed
887 * non-transactionally, then we need to ensure that
888 * the size change gets logged in a synchronous
892 tp
= xfs_trans_alloc(mp
, XFS_TRANS_WRITE_SYNC
);
893 if ((error
= xfs_trans_reserve(tp
, 0,
894 XFS_SWRITE_LOG_RES(mp
),
896 /* Transaction reserve failed */
897 xfs_trans_cancel(tp
, 0);
899 /* Transaction reserve successful */
900 xfs_ilock(xip
, XFS_ILOCK_EXCL
);
901 xfs_trans_ijoin(tp
, xip
, XFS_ILOCK_EXCL
);
902 xfs_trans_ihold(tp
, xip
);
903 xfs_trans_log_inode(tp
, xip
, XFS_ILOG_CORE
);
904 xfs_trans_set_sync(tp
);
905 error
= xfs_trans_commit(tp
, 0, NULL
);
906 xfs_iunlock(xip
, XFS_ILOCK_EXCL
);
909 goto out_unlock_internal
;
912 xfs_rwunlock(bdp
, locktype
);
914 mutex_unlock(&inode
->i_mutex
);
916 error
= sync_page_range(inode
, mapping
, pos
, ret
);
923 xfs_rwunlock(bdp
, locktype
);
926 mutex_unlock(&inode
->i_mutex
);
932 * All xfs metadata buffers except log state machine buffers
933 * get this attached as their b_bdstrat callback function.
934 * This is so that we can catch a buffer
935 * after prematurely unpinning it to forcibly shutdown the filesystem.
938 xfs_bdstrat_cb(struct xfs_buf
*bp
)
942 mp
= XFS_BUF_FSPRIVATE3(bp
, xfs_mount_t
*);
943 if (!XFS_FORCED_SHUTDOWN(mp
)) {
944 xfs_buf_iorequest(bp
);
947 xfs_buftrace("XFS__BDSTRAT IOERROR", bp
);
949 * Metadata write that didn't get logged but
950 * written delayed anyway. These aren't associated
951 * with a transaction, and can be ignored.
953 if (XFS_BUF_IODONE_FUNC(bp
) == NULL
&&
954 (XFS_BUF_ISREAD(bp
)) == 0)
955 return (xfs_bioerror_relse(bp
));
957 return (xfs_bioerror(bp
));
963 xfs_bmap(bhv_desc_t
*bdp
,
970 xfs_inode_t
*ip
= XFS_BHVTOI(bdp
);
971 xfs_iocore_t
*io
= &ip
->i_iocore
;
973 ASSERT((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
);
974 ASSERT(((ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
) != 0) ==
975 ((ip
->i_iocore
.io_flags
& XFS_IOCORE_RT
) != 0));
977 return xfs_iomap(io
, offset
, count
, flags
, iomapp
, niomaps
);
981 * Wrapper around bdstrat so that we can stop data
982 * from going to disk in case we are shutting down the filesystem.
983 * Typically user data goes thru this path; one of the exceptions
988 struct xfs_mount
*mp
,
992 if (!XFS_FORCED_SHUTDOWN(mp
)) {
993 /* Grio redirection would go here
994 * if (XFS_BUF_IS_GRIO(bp)) {
997 xfs_buf_iorequest(bp
);
1001 xfs_buftrace("XFSBDSTRAT IOERROR", bp
);
1002 return (xfs_bioerror_relse(bp
));
1006 * If the underlying (data/log/rt) device is readonly, there are some
1007 * operations that cannot proceed.
1010 xfs_dev_is_read_only(
1014 if (xfs_readonly_buftarg(mp
->m_ddev_targp
) ||
1015 xfs_readonly_buftarg(mp
->m_logdev_targp
) ||
1016 (mp
->m_rtdev_targp
&& xfs_readonly_buftarg(mp
->m_rtdev_targp
))) {
1018 "XFS: %s required on read-only device.", message
);
1020 "XFS: write access unavailable, cannot proceed.");