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! */
128 * Fsync operations on directories are much simpler than on regular files,
129 * as there is no file data to flush, and thus also no need for explicit
130 * cache flush operations, and there are no non-transaction metadata updates
131 * on directories either.
140 struct xfs_inode
*ip
= XFS_I(file
->f_mapping
->host
);
141 struct xfs_mount
*mp
= ip
->i_mount
;
144 trace_xfs_dir_fsync(ip
);
146 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
147 if (xfs_ipincount(ip
))
148 lsn
= ip
->i_itemp
->ili_last_lsn
;
149 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
153 return _xfs_log_force_lsn(mp
, lsn
, XFS_LOG_SYNC
, NULL
);
163 struct inode
*inode
= file
->f_mapping
->host
;
164 struct xfs_inode
*ip
= XFS_I(inode
);
165 struct xfs_mount
*mp
= ip
->i_mount
;
166 struct xfs_trans
*tp
;
171 trace_xfs_file_fsync(ip
);
173 error
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
177 if (XFS_FORCED_SHUTDOWN(mp
))
178 return -XFS_ERROR(EIO
);
180 xfs_iflags_clear(ip
, XFS_ITRUNCATED
);
182 if (mp
->m_flags
& XFS_MOUNT_BARRIER
) {
184 * If we have an RT and/or log subvolume we need to make sure
185 * to flush the write cache the device used for file data
186 * first. This is to ensure newly written file data make
187 * it to disk before logging the new inode size in case of
188 * an extending write.
190 if (XFS_IS_REALTIME_INODE(ip
))
191 xfs_blkdev_issue_flush(mp
->m_rtdev_targp
);
192 else if (mp
->m_logdev_targp
!= mp
->m_ddev_targp
)
193 xfs_blkdev_issue_flush(mp
->m_ddev_targp
);
197 * We always need to make sure that the required inode state is safe on
198 * disk. The inode might be clean but we still might need to force the
199 * log because of committed transactions that haven't hit the disk yet.
200 * Likewise, there could be unflushed non-transactional changes to the
201 * inode core that have to go to disk and this requires us to issue
202 * a synchronous transaction to capture these changes correctly.
204 * This code relies on the assumption that if the i_update_core field
205 * of the inode is clear and the inode is unpinned then it is clean
206 * and no action is required.
208 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
211 * First check if the VFS inode is marked dirty. All the dirtying
212 * of non-transactional updates do not go through mark_inode_dirty*,
213 * which allows us to distinguish between pure timestamp updates
214 * and i_size updates which need to be caught for fdatasync.
215 * After that also check for the dirty state in the XFS inode, which
216 * might gets cleared when the inode gets written out via the AIL
217 * or xfs_iflush_cluster.
219 if (((inode
->i_state
& I_DIRTY_DATASYNC
) ||
220 ((inode
->i_state
& I_DIRTY_SYNC
) && !datasync
)) &&
223 * Kick off a transaction to log the inode core to get the
224 * updates. The sync transaction will also force the log.
226 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
227 tp
= xfs_trans_alloc(mp
, XFS_TRANS_FSYNC_TS
);
228 error
= xfs_trans_reserve(tp
, 0,
229 XFS_FSYNC_TS_LOG_RES(mp
), 0, 0, 0);
231 xfs_trans_cancel(tp
, 0);
234 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
237 * Note - it's possible that we might have pushed ourselves out
238 * of the way during trans_reserve which would flush the inode.
239 * But there's no guarantee that the inode buffer has actually
240 * gone out yet (it's delwri). Plus the buffer could be pinned
241 * anyway if it's part of an inode in another recent
242 * transaction. So we play it safe and fire off the
243 * transaction anyway.
245 xfs_trans_ijoin(tp
, ip
, 0);
246 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
247 error
= xfs_trans_commit(tp
, 0);
249 lsn
= ip
->i_itemp
->ili_last_lsn
;
250 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
253 * Timestamps/size haven't changed since last inode flush or
254 * inode transaction commit. That means either nothing got
255 * written or a transaction committed which caught the updates.
256 * If the latter happened and the transaction hasn't hit the
257 * disk yet, the inode will be still be pinned. If it is,
260 if (xfs_ipincount(ip
))
261 lsn
= ip
->i_itemp
->ili_last_lsn
;
262 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
266 error
= _xfs_log_force_lsn(mp
, lsn
, XFS_LOG_SYNC
, &log_flushed
);
269 * If we only have a single device, and the log force about was
270 * a no-op we might have to flush the data device cache here.
271 * This can only happen for fdatasync/O_DSYNC if we were overwriting
272 * an already allocated file and thus do not have any metadata to
275 if ((mp
->m_flags
& XFS_MOUNT_BARRIER
) &&
276 mp
->m_logdev_targp
== mp
->m_ddev_targp
&&
277 !XFS_IS_REALTIME_INODE(ip
) &&
279 xfs_blkdev_issue_flush(mp
->m_ddev_targp
);
287 const struct iovec
*iovp
,
288 unsigned long nr_segs
,
291 struct file
*file
= iocb
->ki_filp
;
292 struct inode
*inode
= file
->f_mapping
->host
;
293 struct xfs_inode
*ip
= XFS_I(inode
);
294 struct xfs_mount
*mp
= ip
->i_mount
;
301 XFS_STATS_INC(xs_read_calls
);
303 BUG_ON(iocb
->ki_pos
!= pos
);
305 if (unlikely(file
->f_flags
& O_DIRECT
))
306 ioflags
|= IO_ISDIRECT
;
307 if (file
->f_mode
& FMODE_NOCMTIME
)
310 /* START copy & waste from filemap.c */
311 for (seg
= 0; seg
< nr_segs
; seg
++) {
312 const struct iovec
*iv
= &iovp
[seg
];
315 * If any segment has a negative length, or the cumulative
316 * length ever wraps negative then return -EINVAL.
319 if (unlikely((ssize_t
)(size
|iv
->iov_len
) < 0))
320 return XFS_ERROR(-EINVAL
);
322 /* END copy & waste from filemap.c */
324 if (unlikely(ioflags
& IO_ISDIRECT
)) {
325 xfs_buftarg_t
*target
=
326 XFS_IS_REALTIME_INODE(ip
) ?
327 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
328 if ((iocb
->ki_pos
& target
->bt_smask
) ||
329 (size
& target
->bt_smask
)) {
330 if (iocb
->ki_pos
== i_size_read(inode
))
332 return -XFS_ERROR(EINVAL
);
336 n
= XFS_MAXIOFFSET(mp
) - iocb
->ki_pos
;
337 if (n
<= 0 || size
== 0)
343 if (XFS_FORCED_SHUTDOWN(mp
))
347 * Locking is a bit tricky here. If we take an exclusive lock
348 * for direct IO, we effectively serialise all new concurrent
349 * read IO to this file and block it behind IO that is currently in
350 * progress because IO in progress holds the IO lock shared. We only
351 * need to hold the lock exclusive to blow away the page cache, so
352 * only take lock exclusively if the page cache needs invalidation.
353 * This allows the normal direct IO case of no page cache pages to
354 * proceeed concurrently without serialisation.
356 xfs_rw_ilock(ip
, XFS_IOLOCK_SHARED
);
357 if ((ioflags
& IO_ISDIRECT
) && inode
->i_mapping
->nrpages
) {
358 xfs_rw_iunlock(ip
, XFS_IOLOCK_SHARED
);
359 xfs_rw_ilock(ip
, XFS_IOLOCK_EXCL
);
361 if (inode
->i_mapping
->nrpages
) {
362 ret
= -xfs_flushinval_pages(ip
,
363 (iocb
->ki_pos
& PAGE_CACHE_MASK
),
364 -1, FI_REMAPF_LOCKED
);
366 xfs_rw_iunlock(ip
, XFS_IOLOCK_EXCL
);
370 xfs_rw_ilock_demote(ip
, XFS_IOLOCK_EXCL
);
373 trace_xfs_file_read(ip
, size
, iocb
->ki_pos
, ioflags
);
375 ret
= generic_file_aio_read(iocb
, iovp
, nr_segs
, iocb
->ki_pos
);
377 XFS_STATS_ADD(xs_read_bytes
, ret
);
379 xfs_rw_iunlock(ip
, XFS_IOLOCK_SHARED
);
384 xfs_file_splice_read(
387 struct pipe_inode_info
*pipe
,
391 struct xfs_inode
*ip
= XFS_I(infilp
->f_mapping
->host
);
395 XFS_STATS_INC(xs_read_calls
);
397 if (infilp
->f_mode
& FMODE_NOCMTIME
)
400 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
403 xfs_rw_ilock(ip
, XFS_IOLOCK_SHARED
);
405 trace_xfs_file_splice_read(ip
, count
, *ppos
, ioflags
);
407 ret
= generic_file_splice_read(infilp
, ppos
, pipe
, count
, flags
);
409 XFS_STATS_ADD(xs_read_bytes
, ret
);
411 xfs_rw_iunlock(ip
, XFS_IOLOCK_SHARED
);
416 * If this was a direct or synchronous I/O that failed (such as ENOSPC) then
417 * part of the I/O may have been written to disk before the error occurred. In
418 * this case the on-disk file size may have been adjusted beyond the in-memory
419 * file size and now needs to be truncated back.
422 xfs_aio_write_newsize_update(
423 struct xfs_inode
*ip
,
424 xfs_fsize_t new_size
)
426 if (new_size
== ip
->i_new_size
) {
427 xfs_rw_ilock(ip
, XFS_ILOCK_EXCL
);
428 if (new_size
== ip
->i_new_size
)
430 if (ip
->i_d
.di_size
> i_size_read(VFS_I(ip
)))
431 ip
->i_d
.di_size
= i_size_read(VFS_I(ip
));
432 xfs_rw_iunlock(ip
, XFS_ILOCK_EXCL
);
437 * xfs_file_splice_write() does not use xfs_rw_ilock() because
438 * generic_file_splice_write() takes the i_mutex itself. This, in theory,
439 * couuld cause lock inversions between the aio_write path and the splice path
440 * if someone is doing concurrent splice(2) based writes and write(2) based
441 * writes to the same inode. The only real way to fix this is to re-implement
442 * the generic code here with correct locking orders.
445 xfs_file_splice_write(
446 struct pipe_inode_info
*pipe
,
447 struct file
*outfilp
,
452 struct inode
*inode
= outfilp
->f_mapping
->host
;
453 struct xfs_inode
*ip
= XFS_I(inode
);
454 xfs_fsize_t new_size
;
458 XFS_STATS_INC(xs_write_calls
);
460 if (outfilp
->f_mode
& FMODE_NOCMTIME
)
463 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
466 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
468 new_size
= *ppos
+ count
;
470 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
471 if (new_size
> i_size_read(inode
))
472 ip
->i_new_size
= new_size
;
473 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
475 trace_xfs_file_splice_write(ip
, count
, *ppos
, ioflags
);
477 ret
= generic_file_splice_write(pipe
, outfilp
, ppos
, count
, flags
);
479 XFS_STATS_ADD(xs_write_bytes
, ret
);
481 xfs_aio_write_newsize_update(ip
, new_size
);
482 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
487 * This routine is called to handle zeroing any space in the last
488 * block of the file that is beyond the EOF. We do this since the
489 * size is being increased without writing anything to that block
490 * and we don't want anyone to read the garbage on the disk.
492 STATIC
int /* error (positive) */
498 xfs_fileoff_t last_fsb
;
499 xfs_mount_t
*mp
= ip
->i_mount
;
504 xfs_bmbt_irec_t imap
;
506 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
508 zero_offset
= XFS_B_FSB_OFFSET(mp
, isize
);
509 if (zero_offset
== 0) {
511 * There are no extra bytes in the last block on disk to
517 last_fsb
= XFS_B_TO_FSBT(mp
, isize
);
519 error
= xfs_bmapi_read(ip
, last_fsb
, 1, &imap
, &nimaps
, 0);
524 * If the block underlying isize is just a hole, then there
525 * is nothing to zero.
527 if (imap
.br_startblock
== HOLESTARTBLOCK
) {
531 * Zero the part of the last block beyond the EOF, and write it
532 * out sync. We need to drop the ilock while we do this so we
533 * don't deadlock when the buffer cache calls back to us.
535 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
537 zero_len
= mp
->m_sb
.sb_blocksize
- zero_offset
;
538 if (isize
+ zero_len
> offset
)
539 zero_len
= offset
- isize
;
540 error
= xfs_iozero(ip
, isize
, zero_len
);
542 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
548 * Zero any on disk space between the current EOF and the new,
549 * larger EOF. This handles the normal case of zeroing the remainder
550 * of the last block in the file and the unusual case of zeroing blocks
551 * out beyond the size of the file. This second case only happens
552 * with fixed size extents and when the system crashes before the inode
553 * size was updated but after blocks were allocated. If fill is set,
554 * then any holes in the range are filled and zeroed. If not, the holes
555 * are left alone as holes.
558 int /* error (positive) */
561 xfs_off_t offset
, /* starting I/O offset */
562 xfs_fsize_t isize
) /* current inode size */
564 xfs_mount_t
*mp
= ip
->i_mount
;
565 xfs_fileoff_t start_zero_fsb
;
566 xfs_fileoff_t end_zero_fsb
;
567 xfs_fileoff_t zero_count_fsb
;
568 xfs_fileoff_t last_fsb
;
569 xfs_fileoff_t zero_off
;
570 xfs_fsize_t zero_len
;
573 xfs_bmbt_irec_t imap
;
575 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
|XFS_IOLOCK_EXCL
));
576 ASSERT(offset
> isize
);
579 * First handle zeroing the block on which isize resides.
580 * We only zero a part of that block so it is handled specially.
582 error
= xfs_zero_last_block(ip
, offset
, isize
);
584 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
|XFS_IOLOCK_EXCL
));
589 * Calculate the range between the new size and the old
590 * where blocks needing to be zeroed may exist. To get the
591 * block where the last byte in the file currently resides,
592 * we need to subtract one from the size and truncate back
593 * to a block boundary. We subtract 1 in case the size is
594 * exactly on a block boundary.
596 last_fsb
= isize
? XFS_B_TO_FSBT(mp
, isize
- 1) : (xfs_fileoff_t
)-1;
597 start_zero_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)isize
);
598 end_zero_fsb
= XFS_B_TO_FSBT(mp
, offset
- 1);
599 ASSERT((xfs_sfiloff_t
)last_fsb
< (xfs_sfiloff_t
)start_zero_fsb
);
600 if (last_fsb
== end_zero_fsb
) {
602 * The size was only incremented on its last block.
603 * We took care of that above, so just return.
608 ASSERT(start_zero_fsb
<= end_zero_fsb
);
609 while (start_zero_fsb
<= end_zero_fsb
) {
611 zero_count_fsb
= end_zero_fsb
- start_zero_fsb
+ 1;
612 error
= xfs_bmapi_read(ip
, start_zero_fsb
, zero_count_fsb
,
615 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
|XFS_IOLOCK_EXCL
));
620 if (imap
.br_state
== XFS_EXT_UNWRITTEN
||
621 imap
.br_startblock
== HOLESTARTBLOCK
) {
623 * This loop handles initializing pages that were
624 * partially initialized by the code below this
625 * loop. It basically zeroes the part of the page
626 * that sits on a hole and sets the page as P_HOLE
627 * and calls remapf if it is a mapped file.
629 start_zero_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
630 ASSERT(start_zero_fsb
<= (end_zero_fsb
+ 1));
635 * There are blocks we need to zero.
636 * Drop the inode lock while we're doing the I/O.
637 * We'll still have the iolock to protect us.
639 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
641 zero_off
= XFS_FSB_TO_B(mp
, start_zero_fsb
);
642 zero_len
= XFS_FSB_TO_B(mp
, imap
.br_blockcount
);
644 if ((zero_off
+ zero_len
) > offset
)
645 zero_len
= offset
- zero_off
;
647 error
= xfs_iozero(ip
, zero_off
, zero_len
);
652 start_zero_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
653 ASSERT(start_zero_fsb
<= (end_zero_fsb
+ 1));
655 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
661 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
667 * Common pre-write limit and setup checks.
669 * Returns with iolock held according to @iolock.
672 xfs_file_aio_write_checks(
676 xfs_fsize_t
*new_sizep
,
679 struct inode
*inode
= file
->f_mapping
->host
;
680 struct xfs_inode
*ip
= XFS_I(inode
);
681 xfs_fsize_t new_size
;
684 xfs_rw_ilock(ip
, XFS_ILOCK_EXCL
);
687 error
= generic_write_checks(file
, pos
, count
, S_ISBLK(inode
->i_mode
));
689 xfs_rw_iunlock(ip
, XFS_ILOCK_EXCL
| *iolock
);
694 if (likely(!(file
->f_mode
& FMODE_NOCMTIME
)))
695 file_update_time(file
);
698 * If the offset is beyond the size of the file, we need to zero any
699 * blocks that fall between the existing EOF and the start of this
700 * write. There is no need to issue zeroing if another in-flght IO ends
701 * at or before this one If zeronig is needed and we are currently
702 * holding the iolock shared, we need to update it to exclusive which
703 * involves dropping all locks and relocking to maintain correct locking
704 * order. If we do this, restart the function to ensure all checks and
705 * values are still valid.
707 if ((ip
->i_new_size
&& *pos
> ip
->i_new_size
) ||
708 (!ip
->i_new_size
&& *pos
> i_size_read(inode
))) {
709 if (*iolock
== XFS_IOLOCK_SHARED
) {
710 xfs_rw_iunlock(ip
, XFS_ILOCK_EXCL
| *iolock
);
711 *iolock
= XFS_IOLOCK_EXCL
;
712 xfs_rw_ilock(ip
, XFS_ILOCK_EXCL
| *iolock
);
715 error
= -xfs_zero_eof(ip
, *pos
, i_size_read(inode
));
719 * If this IO extends beyond EOF, we may need to update ip->i_new_size.
720 * We have already zeroed space beyond EOF (if necessary). Only update
721 * ip->i_new_size if this IO ends beyond any other in-flight writes.
723 new_size
= *pos
+ *count
;
724 if (new_size
> i_size_read(inode
)) {
725 if (new_size
> ip
->i_new_size
)
726 ip
->i_new_size
= new_size
;
727 *new_sizep
= new_size
;
730 xfs_rw_iunlock(ip
, XFS_ILOCK_EXCL
);
735 * If we're writing the file then make sure to clear the setuid and
736 * setgid bits if the process is not being run by root. This keeps
737 * people from modifying setuid and setgid binaries.
739 return file_remove_suid(file
);
744 * xfs_file_dio_aio_write - handle direct IO writes
746 * Lock the inode appropriately to prepare for and issue a direct IO write.
747 * By separating it from the buffered write path we remove all the tricky to
748 * follow locking changes and looping.
750 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
751 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
752 * pages are flushed out.
754 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
755 * allowing them to be done in parallel with reads and other direct IO writes.
756 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
757 * needs to do sub-block zeroing and that requires serialisation against other
758 * direct IOs to the same block. In this case we need to serialise the
759 * submission of the unaligned IOs so that we don't get racing block zeroing in
760 * the dio layer. To avoid the problem with aio, we also need to wait for
761 * outstanding IOs to complete so that unwritten extent conversion is completed
762 * before we try to map the overlapping block. This is currently implemented by
763 * hitting it with a big hammer (i.e. inode_dio_wait()).
765 * Returns with locks held indicated by @iolock and errors indicated by
766 * negative return values.
769 xfs_file_dio_aio_write(
771 const struct iovec
*iovp
,
772 unsigned long nr_segs
,
775 xfs_fsize_t
*new_size
,
778 struct file
*file
= iocb
->ki_filp
;
779 struct address_space
*mapping
= file
->f_mapping
;
780 struct inode
*inode
= mapping
->host
;
781 struct xfs_inode
*ip
= XFS_I(inode
);
782 struct xfs_mount
*mp
= ip
->i_mount
;
784 size_t count
= ocount
;
785 int unaligned_io
= 0;
786 struct xfs_buftarg
*target
= XFS_IS_REALTIME_INODE(ip
) ?
787 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
790 if ((pos
& target
->bt_smask
) || (count
& target
->bt_smask
))
791 return -XFS_ERROR(EINVAL
);
793 if ((pos
& mp
->m_blockmask
) || ((pos
+ count
) & mp
->m_blockmask
))
797 * We don't need to take an exclusive lock unless there page cache needs
798 * to be invalidated or unaligned IO is being executed. We don't need to
799 * consider the EOF extension case here because
800 * xfs_file_aio_write_checks() will relock the inode as necessary for
801 * EOF zeroing cases and fill out the new inode size as appropriate.
803 if (unaligned_io
|| mapping
->nrpages
)
804 *iolock
= XFS_IOLOCK_EXCL
;
806 *iolock
= XFS_IOLOCK_SHARED
;
807 xfs_rw_ilock(ip
, *iolock
);
810 * Recheck if there are cached pages that need invalidate after we got
811 * the iolock to protect against other threads adding new pages while
812 * we were waiting for the iolock.
814 if (mapping
->nrpages
&& *iolock
== XFS_IOLOCK_SHARED
) {
815 xfs_rw_iunlock(ip
, *iolock
);
816 *iolock
= XFS_IOLOCK_EXCL
;
817 xfs_rw_ilock(ip
, *iolock
);
820 ret
= xfs_file_aio_write_checks(file
, &pos
, &count
, new_size
, iolock
);
824 if (mapping
->nrpages
) {
825 ret
= -xfs_flushinval_pages(ip
, (pos
& PAGE_CACHE_MASK
), -1,
832 * If we are doing unaligned IO, wait for all other IO to drain,
833 * otherwise demote the lock if we had to flush cached pages
836 inode_dio_wait(inode
);
837 else if (*iolock
== XFS_IOLOCK_EXCL
) {
838 xfs_rw_ilock_demote(ip
, XFS_IOLOCK_EXCL
);
839 *iolock
= XFS_IOLOCK_SHARED
;
842 trace_xfs_file_direct_write(ip
, count
, iocb
->ki_pos
, 0);
843 ret
= generic_file_direct_write(iocb
, iovp
,
844 &nr_segs
, pos
, &iocb
->ki_pos
, count
, ocount
);
846 /* No fallback to buffered IO on errors for XFS. */
847 ASSERT(ret
< 0 || ret
== count
);
852 xfs_file_buffered_aio_write(
854 const struct iovec
*iovp
,
855 unsigned long nr_segs
,
858 xfs_fsize_t
*new_size
,
861 struct file
*file
= iocb
->ki_filp
;
862 struct address_space
*mapping
= file
->f_mapping
;
863 struct inode
*inode
= mapping
->host
;
864 struct xfs_inode
*ip
= XFS_I(inode
);
867 size_t count
= ocount
;
869 *iolock
= XFS_IOLOCK_EXCL
;
870 xfs_rw_ilock(ip
, *iolock
);
872 ret
= xfs_file_aio_write_checks(file
, &pos
, &count
, new_size
, iolock
);
876 /* We can write back this queue in page reclaim */
877 current
->backing_dev_info
= mapping
->backing_dev_info
;
880 trace_xfs_file_buffered_write(ip
, count
, iocb
->ki_pos
, 0);
881 ret
= generic_file_buffered_write(iocb
, iovp
, nr_segs
,
882 pos
, &iocb
->ki_pos
, count
, ret
);
884 * if we just got an ENOSPC, flush the inode now we aren't holding any
885 * page locks and retry *once*
887 if (ret
== -ENOSPC
&& !enospc
) {
888 ret
= -xfs_flush_pages(ip
, 0, -1, 0, FI_NONE
);
894 current
->backing_dev_info
= NULL
;
901 const struct iovec
*iovp
,
902 unsigned long nr_segs
,
905 struct file
*file
= iocb
->ki_filp
;
906 struct address_space
*mapping
= file
->f_mapping
;
907 struct inode
*inode
= mapping
->host
;
908 struct xfs_inode
*ip
= XFS_I(inode
);
912 xfs_fsize_t new_size
= 0;
914 XFS_STATS_INC(xs_write_calls
);
916 BUG_ON(iocb
->ki_pos
!= pos
);
918 ret
= generic_segment_checks(iovp
, &nr_segs
, &ocount
, VERIFY_READ
);
925 xfs_wait_for_freeze(ip
->i_mount
, SB_FREEZE_WRITE
);
927 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
930 if (unlikely(file
->f_flags
& O_DIRECT
))
931 ret
= xfs_file_dio_aio_write(iocb
, iovp
, nr_segs
, pos
,
932 ocount
, &new_size
, &iolock
);
934 ret
= xfs_file_buffered_aio_write(iocb
, iovp
, nr_segs
, pos
,
935 ocount
, &new_size
, &iolock
);
940 XFS_STATS_ADD(xs_write_bytes
, ret
);
942 /* Handle various SYNC-type writes */
943 if ((file
->f_flags
& O_DSYNC
) || IS_SYNC(inode
)) {
944 loff_t end
= pos
+ ret
- 1;
947 xfs_rw_iunlock(ip
, iolock
);
948 error
= xfs_file_fsync(file
, pos
, end
,
949 (file
->f_flags
& __O_SYNC
) ? 0 : 1);
950 xfs_rw_ilock(ip
, iolock
);
956 xfs_aio_write_newsize_update(ip
, new_size
);
957 xfs_rw_iunlock(ip
, iolock
);
968 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
972 xfs_inode_t
*ip
= XFS_I(inode
);
973 int cmd
= XFS_IOC_RESVSP
;
974 int attr_flags
= XFS_ATTR_NOLOCK
;
976 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
983 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
985 if (mode
& FALLOC_FL_PUNCH_HOLE
)
986 cmd
= XFS_IOC_UNRESVSP
;
988 /* check the new inode size is valid before allocating */
989 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
990 offset
+ len
> i_size_read(inode
)) {
991 new_size
= offset
+ len
;
992 error
= inode_newsize_ok(inode
, new_size
);
997 if (file
->f_flags
& O_DSYNC
)
998 attr_flags
|= XFS_ATTR_SYNC
;
1000 error
= -xfs_change_file_space(ip
, cmd
, &bf
, 0, attr_flags
);
1004 /* Change file size if needed */
1008 iattr
.ia_valid
= ATTR_SIZE
;
1009 iattr
.ia_size
= new_size
;
1010 error
= -xfs_setattr_size(ip
, &iattr
, XFS_ATTR_NOLOCK
);
1014 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1021 struct inode
*inode
,
1024 if (!(file
->f_flags
& O_LARGEFILE
) && i_size_read(inode
) > MAX_NON_LFS
)
1026 if (XFS_FORCED_SHUTDOWN(XFS_M(inode
->i_sb
)))
1033 struct inode
*inode
,
1036 struct xfs_inode
*ip
= XFS_I(inode
);
1040 error
= xfs_file_open(inode
, file
);
1045 * If there are any blocks, read-ahead block 0 as we're almost
1046 * certain to have the next operation be a read there.
1048 mode
= xfs_ilock_map_shared(ip
);
1049 if (ip
->i_d
.di_nextents
> 0)
1050 xfs_da_reada_buf(NULL
, ip
, 0, XFS_DATA_FORK
);
1051 xfs_iunlock(ip
, mode
);
1057 struct inode
*inode
,
1060 return -xfs_release(XFS_I(inode
));
1069 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1070 xfs_inode_t
*ip
= XFS_I(inode
);
1075 * The Linux API doesn't pass down the total size of the buffer
1076 * we read into down to the filesystem. With the filldir concept
1077 * it's not needed for correct information, but the XFS dir2 leaf
1078 * code wants an estimate of the buffer size to calculate it's
1079 * readahead window and size the buffers used for mapping to
1082 * Try to give it an estimate that's good enough, maybe at some
1083 * point we can change the ->readdir prototype to include the
1084 * buffer size. For now we use the current glibc buffer size.
1086 bufsize
= (size_t)min_t(loff_t
, 32768, ip
->i_d
.di_size
);
1088 error
= xfs_readdir(ip
, dirent
, bufsize
,
1089 (xfs_off_t
*)&filp
->f_pos
, filldir
);
1098 struct vm_area_struct
*vma
)
1100 vma
->vm_ops
= &xfs_file_vm_ops
;
1101 vma
->vm_flags
|= VM_CAN_NONLINEAR
;
1103 file_accessed(filp
);
1108 * mmap()d file has taken write protection fault and is being made
1109 * writable. We can set the page state up correctly for a writable
1110 * page, which means we can do correct delalloc accounting (ENOSPC
1111 * checking!) and unwritten extent mapping.
1114 xfs_vm_page_mkwrite(
1115 struct vm_area_struct
*vma
,
1116 struct vm_fault
*vmf
)
1118 return block_page_mkwrite(vma
, vmf
, xfs_get_blocks
);
1121 const struct file_operations xfs_file_operations
= {
1122 .llseek
= generic_file_llseek
,
1123 .read
= do_sync_read
,
1124 .write
= do_sync_write
,
1125 .aio_read
= xfs_file_aio_read
,
1126 .aio_write
= xfs_file_aio_write
,
1127 .splice_read
= xfs_file_splice_read
,
1128 .splice_write
= xfs_file_splice_write
,
1129 .unlocked_ioctl
= xfs_file_ioctl
,
1130 #ifdef CONFIG_COMPAT
1131 .compat_ioctl
= xfs_file_compat_ioctl
,
1133 .mmap
= xfs_file_mmap
,
1134 .open
= xfs_file_open
,
1135 .release
= xfs_file_release
,
1136 .fsync
= xfs_file_fsync
,
1137 .fallocate
= xfs_file_fallocate
,
1140 const struct file_operations xfs_dir_file_operations
= {
1141 .open
= xfs_dir_open
,
1142 .read
= generic_read_dir
,
1143 .readdir
= xfs_file_readdir
,
1144 .llseek
= generic_file_llseek
,
1145 .unlocked_ioctl
= xfs_file_ioctl
,
1146 #ifdef CONFIG_COMPAT
1147 .compat_ioctl
= xfs_file_compat_ioctl
,
1149 .fsync
= xfs_dir_fsync
,
1152 static const struct vm_operations_struct xfs_file_vm_ops
= {
1153 .fault
= filemap_fault
,
1154 .page_mkwrite
= xfs_vm_page_mkwrite
,