2 * Copyright (c) 2000-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
25 #include "xfs_trans.h"
26 #include "xfs_mount.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_alloc.h"
29 #include "xfs_dinode.h"
30 #include "xfs_inode.h"
31 #include "xfs_inode_item.h"
33 #include "xfs_error.h"
34 #include "xfs_vnodeops.h"
35 #include "xfs_da_btree.h"
36 #include "xfs_ioctl.h"
37 #include "xfs_trace.h"
39 #include <linux/dcache.h>
40 #include <linux/falloc.h>
42 static const struct vm_operations_struct xfs_file_vm_ops
;
45 * Locking primitives for read and write IO paths to ensure we consistently use
46 * and order the inode->i_mutex, ip->i_lock and ip->i_iolock.
53 if (type
& XFS_IOLOCK_EXCL
)
54 mutex_lock(&VFS_I(ip
)->i_mutex
);
63 xfs_iunlock(ip
, type
);
64 if (type
& XFS_IOLOCK_EXCL
)
65 mutex_unlock(&VFS_I(ip
)->i_mutex
);
73 xfs_ilock_demote(ip
, type
);
74 if (type
& XFS_IOLOCK_EXCL
)
75 mutex_unlock(&VFS_I(ip
)->i_mutex
);
81 * xfs_iozero clears the specified range of buffer supplied,
82 * and marks all the affected blocks as valid and modified. If
83 * an affected block is not allocated, it will be allocated. If
84 * an affected block is not completely overwritten, and is not
85 * valid before the operation, it will be read from disk before
86 * being partially zeroed.
90 struct xfs_inode
*ip
, /* inode */
91 loff_t pos
, /* offset in file */
92 size_t count
) /* size of data to zero */
95 struct address_space
*mapping
;
98 mapping
= VFS_I(ip
)->i_mapping
;
100 unsigned offset
, bytes
;
103 offset
= (pos
& (PAGE_CACHE_SIZE
-1)); /* Within page */
104 bytes
= PAGE_CACHE_SIZE
- offset
;
108 status
= pagecache_write_begin(NULL
, mapping
, pos
, bytes
,
109 AOP_FLAG_UNINTERRUPTIBLE
,
114 zero_user(page
, offset
, bytes
);
116 status
= pagecache_write_end(NULL
, mapping
, pos
, bytes
, bytes
,
118 WARN_ON(status
<= 0); /* can't return less than zero! */
132 struct inode
*inode
= file
->f_mapping
->host
;
133 struct xfs_inode
*ip
= XFS_I(inode
);
134 struct xfs_trans
*tp
;
138 trace_xfs_file_fsync(ip
);
140 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
141 return -XFS_ERROR(EIO
);
143 xfs_iflags_clear(ip
, XFS_ITRUNCATED
);
148 * We always need to make sure that the required inode state is safe on
149 * disk. The inode might be clean but we still might need to force the
150 * log because of committed transactions that haven't hit the disk yet.
151 * Likewise, there could be unflushed non-transactional changes to the
152 * inode core that have to go to disk and this requires us to issue
153 * a synchronous transaction to capture these changes correctly.
155 * This code relies on the assumption that if the i_update_core field
156 * of the inode is clear and the inode is unpinned then it is clean
157 * and no action is required.
159 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
162 * First check if the VFS inode is marked dirty. All the dirtying
163 * of non-transactional updates no goes through mark_inode_dirty*,
164 * which allows us to distinguish beteeen pure timestamp updates
165 * and i_size updates which need to be caught for fdatasync.
166 * After that also theck for the dirty state in the XFS inode, which
167 * might gets cleared when the inode gets written out via the AIL
168 * or xfs_iflush_cluster.
170 if (((inode
->i_state
& I_DIRTY_DATASYNC
) ||
171 ((inode
->i_state
& I_DIRTY_SYNC
) && !datasync
)) &&
174 * Kick off a transaction to log the inode core to get the
175 * updates. The sync transaction will also force the log.
177 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
178 tp
= xfs_trans_alloc(ip
->i_mount
, XFS_TRANS_FSYNC_TS
);
179 error
= xfs_trans_reserve(tp
, 0,
180 XFS_FSYNC_TS_LOG_RES(ip
->i_mount
), 0, 0, 0);
182 xfs_trans_cancel(tp
, 0);
185 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
188 * Note - it's possible that we might have pushed ourselves out
189 * of the way during trans_reserve which would flush the inode.
190 * But there's no guarantee that the inode buffer has actually
191 * gone out yet (it's delwri). Plus the buffer could be pinned
192 * anyway if it's part of an inode in another recent
193 * transaction. So we play it safe and fire off the
194 * transaction anyway.
196 xfs_trans_ijoin(tp
, ip
);
197 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
198 xfs_trans_set_sync(tp
);
199 error
= _xfs_trans_commit(tp
, 0, &log_flushed
);
201 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
204 * Timestamps/size haven't changed since last inode flush or
205 * inode transaction commit. That means either nothing got
206 * written or a transaction committed which caught the updates.
207 * If the latter happened and the transaction hasn't hit the
208 * disk yet, the inode will be still be pinned. If it is,
211 if (xfs_ipincount(ip
)) {
212 error
= _xfs_log_force_lsn(ip
->i_mount
,
213 ip
->i_itemp
->ili_last_lsn
,
214 XFS_LOG_SYNC
, &log_flushed
);
216 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
219 if (ip
->i_mount
->m_flags
& XFS_MOUNT_BARRIER
) {
221 * If the log write didn't issue an ordered tag we need
222 * to flush the disk cache for the data device now.
225 xfs_blkdev_issue_flush(ip
->i_mount
->m_ddev_targp
);
228 * If this inode is on the RT dev we need to flush that
231 if (XFS_IS_REALTIME_INODE(ip
))
232 xfs_blkdev_issue_flush(ip
->i_mount
->m_rtdev_targp
);
241 const struct iovec
*iovp
,
242 unsigned long nr_segs
,
245 struct file
*file
= iocb
->ki_filp
;
246 struct inode
*inode
= file
->f_mapping
->host
;
247 struct xfs_inode
*ip
= XFS_I(inode
);
248 struct xfs_mount
*mp
= ip
->i_mount
;
255 XFS_STATS_INC(xs_read_calls
);
257 BUG_ON(iocb
->ki_pos
!= pos
);
259 if (unlikely(file
->f_flags
& O_DIRECT
))
260 ioflags
|= IO_ISDIRECT
;
261 if (file
->f_mode
& FMODE_NOCMTIME
)
264 /* START copy & waste from filemap.c */
265 for (seg
= 0; seg
< nr_segs
; seg
++) {
266 const struct iovec
*iv
= &iovp
[seg
];
269 * If any segment has a negative length, or the cumulative
270 * length ever wraps negative then return -EINVAL.
273 if (unlikely((ssize_t
)(size
|iv
->iov_len
) < 0))
274 return XFS_ERROR(-EINVAL
);
276 /* END copy & waste from filemap.c */
278 if (unlikely(ioflags
& IO_ISDIRECT
)) {
279 xfs_buftarg_t
*target
=
280 XFS_IS_REALTIME_INODE(ip
) ?
281 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
282 if ((iocb
->ki_pos
& target
->bt_smask
) ||
283 (size
& target
->bt_smask
)) {
284 if (iocb
->ki_pos
== ip
->i_size
)
286 return -XFS_ERROR(EINVAL
);
290 n
= XFS_MAXIOFFSET(mp
) - iocb
->ki_pos
;
291 if (n
<= 0 || size
== 0)
297 if (XFS_FORCED_SHUTDOWN(mp
))
300 if (unlikely(ioflags
& IO_ISDIRECT
)) {
301 xfs_rw_ilock(ip
, XFS_IOLOCK_EXCL
);
303 if (inode
->i_mapping
->nrpages
) {
304 ret
= -xfs_flushinval_pages(ip
,
305 (iocb
->ki_pos
& PAGE_CACHE_MASK
),
306 -1, FI_REMAPF_LOCKED
);
308 xfs_rw_iunlock(ip
, XFS_IOLOCK_EXCL
);
312 xfs_rw_ilock_demote(ip
, XFS_IOLOCK_EXCL
);
314 xfs_rw_ilock(ip
, XFS_IOLOCK_SHARED
);
316 trace_xfs_file_read(ip
, size
, iocb
->ki_pos
, ioflags
);
318 ret
= generic_file_aio_read(iocb
, iovp
, nr_segs
, iocb
->ki_pos
);
320 XFS_STATS_ADD(xs_read_bytes
, ret
);
322 xfs_rw_iunlock(ip
, XFS_IOLOCK_SHARED
);
327 xfs_file_splice_read(
330 struct pipe_inode_info
*pipe
,
334 struct xfs_inode
*ip
= XFS_I(infilp
->f_mapping
->host
);
338 XFS_STATS_INC(xs_read_calls
);
340 if (infilp
->f_mode
& FMODE_NOCMTIME
)
343 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
346 xfs_rw_ilock(ip
, XFS_IOLOCK_SHARED
);
348 trace_xfs_file_splice_read(ip
, count
, *ppos
, ioflags
);
350 ret
= generic_file_splice_read(infilp
, ppos
, pipe
, count
, flags
);
352 XFS_STATS_ADD(xs_read_bytes
, ret
);
354 xfs_rw_iunlock(ip
, XFS_IOLOCK_SHARED
);
359 xfs_aio_write_isize_update(
362 ssize_t bytes_written
)
364 struct xfs_inode
*ip
= XFS_I(inode
);
365 xfs_fsize_t isize
= i_size_read(inode
);
367 if (bytes_written
> 0)
368 XFS_STATS_ADD(xs_write_bytes
, bytes_written
);
370 if (unlikely(bytes_written
< 0 && bytes_written
!= -EFAULT
&&
374 if (*ppos
> ip
->i_size
) {
375 xfs_rw_ilock(ip
, XFS_ILOCK_EXCL
);
376 if (*ppos
> ip
->i_size
)
378 xfs_rw_iunlock(ip
, XFS_ILOCK_EXCL
);
383 * If this was a direct or synchronous I/O that failed (such as ENOSPC) then
384 * part of the I/O may have been written to disk before the error occurred. In
385 * this case the on-disk file size may have been adjusted beyond the in-memory
386 * file size and now needs to be truncated back.
389 xfs_aio_write_newsize_update(
390 struct xfs_inode
*ip
)
392 if (ip
->i_new_size
) {
393 xfs_rw_ilock(ip
, XFS_ILOCK_EXCL
);
395 if (ip
->i_d
.di_size
> ip
->i_size
)
396 ip
->i_d
.di_size
= ip
->i_size
;
397 xfs_rw_iunlock(ip
, XFS_ILOCK_EXCL
);
402 * xfs_file_splice_write() does not use xfs_rw_ilock() because
403 * generic_file_splice_write() takes the i_mutex itself. This, in theory,
404 * couuld cause lock inversions between the aio_write path and the splice path
405 * if someone is doing concurrent splice(2) based writes and write(2) based
406 * writes to the same inode. The only real way to fix this is to re-implement
407 * the generic code here with correct locking orders.
410 xfs_file_splice_write(
411 struct pipe_inode_info
*pipe
,
412 struct file
*outfilp
,
417 struct inode
*inode
= outfilp
->f_mapping
->host
;
418 struct xfs_inode
*ip
= XFS_I(inode
);
419 xfs_fsize_t new_size
;
423 XFS_STATS_INC(xs_write_calls
);
425 if (outfilp
->f_mode
& FMODE_NOCMTIME
)
428 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
431 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
433 new_size
= *ppos
+ count
;
435 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
436 if (new_size
> ip
->i_size
)
437 ip
->i_new_size
= new_size
;
438 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
440 trace_xfs_file_splice_write(ip
, count
, *ppos
, ioflags
);
442 ret
= generic_file_splice_write(pipe
, outfilp
, ppos
, count
, flags
);
444 xfs_aio_write_isize_update(inode
, ppos
, ret
);
445 xfs_aio_write_newsize_update(ip
);
446 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
451 * This routine is called to handle zeroing any space in the last
452 * block of the file that is beyond the EOF. We do this since the
453 * size is being increased without writing anything to that block
454 * and we don't want anyone to read the garbage on the disk.
456 STATIC
int /* error (positive) */
462 xfs_fileoff_t last_fsb
;
463 xfs_mount_t
*mp
= ip
->i_mount
;
468 xfs_bmbt_irec_t imap
;
470 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
472 zero_offset
= XFS_B_FSB_OFFSET(mp
, isize
);
473 if (zero_offset
== 0) {
475 * There are no extra bytes in the last block on disk to
481 last_fsb
= XFS_B_TO_FSBT(mp
, isize
);
483 error
= xfs_bmapi(NULL
, ip
, last_fsb
, 1, 0, NULL
, 0, &imap
,
490 * If the block underlying isize is just a hole, then there
491 * is nothing to zero.
493 if (imap
.br_startblock
== HOLESTARTBLOCK
) {
497 * Zero the part of the last block beyond the EOF, and write it
498 * out sync. We need to drop the ilock while we do this so we
499 * don't deadlock when the buffer cache calls back to us.
501 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
503 zero_len
= mp
->m_sb
.sb_blocksize
- zero_offset
;
504 if (isize
+ zero_len
> offset
)
505 zero_len
= offset
- isize
;
506 error
= xfs_iozero(ip
, isize
, zero_len
);
508 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
514 * Zero any on disk space between the current EOF and the new,
515 * larger EOF. This handles the normal case of zeroing the remainder
516 * of the last block in the file and the unusual case of zeroing blocks
517 * out beyond the size of the file. This second case only happens
518 * with fixed size extents and when the system crashes before the inode
519 * size was updated but after blocks were allocated. If fill is set,
520 * then any holes in the range are filled and zeroed. If not, the holes
521 * are left alone as holes.
524 int /* error (positive) */
527 xfs_off_t offset
, /* starting I/O offset */
528 xfs_fsize_t isize
) /* current inode size */
530 xfs_mount_t
*mp
= ip
->i_mount
;
531 xfs_fileoff_t start_zero_fsb
;
532 xfs_fileoff_t end_zero_fsb
;
533 xfs_fileoff_t zero_count_fsb
;
534 xfs_fileoff_t last_fsb
;
535 xfs_fileoff_t zero_off
;
536 xfs_fsize_t zero_len
;
539 xfs_bmbt_irec_t imap
;
541 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
|XFS_IOLOCK_EXCL
));
542 ASSERT(offset
> isize
);
545 * First handle zeroing the block on which isize resides.
546 * We only zero a part of that block so it is handled specially.
548 error
= xfs_zero_last_block(ip
, offset
, isize
);
550 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
|XFS_IOLOCK_EXCL
));
555 * Calculate the range between the new size and the old
556 * where blocks needing to be zeroed may exist. To get the
557 * block where the last byte in the file currently resides,
558 * we need to subtract one from the size and truncate back
559 * to a block boundary. We subtract 1 in case the size is
560 * exactly on a block boundary.
562 last_fsb
= isize
? XFS_B_TO_FSBT(mp
, isize
- 1) : (xfs_fileoff_t
)-1;
563 start_zero_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)isize
);
564 end_zero_fsb
= XFS_B_TO_FSBT(mp
, offset
- 1);
565 ASSERT((xfs_sfiloff_t
)last_fsb
< (xfs_sfiloff_t
)start_zero_fsb
);
566 if (last_fsb
== end_zero_fsb
) {
568 * The size was only incremented on its last block.
569 * We took care of that above, so just return.
574 ASSERT(start_zero_fsb
<= end_zero_fsb
);
575 while (start_zero_fsb
<= end_zero_fsb
) {
577 zero_count_fsb
= end_zero_fsb
- start_zero_fsb
+ 1;
578 error
= xfs_bmapi(NULL
, ip
, start_zero_fsb
, zero_count_fsb
,
579 0, NULL
, 0, &imap
, &nimaps
, NULL
);
581 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
|XFS_IOLOCK_EXCL
));
586 if (imap
.br_state
== XFS_EXT_UNWRITTEN
||
587 imap
.br_startblock
== HOLESTARTBLOCK
) {
589 * This loop handles initializing pages that were
590 * partially initialized by the code below this
591 * loop. It basically zeroes the part of the page
592 * that sits on a hole and sets the page as P_HOLE
593 * and calls remapf if it is a mapped file.
595 start_zero_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
596 ASSERT(start_zero_fsb
<= (end_zero_fsb
+ 1));
601 * There are blocks we need to zero.
602 * Drop the inode lock while we're doing the I/O.
603 * We'll still have the iolock to protect us.
605 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
607 zero_off
= XFS_FSB_TO_B(mp
, start_zero_fsb
);
608 zero_len
= XFS_FSB_TO_B(mp
, imap
.br_blockcount
);
610 if ((zero_off
+ zero_len
) > offset
)
611 zero_len
= offset
- zero_off
;
613 error
= xfs_iozero(ip
, zero_off
, zero_len
);
618 start_zero_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
619 ASSERT(start_zero_fsb
<= (end_zero_fsb
+ 1));
621 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
627 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
633 * Common pre-write limit and setup checks.
635 * Returns with iolock held according to @iolock.
638 xfs_file_aio_write_checks(
644 struct inode
*inode
= file
->f_mapping
->host
;
645 struct xfs_inode
*ip
= XFS_I(inode
);
646 xfs_fsize_t new_size
;
649 error
= generic_write_checks(file
, pos
, count
, S_ISBLK(inode
->i_mode
));
651 xfs_rw_iunlock(ip
, XFS_ILOCK_EXCL
| *iolock
);
656 new_size
= *pos
+ *count
;
657 if (new_size
> ip
->i_size
)
658 ip
->i_new_size
= new_size
;
660 if (likely(!(file
->f_mode
& FMODE_NOCMTIME
)))
661 file_update_time(file
);
664 * If the offset is beyond the size of the file, we need to zero any
665 * blocks that fall between the existing EOF and the start of this
668 if (*pos
> ip
->i_size
)
669 error
= -xfs_zero_eof(ip
, *pos
, ip
->i_size
);
671 xfs_rw_iunlock(ip
, XFS_ILOCK_EXCL
);
676 * If we're writing the file then make sure to clear the setuid and
677 * setgid bits if the process is not being run by root. This keeps
678 * people from modifying setuid and setgid binaries.
680 return file_remove_suid(file
);
685 * xfs_file_dio_aio_write - handle direct IO writes
687 * Lock the inode appropriately to prepare for and issue a direct IO write.
688 * By separating it from the buffered write path we remove all the tricky to
689 * follow locking changes and looping.
691 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
692 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
693 * pages are flushed out.
695 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
696 * allowing them to be done in parallel with reads and other direct IO writes.
697 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
698 * needs to do sub-block zeroing and that requires serialisation against other
699 * direct IOs to the same block. In this case we need to serialise the
700 * submission of the unaligned IOs so that we don't get racing block zeroing in
701 * the dio layer. To avoid the problem with aio, we also need to wait for
702 * outstanding IOs to complete so that unwritten extent conversion is completed
703 * before we try to map the overlapping block. This is currently implemented by
704 * hitting it with a big hammer (i.e. xfs_ioend_wait()).
706 * Returns with locks held indicated by @iolock and errors indicated by
707 * negative return values.
710 xfs_file_dio_aio_write(
712 const struct iovec
*iovp
,
713 unsigned long nr_segs
,
718 struct file
*file
= iocb
->ki_filp
;
719 struct address_space
*mapping
= file
->f_mapping
;
720 struct inode
*inode
= mapping
->host
;
721 struct xfs_inode
*ip
= XFS_I(inode
);
722 struct xfs_mount
*mp
= ip
->i_mount
;
724 size_t count
= ocount
;
725 int unaligned_io
= 0;
726 struct xfs_buftarg
*target
= XFS_IS_REALTIME_INODE(ip
) ?
727 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
730 if ((pos
& target
->bt_smask
) || (count
& target
->bt_smask
))
731 return -XFS_ERROR(EINVAL
);
733 if ((pos
& mp
->m_blockmask
) || ((pos
+ count
) & mp
->m_blockmask
))
736 if (unaligned_io
|| mapping
->nrpages
|| pos
> ip
->i_size
)
737 *iolock
= XFS_IOLOCK_EXCL
;
739 *iolock
= XFS_IOLOCK_SHARED
;
740 xfs_rw_ilock(ip
, XFS_ILOCK_EXCL
| *iolock
);
742 ret
= xfs_file_aio_write_checks(file
, &pos
, &count
, iolock
);
746 if (mapping
->nrpages
) {
747 WARN_ON(*iolock
!= XFS_IOLOCK_EXCL
);
748 ret
= -xfs_flushinval_pages(ip
, (pos
& PAGE_CACHE_MASK
), -1,
755 * If we are doing unaligned IO, wait for all other IO to drain,
756 * otherwise demote the lock if we had to flush cached pages
760 else if (*iolock
== XFS_IOLOCK_EXCL
) {
761 xfs_rw_ilock_demote(ip
, XFS_IOLOCK_EXCL
);
762 *iolock
= XFS_IOLOCK_SHARED
;
765 trace_xfs_file_direct_write(ip
, count
, iocb
->ki_pos
, 0);
766 ret
= generic_file_direct_write(iocb
, iovp
,
767 &nr_segs
, pos
, &iocb
->ki_pos
, count
, ocount
);
769 /* No fallback to buffered IO on errors for XFS. */
770 ASSERT(ret
< 0 || ret
== count
);
775 xfs_file_buffered_aio_write(
777 const struct iovec
*iovp
,
778 unsigned long nr_segs
,
783 struct file
*file
= iocb
->ki_filp
;
784 struct address_space
*mapping
= file
->f_mapping
;
785 struct inode
*inode
= mapping
->host
;
786 struct xfs_inode
*ip
= XFS_I(inode
);
789 size_t count
= ocount
;
791 *iolock
= XFS_IOLOCK_EXCL
;
792 xfs_rw_ilock(ip
, XFS_ILOCK_EXCL
| *iolock
);
794 ret
= xfs_file_aio_write_checks(file
, &pos
, &count
, iolock
);
798 /* We can write back this queue in page reclaim */
799 current
->backing_dev_info
= mapping
->backing_dev_info
;
802 trace_xfs_file_buffered_write(ip
, count
, iocb
->ki_pos
, 0);
803 ret
= generic_file_buffered_write(iocb
, iovp
, nr_segs
,
804 pos
, &iocb
->ki_pos
, count
, ret
);
806 * if we just got an ENOSPC, flush the inode now we aren't holding any
807 * page locks and retry *once*
809 if (ret
== -ENOSPC
&& !enospc
) {
810 ret
= -xfs_flush_pages(ip
, 0, -1, 0, FI_NONE
);
816 current
->backing_dev_info
= NULL
;
823 const struct iovec
*iovp
,
824 unsigned long nr_segs
,
827 struct file
*file
= iocb
->ki_filp
;
828 struct address_space
*mapping
= file
->f_mapping
;
829 struct inode
*inode
= mapping
->host
;
830 struct xfs_inode
*ip
= XFS_I(inode
);
835 XFS_STATS_INC(xs_write_calls
);
837 BUG_ON(iocb
->ki_pos
!= pos
);
839 ret
= generic_segment_checks(iovp
, &nr_segs
, &ocount
, VERIFY_READ
);
846 xfs_wait_for_freeze(ip
->i_mount
, SB_FREEZE_WRITE
);
848 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
851 if (unlikely(file
->f_flags
& O_DIRECT
))
852 ret
= xfs_file_dio_aio_write(iocb
, iovp
, nr_segs
, pos
,
855 ret
= xfs_file_buffered_aio_write(iocb
, iovp
, nr_segs
, pos
,
858 xfs_aio_write_isize_update(inode
, &iocb
->ki_pos
, ret
);
863 /* Handle various SYNC-type writes */
864 if ((file
->f_flags
& O_DSYNC
) || IS_SYNC(inode
)) {
865 loff_t end
= pos
+ ret
- 1;
868 xfs_rw_iunlock(ip
, iolock
);
869 error
= filemap_write_and_wait_range(mapping
, pos
, end
);
870 xfs_rw_ilock(ip
, iolock
);
872 error2
= -xfs_file_fsync(file
,
873 (file
->f_flags
& __O_SYNC
) ? 0 : 1);
881 xfs_aio_write_newsize_update(ip
);
882 xfs_rw_iunlock(ip
, iolock
);
893 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
897 xfs_inode_t
*ip
= XFS_I(inode
);
898 int cmd
= XFS_IOC_RESVSP
;
899 int attr_flags
= XFS_ATTR_NOLOCK
;
901 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
908 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
910 if (mode
& FALLOC_FL_PUNCH_HOLE
)
911 cmd
= XFS_IOC_UNRESVSP
;
913 /* check the new inode size is valid before allocating */
914 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
915 offset
+ len
> i_size_read(inode
)) {
916 new_size
= offset
+ len
;
917 error
= inode_newsize_ok(inode
, new_size
);
922 if (file
->f_flags
& O_DSYNC
)
923 attr_flags
|= XFS_ATTR_SYNC
;
925 error
= -xfs_change_file_space(ip
, cmd
, &bf
, 0, attr_flags
);
929 /* Change file size if needed */
933 iattr
.ia_valid
= ATTR_SIZE
;
934 iattr
.ia_size
= new_size
;
935 error
= -xfs_setattr(ip
, &iattr
, XFS_ATTR_NOLOCK
);
939 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
949 if (!(file
->f_flags
& O_LARGEFILE
) && i_size_read(inode
) > MAX_NON_LFS
)
951 if (XFS_FORCED_SHUTDOWN(XFS_M(inode
->i_sb
)))
961 struct xfs_inode
*ip
= XFS_I(inode
);
965 error
= xfs_file_open(inode
, file
);
970 * If there are any blocks, read-ahead block 0 as we're almost
971 * certain to have the next operation be a read there.
973 mode
= xfs_ilock_map_shared(ip
);
974 if (ip
->i_d
.di_nextents
> 0)
975 xfs_da_reada_buf(NULL
, ip
, 0, XFS_DATA_FORK
);
976 xfs_iunlock(ip
, mode
);
985 return -xfs_release(XFS_I(inode
));
994 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
995 xfs_inode_t
*ip
= XFS_I(inode
);
1000 * The Linux API doesn't pass down the total size of the buffer
1001 * we read into down to the filesystem. With the filldir concept
1002 * it's not needed for correct information, but the XFS dir2 leaf
1003 * code wants an estimate of the buffer size to calculate it's
1004 * readahead window and size the buffers used for mapping to
1007 * Try to give it an estimate that's good enough, maybe at some
1008 * point we can change the ->readdir prototype to include the
1009 * buffer size. For now we use the current glibc buffer size.
1011 bufsize
= (size_t)min_t(loff_t
, 32768, ip
->i_d
.di_size
);
1013 error
= xfs_readdir(ip
, dirent
, bufsize
,
1014 (xfs_off_t
*)&filp
->f_pos
, filldir
);
1023 struct vm_area_struct
*vma
)
1025 vma
->vm_ops
= &xfs_file_vm_ops
;
1026 vma
->vm_flags
|= VM_CAN_NONLINEAR
;
1028 file_accessed(filp
);
1033 * mmap()d file has taken write protection fault and is being made
1034 * writable. We can set the page state up correctly for a writable
1035 * page, which means we can do correct delalloc accounting (ENOSPC
1036 * checking!) and unwritten extent mapping.
1039 xfs_vm_page_mkwrite(
1040 struct vm_area_struct
*vma
,
1041 struct vm_fault
*vmf
)
1043 return block_page_mkwrite(vma
, vmf
, xfs_get_blocks
);
1046 const struct file_operations xfs_file_operations
= {
1047 .llseek
= generic_file_llseek
,
1048 .read
= do_sync_read
,
1049 .write
= do_sync_write
,
1050 .aio_read
= xfs_file_aio_read
,
1051 .aio_write
= xfs_file_aio_write
,
1052 .splice_read
= xfs_file_splice_read
,
1053 .splice_write
= xfs_file_splice_write
,
1054 .unlocked_ioctl
= xfs_file_ioctl
,
1055 #ifdef CONFIG_COMPAT
1056 .compat_ioctl
= xfs_file_compat_ioctl
,
1058 .mmap
= xfs_file_mmap
,
1059 .open
= xfs_file_open
,
1060 .release
= xfs_file_release
,
1061 .fsync
= xfs_file_fsync
,
1062 .fallocate
= xfs_file_fallocate
,
1065 const struct file_operations xfs_dir_file_operations
= {
1066 .open
= xfs_dir_open
,
1067 .read
= generic_read_dir
,
1068 .readdir
= xfs_file_readdir
,
1069 .llseek
= generic_file_llseek
,
1070 .unlocked_ioctl
= xfs_file_ioctl
,
1071 #ifdef CONFIG_COMPAT
1072 .compat_ioctl
= xfs_file_compat_ioctl
,
1074 .fsync
= xfs_file_fsync
,
1077 static const struct vm_operations_struct xfs_file_vm_ops
= {
1078 .fault
= filemap_fault
,
1079 .page_mkwrite
= xfs_vm_page_mkwrite
,