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
23 #include "xfs_trans.h"
24 #include "xfs_mount.h"
25 #include "xfs_bmap_btree.h"
26 #include "xfs_alloc.h"
27 #include "xfs_dinode.h"
28 #include "xfs_inode.h"
29 #include "xfs_inode_item.h"
31 #include "xfs_error.h"
32 #include "xfs_vnodeops.h"
33 #include "xfs_da_btree.h"
34 #include "xfs_ioctl.h"
35 #include "xfs_trace.h"
37 #include <linux/dcache.h>
38 #include <linux/falloc.h>
39 #include <linux/pagevec.h>
41 static const struct vm_operations_struct xfs_file_vm_ops
;
44 * Locking primitives for read and write IO paths to ensure we consistently use
45 * and order the inode->i_mutex, ip->i_lock and ip->i_iolock.
52 if (type
& XFS_IOLOCK_EXCL
)
53 mutex_lock(&VFS_I(ip
)->i_mutex
);
62 xfs_iunlock(ip
, type
);
63 if (type
& XFS_IOLOCK_EXCL
)
64 mutex_unlock(&VFS_I(ip
)->i_mutex
);
72 xfs_ilock_demote(ip
, type
);
73 if (type
& XFS_IOLOCK_EXCL
)
74 mutex_unlock(&VFS_I(ip
)->i_mutex
);
80 * xfs_iozero clears the specified range of buffer supplied,
81 * and marks all the affected blocks as valid and modified. If
82 * an affected block is not allocated, it will be allocated. If
83 * an affected block is not completely overwritten, and is not
84 * valid before the operation, it will be read from disk before
85 * being partially zeroed.
89 struct xfs_inode
*ip
, /* inode */
90 loff_t pos
, /* offset in file */
91 size_t count
) /* size of data to zero */
94 struct address_space
*mapping
;
97 mapping
= VFS_I(ip
)->i_mapping
;
99 unsigned offset
, bytes
;
102 offset
= (pos
& (PAGE_CACHE_SIZE
-1)); /* Within page */
103 bytes
= PAGE_CACHE_SIZE
- offset
;
107 status
= pagecache_write_begin(NULL
, mapping
, pos
, bytes
,
108 AOP_FLAG_UNINTERRUPTIBLE
,
113 zero_user(page
, offset
, bytes
);
115 status
= pagecache_write_end(NULL
, mapping
, pos
, bytes
, bytes
,
117 WARN_ON(status
<= 0); /* can't return less than zero! */
127 * Fsync operations on directories are much simpler than on regular files,
128 * as there is no file data to flush, and thus also no need for explicit
129 * cache flush operations, and there are no non-transaction metadata updates
130 * on directories either.
139 struct xfs_inode
*ip
= XFS_I(file
->f_mapping
->host
);
140 struct xfs_mount
*mp
= ip
->i_mount
;
143 trace_xfs_dir_fsync(ip
);
145 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
146 if (xfs_ipincount(ip
))
147 lsn
= ip
->i_itemp
->ili_last_lsn
;
148 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
152 return _xfs_log_force_lsn(mp
, lsn
, XFS_LOG_SYNC
, NULL
);
162 struct inode
*inode
= file
->f_mapping
->host
;
163 struct xfs_inode
*ip
= XFS_I(inode
);
164 struct xfs_mount
*mp
= ip
->i_mount
;
169 trace_xfs_file_fsync(ip
);
171 error
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
175 if (XFS_FORCED_SHUTDOWN(mp
))
176 return -XFS_ERROR(EIO
);
178 xfs_iflags_clear(ip
, XFS_ITRUNCATED
);
180 if (mp
->m_flags
& XFS_MOUNT_BARRIER
) {
182 * If we have an RT and/or log subvolume we need to make sure
183 * to flush the write cache the device used for file data
184 * first. This is to ensure newly written file data make
185 * it to disk before logging the new inode size in case of
186 * an extending write.
188 if (XFS_IS_REALTIME_INODE(ip
))
189 xfs_blkdev_issue_flush(mp
->m_rtdev_targp
);
190 else if (mp
->m_logdev_targp
!= mp
->m_ddev_targp
)
191 xfs_blkdev_issue_flush(mp
->m_ddev_targp
);
195 * All metadata updates are logged, which means that we just have
196 * to flush the log up to the latest LSN that touched the inode.
198 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
199 if (xfs_ipincount(ip
)) {
201 (ip
->i_itemp
->ili_fields
& ~XFS_ILOG_TIMESTAMP
))
202 lsn
= ip
->i_itemp
->ili_last_lsn
;
204 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
207 error
= _xfs_log_force_lsn(mp
, lsn
, XFS_LOG_SYNC
, &log_flushed
);
210 * If we only have a single device, and the log force about was
211 * a no-op we might have to flush the data device cache here.
212 * This can only happen for fdatasync/O_DSYNC if we were overwriting
213 * an already allocated file and thus do not have any metadata to
216 if ((mp
->m_flags
& XFS_MOUNT_BARRIER
) &&
217 mp
->m_logdev_targp
== mp
->m_ddev_targp
&&
218 !XFS_IS_REALTIME_INODE(ip
) &&
220 xfs_blkdev_issue_flush(mp
->m_ddev_targp
);
228 const struct iovec
*iovp
,
229 unsigned long nr_segs
,
232 struct file
*file
= iocb
->ki_filp
;
233 struct inode
*inode
= file
->f_mapping
->host
;
234 struct xfs_inode
*ip
= XFS_I(inode
);
235 struct xfs_mount
*mp
= ip
->i_mount
;
241 XFS_STATS_INC(xs_read_calls
);
243 BUG_ON(iocb
->ki_pos
!= pos
);
245 if (unlikely(file
->f_flags
& O_DIRECT
))
246 ioflags
|= IO_ISDIRECT
;
247 if (file
->f_mode
& FMODE_NOCMTIME
)
250 ret
= generic_segment_checks(iovp
, &nr_segs
, &size
, VERIFY_WRITE
);
254 if (unlikely(ioflags
& IO_ISDIRECT
)) {
255 xfs_buftarg_t
*target
=
256 XFS_IS_REALTIME_INODE(ip
) ?
257 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
258 if ((iocb
->ki_pos
& target
->bt_smask
) ||
259 (size
& target
->bt_smask
)) {
260 if (iocb
->ki_pos
== i_size_read(inode
))
262 return -XFS_ERROR(EINVAL
);
266 n
= mp
->m_super
->s_maxbytes
- iocb
->ki_pos
;
267 if (n
<= 0 || size
== 0)
273 if (XFS_FORCED_SHUTDOWN(mp
))
277 * Locking is a bit tricky here. If we take an exclusive lock
278 * for direct IO, we effectively serialise all new concurrent
279 * read IO to this file and block it behind IO that is currently in
280 * progress because IO in progress holds the IO lock shared. We only
281 * need to hold the lock exclusive to blow away the page cache, so
282 * only take lock exclusively if the page cache needs invalidation.
283 * This allows the normal direct IO case of no page cache pages to
284 * proceeed concurrently without serialisation.
286 xfs_rw_ilock(ip
, XFS_IOLOCK_SHARED
);
287 if ((ioflags
& IO_ISDIRECT
) && inode
->i_mapping
->nrpages
) {
288 xfs_rw_iunlock(ip
, XFS_IOLOCK_SHARED
);
289 xfs_rw_ilock(ip
, XFS_IOLOCK_EXCL
);
291 if (inode
->i_mapping
->nrpages
) {
292 ret
= -xfs_flushinval_pages(ip
,
293 (iocb
->ki_pos
& PAGE_CACHE_MASK
),
294 -1, FI_REMAPF_LOCKED
);
296 xfs_rw_iunlock(ip
, XFS_IOLOCK_EXCL
);
300 xfs_rw_ilock_demote(ip
, XFS_IOLOCK_EXCL
);
303 trace_xfs_file_read(ip
, size
, iocb
->ki_pos
, ioflags
);
305 ret
= generic_file_aio_read(iocb
, iovp
, nr_segs
, iocb
->ki_pos
);
307 XFS_STATS_ADD(xs_read_bytes
, ret
);
309 xfs_rw_iunlock(ip
, XFS_IOLOCK_SHARED
);
314 xfs_file_splice_read(
317 struct pipe_inode_info
*pipe
,
321 struct xfs_inode
*ip
= XFS_I(infilp
->f_mapping
->host
);
325 XFS_STATS_INC(xs_read_calls
);
327 if (infilp
->f_mode
& FMODE_NOCMTIME
)
330 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
333 xfs_rw_ilock(ip
, XFS_IOLOCK_SHARED
);
335 trace_xfs_file_splice_read(ip
, count
, *ppos
, ioflags
);
337 ret
= generic_file_splice_read(infilp
, ppos
, pipe
, count
, flags
);
339 XFS_STATS_ADD(xs_read_bytes
, ret
);
341 xfs_rw_iunlock(ip
, XFS_IOLOCK_SHARED
);
346 * xfs_file_splice_write() does not use xfs_rw_ilock() because
347 * generic_file_splice_write() takes the i_mutex itself. This, in theory,
348 * couuld cause lock inversions between the aio_write path and the splice path
349 * if someone is doing concurrent splice(2) based writes and write(2) based
350 * writes to the same inode. The only real way to fix this is to re-implement
351 * the generic code here with correct locking orders.
354 xfs_file_splice_write(
355 struct pipe_inode_info
*pipe
,
356 struct file
*outfilp
,
361 struct inode
*inode
= outfilp
->f_mapping
->host
;
362 struct xfs_inode
*ip
= XFS_I(inode
);
366 XFS_STATS_INC(xs_write_calls
);
368 if (outfilp
->f_mode
& FMODE_NOCMTIME
)
371 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
374 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
376 trace_xfs_file_splice_write(ip
, count
, *ppos
, ioflags
);
378 ret
= generic_file_splice_write(pipe
, outfilp
, ppos
, count
, flags
);
380 XFS_STATS_ADD(xs_write_bytes
, ret
);
382 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
387 * This routine is called to handle zeroing any space in the last block of the
388 * file that is beyond the EOF. We do this since the size is being increased
389 * without writing anything to that block and we don't want to read the
390 * garbage on the disk.
392 STATIC
int /* error (positive) */
394 struct xfs_inode
*ip
,
398 struct xfs_mount
*mp
= ip
->i_mount
;
399 xfs_fileoff_t last_fsb
= XFS_B_TO_FSBT(mp
, isize
);
400 int zero_offset
= XFS_B_FSB_OFFSET(mp
, isize
);
404 struct xfs_bmbt_irec imap
;
406 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
407 error
= xfs_bmapi_read(ip
, last_fsb
, 1, &imap
, &nimaps
, 0);
408 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
415 * If the block underlying isize is just a hole, then there
416 * is nothing to zero.
418 if (imap
.br_startblock
== HOLESTARTBLOCK
)
421 zero_len
= mp
->m_sb
.sb_blocksize
- zero_offset
;
422 if (isize
+ zero_len
> offset
)
423 zero_len
= offset
- isize
;
424 return xfs_iozero(ip
, isize
, zero_len
);
428 * Zero any on disk space between the current EOF and the new, larger EOF.
430 * This handles the normal case of zeroing the remainder of the last block in
431 * the file and the unusual case of zeroing blocks out beyond the size of the
432 * file. This second case only happens with fixed size extents and when the
433 * system crashes before the inode size was updated but after blocks were
436 * Expects the iolock to be held exclusive, and will take the ilock internally.
438 int /* error (positive) */
440 struct xfs_inode
*ip
,
441 xfs_off_t offset
, /* starting I/O offset */
442 xfs_fsize_t isize
) /* current inode size */
444 struct xfs_mount
*mp
= ip
->i_mount
;
445 xfs_fileoff_t start_zero_fsb
;
446 xfs_fileoff_t end_zero_fsb
;
447 xfs_fileoff_t zero_count_fsb
;
448 xfs_fileoff_t last_fsb
;
449 xfs_fileoff_t zero_off
;
450 xfs_fsize_t zero_len
;
453 struct xfs_bmbt_irec imap
;
455 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
456 ASSERT(offset
> isize
);
459 * First handle zeroing the block on which isize resides.
461 * We only zero a part of that block so it is handled specially.
463 if (XFS_B_FSB_OFFSET(mp
, isize
) != 0) {
464 error
= xfs_zero_last_block(ip
, offset
, isize
);
470 * Calculate the range between the new size and the old where blocks
471 * needing to be zeroed may exist.
473 * To get the block where the last byte in the file currently resides,
474 * we need to subtract one from the size and truncate back to a block
475 * boundary. We subtract 1 in case the size is exactly on a block
478 last_fsb
= isize
? XFS_B_TO_FSBT(mp
, isize
- 1) : (xfs_fileoff_t
)-1;
479 start_zero_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)isize
);
480 end_zero_fsb
= XFS_B_TO_FSBT(mp
, offset
- 1);
481 ASSERT((xfs_sfiloff_t
)last_fsb
< (xfs_sfiloff_t
)start_zero_fsb
);
482 if (last_fsb
== end_zero_fsb
) {
484 * The size was only incremented on its last block.
485 * We took care of that above, so just return.
490 ASSERT(start_zero_fsb
<= end_zero_fsb
);
491 while (start_zero_fsb
<= end_zero_fsb
) {
493 zero_count_fsb
= end_zero_fsb
- start_zero_fsb
+ 1;
495 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
496 error
= xfs_bmapi_read(ip
, start_zero_fsb
, zero_count_fsb
,
498 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
504 if (imap
.br_state
== XFS_EXT_UNWRITTEN
||
505 imap
.br_startblock
== HOLESTARTBLOCK
) {
506 start_zero_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
507 ASSERT(start_zero_fsb
<= (end_zero_fsb
+ 1));
512 * There are blocks we need to zero.
514 zero_off
= XFS_FSB_TO_B(mp
, start_zero_fsb
);
515 zero_len
= XFS_FSB_TO_B(mp
, imap
.br_blockcount
);
517 if ((zero_off
+ zero_len
) > offset
)
518 zero_len
= offset
- zero_off
;
520 error
= xfs_iozero(ip
, zero_off
, zero_len
);
524 start_zero_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
525 ASSERT(start_zero_fsb
<= (end_zero_fsb
+ 1));
532 * Common pre-write limit and setup checks.
534 * Called with the iolocked held either shared and exclusive according to
535 * @iolock, and returns with it held. Might upgrade the iolock to exclusive
536 * if called for a direct write beyond i_size.
539 xfs_file_aio_write_checks(
545 struct inode
*inode
= file
->f_mapping
->host
;
546 struct xfs_inode
*ip
= XFS_I(inode
);
550 error
= generic_write_checks(file
, pos
, count
, S_ISBLK(inode
->i_mode
));
555 * If the offset is beyond the size of the file, we need to zero any
556 * blocks that fall between the existing EOF and the start of this
557 * write. If zeroing is needed and we are currently holding the
558 * iolock shared, we need to update it to exclusive which implies
559 * having to redo all checks before.
561 if (*pos
> i_size_read(inode
)) {
562 if (*iolock
== XFS_IOLOCK_SHARED
) {
563 xfs_rw_iunlock(ip
, *iolock
);
564 *iolock
= XFS_IOLOCK_EXCL
;
565 xfs_rw_ilock(ip
, *iolock
);
568 error
= -xfs_zero_eof(ip
, *pos
, i_size_read(inode
));
574 * Updating the timestamps will grab the ilock again from
575 * xfs_fs_dirty_inode, so we have to call it after dropping the
576 * lock above. Eventually we should look into a way to avoid
577 * the pointless lock roundtrip.
579 if (likely(!(file
->f_mode
& FMODE_NOCMTIME
))) {
580 error
= file_update_time(file
);
586 * If we're writing the file then make sure to clear the setuid and
587 * setgid bits if the process is not being run by root. This keeps
588 * people from modifying setuid and setgid binaries.
590 return file_remove_suid(file
);
594 * xfs_file_dio_aio_write - handle direct IO writes
596 * Lock the inode appropriately to prepare for and issue a direct IO write.
597 * By separating it from the buffered write path we remove all the tricky to
598 * follow locking changes and looping.
600 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
601 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
602 * pages are flushed out.
604 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
605 * allowing them to be done in parallel with reads and other direct IO writes.
606 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
607 * needs to do sub-block zeroing and that requires serialisation against other
608 * direct IOs to the same block. In this case we need to serialise the
609 * submission of the unaligned IOs so that we don't get racing block zeroing in
610 * the dio layer. To avoid the problem with aio, we also need to wait for
611 * outstanding IOs to complete so that unwritten extent conversion is completed
612 * before we try to map the overlapping block. This is currently implemented by
613 * hitting it with a big hammer (i.e. inode_dio_wait()).
615 * Returns with locks held indicated by @iolock and errors indicated by
616 * negative return values.
619 xfs_file_dio_aio_write(
621 const struct iovec
*iovp
,
622 unsigned long nr_segs
,
626 struct file
*file
= iocb
->ki_filp
;
627 struct address_space
*mapping
= file
->f_mapping
;
628 struct inode
*inode
= mapping
->host
;
629 struct xfs_inode
*ip
= XFS_I(inode
);
630 struct xfs_mount
*mp
= ip
->i_mount
;
632 size_t count
= ocount
;
633 int unaligned_io
= 0;
635 struct xfs_buftarg
*target
= XFS_IS_REALTIME_INODE(ip
) ?
636 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
638 if ((pos
& target
->bt_smask
) || (count
& target
->bt_smask
))
639 return -XFS_ERROR(EINVAL
);
641 if ((pos
& mp
->m_blockmask
) || ((pos
+ count
) & mp
->m_blockmask
))
645 * We don't need to take an exclusive lock unless there page cache needs
646 * to be invalidated or unaligned IO is being executed. We don't need to
647 * consider the EOF extension case here because
648 * xfs_file_aio_write_checks() will relock the inode as necessary for
649 * EOF zeroing cases and fill out the new inode size as appropriate.
651 if (unaligned_io
|| mapping
->nrpages
)
652 iolock
= XFS_IOLOCK_EXCL
;
654 iolock
= XFS_IOLOCK_SHARED
;
655 xfs_rw_ilock(ip
, iolock
);
658 * Recheck if there are cached pages that need invalidate after we got
659 * the iolock to protect against other threads adding new pages while
660 * we were waiting for the iolock.
662 if (mapping
->nrpages
&& iolock
== XFS_IOLOCK_SHARED
) {
663 xfs_rw_iunlock(ip
, iolock
);
664 iolock
= XFS_IOLOCK_EXCL
;
665 xfs_rw_ilock(ip
, iolock
);
668 ret
= xfs_file_aio_write_checks(file
, &pos
, &count
, &iolock
);
672 if (mapping
->nrpages
) {
673 ret
= -xfs_flushinval_pages(ip
, (pos
& PAGE_CACHE_MASK
), -1,
680 * If we are doing unaligned IO, wait for all other IO to drain,
681 * otherwise demote the lock if we had to flush cached pages
684 inode_dio_wait(inode
);
685 else if (iolock
== XFS_IOLOCK_EXCL
) {
686 xfs_rw_ilock_demote(ip
, XFS_IOLOCK_EXCL
);
687 iolock
= XFS_IOLOCK_SHARED
;
690 trace_xfs_file_direct_write(ip
, count
, iocb
->ki_pos
, 0);
691 ret
= generic_file_direct_write(iocb
, iovp
,
692 &nr_segs
, pos
, &iocb
->ki_pos
, count
, ocount
);
695 xfs_rw_iunlock(ip
, iolock
);
697 /* No fallback to buffered IO on errors for XFS. */
698 ASSERT(ret
< 0 || ret
== count
);
703 xfs_file_buffered_aio_write(
705 const struct iovec
*iovp
,
706 unsigned long nr_segs
,
710 struct file
*file
= iocb
->ki_filp
;
711 struct address_space
*mapping
= file
->f_mapping
;
712 struct inode
*inode
= mapping
->host
;
713 struct xfs_inode
*ip
= XFS_I(inode
);
716 int iolock
= XFS_IOLOCK_EXCL
;
717 size_t count
= ocount
;
719 xfs_rw_ilock(ip
, iolock
);
721 ret
= xfs_file_aio_write_checks(file
, &pos
, &count
, &iolock
);
725 /* We can write back this queue in page reclaim */
726 current
->backing_dev_info
= mapping
->backing_dev_info
;
729 trace_xfs_file_buffered_write(ip
, count
, iocb
->ki_pos
, 0);
730 ret
= generic_file_buffered_write(iocb
, iovp
, nr_segs
,
731 pos
, &iocb
->ki_pos
, count
, ret
);
733 * if we just got an ENOSPC, flush the inode now we aren't holding any
734 * page locks and retry *once*
736 if (ret
== -ENOSPC
&& !enospc
) {
738 ret
= -xfs_flush_pages(ip
, 0, -1, 0, FI_NONE
);
743 current
->backing_dev_info
= NULL
;
745 xfs_rw_iunlock(ip
, iolock
);
752 const struct iovec
*iovp
,
753 unsigned long nr_segs
,
756 struct file
*file
= iocb
->ki_filp
;
757 struct address_space
*mapping
= file
->f_mapping
;
758 struct inode
*inode
= mapping
->host
;
759 struct xfs_inode
*ip
= XFS_I(inode
);
763 XFS_STATS_INC(xs_write_calls
);
765 BUG_ON(iocb
->ki_pos
!= pos
);
767 ret
= generic_segment_checks(iovp
, &nr_segs
, &ocount
, VERIFY_READ
);
774 sb_start_write(inode
->i_sb
);
776 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
)) {
781 if (unlikely(file
->f_flags
& O_DIRECT
))
782 ret
= xfs_file_dio_aio_write(iocb
, iovp
, nr_segs
, pos
, ocount
);
784 ret
= xfs_file_buffered_aio_write(iocb
, iovp
, nr_segs
, pos
,
790 XFS_STATS_ADD(xs_write_bytes
, ret
);
792 /* Handle various SYNC-type writes */
793 err
= generic_write_sync(file
, pos
, ret
);
799 sb_end_write(inode
->i_sb
);
810 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
814 xfs_inode_t
*ip
= XFS_I(inode
);
815 int cmd
= XFS_IOC_RESVSP
;
816 int attr_flags
= XFS_ATTR_NOLOCK
;
818 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
825 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
827 if (mode
& FALLOC_FL_PUNCH_HOLE
)
828 cmd
= XFS_IOC_UNRESVSP
;
830 /* check the new inode size is valid before allocating */
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 if (file
->f_flags
& O_DSYNC
)
840 attr_flags
|= XFS_ATTR_SYNC
;
842 error
= -xfs_change_file_space(ip
, cmd
, &bf
, 0, attr_flags
);
846 /* Change file size if needed */
850 iattr
.ia_valid
= ATTR_SIZE
;
851 iattr
.ia_size
= new_size
;
852 error
= -xfs_setattr_size(ip
, &iattr
, XFS_ATTR_NOLOCK
);
856 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
866 if (!(file
->f_flags
& O_LARGEFILE
) && i_size_read(inode
) > MAX_NON_LFS
)
868 if (XFS_FORCED_SHUTDOWN(XFS_M(inode
->i_sb
)))
878 struct xfs_inode
*ip
= XFS_I(inode
);
882 error
= xfs_file_open(inode
, file
);
887 * If there are any blocks, read-ahead block 0 as we're almost
888 * certain to have the next operation be a read there.
890 mode
= xfs_ilock_map_shared(ip
);
891 if (ip
->i_d
.di_nextents
> 0)
892 xfs_da_reada_buf(NULL
, ip
, 0, XFS_DATA_FORK
);
893 xfs_iunlock(ip
, mode
);
902 return -xfs_release(XFS_I(inode
));
911 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
912 xfs_inode_t
*ip
= XFS_I(inode
);
917 * The Linux API doesn't pass down the total size of the buffer
918 * we read into down to the filesystem. With the filldir concept
919 * it's not needed for correct information, but the XFS dir2 leaf
920 * code wants an estimate of the buffer size to calculate it's
921 * readahead window and size the buffers used for mapping to
924 * Try to give it an estimate that's good enough, maybe at some
925 * point we can change the ->readdir prototype to include the
926 * buffer size. For now we use the current glibc buffer size.
928 bufsize
= (size_t)min_t(loff_t
, 32768, ip
->i_d
.di_size
);
930 error
= xfs_readdir(ip
, dirent
, bufsize
,
931 (xfs_off_t
*)&filp
->f_pos
, filldir
);
940 struct vm_area_struct
*vma
)
942 vma
->vm_ops
= &xfs_file_vm_ops
;
943 vma
->vm_flags
|= VM_CAN_NONLINEAR
;
950 * mmap()d file has taken write protection fault and is being made
951 * writable. We can set the page state up correctly for a writable
952 * page, which means we can do correct delalloc accounting (ENOSPC
953 * checking!) and unwritten extent mapping.
957 struct vm_area_struct
*vma
,
958 struct vm_fault
*vmf
)
960 return block_page_mkwrite(vma
, vmf
, xfs_get_blocks
);
964 * This type is designed to indicate the type of offset we would like
965 * to search from page cache for either xfs_seek_data() or xfs_seek_hole().
973 * Lookup the desired type of offset from the given page.
975 * On success, return true and the offset argument will point to the
976 * start of the region that was found. Otherwise this function will
977 * return false and keep the offset argument unchanged.
980 xfs_lookup_buffer_offset(
985 loff_t lastoff
= page_offset(page
);
987 struct buffer_head
*bh
, *head
;
989 bh
= head
= page_buffers(page
);
992 * Unwritten extents that have data in the page
993 * cache covering them can be identified by the
994 * BH_Unwritten state flag. Pages with multiple
995 * buffers might have a mix of holes, data and
996 * unwritten extents - any buffer with valid
997 * data in it should have BH_Uptodate flag set
1000 if (buffer_unwritten(bh
) ||
1001 buffer_uptodate(bh
)) {
1002 if (type
== DATA_OFF
)
1005 if (type
== HOLE_OFF
)
1013 lastoff
+= bh
->b_size
;
1014 } while ((bh
= bh
->b_this_page
) != head
);
1020 * This routine is called to find out and return a data or hole offset
1021 * from the page cache for unwritten extents according to the desired
1022 * type for xfs_seek_data() or xfs_seek_hole().
1024 * The argument offset is used to tell where we start to search from the
1025 * page cache. Map is used to figure out the end points of the range to
1028 * Return true if the desired type of offset was found, and the argument
1029 * offset is filled with that address. Otherwise, return false and keep
1033 xfs_find_get_desired_pgoff(
1034 struct inode
*inode
,
1035 struct xfs_bmbt_irec
*map
,
1039 struct xfs_inode
*ip
= XFS_I(inode
);
1040 struct xfs_mount
*mp
= ip
->i_mount
;
1041 struct pagevec pvec
;
1045 loff_t startoff
= *offset
;
1046 loff_t lastoff
= startoff
;
1049 pagevec_init(&pvec
, 0);
1051 index
= startoff
>> PAGE_CACHE_SHIFT
;
1052 endoff
= XFS_FSB_TO_B(mp
, map
->br_startoff
+ map
->br_blockcount
);
1053 end
= endoff
>> PAGE_CACHE_SHIFT
;
1059 want
= min_t(pgoff_t
, end
- index
, PAGEVEC_SIZE
);
1060 nr_pages
= pagevec_lookup(&pvec
, inode
->i_mapping
, index
,
1063 * No page mapped into given range. If we are searching holes
1064 * and if this is the first time we got into the loop, it means
1065 * that the given offset is landed in a hole, return it.
1067 * If we have already stepped through some block buffers to find
1068 * holes but they all contains data. In this case, the last
1069 * offset is already updated and pointed to the end of the last
1070 * mapped page, if it does not reach the endpoint to search,
1071 * that means there should be a hole between them.
1073 if (nr_pages
== 0) {
1074 /* Data search found nothing */
1075 if (type
== DATA_OFF
)
1078 ASSERT(type
== HOLE_OFF
);
1079 if (lastoff
== startoff
|| lastoff
< endoff
) {
1087 * At lease we found one page. If this is the first time we
1088 * step into the loop, and if the first page index offset is
1089 * greater than the given search offset, a hole was found.
1091 if (type
== HOLE_OFF
&& lastoff
== startoff
&&
1092 lastoff
< page_offset(pvec
.pages
[0])) {
1097 for (i
= 0; i
< nr_pages
; i
++) {
1098 struct page
*page
= pvec
.pages
[i
];
1102 * At this point, the page may be truncated or
1103 * invalidated (changing page->mapping to NULL),
1104 * or even swizzled back from swapper_space to tmpfs
1105 * file mapping. However, page->index will not change
1106 * because we have a reference on the page.
1108 * Searching done if the page index is out of range.
1109 * If the current offset is not reaches the end of
1110 * the specified search range, there should be a hole
1113 if (page
->index
> end
) {
1114 if (type
== HOLE_OFF
&& lastoff
< endoff
) {
1123 * Page truncated or invalidated(page->mapping == NULL).
1124 * We can freely skip it and proceed to check the next
1127 if (unlikely(page
->mapping
!= inode
->i_mapping
)) {
1132 if (!page_has_buffers(page
)) {
1137 found
= xfs_lookup_buffer_offset(page
, &b_offset
, type
);
1140 * The found offset may be less than the start
1141 * point to search if this is the first time to
1144 *offset
= max_t(loff_t
, startoff
, b_offset
);
1150 * We either searching data but nothing was found, or
1151 * searching hole but found a data buffer. In either
1152 * case, probably the next page contains the desired
1153 * things, update the last offset to it so.
1155 lastoff
= page_offset(page
) + PAGE_SIZE
;
1160 * The number of returned pages less than our desired, search
1161 * done. In this case, nothing was found for searching data,
1162 * but we found a hole behind the last offset.
1164 if (nr_pages
< want
) {
1165 if (type
== HOLE_OFF
) {
1172 index
= pvec
.pages
[i
- 1]->index
+ 1;
1173 pagevec_release(&pvec
);
1174 } while (index
<= end
);
1177 pagevec_release(&pvec
);
1186 struct inode
*inode
= file
->f_mapping
->host
;
1187 struct xfs_inode
*ip
= XFS_I(inode
);
1188 struct xfs_mount
*mp
= ip
->i_mount
;
1189 loff_t
uninitialized_var(offset
);
1191 xfs_fileoff_t fsbno
;
1196 lock
= xfs_ilock_map_shared(ip
);
1198 isize
= i_size_read(inode
);
1199 if (start
>= isize
) {
1205 * Try to read extents from the first block indicated
1206 * by fsbno to the end block of the file.
1208 fsbno
= XFS_B_TO_FSBT(mp
, start
);
1209 end
= XFS_B_TO_FSB(mp
, isize
);
1211 struct xfs_bmbt_irec map
[2];
1215 error
= xfs_bmapi_read(ip
, fsbno
, end
- fsbno
, map
, &nmap
,
1220 /* No extents at given offset, must be beyond EOF */
1226 for (i
= 0; i
< nmap
; i
++) {
1227 offset
= max_t(loff_t
, start
,
1228 XFS_FSB_TO_B(mp
, map
[i
].br_startoff
));
1230 /* Landed in a data extent */
1231 if (map
[i
].br_startblock
== DELAYSTARTBLOCK
||
1232 (map
[i
].br_state
== XFS_EXT_NORM
&&
1233 !isnullstartblock(map
[i
].br_startblock
)))
1237 * Landed in an unwritten extent, try to search data
1240 if (map
[i
].br_state
== XFS_EXT_UNWRITTEN
) {
1241 if (xfs_find_get_desired_pgoff(inode
, &map
[i
],
1248 * map[0] is hole or its an unwritten extent but
1249 * without data in page cache. Probably means that
1250 * we are reading after EOF if nothing in map[1].
1260 * Nothing was found, proceed to the next round of search
1261 * if reading offset not beyond or hit EOF.
1263 fsbno
= map
[i
- 1].br_startoff
+ map
[i
- 1].br_blockcount
;
1264 start
= XFS_FSB_TO_B(mp
, fsbno
);
1265 if (start
>= isize
) {
1272 if (offset
!= file
->f_pos
)
1273 file
->f_pos
= offset
;
1276 xfs_iunlock_map_shared(ip
, lock
);
1288 struct inode
*inode
= file
->f_mapping
->host
;
1289 struct xfs_inode
*ip
= XFS_I(inode
);
1290 struct xfs_mount
*mp
= ip
->i_mount
;
1291 loff_t
uninitialized_var(offset
);
1293 xfs_fileoff_t fsbno
;
1298 if (XFS_FORCED_SHUTDOWN(mp
))
1299 return -XFS_ERROR(EIO
);
1301 lock
= xfs_ilock_map_shared(ip
);
1303 isize
= i_size_read(inode
);
1304 if (start
>= isize
) {
1309 fsbno
= XFS_B_TO_FSBT(mp
, start
);
1310 end
= XFS_B_TO_FSB(mp
, isize
);
1313 struct xfs_bmbt_irec map
[2];
1317 error
= xfs_bmapi_read(ip
, fsbno
, end
- fsbno
, map
, &nmap
,
1322 /* No extents at given offset, must be beyond EOF */
1328 for (i
= 0; i
< nmap
; i
++) {
1329 offset
= max_t(loff_t
, start
,
1330 XFS_FSB_TO_B(mp
, map
[i
].br_startoff
));
1332 /* Landed in a hole */
1333 if (map
[i
].br_startblock
== HOLESTARTBLOCK
)
1337 * Landed in an unwritten extent, try to search hole
1340 if (map
[i
].br_state
== XFS_EXT_UNWRITTEN
) {
1341 if (xfs_find_get_desired_pgoff(inode
, &map
[i
],
1348 * map[0] contains data or its unwritten but contains
1349 * data in page cache, probably means that we are
1350 * reading after EOF. We should fix offset to point
1351 * to the end of the file(i.e., there is an implicit
1352 * hole at the end of any file).
1362 * Both mappings contains data, proceed to the next round of
1363 * search if the current reading offset not beyond or hit EOF.
1365 fsbno
= map
[i
- 1].br_startoff
+ map
[i
- 1].br_blockcount
;
1366 start
= XFS_FSB_TO_B(mp
, fsbno
);
1367 if (start
>= isize
) {
1375 * At this point, we must have found a hole. However, the returned
1376 * offset may be bigger than the file size as it may be aligned to
1377 * page boundary for unwritten extents, we need to deal with this
1378 * situation in particular.
1380 offset
= min_t(loff_t
, offset
, isize
);
1381 if (offset
!= file
->f_pos
)
1382 file
->f_pos
= offset
;
1385 xfs_iunlock_map_shared(ip
, lock
);
1402 return generic_file_llseek(file
, offset
, origin
);
1404 return xfs_seek_data(file
, offset
);
1406 return xfs_seek_hole(file
, offset
);
1412 const struct file_operations xfs_file_operations
= {
1413 .llseek
= xfs_file_llseek
,
1414 .read
= do_sync_read
,
1415 .write
= do_sync_write
,
1416 .aio_read
= xfs_file_aio_read
,
1417 .aio_write
= xfs_file_aio_write
,
1418 .splice_read
= xfs_file_splice_read
,
1419 .splice_write
= xfs_file_splice_write
,
1420 .unlocked_ioctl
= xfs_file_ioctl
,
1421 #ifdef CONFIG_COMPAT
1422 .compat_ioctl
= xfs_file_compat_ioctl
,
1424 .mmap
= xfs_file_mmap
,
1425 .open
= xfs_file_open
,
1426 .release
= xfs_file_release
,
1427 .fsync
= xfs_file_fsync
,
1428 .fallocate
= xfs_file_fallocate
,
1431 const struct file_operations xfs_dir_file_operations
= {
1432 .open
= xfs_dir_open
,
1433 .read
= generic_read_dir
,
1434 .readdir
= xfs_file_readdir
,
1435 .llseek
= generic_file_llseek
,
1436 .unlocked_ioctl
= xfs_file_ioctl
,
1437 #ifdef CONFIG_COMPAT
1438 .compat_ioctl
= xfs_file_compat_ioctl
,
1440 .fsync
= xfs_dir_fsync
,
1443 static const struct vm_operations_struct xfs_file_vm_ops
= {
1444 .fault
= filemap_fault
,
1445 .page_mkwrite
= xfs_vm_page_mkwrite
,