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
20 #include "xfs_shared.h"
24 #include "xfs_trans.h"
25 #include "xfs_mount.h"
26 #include "xfs_da_format.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_alloc.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
32 #include "xfs_inode_item.h"
34 #include "xfs_bmap_util.h"
35 #include "xfs_error.h"
37 #include "xfs_dir2_priv.h"
38 #include "xfs_ioctl.h"
39 #include "xfs_trace.h"
41 #include <linux/aio.h>
42 #include <linux/dcache.h>
43 #include <linux/falloc.h>
44 #include <linux/pagevec.h>
46 static const struct vm_operations_struct xfs_file_vm_ops
;
49 * Locking primitives for read and write IO paths to ensure we consistently use
50 * and order the inode->i_mutex, ip->i_lock and ip->i_iolock.
57 if (type
& XFS_IOLOCK_EXCL
)
58 mutex_lock(&VFS_I(ip
)->i_mutex
);
67 xfs_iunlock(ip
, type
);
68 if (type
& XFS_IOLOCK_EXCL
)
69 mutex_unlock(&VFS_I(ip
)->i_mutex
);
77 xfs_ilock_demote(ip
, type
);
78 if (type
& XFS_IOLOCK_EXCL
)
79 mutex_unlock(&VFS_I(ip
)->i_mutex
);
85 * xfs_iozero clears the specified range of buffer supplied,
86 * and marks all the affected blocks as valid and modified. If
87 * an affected block is not allocated, it will be allocated. If
88 * an affected block is not completely overwritten, and is not
89 * valid before the operation, it will be read from disk before
90 * being partially zeroed.
94 struct xfs_inode
*ip
, /* inode */
95 loff_t pos
, /* offset in file */
96 size_t count
) /* size of data to zero */
99 struct address_space
*mapping
;
102 mapping
= VFS_I(ip
)->i_mapping
;
104 unsigned offset
, bytes
;
107 offset
= (pos
& (PAGE_CACHE_SIZE
-1)); /* Within page */
108 bytes
= PAGE_CACHE_SIZE
- offset
;
112 status
= pagecache_write_begin(NULL
, mapping
, pos
, bytes
,
113 AOP_FLAG_UNINTERRUPTIBLE
,
118 zero_user(page
, offset
, bytes
);
120 status
= pagecache_write_end(NULL
, mapping
, pos
, bytes
, bytes
,
122 WARN_ON(status
<= 0); /* can't return less than zero! */
132 * Fsync operations on directories are much simpler than on regular files,
133 * as there is no file data to flush, and thus also no need for explicit
134 * cache flush operations, and there are no non-transaction metadata updates
135 * on directories either.
144 struct xfs_inode
*ip
= XFS_I(file
->f_mapping
->host
);
145 struct xfs_mount
*mp
= ip
->i_mount
;
148 trace_xfs_dir_fsync(ip
);
150 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
151 if (xfs_ipincount(ip
))
152 lsn
= ip
->i_itemp
->ili_last_lsn
;
153 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
157 return _xfs_log_force_lsn(mp
, lsn
, XFS_LOG_SYNC
, NULL
);
167 struct inode
*inode
= file
->f_mapping
->host
;
168 struct xfs_inode
*ip
= XFS_I(inode
);
169 struct xfs_mount
*mp
= ip
->i_mount
;
174 trace_xfs_file_fsync(ip
);
176 error
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
180 if (XFS_FORCED_SHUTDOWN(mp
))
181 return -XFS_ERROR(EIO
);
183 xfs_iflags_clear(ip
, XFS_ITRUNCATED
);
185 if (mp
->m_flags
& XFS_MOUNT_BARRIER
) {
187 * If we have an RT and/or log subvolume we need to make sure
188 * to flush the write cache the device used for file data
189 * first. This is to ensure newly written file data make
190 * it to disk before logging the new inode size in case of
191 * an extending write.
193 if (XFS_IS_REALTIME_INODE(ip
))
194 xfs_blkdev_issue_flush(mp
->m_rtdev_targp
);
195 else if (mp
->m_logdev_targp
!= mp
->m_ddev_targp
)
196 xfs_blkdev_issue_flush(mp
->m_ddev_targp
);
200 * All metadata updates are logged, which means that we just have
201 * to flush the log up to the latest LSN that touched the inode.
203 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
204 if (xfs_ipincount(ip
)) {
206 (ip
->i_itemp
->ili_fields
& ~XFS_ILOG_TIMESTAMP
))
207 lsn
= ip
->i_itemp
->ili_last_lsn
;
209 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
212 error
= _xfs_log_force_lsn(mp
, lsn
, XFS_LOG_SYNC
, &log_flushed
);
215 * If we only have a single device, and the log force about was
216 * a no-op we might have to flush the data device cache here.
217 * This can only happen for fdatasync/O_DSYNC if we were overwriting
218 * an already allocated file and thus do not have any metadata to
221 if ((mp
->m_flags
& XFS_MOUNT_BARRIER
) &&
222 mp
->m_logdev_targp
== mp
->m_ddev_targp
&&
223 !XFS_IS_REALTIME_INODE(ip
) &&
225 xfs_blkdev_issue_flush(mp
->m_ddev_targp
);
233 const struct iovec
*iovp
,
234 unsigned long nr_segs
,
237 struct file
*file
= iocb
->ki_filp
;
238 struct inode
*inode
= file
->f_mapping
->host
;
239 struct xfs_inode
*ip
= XFS_I(inode
);
240 struct xfs_mount
*mp
= ip
->i_mount
;
246 XFS_STATS_INC(xs_read_calls
);
248 BUG_ON(iocb
->ki_pos
!= pos
);
250 if (unlikely(file
->f_flags
& O_DIRECT
))
251 ioflags
|= IO_ISDIRECT
;
252 if (file
->f_mode
& FMODE_NOCMTIME
)
255 ret
= generic_segment_checks(iovp
, &nr_segs
, &size
, VERIFY_WRITE
);
259 if (unlikely(ioflags
& IO_ISDIRECT
)) {
260 xfs_buftarg_t
*target
=
261 XFS_IS_REALTIME_INODE(ip
) ?
262 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
263 if ((pos
& target
->bt_smask
) || (size
& target
->bt_smask
)) {
264 if (pos
== i_size_read(inode
))
266 return -XFS_ERROR(EINVAL
);
270 n
= mp
->m_super
->s_maxbytes
- pos
;
271 if (n
<= 0 || size
== 0)
277 if (XFS_FORCED_SHUTDOWN(mp
))
281 * Locking is a bit tricky here. If we take an exclusive lock
282 * for direct IO, we effectively serialise all new concurrent
283 * read IO to this file and block it behind IO that is currently in
284 * progress because IO in progress holds the IO lock shared. We only
285 * need to hold the lock exclusive to blow away the page cache, so
286 * only take lock exclusively if the page cache needs invalidation.
287 * This allows the normal direct IO case of no page cache pages to
288 * proceeed concurrently without serialisation.
290 xfs_rw_ilock(ip
, XFS_IOLOCK_SHARED
);
291 if ((ioflags
& IO_ISDIRECT
) && inode
->i_mapping
->nrpages
) {
292 xfs_rw_iunlock(ip
, XFS_IOLOCK_SHARED
);
293 xfs_rw_ilock(ip
, XFS_IOLOCK_EXCL
);
295 if (inode
->i_mapping
->nrpages
) {
296 ret
= -filemap_write_and_wait_range(
297 VFS_I(ip
)->i_mapping
,
300 xfs_rw_iunlock(ip
, XFS_IOLOCK_EXCL
);
303 truncate_pagecache_range(VFS_I(ip
), pos
, -1);
305 xfs_rw_ilock_demote(ip
, XFS_IOLOCK_EXCL
);
308 trace_xfs_file_read(ip
, size
, pos
, ioflags
);
310 ret
= generic_file_aio_read(iocb
, iovp
, nr_segs
, pos
);
312 XFS_STATS_ADD(xs_read_bytes
, ret
);
314 xfs_rw_iunlock(ip
, XFS_IOLOCK_SHARED
);
319 xfs_file_splice_read(
322 struct pipe_inode_info
*pipe
,
326 struct xfs_inode
*ip
= XFS_I(infilp
->f_mapping
->host
);
330 XFS_STATS_INC(xs_read_calls
);
332 if (infilp
->f_mode
& FMODE_NOCMTIME
)
335 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
338 xfs_rw_ilock(ip
, XFS_IOLOCK_SHARED
);
340 trace_xfs_file_splice_read(ip
, count
, *ppos
, ioflags
);
342 ret
= generic_file_splice_read(infilp
, ppos
, pipe
, count
, flags
);
344 XFS_STATS_ADD(xs_read_bytes
, ret
);
346 xfs_rw_iunlock(ip
, XFS_IOLOCK_SHARED
);
351 * xfs_file_splice_write() does not use xfs_rw_ilock() because
352 * generic_file_splice_write() takes the i_mutex itself. This, in theory,
353 * couuld cause lock inversions between the aio_write path and the splice path
354 * if someone is doing concurrent splice(2) based writes and write(2) based
355 * writes to the same inode. The only real way to fix this is to re-implement
356 * the generic code here with correct locking orders.
359 xfs_file_splice_write(
360 struct pipe_inode_info
*pipe
,
361 struct file
*outfilp
,
366 struct inode
*inode
= outfilp
->f_mapping
->host
;
367 struct xfs_inode
*ip
= XFS_I(inode
);
371 XFS_STATS_INC(xs_write_calls
);
373 if (outfilp
->f_mode
& FMODE_NOCMTIME
)
376 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
379 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
381 trace_xfs_file_splice_write(ip
, count
, *ppos
, ioflags
);
383 ret
= generic_file_splice_write(pipe
, outfilp
, ppos
, count
, flags
);
385 XFS_STATS_ADD(xs_write_bytes
, ret
);
387 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
392 * This routine is called to handle zeroing any space in the last block of the
393 * file that is beyond the EOF. We do this since the size is being increased
394 * without writing anything to that block and we don't want to read the
395 * garbage on the disk.
397 STATIC
int /* error (positive) */
399 struct xfs_inode
*ip
,
403 struct xfs_mount
*mp
= ip
->i_mount
;
404 xfs_fileoff_t last_fsb
= XFS_B_TO_FSBT(mp
, isize
);
405 int zero_offset
= XFS_B_FSB_OFFSET(mp
, isize
);
409 struct xfs_bmbt_irec imap
;
411 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
412 error
= xfs_bmapi_read(ip
, last_fsb
, 1, &imap
, &nimaps
, 0);
413 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
420 * If the block underlying isize is just a hole, then there
421 * is nothing to zero.
423 if (imap
.br_startblock
== HOLESTARTBLOCK
)
426 zero_len
= mp
->m_sb
.sb_blocksize
- zero_offset
;
427 if (isize
+ zero_len
> offset
)
428 zero_len
= offset
- isize
;
429 return xfs_iozero(ip
, isize
, zero_len
);
433 * Zero any on disk space between the current EOF and the new, larger EOF.
435 * This handles the normal case of zeroing the remainder of the last block in
436 * the file and the unusual case of zeroing blocks out beyond the size of the
437 * file. This second case only happens with fixed size extents and when the
438 * system crashes before the inode size was updated but after blocks were
441 * Expects the iolock to be held exclusive, and will take the ilock internally.
443 int /* error (positive) */
445 struct xfs_inode
*ip
,
446 xfs_off_t offset
, /* starting I/O offset */
447 xfs_fsize_t isize
) /* current inode size */
449 struct xfs_mount
*mp
= ip
->i_mount
;
450 xfs_fileoff_t start_zero_fsb
;
451 xfs_fileoff_t end_zero_fsb
;
452 xfs_fileoff_t zero_count_fsb
;
453 xfs_fileoff_t last_fsb
;
454 xfs_fileoff_t zero_off
;
455 xfs_fsize_t zero_len
;
458 struct xfs_bmbt_irec imap
;
460 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
461 ASSERT(offset
> isize
);
464 * First handle zeroing the block on which isize resides.
466 * We only zero a part of that block so it is handled specially.
468 if (XFS_B_FSB_OFFSET(mp
, isize
) != 0) {
469 error
= xfs_zero_last_block(ip
, offset
, isize
);
475 * Calculate the range between the new size and the old where blocks
476 * needing to be zeroed may exist.
478 * To get the block where the last byte in the file currently resides,
479 * we need to subtract one from the size and truncate back to a block
480 * boundary. We subtract 1 in case the size is exactly on a block
483 last_fsb
= isize
? XFS_B_TO_FSBT(mp
, isize
- 1) : (xfs_fileoff_t
)-1;
484 start_zero_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)isize
);
485 end_zero_fsb
= XFS_B_TO_FSBT(mp
, offset
- 1);
486 ASSERT((xfs_sfiloff_t
)last_fsb
< (xfs_sfiloff_t
)start_zero_fsb
);
487 if (last_fsb
== end_zero_fsb
) {
489 * The size was only incremented on its last block.
490 * We took care of that above, so just return.
495 ASSERT(start_zero_fsb
<= end_zero_fsb
);
496 while (start_zero_fsb
<= end_zero_fsb
) {
498 zero_count_fsb
= end_zero_fsb
- start_zero_fsb
+ 1;
500 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
501 error
= xfs_bmapi_read(ip
, start_zero_fsb
, zero_count_fsb
,
503 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
509 if (imap
.br_state
== XFS_EXT_UNWRITTEN
||
510 imap
.br_startblock
== HOLESTARTBLOCK
) {
511 start_zero_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
512 ASSERT(start_zero_fsb
<= (end_zero_fsb
+ 1));
517 * There are blocks we need to zero.
519 zero_off
= XFS_FSB_TO_B(mp
, start_zero_fsb
);
520 zero_len
= XFS_FSB_TO_B(mp
, imap
.br_blockcount
);
522 if ((zero_off
+ zero_len
) > offset
)
523 zero_len
= offset
- zero_off
;
525 error
= xfs_iozero(ip
, zero_off
, zero_len
);
529 start_zero_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
530 ASSERT(start_zero_fsb
<= (end_zero_fsb
+ 1));
537 * Common pre-write limit and setup checks.
539 * Called with the iolocked held either shared and exclusive according to
540 * @iolock, and returns with it held. Might upgrade the iolock to exclusive
541 * if called for a direct write beyond i_size.
544 xfs_file_aio_write_checks(
550 struct inode
*inode
= file
->f_mapping
->host
;
551 struct xfs_inode
*ip
= XFS_I(inode
);
555 error
= generic_write_checks(file
, pos
, count
, S_ISBLK(inode
->i_mode
));
560 * If the offset is beyond the size of the file, we need to zero any
561 * blocks that fall between the existing EOF and the start of this
562 * write. If zeroing is needed and we are currently holding the
563 * iolock shared, we need to update it to exclusive which implies
564 * having to redo all checks before.
566 if (*pos
> i_size_read(inode
)) {
567 if (*iolock
== XFS_IOLOCK_SHARED
) {
568 xfs_rw_iunlock(ip
, *iolock
);
569 *iolock
= XFS_IOLOCK_EXCL
;
570 xfs_rw_ilock(ip
, *iolock
);
573 error
= -xfs_zero_eof(ip
, *pos
, i_size_read(inode
));
579 * Updating the timestamps will grab the ilock again from
580 * xfs_fs_dirty_inode, so we have to call it after dropping the
581 * lock above. Eventually we should look into a way to avoid
582 * the pointless lock roundtrip.
584 if (likely(!(file
->f_mode
& FMODE_NOCMTIME
))) {
585 error
= file_update_time(file
);
591 * If we're writing the file then make sure to clear the setuid and
592 * setgid bits if the process is not being run by root. This keeps
593 * people from modifying setuid and setgid binaries.
595 return file_remove_suid(file
);
599 * xfs_file_dio_aio_write - handle direct IO writes
601 * Lock the inode appropriately to prepare for and issue a direct IO write.
602 * By separating it from the buffered write path we remove all the tricky to
603 * follow locking changes and looping.
605 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
606 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
607 * pages are flushed out.
609 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
610 * allowing them to be done in parallel with reads and other direct IO writes.
611 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
612 * needs to do sub-block zeroing and that requires serialisation against other
613 * direct IOs to the same block. In this case we need to serialise the
614 * submission of the unaligned IOs so that we don't get racing block zeroing in
615 * the dio layer. To avoid the problem with aio, we also need to wait for
616 * outstanding IOs to complete so that unwritten extent conversion is completed
617 * before we try to map the overlapping block. This is currently implemented by
618 * hitting it with a big hammer (i.e. inode_dio_wait()).
620 * Returns with locks held indicated by @iolock and errors indicated by
621 * negative return values.
624 xfs_file_dio_aio_write(
626 const struct iovec
*iovp
,
627 unsigned long nr_segs
,
631 struct file
*file
= iocb
->ki_filp
;
632 struct address_space
*mapping
= file
->f_mapping
;
633 struct inode
*inode
= mapping
->host
;
634 struct xfs_inode
*ip
= XFS_I(inode
);
635 struct xfs_mount
*mp
= ip
->i_mount
;
637 size_t count
= ocount
;
638 int unaligned_io
= 0;
640 struct xfs_buftarg
*target
= XFS_IS_REALTIME_INODE(ip
) ?
641 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
643 if ((pos
& target
->bt_smask
) || (count
& target
->bt_smask
))
644 return -XFS_ERROR(EINVAL
);
646 if ((pos
& mp
->m_blockmask
) || ((pos
+ count
) & mp
->m_blockmask
))
650 * We don't need to take an exclusive lock unless there page cache needs
651 * to be invalidated or unaligned IO is being executed. We don't need to
652 * consider the EOF extension case here because
653 * xfs_file_aio_write_checks() will relock the inode as necessary for
654 * EOF zeroing cases and fill out the new inode size as appropriate.
656 if (unaligned_io
|| mapping
->nrpages
)
657 iolock
= XFS_IOLOCK_EXCL
;
659 iolock
= XFS_IOLOCK_SHARED
;
660 xfs_rw_ilock(ip
, iolock
);
663 * Recheck if there are cached pages that need invalidate after we got
664 * the iolock to protect against other threads adding new pages while
665 * we were waiting for the iolock.
667 if (mapping
->nrpages
&& iolock
== XFS_IOLOCK_SHARED
) {
668 xfs_rw_iunlock(ip
, iolock
);
669 iolock
= XFS_IOLOCK_EXCL
;
670 xfs_rw_ilock(ip
, iolock
);
673 ret
= xfs_file_aio_write_checks(file
, &pos
, &count
, &iolock
);
677 if (mapping
->nrpages
) {
678 ret
= -filemap_write_and_wait_range(VFS_I(ip
)->i_mapping
,
682 truncate_pagecache_range(VFS_I(ip
), pos
, -1);
686 * If we are doing unaligned IO, wait for all other IO to drain,
687 * otherwise demote the lock if we had to flush cached pages
690 inode_dio_wait(inode
);
691 else if (iolock
== XFS_IOLOCK_EXCL
) {
692 xfs_rw_ilock_demote(ip
, XFS_IOLOCK_EXCL
);
693 iolock
= XFS_IOLOCK_SHARED
;
696 trace_xfs_file_direct_write(ip
, count
, iocb
->ki_pos
, 0);
697 ret
= generic_file_direct_write(iocb
, iovp
,
698 &nr_segs
, pos
, &iocb
->ki_pos
, count
, ocount
);
701 xfs_rw_iunlock(ip
, iolock
);
703 /* No fallback to buffered IO on errors for XFS. */
704 ASSERT(ret
< 0 || ret
== count
);
709 xfs_file_buffered_aio_write(
711 const struct iovec
*iovp
,
712 unsigned long nr_segs
,
716 struct file
*file
= iocb
->ki_filp
;
717 struct address_space
*mapping
= file
->f_mapping
;
718 struct inode
*inode
= mapping
->host
;
719 struct xfs_inode
*ip
= XFS_I(inode
);
722 int iolock
= XFS_IOLOCK_EXCL
;
723 size_t count
= ocount
;
725 xfs_rw_ilock(ip
, iolock
);
727 ret
= xfs_file_aio_write_checks(file
, &pos
, &count
, &iolock
);
731 /* We can write back this queue in page reclaim */
732 current
->backing_dev_info
= mapping
->backing_dev_info
;
735 trace_xfs_file_buffered_write(ip
, count
, iocb
->ki_pos
, 0);
736 ret
= generic_file_buffered_write(iocb
, iovp
, nr_segs
,
737 pos
, &iocb
->ki_pos
, count
, 0);
740 * If we just got an ENOSPC, try to write back all dirty inodes to
741 * convert delalloc space to free up some of the excess reserved
744 if (ret
== -ENOSPC
&& !enospc
) {
746 xfs_flush_inodes(ip
->i_mount
);
750 current
->backing_dev_info
= NULL
;
752 xfs_rw_iunlock(ip
, iolock
);
759 const struct iovec
*iovp
,
760 unsigned long nr_segs
,
763 struct file
*file
= iocb
->ki_filp
;
764 struct address_space
*mapping
= file
->f_mapping
;
765 struct inode
*inode
= mapping
->host
;
766 struct xfs_inode
*ip
= XFS_I(inode
);
770 XFS_STATS_INC(xs_write_calls
);
772 BUG_ON(iocb
->ki_pos
!= pos
);
774 ret
= generic_segment_checks(iovp
, &nr_segs
, &ocount
, VERIFY_READ
);
781 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
)) {
786 if (unlikely(file
->f_flags
& O_DIRECT
))
787 ret
= xfs_file_dio_aio_write(iocb
, iovp
, nr_segs
, pos
, ocount
);
789 ret
= xfs_file_buffered_aio_write(iocb
, iovp
, nr_segs
, pos
,
795 XFS_STATS_ADD(xs_write_bytes
, ret
);
797 /* Handle various SYNC-type writes */
798 err
= generic_write_sync(file
, pos
, ret
);
814 struct inode
*inode
= file_inode(file
);
815 struct xfs_inode
*ip
= XFS_I(inode
);
816 struct xfs_trans
*tp
;
820 if (!S_ISREG(inode
->i_mode
))
822 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
825 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
826 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
827 error
= xfs_free_file_space(ip
, offset
, len
);
831 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
832 offset
+ len
> i_size_read(inode
)) {
833 new_size
= offset
+ len
;
834 error
= -inode_newsize_ok(inode
, new_size
);
839 error
= xfs_alloc_file_space(ip
, offset
, len
,
845 tp
= xfs_trans_alloc(ip
->i_mount
, XFS_TRANS_WRITEID
);
846 error
= xfs_trans_reserve(tp
, &M_RES(ip
->i_mount
)->tr_writeid
, 0, 0);
848 xfs_trans_cancel(tp
, 0);
852 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
853 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
854 ip
->i_d
.di_mode
&= ~S_ISUID
;
855 if (ip
->i_d
.di_mode
& S_IXGRP
)
856 ip
->i_d
.di_mode
&= ~S_ISGID
;
858 if (!(mode
& FALLOC_FL_PUNCH_HOLE
))
859 ip
->i_d
.di_flags
|= XFS_DIFLAG_PREALLOC
;
861 xfs_trans_ichgtime(tp
, ip
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
862 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
864 if (file
->f_flags
& O_DSYNC
)
865 xfs_trans_set_sync(tp
);
866 error
= xfs_trans_commit(tp
, 0);
870 /* Change file size if needed */
874 iattr
.ia_valid
= ATTR_SIZE
;
875 iattr
.ia_size
= new_size
;
876 error
= xfs_setattr_size(ip
, &iattr
);
880 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
890 if (!(file
->f_flags
& O_LARGEFILE
) && i_size_read(inode
) > MAX_NON_LFS
)
892 if (XFS_FORCED_SHUTDOWN(XFS_M(inode
->i_sb
)))
902 struct xfs_inode
*ip
= XFS_I(inode
);
906 error
= xfs_file_open(inode
, file
);
911 * If there are any blocks, read-ahead block 0 as we're almost
912 * certain to have the next operation be a read there.
914 mode
= xfs_ilock_map_shared(ip
);
915 if (ip
->i_d
.di_nextents
> 0)
916 xfs_dir3_data_readahead(NULL
, ip
, 0, -1);
917 xfs_iunlock(ip
, mode
);
926 return -xfs_release(XFS_I(inode
));
932 struct dir_context
*ctx
)
934 struct inode
*inode
= file_inode(file
);
935 xfs_inode_t
*ip
= XFS_I(inode
);
940 * The Linux API doesn't pass down the total size of the buffer
941 * we read into down to the filesystem. With the filldir concept
942 * it's not needed for correct information, but the XFS dir2 leaf
943 * code wants an estimate of the buffer size to calculate it's
944 * readahead window and size the buffers used for mapping to
947 * Try to give it an estimate that's good enough, maybe at some
948 * point we can change the ->readdir prototype to include the
949 * buffer size. For now we use the current glibc buffer size.
951 bufsize
= (size_t)min_t(loff_t
, 32768, ip
->i_d
.di_size
);
953 error
= xfs_readdir(ip
, ctx
, bufsize
);
962 struct vm_area_struct
*vma
)
964 vma
->vm_ops
= &xfs_file_vm_ops
;
971 * mmap()d file has taken write protection fault and is being made
972 * writable. We can set the page state up correctly for a writable
973 * page, which means we can do correct delalloc accounting (ENOSPC
974 * checking!) and unwritten extent mapping.
978 struct vm_area_struct
*vma
,
979 struct vm_fault
*vmf
)
981 return block_page_mkwrite(vma
, vmf
, xfs_get_blocks
);
985 * This type is designed to indicate the type of offset we would like
986 * to search from page cache for either xfs_seek_data() or xfs_seek_hole().
994 * Lookup the desired type of offset from the given page.
996 * On success, return true and the offset argument will point to the
997 * start of the region that was found. Otherwise this function will
998 * return false and keep the offset argument unchanged.
1001 xfs_lookup_buffer_offset(
1006 loff_t lastoff
= page_offset(page
);
1008 struct buffer_head
*bh
, *head
;
1010 bh
= head
= page_buffers(page
);
1013 * Unwritten extents that have data in the page
1014 * cache covering them can be identified by the
1015 * BH_Unwritten state flag. Pages with multiple
1016 * buffers might have a mix of holes, data and
1017 * unwritten extents - any buffer with valid
1018 * data in it should have BH_Uptodate flag set
1021 if (buffer_unwritten(bh
) ||
1022 buffer_uptodate(bh
)) {
1023 if (type
== DATA_OFF
)
1026 if (type
== HOLE_OFF
)
1034 lastoff
+= bh
->b_size
;
1035 } while ((bh
= bh
->b_this_page
) != head
);
1041 * This routine is called to find out and return a data or hole offset
1042 * from the page cache for unwritten extents according to the desired
1043 * type for xfs_seek_data() or xfs_seek_hole().
1045 * The argument offset is used to tell where we start to search from the
1046 * page cache. Map is used to figure out the end points of the range to
1049 * Return true if the desired type of offset was found, and the argument
1050 * offset is filled with that address. Otherwise, return false and keep
1054 xfs_find_get_desired_pgoff(
1055 struct inode
*inode
,
1056 struct xfs_bmbt_irec
*map
,
1060 struct xfs_inode
*ip
= XFS_I(inode
);
1061 struct xfs_mount
*mp
= ip
->i_mount
;
1062 struct pagevec pvec
;
1066 loff_t startoff
= *offset
;
1067 loff_t lastoff
= startoff
;
1070 pagevec_init(&pvec
, 0);
1072 index
= startoff
>> PAGE_CACHE_SHIFT
;
1073 endoff
= XFS_FSB_TO_B(mp
, map
->br_startoff
+ map
->br_blockcount
);
1074 end
= endoff
>> PAGE_CACHE_SHIFT
;
1080 want
= min_t(pgoff_t
, end
- index
, PAGEVEC_SIZE
);
1081 nr_pages
= pagevec_lookup(&pvec
, inode
->i_mapping
, index
,
1084 * No page mapped into given range. If we are searching holes
1085 * and if this is the first time we got into the loop, it means
1086 * that the given offset is landed in a hole, return it.
1088 * If we have already stepped through some block buffers to find
1089 * holes but they all contains data. In this case, the last
1090 * offset is already updated and pointed to the end of the last
1091 * mapped page, if it does not reach the endpoint to search,
1092 * that means there should be a hole between them.
1094 if (nr_pages
== 0) {
1095 /* Data search found nothing */
1096 if (type
== DATA_OFF
)
1099 ASSERT(type
== HOLE_OFF
);
1100 if (lastoff
== startoff
|| lastoff
< endoff
) {
1108 * At lease we found one page. If this is the first time we
1109 * step into the loop, and if the first page index offset is
1110 * greater than the given search offset, a hole was found.
1112 if (type
== HOLE_OFF
&& lastoff
== startoff
&&
1113 lastoff
< page_offset(pvec
.pages
[0])) {
1118 for (i
= 0; i
< nr_pages
; i
++) {
1119 struct page
*page
= pvec
.pages
[i
];
1123 * At this point, the page may be truncated or
1124 * invalidated (changing page->mapping to NULL),
1125 * or even swizzled back from swapper_space to tmpfs
1126 * file mapping. However, page->index will not change
1127 * because we have a reference on the page.
1129 * Searching done if the page index is out of range.
1130 * If the current offset is not reaches the end of
1131 * the specified search range, there should be a hole
1134 if (page
->index
> end
) {
1135 if (type
== HOLE_OFF
&& lastoff
< endoff
) {
1144 * Page truncated or invalidated(page->mapping == NULL).
1145 * We can freely skip it and proceed to check the next
1148 if (unlikely(page
->mapping
!= inode
->i_mapping
)) {
1153 if (!page_has_buffers(page
)) {
1158 found
= xfs_lookup_buffer_offset(page
, &b_offset
, type
);
1161 * The found offset may be less than the start
1162 * point to search if this is the first time to
1165 *offset
= max_t(loff_t
, startoff
, b_offset
);
1171 * We either searching data but nothing was found, or
1172 * searching hole but found a data buffer. In either
1173 * case, probably the next page contains the desired
1174 * things, update the last offset to it so.
1176 lastoff
= page_offset(page
) + PAGE_SIZE
;
1181 * The number of returned pages less than our desired, search
1182 * done. In this case, nothing was found for searching data,
1183 * but we found a hole behind the last offset.
1185 if (nr_pages
< want
) {
1186 if (type
== HOLE_OFF
) {
1193 index
= pvec
.pages
[i
- 1]->index
+ 1;
1194 pagevec_release(&pvec
);
1195 } while (index
<= end
);
1198 pagevec_release(&pvec
);
1207 struct inode
*inode
= file
->f_mapping
->host
;
1208 struct xfs_inode
*ip
= XFS_I(inode
);
1209 struct xfs_mount
*mp
= ip
->i_mount
;
1210 loff_t
uninitialized_var(offset
);
1212 xfs_fileoff_t fsbno
;
1217 lock
= xfs_ilock_map_shared(ip
);
1219 isize
= i_size_read(inode
);
1220 if (start
>= isize
) {
1226 * Try to read extents from the first block indicated
1227 * by fsbno to the end block of the file.
1229 fsbno
= XFS_B_TO_FSBT(mp
, start
);
1230 end
= XFS_B_TO_FSB(mp
, isize
);
1232 struct xfs_bmbt_irec map
[2];
1236 error
= xfs_bmapi_read(ip
, fsbno
, end
- fsbno
, map
, &nmap
,
1241 /* No extents at given offset, must be beyond EOF */
1247 for (i
= 0; i
< nmap
; i
++) {
1248 offset
= max_t(loff_t
, start
,
1249 XFS_FSB_TO_B(mp
, map
[i
].br_startoff
));
1251 /* Landed in a data extent */
1252 if (map
[i
].br_startblock
== DELAYSTARTBLOCK
||
1253 (map
[i
].br_state
== XFS_EXT_NORM
&&
1254 !isnullstartblock(map
[i
].br_startblock
)))
1258 * Landed in an unwritten extent, try to search data
1261 if (map
[i
].br_state
== XFS_EXT_UNWRITTEN
) {
1262 if (xfs_find_get_desired_pgoff(inode
, &map
[i
],
1269 * map[0] is hole or its an unwritten extent but
1270 * without data in page cache. Probably means that
1271 * we are reading after EOF if nothing in map[1].
1281 * Nothing was found, proceed to the next round of search
1282 * if reading offset not beyond or hit EOF.
1284 fsbno
= map
[i
- 1].br_startoff
+ map
[i
- 1].br_blockcount
;
1285 start
= XFS_FSB_TO_B(mp
, fsbno
);
1286 if (start
>= isize
) {
1293 offset
= vfs_setpos(file
, offset
, inode
->i_sb
->s_maxbytes
);
1296 xfs_iunlock_map_shared(ip
, lock
);
1308 struct inode
*inode
= file
->f_mapping
->host
;
1309 struct xfs_inode
*ip
= XFS_I(inode
);
1310 struct xfs_mount
*mp
= ip
->i_mount
;
1311 loff_t
uninitialized_var(offset
);
1313 xfs_fileoff_t fsbno
;
1318 if (XFS_FORCED_SHUTDOWN(mp
))
1319 return -XFS_ERROR(EIO
);
1321 lock
= xfs_ilock_map_shared(ip
);
1323 isize
= i_size_read(inode
);
1324 if (start
>= isize
) {
1329 fsbno
= XFS_B_TO_FSBT(mp
, start
);
1330 end
= XFS_B_TO_FSB(mp
, isize
);
1333 struct xfs_bmbt_irec map
[2];
1337 error
= xfs_bmapi_read(ip
, fsbno
, end
- fsbno
, map
, &nmap
,
1342 /* No extents at given offset, must be beyond EOF */
1348 for (i
= 0; i
< nmap
; i
++) {
1349 offset
= max_t(loff_t
, start
,
1350 XFS_FSB_TO_B(mp
, map
[i
].br_startoff
));
1352 /* Landed in a hole */
1353 if (map
[i
].br_startblock
== HOLESTARTBLOCK
)
1357 * Landed in an unwritten extent, try to search hole
1360 if (map
[i
].br_state
== XFS_EXT_UNWRITTEN
) {
1361 if (xfs_find_get_desired_pgoff(inode
, &map
[i
],
1368 * map[0] contains data or its unwritten but contains
1369 * data in page cache, probably means that we are
1370 * reading after EOF. We should fix offset to point
1371 * to the end of the file(i.e., there is an implicit
1372 * hole at the end of any file).
1382 * Both mappings contains data, proceed to the next round of
1383 * search if the current reading offset not beyond or hit EOF.
1385 fsbno
= map
[i
- 1].br_startoff
+ map
[i
- 1].br_blockcount
;
1386 start
= XFS_FSB_TO_B(mp
, fsbno
);
1387 if (start
>= isize
) {
1395 * At this point, we must have found a hole. However, the returned
1396 * offset may be bigger than the file size as it may be aligned to
1397 * page boundary for unwritten extents, we need to deal with this
1398 * situation in particular.
1400 offset
= min_t(loff_t
, offset
, isize
);
1401 offset
= vfs_setpos(file
, offset
, inode
->i_sb
->s_maxbytes
);
1404 xfs_iunlock_map_shared(ip
, lock
);
1421 return generic_file_llseek(file
, offset
, origin
);
1423 return xfs_seek_data(file
, offset
);
1425 return xfs_seek_hole(file
, offset
);
1431 const struct file_operations xfs_file_operations
= {
1432 .llseek
= xfs_file_llseek
,
1433 .read
= do_sync_read
,
1434 .write
= do_sync_write
,
1435 .aio_read
= xfs_file_aio_read
,
1436 .aio_write
= xfs_file_aio_write
,
1437 .splice_read
= xfs_file_splice_read
,
1438 .splice_write
= xfs_file_splice_write
,
1439 .unlocked_ioctl
= xfs_file_ioctl
,
1440 #ifdef CONFIG_COMPAT
1441 .compat_ioctl
= xfs_file_compat_ioctl
,
1443 .mmap
= xfs_file_mmap
,
1444 .open
= xfs_file_open
,
1445 .release
= xfs_file_release
,
1446 .fsync
= xfs_file_fsync
,
1447 .fallocate
= xfs_file_fallocate
,
1450 const struct file_operations xfs_dir_file_operations
= {
1451 .open
= xfs_dir_open
,
1452 .read
= generic_read_dir
,
1453 .iterate
= xfs_file_readdir
,
1454 .llseek
= generic_file_llseek
,
1455 .unlocked_ioctl
= xfs_file_ioctl
,
1456 #ifdef CONFIG_COMPAT
1457 .compat_ioctl
= xfs_file_compat_ioctl
,
1459 .fsync
= xfs_dir_fsync
,
1462 static const struct vm_operations_struct xfs_file_vm_ops
= {
1463 .fault
= filemap_fault
,
1464 .page_mkwrite
= xfs_vm_page_mkwrite
,
1465 .remap_pages
= generic_file_remap_pages
,