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! */
134 struct inode
*inode
= file
->f_mapping
->host
;
135 struct xfs_inode
*ip
= XFS_I(inode
);
136 struct xfs_mount
*mp
= ip
->i_mount
;
137 struct xfs_trans
*tp
;
141 trace_xfs_file_fsync(ip
);
143 error
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
147 if (XFS_FORCED_SHUTDOWN(mp
))
148 return -XFS_ERROR(EIO
);
150 xfs_iflags_clear(ip
, XFS_ITRUNCATED
);
152 if (mp
->m_flags
& XFS_MOUNT_BARRIER
) {
154 * If we have an RT and/or log subvolume we need to make sure
155 * to flush the write cache the device used for file data
156 * first. This is to ensure newly written file data make
157 * it to disk before logging the new inode size in case of
158 * an extending write.
160 if (XFS_IS_REALTIME_INODE(ip
))
161 xfs_blkdev_issue_flush(mp
->m_rtdev_targp
);
162 else if (mp
->m_logdev_targp
!= mp
->m_ddev_targp
)
163 xfs_blkdev_issue_flush(mp
->m_ddev_targp
);
167 * We always need to make sure that the required inode state is safe on
168 * disk. The inode might be clean but we still might need to force the
169 * log because of committed transactions that haven't hit the disk yet.
170 * Likewise, there could be unflushed non-transactional changes to the
171 * inode core that have to go to disk and this requires us to issue
172 * a synchronous transaction to capture these changes correctly.
174 * This code relies on the assumption that if the i_update_core field
175 * of the inode is clear and the inode is unpinned then it is clean
176 * and no action is required.
178 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
181 * First check if the VFS inode is marked dirty. All the dirtying
182 * of non-transactional updates no goes through mark_inode_dirty*,
183 * which allows us to distinguish beteeen pure timestamp updates
184 * and i_size updates which need to be caught for fdatasync.
185 * After that also theck for the dirty state in the XFS inode, which
186 * might gets cleared when the inode gets written out via the AIL
187 * or xfs_iflush_cluster.
189 if (((inode
->i_state
& I_DIRTY_DATASYNC
) ||
190 ((inode
->i_state
& I_DIRTY_SYNC
) && !datasync
)) &&
193 * Kick off a transaction to log the inode core to get the
194 * updates. The sync transaction will also force the log.
196 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
197 tp
= xfs_trans_alloc(mp
, XFS_TRANS_FSYNC_TS
);
198 error
= xfs_trans_reserve(tp
, 0,
199 XFS_FSYNC_TS_LOG_RES(mp
), 0, 0, 0);
201 xfs_trans_cancel(tp
, 0);
204 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
207 * Note - it's possible that we might have pushed ourselves out
208 * of the way during trans_reserve which would flush the inode.
209 * But there's no guarantee that the inode buffer has actually
210 * gone out yet (it's delwri). Plus the buffer could be pinned
211 * anyway if it's part of an inode in another recent
212 * transaction. So we play it safe and fire off the
213 * transaction anyway.
215 xfs_trans_ijoin(tp
, ip
);
216 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
217 xfs_trans_set_sync(tp
);
218 error
= _xfs_trans_commit(tp
, 0, &log_flushed
);
220 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
223 * Timestamps/size haven't changed since last inode flush or
224 * inode transaction commit. That means either nothing got
225 * written or a transaction committed which caught the updates.
226 * If the latter happened and the transaction hasn't hit the
227 * disk yet, the inode will be still be pinned. If it is,
230 if (xfs_ipincount(ip
)) {
231 error
= _xfs_log_force_lsn(mp
,
232 ip
->i_itemp
->ili_last_lsn
,
233 XFS_LOG_SYNC
, &log_flushed
);
235 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
239 * If we only have a single device, and the log force about was
240 * a no-op we might have to flush the data device cache here.
241 * This can only happen for fdatasync/O_DSYNC if we were overwriting
242 * an already allocated file and thus do not have any metadata to
245 if ((mp
->m_flags
& XFS_MOUNT_BARRIER
) &&
246 mp
->m_logdev_targp
== mp
->m_ddev_targp
&&
247 !XFS_IS_REALTIME_INODE(ip
) &&
249 xfs_blkdev_issue_flush(mp
->m_ddev_targp
);
257 const struct iovec
*iovp
,
258 unsigned long nr_segs
,
261 struct file
*file
= iocb
->ki_filp
;
262 struct inode
*inode
= file
->f_mapping
->host
;
263 struct xfs_inode
*ip
= XFS_I(inode
);
264 struct xfs_mount
*mp
= ip
->i_mount
;
271 XFS_STATS_INC(xs_read_calls
);
273 BUG_ON(iocb
->ki_pos
!= pos
);
275 if (unlikely(file
->f_flags
& O_DIRECT
))
276 ioflags
|= IO_ISDIRECT
;
277 if (file
->f_mode
& FMODE_NOCMTIME
)
280 /* START copy & waste from filemap.c */
281 for (seg
= 0; seg
< nr_segs
; seg
++) {
282 const struct iovec
*iv
= &iovp
[seg
];
285 * If any segment has a negative length, or the cumulative
286 * length ever wraps negative then return -EINVAL.
289 if (unlikely((ssize_t
)(size
|iv
->iov_len
) < 0))
290 return XFS_ERROR(-EINVAL
);
292 /* END copy & waste from filemap.c */
294 if (unlikely(ioflags
& IO_ISDIRECT
)) {
295 xfs_buftarg_t
*target
=
296 XFS_IS_REALTIME_INODE(ip
) ?
297 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
298 if ((iocb
->ki_pos
& target
->bt_smask
) ||
299 (size
& target
->bt_smask
)) {
300 if (iocb
->ki_pos
== ip
->i_size
)
302 return -XFS_ERROR(EINVAL
);
306 n
= XFS_MAXIOFFSET(mp
) - iocb
->ki_pos
;
307 if (n
<= 0 || size
== 0)
313 if (XFS_FORCED_SHUTDOWN(mp
))
317 * Locking is a bit tricky here. If we take an exclusive lock
318 * for direct IO, we effectively serialise all new concurrent
319 * read IO to this file and block it behind IO that is currently in
320 * progress because IO in progress holds the IO lock shared. We only
321 * need to hold the lock exclusive to blow away the page cache, so
322 * only take lock exclusively if the page cache needs invalidation.
323 * This allows the normal direct IO case of no page cache pages to
324 * proceeed concurrently without serialisation.
326 xfs_rw_ilock(ip
, XFS_IOLOCK_SHARED
);
327 if ((ioflags
& IO_ISDIRECT
) && inode
->i_mapping
->nrpages
) {
328 xfs_rw_iunlock(ip
, XFS_IOLOCK_SHARED
);
329 xfs_rw_ilock(ip
, XFS_IOLOCK_EXCL
);
331 if (inode
->i_mapping
->nrpages
) {
332 ret
= -xfs_flushinval_pages(ip
,
333 (iocb
->ki_pos
& PAGE_CACHE_MASK
),
334 -1, FI_REMAPF_LOCKED
);
336 xfs_rw_iunlock(ip
, XFS_IOLOCK_EXCL
);
340 xfs_rw_ilock_demote(ip
, XFS_IOLOCK_EXCL
);
343 trace_xfs_file_read(ip
, size
, iocb
->ki_pos
, ioflags
);
345 ret
= generic_file_aio_read(iocb
, iovp
, nr_segs
, iocb
->ki_pos
);
347 XFS_STATS_ADD(xs_read_bytes
, ret
);
349 xfs_rw_iunlock(ip
, XFS_IOLOCK_SHARED
);
354 xfs_file_splice_read(
357 struct pipe_inode_info
*pipe
,
361 struct xfs_inode
*ip
= XFS_I(infilp
->f_mapping
->host
);
365 XFS_STATS_INC(xs_read_calls
);
367 if (infilp
->f_mode
& FMODE_NOCMTIME
)
370 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
373 xfs_rw_ilock(ip
, XFS_IOLOCK_SHARED
);
375 trace_xfs_file_splice_read(ip
, count
, *ppos
, ioflags
);
377 ret
= generic_file_splice_read(infilp
, ppos
, pipe
, count
, flags
);
379 XFS_STATS_ADD(xs_read_bytes
, ret
);
381 xfs_rw_iunlock(ip
, XFS_IOLOCK_SHARED
);
386 xfs_aio_write_isize_update(
389 ssize_t bytes_written
)
391 struct xfs_inode
*ip
= XFS_I(inode
);
392 xfs_fsize_t isize
= i_size_read(inode
);
394 if (bytes_written
> 0)
395 XFS_STATS_ADD(xs_write_bytes
, bytes_written
);
397 if (unlikely(bytes_written
< 0 && bytes_written
!= -EFAULT
&&
401 if (*ppos
> ip
->i_size
) {
402 xfs_rw_ilock(ip
, XFS_ILOCK_EXCL
);
403 if (*ppos
> ip
->i_size
)
405 xfs_rw_iunlock(ip
, XFS_ILOCK_EXCL
);
410 * If this was a direct or synchronous I/O that failed (such as ENOSPC) then
411 * part of the I/O may have been written to disk before the error occurred. In
412 * this case the on-disk file size may have been adjusted beyond the in-memory
413 * file size and now needs to be truncated back.
416 xfs_aio_write_newsize_update(
417 struct xfs_inode
*ip
,
418 xfs_fsize_t new_size
)
420 if (new_size
== ip
->i_new_size
) {
421 xfs_rw_ilock(ip
, XFS_ILOCK_EXCL
);
422 if (new_size
== ip
->i_new_size
)
424 if (ip
->i_d
.di_size
> ip
->i_size
)
425 ip
->i_d
.di_size
= ip
->i_size
;
426 xfs_rw_iunlock(ip
, XFS_ILOCK_EXCL
);
431 * xfs_file_splice_write() does not use xfs_rw_ilock() because
432 * generic_file_splice_write() takes the i_mutex itself. This, in theory,
433 * couuld cause lock inversions between the aio_write path and the splice path
434 * if someone is doing concurrent splice(2) based writes and write(2) based
435 * writes to the same inode. The only real way to fix this is to re-implement
436 * the generic code here with correct locking orders.
439 xfs_file_splice_write(
440 struct pipe_inode_info
*pipe
,
441 struct file
*outfilp
,
446 struct inode
*inode
= outfilp
->f_mapping
->host
;
447 struct xfs_inode
*ip
= XFS_I(inode
);
448 xfs_fsize_t new_size
;
452 XFS_STATS_INC(xs_write_calls
);
454 if (outfilp
->f_mode
& FMODE_NOCMTIME
)
457 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
460 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
462 new_size
= *ppos
+ count
;
464 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
465 if (new_size
> ip
->i_size
)
466 ip
->i_new_size
= new_size
;
467 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
469 trace_xfs_file_splice_write(ip
, count
, *ppos
, ioflags
);
471 ret
= generic_file_splice_write(pipe
, outfilp
, ppos
, count
, flags
);
473 xfs_aio_write_isize_update(inode
, ppos
, ret
);
474 xfs_aio_write_newsize_update(ip
, new_size
);
475 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
480 * This routine is called to handle zeroing any space in the last
481 * block of the file that is beyond the EOF. We do this since the
482 * size is being increased without writing anything to that block
483 * and we don't want anyone to read the garbage on the disk.
485 STATIC
int /* error (positive) */
491 xfs_fileoff_t last_fsb
;
492 xfs_mount_t
*mp
= ip
->i_mount
;
497 xfs_bmbt_irec_t imap
;
499 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
501 zero_offset
= XFS_B_FSB_OFFSET(mp
, isize
);
502 if (zero_offset
== 0) {
504 * There are no extra bytes in the last block on disk to
510 last_fsb
= XFS_B_TO_FSBT(mp
, isize
);
512 error
= xfs_bmapi_read(ip
, last_fsb
, 1, &imap
, &nimaps
, 0);
517 * If the block underlying isize is just a hole, then there
518 * is nothing to zero.
520 if (imap
.br_startblock
== HOLESTARTBLOCK
) {
524 * Zero the part of the last block beyond the EOF, and write it
525 * out sync. We need to drop the ilock while we do this so we
526 * don't deadlock when the buffer cache calls back to us.
528 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
530 zero_len
= mp
->m_sb
.sb_blocksize
- zero_offset
;
531 if (isize
+ zero_len
> offset
)
532 zero_len
= offset
- isize
;
533 error
= xfs_iozero(ip
, isize
, zero_len
);
535 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
541 * Zero any on disk space between the current EOF and the new,
542 * larger EOF. This handles the normal case of zeroing the remainder
543 * of the last block in the file and the unusual case of zeroing blocks
544 * out beyond the size of the file. This second case only happens
545 * with fixed size extents and when the system crashes before the inode
546 * size was updated but after blocks were allocated. If fill is set,
547 * then any holes in the range are filled and zeroed. If not, the holes
548 * are left alone as holes.
551 int /* error (positive) */
554 xfs_off_t offset
, /* starting I/O offset */
555 xfs_fsize_t isize
) /* current inode size */
557 xfs_mount_t
*mp
= ip
->i_mount
;
558 xfs_fileoff_t start_zero_fsb
;
559 xfs_fileoff_t end_zero_fsb
;
560 xfs_fileoff_t zero_count_fsb
;
561 xfs_fileoff_t last_fsb
;
562 xfs_fileoff_t zero_off
;
563 xfs_fsize_t zero_len
;
566 xfs_bmbt_irec_t imap
;
568 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
|XFS_IOLOCK_EXCL
));
569 ASSERT(offset
> isize
);
572 * First handle zeroing the block on which isize resides.
573 * We only zero a part of that block so it is handled specially.
575 error
= xfs_zero_last_block(ip
, offset
, isize
);
577 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
|XFS_IOLOCK_EXCL
));
582 * Calculate the range between the new size and the old
583 * where blocks needing to be zeroed may exist. To get the
584 * block where the last byte in the file currently resides,
585 * we need to subtract one from the size and truncate back
586 * to a block boundary. We subtract 1 in case the size is
587 * exactly on a block boundary.
589 last_fsb
= isize
? XFS_B_TO_FSBT(mp
, isize
- 1) : (xfs_fileoff_t
)-1;
590 start_zero_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)isize
);
591 end_zero_fsb
= XFS_B_TO_FSBT(mp
, offset
- 1);
592 ASSERT((xfs_sfiloff_t
)last_fsb
< (xfs_sfiloff_t
)start_zero_fsb
);
593 if (last_fsb
== end_zero_fsb
) {
595 * The size was only incremented on its last block.
596 * We took care of that above, so just return.
601 ASSERT(start_zero_fsb
<= end_zero_fsb
);
602 while (start_zero_fsb
<= end_zero_fsb
) {
604 zero_count_fsb
= end_zero_fsb
- start_zero_fsb
+ 1;
605 error
= xfs_bmapi_read(ip
, start_zero_fsb
, zero_count_fsb
,
608 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
|XFS_IOLOCK_EXCL
));
613 if (imap
.br_state
== XFS_EXT_UNWRITTEN
||
614 imap
.br_startblock
== HOLESTARTBLOCK
) {
616 * This loop handles initializing pages that were
617 * partially initialized by the code below this
618 * loop. It basically zeroes the part of the page
619 * that sits on a hole and sets the page as P_HOLE
620 * and calls remapf if it is a mapped file.
622 start_zero_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
623 ASSERT(start_zero_fsb
<= (end_zero_fsb
+ 1));
628 * There are blocks we need to zero.
629 * Drop the inode lock while we're doing the I/O.
630 * We'll still have the iolock to protect us.
632 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
634 zero_off
= XFS_FSB_TO_B(mp
, start_zero_fsb
);
635 zero_len
= XFS_FSB_TO_B(mp
, imap
.br_blockcount
);
637 if ((zero_off
+ zero_len
) > offset
)
638 zero_len
= offset
- zero_off
;
640 error
= xfs_iozero(ip
, zero_off
, zero_len
);
645 start_zero_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
646 ASSERT(start_zero_fsb
<= (end_zero_fsb
+ 1));
648 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
654 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
660 * Common pre-write limit and setup checks.
662 * Returns with iolock held according to @iolock.
665 xfs_file_aio_write_checks(
669 xfs_fsize_t
*new_sizep
,
672 struct inode
*inode
= file
->f_mapping
->host
;
673 struct xfs_inode
*ip
= XFS_I(inode
);
674 xfs_fsize_t new_size
;
677 xfs_rw_ilock(ip
, XFS_ILOCK_EXCL
);
680 error
= generic_write_checks(file
, pos
, count
, S_ISBLK(inode
->i_mode
));
682 xfs_rw_iunlock(ip
, XFS_ILOCK_EXCL
| *iolock
);
687 if (likely(!(file
->f_mode
& FMODE_NOCMTIME
)))
688 file_update_time(file
);
691 * If the offset is beyond the size of the file, we need to zero any
692 * blocks that fall between the existing EOF and the start of this
693 * write. There is no need to issue zeroing if another in-flght IO ends
694 * at or before this one If zeronig is needed and we are currently
695 * holding the iolock shared, we need to update it to exclusive which
696 * involves dropping all locks and relocking to maintain correct locking
697 * order. If we do this, restart the function to ensure all checks and
698 * values are still valid.
700 if ((ip
->i_new_size
&& *pos
> ip
->i_new_size
) ||
701 (!ip
->i_new_size
&& *pos
> ip
->i_size
)) {
702 if (*iolock
== XFS_IOLOCK_SHARED
) {
703 xfs_rw_iunlock(ip
, XFS_ILOCK_EXCL
| *iolock
);
704 *iolock
= XFS_IOLOCK_EXCL
;
705 xfs_rw_ilock(ip
, XFS_ILOCK_EXCL
| *iolock
);
708 error
= -xfs_zero_eof(ip
, *pos
, ip
->i_size
);
712 * If this IO extends beyond EOF, we may need to update ip->i_new_size.
713 * We have already zeroed space beyond EOF (if necessary). Only update
714 * ip->i_new_size if this IO ends beyond any other in-flight writes.
716 new_size
= *pos
+ *count
;
717 if (new_size
> ip
->i_size
) {
718 if (new_size
> ip
->i_new_size
)
719 ip
->i_new_size
= new_size
;
720 *new_sizep
= new_size
;
723 xfs_rw_iunlock(ip
, XFS_ILOCK_EXCL
);
728 * If we're writing the file then make sure to clear the setuid and
729 * setgid bits if the process is not being run by root. This keeps
730 * people from modifying setuid and setgid binaries.
732 return file_remove_suid(file
);
737 * xfs_file_dio_aio_write - handle direct IO writes
739 * Lock the inode appropriately to prepare for and issue a direct IO write.
740 * By separating it from the buffered write path we remove all the tricky to
741 * follow locking changes and looping.
743 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
744 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
745 * pages are flushed out.
747 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
748 * allowing them to be done in parallel with reads and other direct IO writes.
749 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
750 * needs to do sub-block zeroing and that requires serialisation against other
751 * direct IOs to the same block. In this case we need to serialise the
752 * submission of the unaligned IOs so that we don't get racing block zeroing in
753 * the dio layer. To avoid the problem with aio, we also need to wait for
754 * outstanding IOs to complete so that unwritten extent conversion is completed
755 * before we try to map the overlapping block. This is currently implemented by
756 * hitting it with a big hammer (i.e. inode_dio_wait()).
758 * Returns with locks held indicated by @iolock and errors indicated by
759 * negative return values.
762 xfs_file_dio_aio_write(
764 const struct iovec
*iovp
,
765 unsigned long nr_segs
,
768 xfs_fsize_t
*new_size
,
771 struct file
*file
= iocb
->ki_filp
;
772 struct address_space
*mapping
= file
->f_mapping
;
773 struct inode
*inode
= mapping
->host
;
774 struct xfs_inode
*ip
= XFS_I(inode
);
775 struct xfs_mount
*mp
= ip
->i_mount
;
777 size_t count
= ocount
;
778 int unaligned_io
= 0;
779 struct xfs_buftarg
*target
= XFS_IS_REALTIME_INODE(ip
) ?
780 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
783 if ((pos
& target
->bt_smask
) || (count
& target
->bt_smask
))
784 return -XFS_ERROR(EINVAL
);
786 if ((pos
& mp
->m_blockmask
) || ((pos
+ count
) & mp
->m_blockmask
))
790 * We don't need to take an exclusive lock unless there page cache needs
791 * to be invalidated or unaligned IO is being executed. We don't need to
792 * consider the EOF extension case here because
793 * xfs_file_aio_write_checks() will relock the inode as necessary for
794 * EOF zeroing cases and fill out the new inode size as appropriate.
796 if (unaligned_io
|| mapping
->nrpages
)
797 *iolock
= XFS_IOLOCK_EXCL
;
799 *iolock
= XFS_IOLOCK_SHARED
;
800 xfs_rw_ilock(ip
, *iolock
);
803 * Recheck if there are cached pages that need invalidate after we got
804 * the iolock to protect against other threads adding new pages while
805 * we were waiting for the iolock.
807 if (mapping
->nrpages
&& *iolock
== XFS_IOLOCK_SHARED
) {
808 xfs_rw_iunlock(ip
, *iolock
);
809 *iolock
= XFS_IOLOCK_EXCL
;
810 xfs_rw_ilock(ip
, *iolock
);
813 ret
= xfs_file_aio_write_checks(file
, &pos
, &count
, new_size
, iolock
);
817 if (mapping
->nrpages
) {
818 ret
= -xfs_flushinval_pages(ip
, (pos
& PAGE_CACHE_MASK
), -1,
825 * If we are doing unaligned IO, wait for all other IO to drain,
826 * otherwise demote the lock if we had to flush cached pages
829 inode_dio_wait(inode
);
830 else if (*iolock
== XFS_IOLOCK_EXCL
) {
831 xfs_rw_ilock_demote(ip
, XFS_IOLOCK_EXCL
);
832 *iolock
= XFS_IOLOCK_SHARED
;
835 trace_xfs_file_direct_write(ip
, count
, iocb
->ki_pos
, 0);
836 ret
= generic_file_direct_write(iocb
, iovp
,
837 &nr_segs
, pos
, &iocb
->ki_pos
, count
, ocount
);
839 /* No fallback to buffered IO on errors for XFS. */
840 ASSERT(ret
< 0 || ret
== count
);
845 xfs_file_buffered_aio_write(
847 const struct iovec
*iovp
,
848 unsigned long nr_segs
,
851 xfs_fsize_t
*new_size
,
854 struct file
*file
= iocb
->ki_filp
;
855 struct address_space
*mapping
= file
->f_mapping
;
856 struct inode
*inode
= mapping
->host
;
857 struct xfs_inode
*ip
= XFS_I(inode
);
860 size_t count
= ocount
;
862 *iolock
= XFS_IOLOCK_EXCL
;
863 xfs_rw_ilock(ip
, *iolock
);
865 ret
= xfs_file_aio_write_checks(file
, &pos
, &count
, new_size
, iolock
);
869 /* We can write back this queue in page reclaim */
870 current
->backing_dev_info
= mapping
->backing_dev_info
;
873 trace_xfs_file_buffered_write(ip
, count
, iocb
->ki_pos
, 0);
874 ret
= generic_file_buffered_write(iocb
, iovp
, nr_segs
,
875 pos
, &iocb
->ki_pos
, count
, ret
);
877 * if we just got an ENOSPC, flush the inode now we aren't holding any
878 * page locks and retry *once*
880 if (ret
== -ENOSPC
&& !enospc
) {
881 ret
= -xfs_flush_pages(ip
, 0, -1, 0, FI_NONE
);
887 current
->backing_dev_info
= NULL
;
894 const struct iovec
*iovp
,
895 unsigned long nr_segs
,
898 struct file
*file
= iocb
->ki_filp
;
899 struct address_space
*mapping
= file
->f_mapping
;
900 struct inode
*inode
= mapping
->host
;
901 struct xfs_inode
*ip
= XFS_I(inode
);
905 xfs_fsize_t new_size
= 0;
907 XFS_STATS_INC(xs_write_calls
);
909 BUG_ON(iocb
->ki_pos
!= pos
);
911 ret
= generic_segment_checks(iovp
, &nr_segs
, &ocount
, VERIFY_READ
);
918 xfs_wait_for_freeze(ip
->i_mount
, SB_FREEZE_WRITE
);
920 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
923 if (unlikely(file
->f_flags
& O_DIRECT
))
924 ret
= xfs_file_dio_aio_write(iocb
, iovp
, nr_segs
, pos
,
925 ocount
, &new_size
, &iolock
);
927 ret
= xfs_file_buffered_aio_write(iocb
, iovp
, nr_segs
, pos
,
928 ocount
, &new_size
, &iolock
);
930 xfs_aio_write_isize_update(inode
, &iocb
->ki_pos
, ret
);
935 /* Handle various SYNC-type writes */
936 if ((file
->f_flags
& O_DSYNC
) || IS_SYNC(inode
)) {
937 loff_t end
= pos
+ ret
- 1;
940 xfs_rw_iunlock(ip
, iolock
);
941 error
= xfs_file_fsync(file
, pos
, end
,
942 (file
->f_flags
& __O_SYNC
) ? 0 : 1);
943 xfs_rw_ilock(ip
, iolock
);
949 xfs_aio_write_newsize_update(ip
, new_size
);
950 xfs_rw_iunlock(ip
, iolock
);
961 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
965 xfs_inode_t
*ip
= XFS_I(inode
);
966 int cmd
= XFS_IOC_RESVSP
;
967 int attr_flags
= XFS_ATTR_NOLOCK
;
969 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
976 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
978 if (mode
& FALLOC_FL_PUNCH_HOLE
)
979 cmd
= XFS_IOC_UNRESVSP
;
981 /* check the new inode size is valid before allocating */
982 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
983 offset
+ len
> i_size_read(inode
)) {
984 new_size
= offset
+ len
;
985 error
= inode_newsize_ok(inode
, new_size
);
990 if (file
->f_flags
& O_DSYNC
)
991 attr_flags
|= XFS_ATTR_SYNC
;
993 error
= -xfs_change_file_space(ip
, cmd
, &bf
, 0, attr_flags
);
997 /* Change file size if needed */
1001 iattr
.ia_valid
= ATTR_SIZE
;
1002 iattr
.ia_size
= new_size
;
1003 error
= -xfs_setattr_size(ip
, &iattr
, XFS_ATTR_NOLOCK
);
1007 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1014 struct inode
*inode
,
1017 if (!(file
->f_flags
& O_LARGEFILE
) && i_size_read(inode
) > MAX_NON_LFS
)
1019 if (XFS_FORCED_SHUTDOWN(XFS_M(inode
->i_sb
)))
1026 struct inode
*inode
,
1029 struct xfs_inode
*ip
= XFS_I(inode
);
1033 error
= xfs_file_open(inode
, file
);
1038 * If there are any blocks, read-ahead block 0 as we're almost
1039 * certain to have the next operation be a read there.
1041 mode
= xfs_ilock_map_shared(ip
);
1042 if (ip
->i_d
.di_nextents
> 0)
1043 xfs_da_reada_buf(NULL
, ip
, 0, XFS_DATA_FORK
);
1044 xfs_iunlock(ip
, mode
);
1050 struct inode
*inode
,
1053 return -xfs_release(XFS_I(inode
));
1062 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1063 xfs_inode_t
*ip
= XFS_I(inode
);
1068 * The Linux API doesn't pass down the total size of the buffer
1069 * we read into down to the filesystem. With the filldir concept
1070 * it's not needed for correct information, but the XFS dir2 leaf
1071 * code wants an estimate of the buffer size to calculate it's
1072 * readahead window and size the buffers used for mapping to
1075 * Try to give it an estimate that's good enough, maybe at some
1076 * point we can change the ->readdir prototype to include the
1077 * buffer size. For now we use the current glibc buffer size.
1079 bufsize
= (size_t)min_t(loff_t
, 32768, ip
->i_d
.di_size
);
1081 error
= xfs_readdir(ip
, dirent
, bufsize
,
1082 (xfs_off_t
*)&filp
->f_pos
, filldir
);
1091 struct vm_area_struct
*vma
)
1093 vma
->vm_ops
= &xfs_file_vm_ops
;
1094 vma
->vm_flags
|= VM_CAN_NONLINEAR
;
1096 file_accessed(filp
);
1101 * mmap()d file has taken write protection fault and is being made
1102 * writable. We can set the page state up correctly for a writable
1103 * page, which means we can do correct delalloc accounting (ENOSPC
1104 * checking!) and unwritten extent mapping.
1107 xfs_vm_page_mkwrite(
1108 struct vm_area_struct
*vma
,
1109 struct vm_fault
*vmf
)
1111 return block_page_mkwrite(vma
, vmf
, xfs_get_blocks
);
1114 const struct file_operations xfs_file_operations
= {
1115 .llseek
= generic_file_llseek
,
1116 .read
= do_sync_read
,
1117 .write
= do_sync_write
,
1118 .aio_read
= xfs_file_aio_read
,
1119 .aio_write
= xfs_file_aio_write
,
1120 .splice_read
= xfs_file_splice_read
,
1121 .splice_write
= xfs_file_splice_write
,
1122 .unlocked_ioctl
= xfs_file_ioctl
,
1123 #ifdef CONFIG_COMPAT
1124 .compat_ioctl
= xfs_file_compat_ioctl
,
1126 .mmap
= xfs_file_mmap
,
1127 .open
= xfs_file_open
,
1128 .release
= xfs_file_release
,
1129 .fsync
= xfs_file_fsync
,
1130 .fallocate
= xfs_file_fallocate
,
1133 const struct file_operations xfs_dir_file_operations
= {
1134 .open
= xfs_dir_open
,
1135 .read
= generic_read_dir
,
1136 .readdir
= xfs_file_readdir
,
1137 .llseek
= generic_file_llseek
,
1138 .unlocked_ioctl
= xfs_file_ioctl
,
1139 #ifdef CONFIG_COMPAT
1140 .compat_ioctl
= xfs_file_compat_ioctl
,
1142 .fsync
= xfs_file_fsync
,
1145 static const struct vm_operations_struct xfs_file_vm_ops
= {
1146 .fault
= filemap_fault
,
1147 .page_mkwrite
= xfs_vm_page_mkwrite
,