2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "xfs_trans.h"
26 #include "xfs_mount.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_alloc.h"
29 #include "xfs_dinode.h"
30 #include "xfs_inode.h"
31 #include "xfs_inode_item.h"
33 #include "xfs_error.h"
34 #include "xfs_vnodeops.h"
35 #include "xfs_da_btree.h"
36 #include "xfs_ioctl.h"
37 #include "xfs_trace.h"
39 #include <linux/dcache.h>
40 #include <linux/falloc.h>
42 static const struct vm_operations_struct xfs_file_vm_ops;
45 * Locking primitives for read and write IO paths to ensure we consistently use
46 * and order the inode->i_mutex, ip->i_lock and ip->i_iolock.
53 if (type & XFS_IOLOCK_EXCL)
54 mutex_lock(&VFS_I(ip)->i_mutex);
63 xfs_iunlock(ip, type);
64 if (type & XFS_IOLOCK_EXCL)
65 mutex_unlock(&VFS_I(ip)->i_mutex);
73 xfs_ilock_demote(ip, type);
74 if (type & XFS_IOLOCK_EXCL)
75 mutex_unlock(&VFS_I(ip)->i_mutex);
81 * xfs_iozero clears the specified range of buffer supplied,
82 * and marks all the affected blocks as valid and modified. If
83 * an affected block is not allocated, it will be allocated. If
84 * an affected block is not completely overwritten, and is not
85 * valid before the operation, it will be read from disk before
86 * being partially zeroed.
90 struct xfs_inode *ip, /* inode */
91 loff_t pos, /* offset in file */
92 size_t count) /* size of data to zero */
95 struct address_space *mapping;
98 mapping = VFS_I(ip)->i_mapping;
100 unsigned offset, bytes;
103 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
104 bytes = PAGE_CACHE_SIZE - offset;
108 status = pagecache_write_begin(NULL, mapping, pos, bytes,
109 AOP_FLAG_UNINTERRUPTIBLE,
114 zero_user(page, offset, bytes);
116 status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
118 WARN_ON(status <= 0); /* can't return less than zero! */
128 * Fsync operations on directories are much simpler than on regular files,
129 * as there is no file data to flush, and thus also no need for explicit
130 * cache flush operations, and there are no non-transaction metadata updates
131 * on directories either.
140 struct xfs_inode *ip = XFS_I(file->f_mapping->host);
141 struct xfs_mount *mp = ip->i_mount;
144 trace_xfs_dir_fsync(ip);
146 xfs_ilock(ip, XFS_ILOCK_SHARED);
147 if (xfs_ipincount(ip))
148 lsn = ip->i_itemp->ili_last_lsn;
149 xfs_iunlock(ip, XFS_ILOCK_SHARED);
153 return _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
163 struct inode *inode = file->f_mapping->host;
164 struct xfs_inode *ip = XFS_I(inode);
165 struct xfs_mount *mp = ip->i_mount;
170 trace_xfs_file_fsync(ip);
172 error = filemap_write_and_wait_range(inode->i_mapping, start, end);
176 if (XFS_FORCED_SHUTDOWN(mp))
177 return -XFS_ERROR(EIO);
179 xfs_iflags_clear(ip, XFS_ITRUNCATED);
181 if (mp->m_flags & XFS_MOUNT_BARRIER) {
183 * If we have an RT and/or log subvolume we need to make sure
184 * to flush the write cache the device used for file data
185 * first. This is to ensure newly written file data make
186 * it to disk before logging the new inode size in case of
187 * an extending write.
189 if (XFS_IS_REALTIME_INODE(ip))
190 xfs_blkdev_issue_flush(mp->m_rtdev_targp);
191 else if (mp->m_logdev_targp != mp->m_ddev_targp)
192 xfs_blkdev_issue_flush(mp->m_ddev_targp);
196 * All metadata updates are logged, which means that we just have
197 * to flush the log up to the latest LSN that touched the inode.
199 xfs_ilock(ip, XFS_ILOCK_SHARED);
200 if (xfs_ipincount(ip)) {
202 (ip->i_itemp->ili_fields & ~XFS_ILOG_TIMESTAMP))
203 lsn = ip->i_itemp->ili_last_lsn;
205 xfs_iunlock(ip, XFS_ILOCK_SHARED);
208 error = _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, &log_flushed);
211 * If we only have a single device, and the log force about was
212 * a no-op we might have to flush the data device cache here.
213 * This can only happen for fdatasync/O_DSYNC if we were overwriting
214 * an already allocated file and thus do not have any metadata to
217 if ((mp->m_flags & XFS_MOUNT_BARRIER) &&
218 mp->m_logdev_targp == mp->m_ddev_targp &&
219 !XFS_IS_REALTIME_INODE(ip) &&
221 xfs_blkdev_issue_flush(mp->m_ddev_targp);
229 const struct iovec *iovp,
230 unsigned long nr_segs,
233 struct file *file = iocb->ki_filp;
234 struct inode *inode = file->f_mapping->host;
235 struct xfs_inode *ip = XFS_I(inode);
236 struct xfs_mount *mp = ip->i_mount;
243 XFS_STATS_INC(xs_read_calls);
245 BUG_ON(iocb->ki_pos != pos);
247 if (unlikely(file->f_flags & O_DIRECT))
248 ioflags |= IO_ISDIRECT;
249 if (file->f_mode & FMODE_NOCMTIME)
252 /* START copy & waste from filemap.c */
253 for (seg = 0; seg < nr_segs; seg++) {
254 const struct iovec *iv = &iovp[seg];
257 * If any segment has a negative length, or the cumulative
258 * length ever wraps negative then return -EINVAL.
261 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
262 return XFS_ERROR(-EINVAL);
264 /* END copy & waste from filemap.c */
266 if (unlikely(ioflags & IO_ISDIRECT)) {
267 xfs_buftarg_t *target =
268 XFS_IS_REALTIME_INODE(ip) ?
269 mp->m_rtdev_targp : mp->m_ddev_targp;
270 if ((iocb->ki_pos & target->bt_smask) ||
271 (size & target->bt_smask)) {
272 if (iocb->ki_pos == i_size_read(inode))
274 return -XFS_ERROR(EINVAL);
278 n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
279 if (n <= 0 || size == 0)
285 if (XFS_FORCED_SHUTDOWN(mp))
289 * Locking is a bit tricky here. If we take an exclusive lock
290 * for direct IO, we effectively serialise all new concurrent
291 * read IO to this file and block it behind IO that is currently in
292 * progress because IO in progress holds the IO lock shared. We only
293 * need to hold the lock exclusive to blow away the page cache, so
294 * only take lock exclusively if the page cache needs invalidation.
295 * This allows the normal direct IO case of no page cache pages to
296 * proceeed concurrently without serialisation.
298 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
299 if ((ioflags & IO_ISDIRECT) && inode->i_mapping->nrpages) {
300 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
301 xfs_rw_ilock(ip, XFS_IOLOCK_EXCL);
303 if (inode->i_mapping->nrpages) {
304 ret = -xfs_flushinval_pages(ip,
305 (iocb->ki_pos & PAGE_CACHE_MASK),
306 -1, FI_REMAPF_LOCKED);
308 xfs_rw_iunlock(ip, XFS_IOLOCK_EXCL);
312 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
315 trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags);
317 ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos);
319 XFS_STATS_ADD(xs_read_bytes, ret);
321 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
326 xfs_file_splice_read(
329 struct pipe_inode_info *pipe,
333 struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
337 XFS_STATS_INC(xs_read_calls);
339 if (infilp->f_mode & FMODE_NOCMTIME)
342 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
345 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
347 trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
349 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
351 XFS_STATS_ADD(xs_read_bytes, ret);
353 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
358 * xfs_file_splice_write() does not use xfs_rw_ilock() because
359 * generic_file_splice_write() takes the i_mutex itself. This, in theory,
360 * couuld cause lock inversions between the aio_write path and the splice path
361 * if someone is doing concurrent splice(2) based writes and write(2) based
362 * writes to the same inode. The only real way to fix this is to re-implement
363 * the generic code here with correct locking orders.
366 xfs_file_splice_write(
367 struct pipe_inode_info *pipe,
368 struct file *outfilp,
373 struct inode *inode = outfilp->f_mapping->host;
374 struct xfs_inode *ip = XFS_I(inode);
378 XFS_STATS_INC(xs_write_calls);
380 if (outfilp->f_mode & FMODE_NOCMTIME)
383 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
386 xfs_ilock(ip, XFS_IOLOCK_EXCL);
388 trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
390 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
392 XFS_STATS_ADD(xs_write_bytes, ret);
394 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
399 * This routine is called to handle zeroing any space in the last
400 * block of the file that is beyond the EOF. We do this since the
401 * size is being increased without writing anything to that block
402 * and we don't want anyone to read the garbage on the disk.
404 STATIC int /* error (positive) */
410 xfs_fileoff_t last_fsb;
411 xfs_mount_t *mp = ip->i_mount;
416 xfs_bmbt_irec_t imap;
418 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
420 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
421 if (zero_offset == 0) {
423 * There are no extra bytes in the last block on disk to
429 last_fsb = XFS_B_TO_FSBT(mp, isize);
431 error = xfs_bmapi_read(ip, last_fsb, 1, &imap, &nimaps, 0);
436 * If the block underlying isize is just a hole, then there
437 * is nothing to zero.
439 if (imap.br_startblock == HOLESTARTBLOCK) {
443 * Zero the part of the last block beyond the EOF, and write it
444 * out sync. We need to drop the ilock while we do this so we
445 * don't deadlock when the buffer cache calls back to us.
447 xfs_iunlock(ip, XFS_ILOCK_EXCL);
449 zero_len = mp->m_sb.sb_blocksize - zero_offset;
450 if (isize + zero_len > offset)
451 zero_len = offset - isize;
452 error = xfs_iozero(ip, isize, zero_len);
454 xfs_ilock(ip, XFS_ILOCK_EXCL);
460 * Zero any on disk space between the current EOF and the new,
461 * larger EOF. This handles the normal case of zeroing the remainder
462 * of the last block in the file and the unusual case of zeroing blocks
463 * out beyond the size of the file. This second case only happens
464 * with fixed size extents and when the system crashes before the inode
465 * size was updated but after blocks were allocated. If fill is set,
466 * then any holes in the range are filled and zeroed. If not, the holes
467 * are left alone as holes.
470 int /* error (positive) */
473 xfs_off_t offset, /* starting I/O offset */
474 xfs_fsize_t isize) /* current inode size */
476 xfs_mount_t *mp = ip->i_mount;
477 xfs_fileoff_t start_zero_fsb;
478 xfs_fileoff_t end_zero_fsb;
479 xfs_fileoff_t zero_count_fsb;
480 xfs_fileoff_t last_fsb;
481 xfs_fileoff_t zero_off;
482 xfs_fsize_t zero_len;
485 xfs_bmbt_irec_t imap;
487 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
488 ASSERT(offset > isize);
491 * First handle zeroing the block on which isize resides.
492 * We only zero a part of that block so it is handled specially.
494 error = xfs_zero_last_block(ip, offset, isize);
496 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
501 * Calculate the range between the new size and the old
502 * where blocks needing to be zeroed may exist. To get the
503 * block where the last byte in the file currently resides,
504 * we need to subtract one from the size and truncate back
505 * to a block boundary. We subtract 1 in case the size is
506 * exactly on a block boundary.
508 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
509 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
510 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
511 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
512 if (last_fsb == end_zero_fsb) {
514 * The size was only incremented on its last block.
515 * We took care of that above, so just return.
520 ASSERT(start_zero_fsb <= end_zero_fsb);
521 while (start_zero_fsb <= end_zero_fsb) {
523 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
524 error = xfs_bmapi_read(ip, start_zero_fsb, zero_count_fsb,
527 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
532 if (imap.br_state == XFS_EXT_UNWRITTEN ||
533 imap.br_startblock == HOLESTARTBLOCK) {
535 * This loop handles initializing pages that were
536 * partially initialized by the code below this
537 * loop. It basically zeroes the part of the page
538 * that sits on a hole and sets the page as P_HOLE
539 * and calls remapf if it is a mapped file.
541 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
542 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
547 * There are blocks we need to zero.
548 * Drop the inode lock while we're doing the I/O.
549 * We'll still have the iolock to protect us.
551 xfs_iunlock(ip, XFS_ILOCK_EXCL);
553 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
554 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
556 if ((zero_off + zero_len) > offset)
557 zero_len = offset - zero_off;
559 error = xfs_iozero(ip, zero_off, zero_len);
564 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
565 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
567 xfs_ilock(ip, XFS_ILOCK_EXCL);
573 xfs_ilock(ip, XFS_ILOCK_EXCL);
579 * Common pre-write limit and setup checks.
581 * Called with the iolocked held either shared and exclusive according to
582 * @iolock, and returns with it held. Might upgrade the iolock to exclusive
583 * if called for a direct write beyond i_size.
586 xfs_file_aio_write_checks(
592 struct inode *inode = file->f_mapping->host;
593 struct xfs_inode *ip = XFS_I(inode);
596 xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
598 error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode));
600 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
605 * If the offset is beyond the size of the file, we need to zero any
606 * blocks that fall between the existing EOF and the start of this
607 * write. If zeroing is needed and we are currently holding the
608 * iolock shared, we need to update it to exclusive which involves
609 * dropping all locks and relocking to maintain correct locking order.
610 * If we do this, restart the function to ensure all checks and values
613 if (*pos > i_size_read(inode)) {
614 if (*iolock == XFS_IOLOCK_SHARED) {
615 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL | *iolock);
616 *iolock = XFS_IOLOCK_EXCL;
617 xfs_rw_ilock(ip, XFS_ILOCK_EXCL | *iolock);
620 error = -xfs_zero_eof(ip, *pos, i_size_read(inode));
622 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
627 * Updating the timestamps will grab the ilock again from
628 * xfs_fs_dirty_inode, so we have to call it after dropping the
629 * lock above. Eventually we should look into a way to avoid
630 * the pointless lock roundtrip.
632 if (likely(!(file->f_mode & FMODE_NOCMTIME)))
633 file_update_time(file);
636 * If we're writing the file then make sure to clear the setuid and
637 * setgid bits if the process is not being run by root. This keeps
638 * people from modifying setuid and setgid binaries.
640 return file_remove_suid(file);
645 * xfs_file_dio_aio_write - handle direct IO writes
647 * Lock the inode appropriately to prepare for and issue a direct IO write.
648 * By separating it from the buffered write path we remove all the tricky to
649 * follow locking changes and looping.
651 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
652 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
653 * pages are flushed out.
655 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
656 * allowing them to be done in parallel with reads and other direct IO writes.
657 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
658 * needs to do sub-block zeroing and that requires serialisation against other
659 * direct IOs to the same block. In this case we need to serialise the
660 * submission of the unaligned IOs so that we don't get racing block zeroing in
661 * the dio layer. To avoid the problem with aio, we also need to wait for
662 * outstanding IOs to complete so that unwritten extent conversion is completed
663 * before we try to map the overlapping block. This is currently implemented by
664 * hitting it with a big hammer (i.e. inode_dio_wait()).
666 * Returns with locks held indicated by @iolock and errors indicated by
667 * negative return values.
670 xfs_file_dio_aio_write(
672 const struct iovec *iovp,
673 unsigned long nr_segs,
677 struct file *file = iocb->ki_filp;
678 struct address_space *mapping = file->f_mapping;
679 struct inode *inode = mapping->host;
680 struct xfs_inode *ip = XFS_I(inode);
681 struct xfs_mount *mp = ip->i_mount;
683 size_t count = ocount;
684 int unaligned_io = 0;
686 struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
687 mp->m_rtdev_targp : mp->m_ddev_targp;
689 if ((pos & target->bt_smask) || (count & target->bt_smask))
690 return -XFS_ERROR(EINVAL);
692 if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
696 * We don't need to take an exclusive lock unless there page cache needs
697 * to be invalidated or unaligned IO is being executed. We don't need to
698 * consider the EOF extension case here because
699 * xfs_file_aio_write_checks() will relock the inode as necessary for
700 * EOF zeroing cases and fill out the new inode size as appropriate.
702 if (unaligned_io || mapping->nrpages)
703 iolock = XFS_IOLOCK_EXCL;
705 iolock = XFS_IOLOCK_SHARED;
706 xfs_rw_ilock(ip, iolock);
709 * Recheck if there are cached pages that need invalidate after we got
710 * the iolock to protect against other threads adding new pages while
711 * we were waiting for the iolock.
713 if (mapping->nrpages && iolock == XFS_IOLOCK_SHARED) {
714 xfs_rw_iunlock(ip, iolock);
715 iolock = XFS_IOLOCK_EXCL;
716 xfs_rw_ilock(ip, iolock);
719 ret = xfs_file_aio_write_checks(file, &pos, &count, &iolock);
723 if (mapping->nrpages) {
724 ret = -xfs_flushinval_pages(ip, (pos & PAGE_CACHE_MASK), -1,
731 * If we are doing unaligned IO, wait for all other IO to drain,
732 * otherwise demote the lock if we had to flush cached pages
735 inode_dio_wait(inode);
736 else if (iolock == XFS_IOLOCK_EXCL) {
737 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
738 iolock = XFS_IOLOCK_SHARED;
741 trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
742 ret = generic_file_direct_write(iocb, iovp,
743 &nr_segs, pos, &iocb->ki_pos, count, ocount);
746 xfs_rw_iunlock(ip, iolock);
748 /* No fallback to buffered IO on errors for XFS. */
749 ASSERT(ret < 0 || ret == count);
754 xfs_file_buffered_aio_write(
756 const struct iovec *iovp,
757 unsigned long nr_segs,
761 struct file *file = iocb->ki_filp;
762 struct address_space *mapping = file->f_mapping;
763 struct inode *inode = mapping->host;
764 struct xfs_inode *ip = XFS_I(inode);
767 int iolock = XFS_IOLOCK_EXCL;
768 size_t count = ocount;
770 xfs_rw_ilock(ip, iolock);
772 ret = xfs_file_aio_write_checks(file, &pos, &count, &iolock);
776 /* We can write back this queue in page reclaim */
777 current->backing_dev_info = mapping->backing_dev_info;
780 trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
781 ret = generic_file_buffered_write(iocb, iovp, nr_segs,
782 pos, &iocb->ki_pos, count, ret);
784 * if we just got an ENOSPC, flush the inode now we aren't holding any
785 * page locks and retry *once*
787 if (ret == -ENOSPC && !enospc) {
789 ret = -xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
794 current->backing_dev_info = NULL;
796 xfs_rw_iunlock(ip, iolock);
803 const struct iovec *iovp,
804 unsigned long nr_segs,
807 struct file *file = iocb->ki_filp;
808 struct address_space *mapping = file->f_mapping;
809 struct inode *inode = mapping->host;
810 struct xfs_inode *ip = XFS_I(inode);
814 XFS_STATS_INC(xs_write_calls);
816 BUG_ON(iocb->ki_pos != pos);
818 ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
825 xfs_wait_for_freeze(ip->i_mount, SB_FREEZE_WRITE);
827 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
830 if (unlikely(file->f_flags & O_DIRECT))
831 ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos, ocount);
833 ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
839 XFS_STATS_ADD(xs_write_bytes, ret);
841 /* Handle various SYNC-type writes */
842 err = generic_write_sync(file, pos, ret);
857 struct inode *inode = file->f_path.dentry->d_inode;
861 xfs_inode_t *ip = XFS_I(inode);
862 int cmd = XFS_IOC_RESVSP;
863 int attr_flags = XFS_ATTR_NOLOCK;
865 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
872 xfs_ilock(ip, XFS_IOLOCK_EXCL);
874 if (mode & FALLOC_FL_PUNCH_HOLE)
875 cmd = XFS_IOC_UNRESVSP;
877 /* check the new inode size is valid before allocating */
878 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
879 offset + len > i_size_read(inode)) {
880 new_size = offset + len;
881 error = inode_newsize_ok(inode, new_size);
886 if (file->f_flags & O_DSYNC)
887 attr_flags |= XFS_ATTR_SYNC;
889 error = -xfs_change_file_space(ip, cmd, &bf, 0, attr_flags);
893 /* Change file size if needed */
897 iattr.ia_valid = ATTR_SIZE;
898 iattr.ia_size = new_size;
899 error = -xfs_setattr_size(ip, &iattr, XFS_ATTR_NOLOCK);
903 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
913 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
915 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
925 struct xfs_inode *ip = XFS_I(inode);
929 error = xfs_file_open(inode, file);
934 * If there are any blocks, read-ahead block 0 as we're almost
935 * certain to have the next operation be a read there.
937 mode = xfs_ilock_map_shared(ip);
938 if (ip->i_d.di_nextents > 0)
939 xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
940 xfs_iunlock(ip, mode);
949 return -xfs_release(XFS_I(inode));
958 struct inode *inode = filp->f_path.dentry->d_inode;
959 xfs_inode_t *ip = XFS_I(inode);
964 * The Linux API doesn't pass down the total size of the buffer
965 * we read into down to the filesystem. With the filldir concept
966 * it's not needed for correct information, but the XFS dir2 leaf
967 * code wants an estimate of the buffer size to calculate it's
968 * readahead window and size the buffers used for mapping to
971 * Try to give it an estimate that's good enough, maybe at some
972 * point we can change the ->readdir prototype to include the
973 * buffer size. For now we use the current glibc buffer size.
975 bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
977 error = xfs_readdir(ip, dirent, bufsize,
978 (xfs_off_t *)&filp->f_pos, filldir);
987 struct vm_area_struct *vma)
989 vma->vm_ops = &xfs_file_vm_ops;
990 vma->vm_flags |= VM_CAN_NONLINEAR;
997 * mmap()d file has taken write protection fault and is being made
998 * writable. We can set the page state up correctly for a writable
999 * page, which means we can do correct delalloc accounting (ENOSPC
1000 * checking!) and unwritten extent mapping.
1003 xfs_vm_page_mkwrite(
1004 struct vm_area_struct *vma,
1005 struct vm_fault *vmf)
1007 return block_page_mkwrite(vma, vmf, xfs_get_blocks);
1010 const struct file_operations xfs_file_operations = {
1011 .llseek = generic_file_llseek,
1012 .read = do_sync_read,
1013 .write = do_sync_write,
1014 .aio_read = xfs_file_aio_read,
1015 .aio_write = xfs_file_aio_write,
1016 .splice_read = xfs_file_splice_read,
1017 .splice_write = xfs_file_splice_write,
1018 .unlocked_ioctl = xfs_file_ioctl,
1019 #ifdef CONFIG_COMPAT
1020 .compat_ioctl = xfs_file_compat_ioctl,
1022 .mmap = xfs_file_mmap,
1023 .open = xfs_file_open,
1024 .release = xfs_file_release,
1025 .fsync = xfs_file_fsync,
1026 .fallocate = xfs_file_fallocate,
1029 const struct file_operations xfs_dir_file_operations = {
1030 .open = xfs_dir_open,
1031 .read = generic_read_dir,
1032 .readdir = xfs_file_readdir,
1033 .llseek = generic_file_llseek,
1034 .unlocked_ioctl = xfs_file_ioctl,
1035 #ifdef CONFIG_COMPAT
1036 .compat_ioctl = xfs_file_compat_ioctl,
1038 .fsync = xfs_dir_fsync,
1041 static const struct vm_operations_struct xfs_file_vm_ops = {
1042 .fault = filemap_fault,
1043 .page_mkwrite = xfs_vm_page_mkwrite,