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"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_format.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_inode.h"
30 #include "xfs_trans.h"
31 #include "xfs_inode_item.h"
33 #include "xfs_bmap_util.h"
34 #include "xfs_error.h"
36 #include "xfs_dir2_priv.h"
37 #include "xfs_ioctl.h"
38 #include "xfs_trace.h"
40 #include "xfs_dinode.h"
42 #include <linux/aio.h>
43 #include <linux/dcache.h>
44 #include <linux/falloc.h>
45 #include <linux/pagevec.h>
47 static const struct vm_operations_struct xfs_file_vm_ops;
50 * Locking primitives for read and write IO paths to ensure we consistently use
51 * and order the inode->i_mutex, ip->i_lock and ip->i_iolock.
58 if (type & XFS_IOLOCK_EXCL)
59 mutex_lock(&VFS_I(ip)->i_mutex);
68 xfs_iunlock(ip, type);
69 if (type & XFS_IOLOCK_EXCL)
70 mutex_unlock(&VFS_I(ip)->i_mutex);
78 xfs_ilock_demote(ip, type);
79 if (type & XFS_IOLOCK_EXCL)
80 mutex_unlock(&VFS_I(ip)->i_mutex);
86 * xfs_iozero clears the specified range of buffer supplied,
87 * and marks all the affected blocks as valid and modified. If
88 * an affected block is not allocated, it will be allocated. If
89 * an affected block is not completely overwritten, and is not
90 * valid before the operation, it will be read from disk before
91 * being partially zeroed.
95 struct xfs_inode *ip, /* inode */
96 loff_t pos, /* offset in file */
97 size_t count) /* size of data to zero */
100 struct address_space *mapping;
103 mapping = VFS_I(ip)->i_mapping;
105 unsigned offset, bytes;
108 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
109 bytes = PAGE_CACHE_SIZE - offset;
113 status = pagecache_write_begin(NULL, mapping, pos, bytes,
114 AOP_FLAG_UNINTERRUPTIBLE,
119 zero_user(page, offset, bytes);
121 status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
123 WARN_ON(status <= 0); /* can't return less than zero! */
133 * Fsync operations on directories are much simpler than on regular files,
134 * as there is no file data to flush, and thus also no need for explicit
135 * cache flush operations, and there are no non-transaction metadata updates
136 * on directories either.
145 struct xfs_inode *ip = XFS_I(file->f_mapping->host);
146 struct xfs_mount *mp = ip->i_mount;
149 trace_xfs_dir_fsync(ip);
151 xfs_ilock(ip, XFS_ILOCK_SHARED);
152 if (xfs_ipincount(ip))
153 lsn = ip->i_itemp->ili_last_lsn;
154 xfs_iunlock(ip, XFS_ILOCK_SHARED);
158 return _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
168 struct inode *inode = file->f_mapping->host;
169 struct xfs_inode *ip = XFS_I(inode);
170 struct xfs_mount *mp = ip->i_mount;
175 trace_xfs_file_fsync(ip);
177 error = filemap_write_and_wait_range(inode->i_mapping, start, end);
181 if (XFS_FORCED_SHUTDOWN(mp))
182 return -XFS_ERROR(EIO);
184 xfs_iflags_clear(ip, XFS_ITRUNCATED);
186 if (mp->m_flags & XFS_MOUNT_BARRIER) {
188 * If we have an RT and/or log subvolume we need to make sure
189 * to flush the write cache the device used for file data
190 * first. This is to ensure newly written file data make
191 * it to disk before logging the new inode size in case of
192 * an extending write.
194 if (XFS_IS_REALTIME_INODE(ip))
195 xfs_blkdev_issue_flush(mp->m_rtdev_targp);
196 else if (mp->m_logdev_targp != mp->m_ddev_targp)
197 xfs_blkdev_issue_flush(mp->m_ddev_targp);
201 * All metadata updates are logged, which means that we just have
202 * to flush the log up to the latest LSN that touched the inode.
204 xfs_ilock(ip, XFS_ILOCK_SHARED);
205 if (xfs_ipincount(ip)) {
207 (ip->i_itemp->ili_fields & ~XFS_ILOG_TIMESTAMP))
208 lsn = ip->i_itemp->ili_last_lsn;
210 xfs_iunlock(ip, XFS_ILOCK_SHARED);
213 error = _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, &log_flushed);
216 * If we only have a single device, and the log force about was
217 * a no-op we might have to flush the data device cache here.
218 * This can only happen for fdatasync/O_DSYNC if we were overwriting
219 * an already allocated file and thus do not have any metadata to
222 if ((mp->m_flags & XFS_MOUNT_BARRIER) &&
223 mp->m_logdev_targp == mp->m_ddev_targp &&
224 !XFS_IS_REALTIME_INODE(ip) &&
226 xfs_blkdev_issue_flush(mp->m_ddev_targp);
234 const struct iovec *iovp,
235 unsigned long nr_segs,
238 struct file *file = iocb->ki_filp;
239 struct inode *inode = file->f_mapping->host;
240 struct xfs_inode *ip = XFS_I(inode);
241 struct xfs_mount *mp = ip->i_mount;
247 XFS_STATS_INC(xs_read_calls);
249 BUG_ON(iocb->ki_pos != pos);
251 if (unlikely(file->f_flags & O_DIRECT))
252 ioflags |= IO_ISDIRECT;
253 if (file->f_mode & FMODE_NOCMTIME)
256 ret = generic_segment_checks(iovp, &nr_segs, &size, VERIFY_WRITE);
260 if (unlikely(ioflags & IO_ISDIRECT)) {
261 xfs_buftarg_t *target =
262 XFS_IS_REALTIME_INODE(ip) ?
263 mp->m_rtdev_targp : mp->m_ddev_targp;
264 /* DIO must be aligned to device logical sector size */
265 if ((pos | size) & target->bt_logical_sectormask) {
266 if (pos == i_size_read(inode))
268 return -XFS_ERROR(EINVAL);
272 n = mp->m_super->s_maxbytes - pos;
273 if (n <= 0 || size == 0)
279 if (XFS_FORCED_SHUTDOWN(mp))
283 * Locking is a bit tricky here. If we take an exclusive lock
284 * for direct IO, we effectively serialise all new concurrent
285 * read IO to this file and block it behind IO that is currently in
286 * progress because IO in progress holds the IO lock shared. We only
287 * need to hold the lock exclusive to blow away the page cache, so
288 * only take lock exclusively if the page cache needs invalidation.
289 * This allows the normal direct IO case of no page cache pages to
290 * proceeed concurrently without serialisation.
292 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
293 if ((ioflags & IO_ISDIRECT) && inode->i_mapping->nrpages) {
294 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
295 xfs_rw_ilock(ip, XFS_IOLOCK_EXCL);
297 if (inode->i_mapping->nrpages) {
298 ret = -filemap_write_and_wait_range(
299 VFS_I(ip)->i_mapping,
302 xfs_rw_iunlock(ip, XFS_IOLOCK_EXCL);
307 * Invalidate whole pages. This can return an error if
308 * we fail to invalidate a page, but this should never
309 * happen on XFS. Warn if it does fail.
311 ret = invalidate_inode_pages2_range(VFS_I(ip)->i_mapping,
312 pos >> PAGE_CACHE_SHIFT, -1);
316 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
319 trace_xfs_file_read(ip, size, pos, ioflags);
321 ret = generic_file_aio_read(iocb, iovp, nr_segs, pos);
323 XFS_STATS_ADD(xs_read_bytes, ret);
325 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
330 xfs_file_splice_read(
333 struct pipe_inode_info *pipe,
337 struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
341 XFS_STATS_INC(xs_read_calls);
343 if (infilp->f_mode & FMODE_NOCMTIME)
346 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
349 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
351 trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
353 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
355 XFS_STATS_ADD(xs_read_bytes, ret);
357 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
362 * xfs_file_splice_write() does not use xfs_rw_ilock() because
363 * generic_file_splice_write() takes the i_mutex itself. This, in theory,
364 * couuld cause lock inversions between the aio_write path and the splice path
365 * if someone is doing concurrent splice(2) based writes and write(2) based
366 * writes to the same inode. The only real way to fix this is to re-implement
367 * the generic code here with correct locking orders.
370 xfs_file_splice_write(
371 struct pipe_inode_info *pipe,
372 struct file *outfilp,
377 struct inode *inode = outfilp->f_mapping->host;
378 struct xfs_inode *ip = XFS_I(inode);
382 XFS_STATS_INC(xs_write_calls);
384 if (outfilp->f_mode & FMODE_NOCMTIME)
387 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
390 xfs_ilock(ip, XFS_IOLOCK_EXCL);
392 trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
394 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
396 XFS_STATS_ADD(xs_write_bytes, ret);
398 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
403 * This routine is called to handle zeroing any space in the last block of the
404 * file that is beyond the EOF. We do this since the size is being increased
405 * without writing anything to that block and we don't want to read the
406 * garbage on the disk.
408 STATIC int /* error (positive) */
410 struct xfs_inode *ip,
414 struct xfs_mount *mp = ip->i_mount;
415 xfs_fileoff_t last_fsb = XFS_B_TO_FSBT(mp, isize);
416 int zero_offset = XFS_B_FSB_OFFSET(mp, isize);
420 struct xfs_bmbt_irec imap;
422 xfs_ilock(ip, XFS_ILOCK_EXCL);
423 error = xfs_bmapi_read(ip, last_fsb, 1, &imap, &nimaps, 0);
424 xfs_iunlock(ip, XFS_ILOCK_EXCL);
431 * If the block underlying isize is just a hole, then there
432 * is nothing to zero.
434 if (imap.br_startblock == HOLESTARTBLOCK)
437 zero_len = mp->m_sb.sb_blocksize - zero_offset;
438 if (isize + zero_len > offset)
439 zero_len = offset - isize;
440 return xfs_iozero(ip, isize, zero_len);
444 * Zero any on disk space between the current EOF and the new, larger EOF.
446 * This handles the normal case of zeroing the remainder of the last block in
447 * the file and the unusual case of zeroing blocks out beyond the size of the
448 * file. This second case only happens with fixed size extents and when the
449 * system crashes before the inode size was updated but after blocks were
452 * Expects the iolock to be held exclusive, and will take the ilock internally.
454 int /* error (positive) */
456 struct xfs_inode *ip,
457 xfs_off_t offset, /* starting I/O offset */
458 xfs_fsize_t isize) /* current inode size */
460 struct xfs_mount *mp = ip->i_mount;
461 xfs_fileoff_t start_zero_fsb;
462 xfs_fileoff_t end_zero_fsb;
463 xfs_fileoff_t zero_count_fsb;
464 xfs_fileoff_t last_fsb;
465 xfs_fileoff_t zero_off;
466 xfs_fsize_t zero_len;
469 struct xfs_bmbt_irec imap;
471 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
472 ASSERT(offset > isize);
475 * First handle zeroing the block on which isize resides.
477 * We only zero a part of that block so it is handled specially.
479 if (XFS_B_FSB_OFFSET(mp, isize) != 0) {
480 error = xfs_zero_last_block(ip, offset, isize);
486 * Calculate the range between the new size and the old where blocks
487 * needing to be zeroed may exist.
489 * To get the block where the last byte in the file currently resides,
490 * we need to subtract one from the size and truncate back to a block
491 * boundary. We subtract 1 in case the size is exactly on a block
494 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
495 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
496 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
497 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
498 if (last_fsb == end_zero_fsb) {
500 * The size was only incremented on its last block.
501 * We took care of that above, so just return.
506 ASSERT(start_zero_fsb <= end_zero_fsb);
507 while (start_zero_fsb <= end_zero_fsb) {
509 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
511 xfs_ilock(ip, XFS_ILOCK_EXCL);
512 error = xfs_bmapi_read(ip, start_zero_fsb, zero_count_fsb,
514 xfs_iunlock(ip, XFS_ILOCK_EXCL);
520 if (imap.br_state == XFS_EXT_UNWRITTEN ||
521 imap.br_startblock == HOLESTARTBLOCK) {
522 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
523 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
528 * There are blocks we need to zero.
530 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
531 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
533 if ((zero_off + zero_len) > offset)
534 zero_len = offset - zero_off;
536 error = xfs_iozero(ip, zero_off, zero_len);
540 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
541 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
548 * Common pre-write limit and setup checks.
550 * Called with the iolocked held either shared and exclusive according to
551 * @iolock, and returns with it held. Might upgrade the iolock to exclusive
552 * if called for a direct write beyond i_size.
555 xfs_file_aio_write_checks(
561 struct inode *inode = file->f_mapping->host;
562 struct xfs_inode *ip = XFS_I(inode);
566 error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode));
571 * If the offset is beyond the size of the file, we need to zero any
572 * blocks that fall between the existing EOF and the start of this
573 * write. If zeroing is needed and we are currently holding the
574 * iolock shared, we need to update it to exclusive which implies
575 * having to redo all checks before.
577 if (*pos > i_size_read(inode)) {
578 if (*iolock == XFS_IOLOCK_SHARED) {
579 xfs_rw_iunlock(ip, *iolock);
580 *iolock = XFS_IOLOCK_EXCL;
581 xfs_rw_ilock(ip, *iolock);
584 error = -xfs_zero_eof(ip, *pos, i_size_read(inode));
590 * Updating the timestamps will grab the ilock again from
591 * xfs_fs_dirty_inode, so we have to call it after dropping the
592 * lock above. Eventually we should look into a way to avoid
593 * the pointless lock roundtrip.
595 if (likely(!(file->f_mode & FMODE_NOCMTIME))) {
596 error = file_update_time(file);
602 * If we're writing the file then make sure to clear the setuid and
603 * setgid bits if the process is not being run by root. This keeps
604 * people from modifying setuid and setgid binaries.
606 return file_remove_suid(file);
610 * xfs_file_dio_aio_write - handle direct IO writes
612 * Lock the inode appropriately to prepare for and issue a direct IO write.
613 * By separating it from the buffered write path we remove all the tricky to
614 * follow locking changes and looping.
616 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
617 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
618 * pages are flushed out.
620 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
621 * allowing them to be done in parallel with reads and other direct IO writes.
622 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
623 * needs to do sub-block zeroing and that requires serialisation against other
624 * direct IOs to the same block. In this case we need to serialise the
625 * submission of the unaligned IOs so that we don't get racing block zeroing in
626 * the dio layer. To avoid the problem with aio, we also need to wait for
627 * outstanding IOs to complete so that unwritten extent conversion is completed
628 * before we try to map the overlapping block. This is currently implemented by
629 * hitting it with a big hammer (i.e. inode_dio_wait()).
631 * Returns with locks held indicated by @iolock and errors indicated by
632 * negative return values.
635 xfs_file_dio_aio_write(
637 const struct iovec *iovp,
638 unsigned long nr_segs,
642 struct file *file = iocb->ki_filp;
643 struct address_space *mapping = file->f_mapping;
644 struct inode *inode = mapping->host;
645 struct xfs_inode *ip = XFS_I(inode);
646 struct xfs_mount *mp = ip->i_mount;
648 size_t count = ocount;
649 int unaligned_io = 0;
651 struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
652 mp->m_rtdev_targp : mp->m_ddev_targp;
654 /* DIO must be aligned to device logical sector size */
655 if ((pos | count) & target->bt_logical_sectormask)
656 return -XFS_ERROR(EINVAL);
658 /* "unaligned" here means not aligned to a filesystem block */
659 if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
663 * We don't need to take an exclusive lock unless there page cache needs
664 * to be invalidated or unaligned IO is being executed. We don't need to
665 * consider the EOF extension case here because
666 * xfs_file_aio_write_checks() will relock the inode as necessary for
667 * EOF zeroing cases and fill out the new inode size as appropriate.
669 if (unaligned_io || mapping->nrpages)
670 iolock = XFS_IOLOCK_EXCL;
672 iolock = XFS_IOLOCK_SHARED;
673 xfs_rw_ilock(ip, iolock);
676 * Recheck if there are cached pages that need invalidate after we got
677 * the iolock to protect against other threads adding new pages while
678 * we were waiting for the iolock.
680 if (mapping->nrpages && iolock == XFS_IOLOCK_SHARED) {
681 xfs_rw_iunlock(ip, iolock);
682 iolock = XFS_IOLOCK_EXCL;
683 xfs_rw_ilock(ip, iolock);
686 ret = xfs_file_aio_write_checks(file, &pos, &count, &iolock);
690 if (mapping->nrpages) {
691 ret = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
696 * Invalidate whole pages. This can return an error if
697 * we fail to invalidate a page, but this should never
698 * happen on XFS. Warn if it does fail.
700 ret = invalidate_inode_pages2_range(VFS_I(ip)->i_mapping,
701 pos >> PAGE_CACHE_SHIFT, -1);
707 * If we are doing unaligned IO, wait for all other IO to drain,
708 * otherwise demote the lock if we had to flush cached pages
711 inode_dio_wait(inode);
712 else if (iolock == XFS_IOLOCK_EXCL) {
713 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
714 iolock = XFS_IOLOCK_SHARED;
717 trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
718 ret = generic_file_direct_write(iocb, iovp,
719 &nr_segs, pos, &iocb->ki_pos, count, ocount);
722 xfs_rw_iunlock(ip, iolock);
724 /* No fallback to buffered IO on errors for XFS. */
725 ASSERT(ret < 0 || ret == count);
730 xfs_file_buffered_aio_write(
732 const struct iovec *iovp,
733 unsigned long nr_segs,
737 struct file *file = iocb->ki_filp;
738 struct address_space *mapping = file->f_mapping;
739 struct inode *inode = mapping->host;
740 struct xfs_inode *ip = XFS_I(inode);
743 int iolock = XFS_IOLOCK_EXCL;
744 size_t count = ocount;
746 xfs_rw_ilock(ip, iolock);
748 ret = xfs_file_aio_write_checks(file, &pos, &count, &iolock);
752 /* We can write back this queue in page reclaim */
753 current->backing_dev_info = mapping->backing_dev_info;
756 trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
757 ret = generic_file_buffered_write(iocb, iovp, nr_segs,
758 pos, &iocb->ki_pos, count, 0);
761 * If we just got an ENOSPC, try to write back all dirty inodes to
762 * convert delalloc space to free up some of the excess reserved
765 if (ret == -ENOSPC && !enospc) {
767 xfs_flush_inodes(ip->i_mount);
771 current->backing_dev_info = NULL;
773 xfs_rw_iunlock(ip, iolock);
780 const struct iovec *iovp,
781 unsigned long nr_segs,
784 struct file *file = iocb->ki_filp;
785 struct address_space *mapping = file->f_mapping;
786 struct inode *inode = mapping->host;
787 struct xfs_inode *ip = XFS_I(inode);
791 XFS_STATS_INC(xs_write_calls);
793 BUG_ON(iocb->ki_pos != pos);
795 ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
802 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
807 if (unlikely(file->f_flags & O_DIRECT))
808 ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos, ocount);
810 ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
816 XFS_STATS_ADD(xs_write_bytes, ret);
818 /* Handle various SYNC-type writes */
819 err = generic_write_sync(file, iocb->ki_pos - ret, ret);
835 struct inode *inode = file_inode(file);
836 struct xfs_inode *ip = XFS_I(inode);
837 struct xfs_trans *tp;
841 if (!S_ISREG(inode->i_mode))
843 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
846 xfs_ilock(ip, XFS_IOLOCK_EXCL);
847 if (mode & FALLOC_FL_PUNCH_HOLE) {
848 error = xfs_free_file_space(ip, offset, len);
852 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
853 offset + len > i_size_read(inode)) {
854 new_size = offset + len;
855 error = -inode_newsize_ok(inode, new_size);
860 error = xfs_alloc_file_space(ip, offset, len,
866 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_WRITEID);
867 error = xfs_trans_reserve(tp, &M_RES(ip->i_mount)->tr_writeid, 0, 0);
869 xfs_trans_cancel(tp, 0);
873 xfs_ilock(ip, XFS_ILOCK_EXCL);
874 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
875 ip->i_d.di_mode &= ~S_ISUID;
876 if (ip->i_d.di_mode & S_IXGRP)
877 ip->i_d.di_mode &= ~S_ISGID;
879 if (!(mode & FALLOC_FL_PUNCH_HOLE))
880 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
882 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
883 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
885 if (file->f_flags & O_DSYNC)
886 xfs_trans_set_sync(tp);
887 error = xfs_trans_commit(tp, 0);
891 /* Change file size if needed */
895 iattr.ia_valid = ATTR_SIZE;
896 iattr.ia_size = new_size;
897 error = xfs_setattr_size(ip, &iattr);
901 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
911 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
913 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
923 struct xfs_inode *ip = XFS_I(inode);
927 error = xfs_file_open(inode, file);
932 * If there are any blocks, read-ahead block 0 as we're almost
933 * certain to have the next operation be a read there.
935 mode = xfs_ilock_data_map_shared(ip);
936 if (ip->i_d.di_nextents > 0)
937 xfs_dir3_data_readahead(NULL, ip, 0, -1);
938 xfs_iunlock(ip, mode);
947 return -xfs_release(XFS_I(inode));
953 struct dir_context *ctx)
955 struct inode *inode = file_inode(file);
956 xfs_inode_t *ip = XFS_I(inode);
961 * The Linux API doesn't pass down the total size of the buffer
962 * we read into down to the filesystem. With the filldir concept
963 * it's not needed for correct information, but the XFS dir2 leaf
964 * code wants an estimate of the buffer size to calculate it's
965 * readahead window and size the buffers used for mapping to
968 * Try to give it an estimate that's good enough, maybe at some
969 * point we can change the ->readdir prototype to include the
970 * buffer size. For now we use the current glibc buffer size.
972 bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
974 error = xfs_readdir(ip, ctx, bufsize);
983 struct vm_area_struct *vma)
985 vma->vm_ops = &xfs_file_vm_ops;
992 * mmap()d file has taken write protection fault and is being made
993 * writable. We can set the page state up correctly for a writable
994 * page, which means we can do correct delalloc accounting (ENOSPC
995 * checking!) and unwritten extent mapping.
999 struct vm_area_struct *vma,
1000 struct vm_fault *vmf)
1002 return block_page_mkwrite(vma, vmf, xfs_get_blocks);
1006 * This type is designed to indicate the type of offset we would like
1007 * to search from page cache for either xfs_seek_data() or xfs_seek_hole().
1015 * Lookup the desired type of offset from the given page.
1017 * On success, return true and the offset argument will point to the
1018 * start of the region that was found. Otherwise this function will
1019 * return false and keep the offset argument unchanged.
1022 xfs_lookup_buffer_offset(
1027 loff_t lastoff = page_offset(page);
1029 struct buffer_head *bh, *head;
1031 bh = head = page_buffers(page);
1034 * Unwritten extents that have data in the page
1035 * cache covering them can be identified by the
1036 * BH_Unwritten state flag. Pages with multiple
1037 * buffers might have a mix of holes, data and
1038 * unwritten extents - any buffer with valid
1039 * data in it should have BH_Uptodate flag set
1042 if (buffer_unwritten(bh) ||
1043 buffer_uptodate(bh)) {
1044 if (type == DATA_OFF)
1047 if (type == HOLE_OFF)
1055 lastoff += bh->b_size;
1056 } while ((bh = bh->b_this_page) != head);
1062 * This routine is called to find out and return a data or hole offset
1063 * from the page cache for unwritten extents according to the desired
1064 * type for xfs_seek_data() or xfs_seek_hole().
1066 * The argument offset is used to tell where we start to search from the
1067 * page cache. Map is used to figure out the end points of the range to
1070 * Return true if the desired type of offset was found, and the argument
1071 * offset is filled with that address. Otherwise, return false and keep
1075 xfs_find_get_desired_pgoff(
1076 struct inode *inode,
1077 struct xfs_bmbt_irec *map,
1081 struct xfs_inode *ip = XFS_I(inode);
1082 struct xfs_mount *mp = ip->i_mount;
1083 struct pagevec pvec;
1087 loff_t startoff = *offset;
1088 loff_t lastoff = startoff;
1091 pagevec_init(&pvec, 0);
1093 index = startoff >> PAGE_CACHE_SHIFT;
1094 endoff = XFS_FSB_TO_B(mp, map->br_startoff + map->br_blockcount);
1095 end = endoff >> PAGE_CACHE_SHIFT;
1101 want = min_t(pgoff_t, end - index, PAGEVEC_SIZE);
1102 nr_pages = pagevec_lookup(&pvec, inode->i_mapping, index,
1105 * No page mapped into given range. If we are searching holes
1106 * and if this is the first time we got into the loop, it means
1107 * that the given offset is landed in a hole, return it.
1109 * If we have already stepped through some block buffers to find
1110 * holes but they all contains data. In this case, the last
1111 * offset is already updated and pointed to the end of the last
1112 * mapped page, if it does not reach the endpoint to search,
1113 * that means there should be a hole between them.
1115 if (nr_pages == 0) {
1116 /* Data search found nothing */
1117 if (type == DATA_OFF)
1120 ASSERT(type == HOLE_OFF);
1121 if (lastoff == startoff || lastoff < endoff) {
1129 * At lease we found one page. If this is the first time we
1130 * step into the loop, and if the first page index offset is
1131 * greater than the given search offset, a hole was found.
1133 if (type == HOLE_OFF && lastoff == startoff &&
1134 lastoff < page_offset(pvec.pages[0])) {
1139 for (i = 0; i < nr_pages; i++) {
1140 struct page *page = pvec.pages[i];
1144 * At this point, the page may be truncated or
1145 * invalidated (changing page->mapping to NULL),
1146 * or even swizzled back from swapper_space to tmpfs
1147 * file mapping. However, page->index will not change
1148 * because we have a reference on the page.
1150 * Searching done if the page index is out of range.
1151 * If the current offset is not reaches the end of
1152 * the specified search range, there should be a hole
1155 if (page->index > end) {
1156 if (type == HOLE_OFF && lastoff < endoff) {
1165 * Page truncated or invalidated(page->mapping == NULL).
1166 * We can freely skip it and proceed to check the next
1169 if (unlikely(page->mapping != inode->i_mapping)) {
1174 if (!page_has_buffers(page)) {
1179 found = xfs_lookup_buffer_offset(page, &b_offset, type);
1182 * The found offset may be less than the start
1183 * point to search if this is the first time to
1186 *offset = max_t(loff_t, startoff, b_offset);
1192 * We either searching data but nothing was found, or
1193 * searching hole but found a data buffer. In either
1194 * case, probably the next page contains the desired
1195 * things, update the last offset to it so.
1197 lastoff = page_offset(page) + PAGE_SIZE;
1202 * The number of returned pages less than our desired, search
1203 * done. In this case, nothing was found for searching data,
1204 * but we found a hole behind the last offset.
1206 if (nr_pages < want) {
1207 if (type == HOLE_OFF) {
1214 index = pvec.pages[i - 1]->index + 1;
1215 pagevec_release(&pvec);
1216 } while (index <= end);
1219 pagevec_release(&pvec);
1228 struct inode *inode = file->f_mapping->host;
1229 struct xfs_inode *ip = XFS_I(inode);
1230 struct xfs_mount *mp = ip->i_mount;
1231 loff_t uninitialized_var(offset);
1233 xfs_fileoff_t fsbno;
1238 lock = xfs_ilock_data_map_shared(ip);
1240 isize = i_size_read(inode);
1241 if (start >= isize) {
1247 * Try to read extents from the first block indicated
1248 * by fsbno to the end block of the file.
1250 fsbno = XFS_B_TO_FSBT(mp, start);
1251 end = XFS_B_TO_FSB(mp, isize);
1253 struct xfs_bmbt_irec map[2];
1257 error = xfs_bmapi_read(ip, fsbno, end - fsbno, map, &nmap,
1262 /* No extents at given offset, must be beyond EOF */
1268 for (i = 0; i < nmap; i++) {
1269 offset = max_t(loff_t, start,
1270 XFS_FSB_TO_B(mp, map[i].br_startoff));
1272 /* Landed in a data extent */
1273 if (map[i].br_startblock == DELAYSTARTBLOCK ||
1274 (map[i].br_state == XFS_EXT_NORM &&
1275 !isnullstartblock(map[i].br_startblock)))
1279 * Landed in an unwritten extent, try to search data
1282 if (map[i].br_state == XFS_EXT_UNWRITTEN) {
1283 if (xfs_find_get_desired_pgoff(inode, &map[i],
1290 * map[0] is hole or its an unwritten extent but
1291 * without data in page cache. Probably means that
1292 * we are reading after EOF if nothing in map[1].
1302 * Nothing was found, proceed to the next round of search
1303 * if reading offset not beyond or hit EOF.
1305 fsbno = map[i - 1].br_startoff + map[i - 1].br_blockcount;
1306 start = XFS_FSB_TO_B(mp, fsbno);
1307 if (start >= isize) {
1314 offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
1317 xfs_iunlock(ip, lock);
1329 struct inode *inode = file->f_mapping->host;
1330 struct xfs_inode *ip = XFS_I(inode);
1331 struct xfs_mount *mp = ip->i_mount;
1332 loff_t uninitialized_var(offset);
1334 xfs_fileoff_t fsbno;
1339 if (XFS_FORCED_SHUTDOWN(mp))
1340 return -XFS_ERROR(EIO);
1342 lock = xfs_ilock_data_map_shared(ip);
1344 isize = i_size_read(inode);
1345 if (start >= isize) {
1350 fsbno = XFS_B_TO_FSBT(mp, start);
1351 end = XFS_B_TO_FSB(mp, isize);
1354 struct xfs_bmbt_irec map[2];
1358 error = xfs_bmapi_read(ip, fsbno, end - fsbno, map, &nmap,
1363 /* No extents at given offset, must be beyond EOF */
1369 for (i = 0; i < nmap; i++) {
1370 offset = max_t(loff_t, start,
1371 XFS_FSB_TO_B(mp, map[i].br_startoff));
1373 /* Landed in a hole */
1374 if (map[i].br_startblock == HOLESTARTBLOCK)
1378 * Landed in an unwritten extent, try to search hole
1381 if (map[i].br_state == XFS_EXT_UNWRITTEN) {
1382 if (xfs_find_get_desired_pgoff(inode, &map[i],
1389 * map[0] contains data or its unwritten but contains
1390 * data in page cache, probably means that we are
1391 * reading after EOF. We should fix offset to point
1392 * to the end of the file(i.e., there is an implicit
1393 * hole at the end of any file).
1403 * Both mappings contains data, proceed to the next round of
1404 * search if the current reading offset not beyond or hit EOF.
1406 fsbno = map[i - 1].br_startoff + map[i - 1].br_blockcount;
1407 start = XFS_FSB_TO_B(mp, fsbno);
1408 if (start >= isize) {
1416 * At this point, we must have found a hole. However, the returned
1417 * offset may be bigger than the file size as it may be aligned to
1418 * page boundary for unwritten extents, we need to deal with this
1419 * situation in particular.
1421 offset = min_t(loff_t, offset, isize);
1422 offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
1425 xfs_iunlock(ip, lock);
1442 return generic_file_llseek(file, offset, origin);
1444 return xfs_seek_data(file, offset);
1446 return xfs_seek_hole(file, offset);
1452 const struct file_operations xfs_file_operations = {
1453 .llseek = xfs_file_llseek,
1454 .read = do_sync_read,
1455 .write = do_sync_write,
1456 .aio_read = xfs_file_aio_read,
1457 .aio_write = xfs_file_aio_write,
1458 .splice_read = xfs_file_splice_read,
1459 .splice_write = xfs_file_splice_write,
1460 .unlocked_ioctl = xfs_file_ioctl,
1461 #ifdef CONFIG_COMPAT
1462 .compat_ioctl = xfs_file_compat_ioctl,
1464 .mmap = xfs_file_mmap,
1465 .open = xfs_file_open,
1466 .release = xfs_file_release,
1467 .fsync = xfs_file_fsync,
1468 .fallocate = xfs_file_fallocate,
1471 const struct file_operations xfs_dir_file_operations = {
1472 .open = xfs_dir_open,
1473 .read = generic_read_dir,
1474 .iterate = xfs_file_readdir,
1475 .llseek = generic_file_llseek,
1476 .unlocked_ioctl = xfs_file_ioctl,
1477 #ifdef CONFIG_COMPAT
1478 .compat_ioctl = xfs_file_compat_ioctl,
1480 .fsync = xfs_dir_fsync,
1483 static const struct vm_operations_struct xfs_file_vm_ops = {
1484 .fault = filemap_fault,
1485 .page_mkwrite = xfs_vm_page_mkwrite,
1486 .remap_pages = generic_file_remap_pages,