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
24 #include "xfs_trans.h"
25 #include "xfs_mount.h"
26 #include "xfs_bmap_btree.h"
27 #include "xfs_alloc.h"
28 #include "xfs_dinode.h"
29 #include "xfs_inode.h"
30 #include "xfs_inode_item.h"
32 #include "xfs_error.h"
33 #include "xfs_vnodeops.h"
34 #include "xfs_da_btree.h"
35 #include "xfs_ioctl.h"
36 #include "xfs_trace.h"
38 #include <linux/dcache.h>
39 #include <linux/falloc.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
;
242 XFS_STATS_INC(xs_read_calls
);
244 BUG_ON(iocb
->ki_pos
!= pos
);
246 if (unlikely(file
->f_flags
& O_DIRECT
))
247 ioflags
|= IO_ISDIRECT
;
248 if (file
->f_mode
& FMODE_NOCMTIME
)
251 /* START copy & waste from filemap.c */
252 for (seg
= 0; seg
< nr_segs
; seg
++) {
253 const struct iovec
*iv
= &iovp
[seg
];
256 * If any segment has a negative length, or the cumulative
257 * length ever wraps negative then return -EINVAL.
260 if (unlikely((ssize_t
)(size
|iv
->iov_len
) < 0))
261 return XFS_ERROR(-EINVAL
);
263 /* END copy & waste from filemap.c */
265 if (unlikely(ioflags
& IO_ISDIRECT
)) {
266 xfs_buftarg_t
*target
=
267 XFS_IS_REALTIME_INODE(ip
) ?
268 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
269 if ((iocb
->ki_pos
& target
->bt_smask
) ||
270 (size
& target
->bt_smask
)) {
271 if (iocb
->ki_pos
== i_size_read(inode
))
273 return -XFS_ERROR(EINVAL
);
277 n
= XFS_MAXIOFFSET(mp
) - iocb
->ki_pos
;
278 if (n
<= 0 || size
== 0)
284 if (XFS_FORCED_SHUTDOWN(mp
))
288 * Locking is a bit tricky here. If we take an exclusive lock
289 * for direct IO, we effectively serialise all new concurrent
290 * read IO to this file and block it behind IO that is currently in
291 * progress because IO in progress holds the IO lock shared. We only
292 * need to hold the lock exclusive to blow away the page cache, so
293 * only take lock exclusively if the page cache needs invalidation.
294 * This allows the normal direct IO case of no page cache pages to
295 * proceeed concurrently without serialisation.
297 xfs_rw_ilock(ip
, XFS_IOLOCK_SHARED
);
298 if ((ioflags
& IO_ISDIRECT
) && inode
->i_mapping
->nrpages
) {
299 xfs_rw_iunlock(ip
, XFS_IOLOCK_SHARED
);
300 xfs_rw_ilock(ip
, XFS_IOLOCK_EXCL
);
302 if (inode
->i_mapping
->nrpages
) {
303 ret
= -xfs_flushinval_pages(ip
,
304 (iocb
->ki_pos
& PAGE_CACHE_MASK
),
305 -1, FI_REMAPF_LOCKED
);
307 xfs_rw_iunlock(ip
, XFS_IOLOCK_EXCL
);
311 xfs_rw_ilock_demote(ip
, XFS_IOLOCK_EXCL
);
314 trace_xfs_file_read(ip
, size
, iocb
->ki_pos
, ioflags
);
316 ret
= generic_file_aio_read(iocb
, iovp
, nr_segs
, iocb
->ki_pos
);
318 XFS_STATS_ADD(xs_read_bytes
, ret
);
320 xfs_rw_iunlock(ip
, XFS_IOLOCK_SHARED
);
325 xfs_file_splice_read(
328 struct pipe_inode_info
*pipe
,
332 struct xfs_inode
*ip
= XFS_I(infilp
->f_mapping
->host
);
336 XFS_STATS_INC(xs_read_calls
);
338 if (infilp
->f_mode
& FMODE_NOCMTIME
)
341 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
344 xfs_rw_ilock(ip
, XFS_IOLOCK_SHARED
);
346 trace_xfs_file_splice_read(ip
, count
, *ppos
, ioflags
);
348 ret
= generic_file_splice_read(infilp
, ppos
, pipe
, count
, flags
);
350 XFS_STATS_ADD(xs_read_bytes
, ret
);
352 xfs_rw_iunlock(ip
, XFS_IOLOCK_SHARED
);
357 * xfs_file_splice_write() does not use xfs_rw_ilock() because
358 * generic_file_splice_write() takes the i_mutex itself. This, in theory,
359 * couuld cause lock inversions between the aio_write path and the splice path
360 * if someone is doing concurrent splice(2) based writes and write(2) based
361 * writes to the same inode. The only real way to fix this is to re-implement
362 * the generic code here with correct locking orders.
365 xfs_file_splice_write(
366 struct pipe_inode_info
*pipe
,
367 struct file
*outfilp
,
372 struct inode
*inode
= outfilp
->f_mapping
->host
;
373 struct xfs_inode
*ip
= XFS_I(inode
);
377 XFS_STATS_INC(xs_write_calls
);
379 if (outfilp
->f_mode
& FMODE_NOCMTIME
)
382 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
385 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
387 trace_xfs_file_splice_write(ip
, count
, *ppos
, ioflags
);
389 ret
= generic_file_splice_write(pipe
, outfilp
, ppos
, count
, flags
);
391 XFS_STATS_ADD(xs_write_bytes
, ret
);
393 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
398 * This routine is called to handle zeroing any space in the last block of the
399 * file that is beyond the EOF. We do this since the size is being increased
400 * without writing anything to that block and we don't want to read the
401 * garbage on the disk.
403 STATIC
int /* error (positive) */
405 struct xfs_inode
*ip
,
409 struct xfs_mount
*mp
= ip
->i_mount
;
410 xfs_fileoff_t last_fsb
= XFS_B_TO_FSBT(mp
, isize
);
411 int zero_offset
= XFS_B_FSB_OFFSET(mp
, isize
);
415 struct xfs_bmbt_irec imap
;
417 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
418 error
= xfs_bmapi_read(ip
, last_fsb
, 1, &imap
, &nimaps
, 0);
419 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
426 * If the block underlying isize is just a hole, then there
427 * is nothing to zero.
429 if (imap
.br_startblock
== HOLESTARTBLOCK
)
432 zero_len
= mp
->m_sb
.sb_blocksize
- zero_offset
;
433 if (isize
+ zero_len
> offset
)
434 zero_len
= offset
- isize
;
435 return xfs_iozero(ip
, isize
, zero_len
);
439 * Zero any on disk space between the current EOF and the new, larger EOF.
441 * This handles the normal case of zeroing the remainder of the last block in
442 * the file and the unusual case of zeroing blocks out beyond the size of the
443 * file. This second case only happens with fixed size extents and when the
444 * system crashes before the inode size was updated but after blocks were
447 * Expects the iolock to be held exclusive, and will take the ilock internally.
449 int /* error (positive) */
451 struct xfs_inode
*ip
,
452 xfs_off_t offset
, /* starting I/O offset */
453 xfs_fsize_t isize
) /* current inode size */
455 struct xfs_mount
*mp
= ip
->i_mount
;
456 xfs_fileoff_t start_zero_fsb
;
457 xfs_fileoff_t end_zero_fsb
;
458 xfs_fileoff_t zero_count_fsb
;
459 xfs_fileoff_t last_fsb
;
460 xfs_fileoff_t zero_off
;
461 xfs_fsize_t zero_len
;
464 struct xfs_bmbt_irec imap
;
466 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
467 ASSERT(offset
> isize
);
470 * First handle zeroing the block on which isize resides.
472 * We only zero a part of that block so it is handled specially.
474 if (XFS_B_FSB_OFFSET(mp
, isize
) != 0) {
475 error
= xfs_zero_last_block(ip
, offset
, isize
);
481 * Calculate the range between the new size and the old where blocks
482 * needing to be zeroed may exist.
484 * To get the block where the last byte in the file currently resides,
485 * we need to subtract one from the size and truncate back to a block
486 * boundary. We subtract 1 in case the size is exactly on a block
489 last_fsb
= isize
? XFS_B_TO_FSBT(mp
, isize
- 1) : (xfs_fileoff_t
)-1;
490 start_zero_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)isize
);
491 end_zero_fsb
= XFS_B_TO_FSBT(mp
, offset
- 1);
492 ASSERT((xfs_sfiloff_t
)last_fsb
< (xfs_sfiloff_t
)start_zero_fsb
);
493 if (last_fsb
== end_zero_fsb
) {
495 * The size was only incremented on its last block.
496 * We took care of that above, so just return.
501 ASSERT(start_zero_fsb
<= end_zero_fsb
);
502 while (start_zero_fsb
<= end_zero_fsb
) {
504 zero_count_fsb
= end_zero_fsb
- start_zero_fsb
+ 1;
506 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
507 error
= xfs_bmapi_read(ip
, start_zero_fsb
, zero_count_fsb
,
509 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
515 if (imap
.br_state
== XFS_EXT_UNWRITTEN
||
516 imap
.br_startblock
== HOLESTARTBLOCK
) {
517 start_zero_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
518 ASSERT(start_zero_fsb
<= (end_zero_fsb
+ 1));
523 * There are blocks we need to zero.
525 zero_off
= XFS_FSB_TO_B(mp
, start_zero_fsb
);
526 zero_len
= XFS_FSB_TO_B(mp
, imap
.br_blockcount
);
528 if ((zero_off
+ zero_len
) > offset
)
529 zero_len
= offset
- zero_off
;
531 error
= xfs_iozero(ip
, zero_off
, zero_len
);
535 start_zero_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
536 ASSERT(start_zero_fsb
<= (end_zero_fsb
+ 1));
543 * Common pre-write limit and setup checks.
545 * Called with the iolocked held either shared and exclusive according to
546 * @iolock, and returns with it held. Might upgrade the iolock to exclusive
547 * if called for a direct write beyond i_size.
550 xfs_file_aio_write_checks(
556 struct inode
*inode
= file
->f_mapping
->host
;
557 struct xfs_inode
*ip
= XFS_I(inode
);
561 error
= generic_write_checks(file
, pos
, count
, S_ISBLK(inode
->i_mode
));
566 * If the offset is beyond the size of the file, we need to zero any
567 * blocks that fall between the existing EOF and the start of this
568 * write. If zeroing is needed and we are currently holding the
569 * iolock shared, we need to update it to exclusive which implies
570 * having to redo all checks before.
572 if (*pos
> i_size_read(inode
)) {
573 if (*iolock
== XFS_IOLOCK_SHARED
) {
574 xfs_rw_iunlock(ip
, *iolock
);
575 *iolock
= XFS_IOLOCK_EXCL
;
576 xfs_rw_ilock(ip
, *iolock
);
579 error
= -xfs_zero_eof(ip
, *pos
, i_size_read(inode
));
585 * Updating the timestamps will grab the ilock again from
586 * xfs_fs_dirty_inode, so we have to call it after dropping the
587 * lock above. Eventually we should look into a way to avoid
588 * the pointless lock roundtrip.
590 if (likely(!(file
->f_mode
& FMODE_NOCMTIME
)))
591 file_update_time(file
);
594 * If we're writing the file then make sure to clear the setuid and
595 * setgid bits if the process is not being run by root. This keeps
596 * people from modifying setuid and setgid binaries.
598 return file_remove_suid(file
);
602 * xfs_file_dio_aio_write - handle direct IO writes
604 * Lock the inode appropriately to prepare for and issue a direct IO write.
605 * By separating it from the buffered write path we remove all the tricky to
606 * follow locking changes and looping.
608 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
609 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
610 * pages are flushed out.
612 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
613 * allowing them to be done in parallel with reads and other direct IO writes.
614 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
615 * needs to do sub-block zeroing and that requires serialisation against other
616 * direct IOs to the same block. In this case we need to serialise the
617 * submission of the unaligned IOs so that we don't get racing block zeroing in
618 * the dio layer. To avoid the problem with aio, we also need to wait for
619 * outstanding IOs to complete so that unwritten extent conversion is completed
620 * before we try to map the overlapping block. This is currently implemented by
621 * hitting it with a big hammer (i.e. inode_dio_wait()).
623 * Returns with locks held indicated by @iolock and errors indicated by
624 * negative return values.
627 xfs_file_dio_aio_write(
629 const struct iovec
*iovp
,
630 unsigned long nr_segs
,
634 struct file
*file
= iocb
->ki_filp
;
635 struct address_space
*mapping
= file
->f_mapping
;
636 struct inode
*inode
= mapping
->host
;
637 struct xfs_inode
*ip
= XFS_I(inode
);
638 struct xfs_mount
*mp
= ip
->i_mount
;
640 size_t count
= ocount
;
641 int unaligned_io
= 0;
643 struct xfs_buftarg
*target
= XFS_IS_REALTIME_INODE(ip
) ?
644 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
646 if ((pos
& target
->bt_smask
) || (count
& target
->bt_smask
))
647 return -XFS_ERROR(EINVAL
);
649 if ((pos
& mp
->m_blockmask
) || ((pos
+ count
) & mp
->m_blockmask
))
653 * We don't need to take an exclusive lock unless there page cache needs
654 * to be invalidated or unaligned IO is being executed. We don't need to
655 * consider the EOF extension case here because
656 * xfs_file_aio_write_checks() will relock the inode as necessary for
657 * EOF zeroing cases and fill out the new inode size as appropriate.
659 if (unaligned_io
|| mapping
->nrpages
)
660 iolock
= XFS_IOLOCK_EXCL
;
662 iolock
= XFS_IOLOCK_SHARED
;
663 xfs_rw_ilock(ip
, iolock
);
666 * Recheck if there are cached pages that need invalidate after we got
667 * the iolock to protect against other threads adding new pages while
668 * we were waiting for the iolock.
670 if (mapping
->nrpages
&& iolock
== XFS_IOLOCK_SHARED
) {
671 xfs_rw_iunlock(ip
, iolock
);
672 iolock
= XFS_IOLOCK_EXCL
;
673 xfs_rw_ilock(ip
, iolock
);
676 ret
= xfs_file_aio_write_checks(file
, &pos
, &count
, &iolock
);
680 if (mapping
->nrpages
) {
681 ret
= -xfs_flushinval_pages(ip
, (pos
& PAGE_CACHE_MASK
), -1,
688 * If we are doing unaligned IO, wait for all other IO to drain,
689 * otherwise demote the lock if we had to flush cached pages
692 inode_dio_wait(inode
);
693 else if (iolock
== XFS_IOLOCK_EXCL
) {
694 xfs_rw_ilock_demote(ip
, XFS_IOLOCK_EXCL
);
695 iolock
= XFS_IOLOCK_SHARED
;
698 trace_xfs_file_direct_write(ip
, count
, iocb
->ki_pos
, 0);
699 ret
= generic_file_direct_write(iocb
, iovp
,
700 &nr_segs
, pos
, &iocb
->ki_pos
, count
, ocount
);
703 xfs_rw_iunlock(ip
, iolock
);
705 /* No fallback to buffered IO on errors for XFS. */
706 ASSERT(ret
< 0 || ret
== count
);
711 xfs_file_buffered_aio_write(
713 const struct iovec
*iovp
,
714 unsigned long nr_segs
,
718 struct file
*file
= iocb
->ki_filp
;
719 struct address_space
*mapping
= file
->f_mapping
;
720 struct inode
*inode
= mapping
->host
;
721 struct xfs_inode
*ip
= XFS_I(inode
);
724 int iolock
= XFS_IOLOCK_EXCL
;
725 size_t count
= ocount
;
727 xfs_rw_ilock(ip
, iolock
);
729 ret
= xfs_file_aio_write_checks(file
, &pos
, &count
, &iolock
);
733 /* We can write back this queue in page reclaim */
734 current
->backing_dev_info
= mapping
->backing_dev_info
;
737 trace_xfs_file_buffered_write(ip
, count
, iocb
->ki_pos
, 0);
738 ret
= generic_file_buffered_write(iocb
, iovp
, nr_segs
,
739 pos
, &iocb
->ki_pos
, count
, ret
);
741 * if we just got an ENOSPC, flush the inode now we aren't holding any
742 * page locks and retry *once*
744 if (ret
== -ENOSPC
&& !enospc
) {
746 ret
= -xfs_flush_pages(ip
, 0, -1, 0, FI_NONE
);
751 current
->backing_dev_info
= NULL
;
753 xfs_rw_iunlock(ip
, iolock
);
760 const struct iovec
*iovp
,
761 unsigned long nr_segs
,
764 struct file
*file
= iocb
->ki_filp
;
765 struct address_space
*mapping
= file
->f_mapping
;
766 struct inode
*inode
= mapping
->host
;
767 struct xfs_inode
*ip
= XFS_I(inode
);
771 XFS_STATS_INC(xs_write_calls
);
773 BUG_ON(iocb
->ki_pos
!= pos
);
775 ret
= generic_segment_checks(iovp
, &nr_segs
, &ocount
, VERIFY_READ
);
782 xfs_wait_for_freeze(ip
->i_mount
, SB_FREEZE_WRITE
);
784 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
787 if (unlikely(file
->f_flags
& O_DIRECT
))
788 ret
= xfs_file_dio_aio_write(iocb
, iovp
, nr_segs
, pos
, ocount
);
790 ret
= xfs_file_buffered_aio_write(iocb
, iovp
, nr_segs
, pos
,
796 XFS_STATS_ADD(xs_write_bytes
, ret
);
798 /* Handle various SYNC-type writes */
799 err
= generic_write_sync(file
, pos
, ret
);
814 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
818 xfs_inode_t
*ip
= XFS_I(inode
);
819 int cmd
= XFS_IOC_RESVSP
;
820 int attr_flags
= XFS_ATTR_NOLOCK
;
822 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
829 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
831 if (mode
& FALLOC_FL_PUNCH_HOLE
)
832 cmd
= XFS_IOC_UNRESVSP
;
834 /* check the new inode size is valid before allocating */
835 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
836 offset
+ len
> i_size_read(inode
)) {
837 new_size
= offset
+ len
;
838 error
= inode_newsize_ok(inode
, new_size
);
843 if (file
->f_flags
& O_DSYNC
)
844 attr_flags
|= XFS_ATTR_SYNC
;
846 error
= -xfs_change_file_space(ip
, cmd
, &bf
, 0, attr_flags
);
850 /* Change file size if needed */
854 iattr
.ia_valid
= ATTR_SIZE
;
855 iattr
.ia_size
= new_size
;
856 error
= -xfs_setattr_size(ip
, &iattr
, XFS_ATTR_NOLOCK
);
860 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
870 if (!(file
->f_flags
& O_LARGEFILE
) && i_size_read(inode
) > MAX_NON_LFS
)
872 if (XFS_FORCED_SHUTDOWN(XFS_M(inode
->i_sb
)))
882 struct xfs_inode
*ip
= XFS_I(inode
);
886 error
= xfs_file_open(inode
, file
);
891 * If there are any blocks, read-ahead block 0 as we're almost
892 * certain to have the next operation be a read there.
894 mode
= xfs_ilock_map_shared(ip
);
895 if (ip
->i_d
.di_nextents
> 0)
896 xfs_da_reada_buf(NULL
, ip
, 0, XFS_DATA_FORK
);
897 xfs_iunlock(ip
, mode
);
906 return -xfs_release(XFS_I(inode
));
915 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
916 xfs_inode_t
*ip
= XFS_I(inode
);
921 * The Linux API doesn't pass down the total size of the buffer
922 * we read into down to the filesystem. With the filldir concept
923 * it's not needed for correct information, but the XFS dir2 leaf
924 * code wants an estimate of the buffer size to calculate it's
925 * readahead window and size the buffers used for mapping to
928 * Try to give it an estimate that's good enough, maybe at some
929 * point we can change the ->readdir prototype to include the
930 * buffer size. For now we use the current glibc buffer size.
932 bufsize
= (size_t)min_t(loff_t
, 32768, ip
->i_d
.di_size
);
934 error
= xfs_readdir(ip
, dirent
, bufsize
,
935 (xfs_off_t
*)&filp
->f_pos
, filldir
);
944 struct vm_area_struct
*vma
)
946 vma
->vm_ops
= &xfs_file_vm_ops
;
947 vma
->vm_flags
|= VM_CAN_NONLINEAR
;
954 * mmap()d file has taken write protection fault and is being made
955 * writable. We can set the page state up correctly for a writable
956 * page, which means we can do correct delalloc accounting (ENOSPC
957 * checking!) and unwritten extent mapping.
961 struct vm_area_struct
*vma
,
962 struct vm_fault
*vmf
)
964 return block_page_mkwrite(vma
, vmf
, xfs_get_blocks
);
967 const struct file_operations xfs_file_operations
= {
968 .llseek
= generic_file_llseek
,
969 .read
= do_sync_read
,
970 .write
= do_sync_write
,
971 .aio_read
= xfs_file_aio_read
,
972 .aio_write
= xfs_file_aio_write
,
973 .splice_read
= xfs_file_splice_read
,
974 .splice_write
= xfs_file_splice_write
,
975 .unlocked_ioctl
= xfs_file_ioctl
,
977 .compat_ioctl
= xfs_file_compat_ioctl
,
979 .mmap
= xfs_file_mmap
,
980 .open
= xfs_file_open
,
981 .release
= xfs_file_release
,
982 .fsync
= xfs_file_fsync
,
983 .fallocate
= xfs_file_fallocate
,
986 const struct file_operations xfs_dir_file_operations
= {
987 .open
= xfs_dir_open
,
988 .read
= generic_read_dir
,
989 .readdir
= xfs_file_readdir
,
990 .llseek
= generic_file_llseek
,
991 .unlocked_ioctl
= xfs_file_ioctl
,
993 .compat_ioctl
= xfs_file_compat_ioctl
,
995 .fsync
= xfs_dir_fsync
,
998 static const struct vm_operations_struct xfs_file_vm_ops
= {
999 .fault
= filemap_fault
,
1000 .page_mkwrite
= xfs_vm_page_mkwrite
,