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>
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! */
131 struct inode *inode = file->f_mapping->host;
132 struct xfs_inode *ip = XFS_I(inode);
133 struct xfs_trans *tp;
137 trace_xfs_file_fsync(ip);
139 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
140 return -XFS_ERROR(EIO);
142 xfs_iflags_clear(ip, XFS_ITRUNCATED);
147 * We always need to make sure that the required inode state is safe on
148 * disk. The inode might be clean but we still might need to force the
149 * log because of committed transactions that haven't hit the disk yet.
150 * Likewise, there could be unflushed non-transactional changes to the
151 * inode core that have to go to disk and this requires us to issue
152 * a synchronous transaction to capture these changes correctly.
154 * This code relies on the assumption that if the i_update_core field
155 * of the inode is clear and the inode is unpinned then it is clean
156 * and no action is required.
158 xfs_ilock(ip, XFS_ILOCK_SHARED);
161 * First check if the VFS inode is marked dirty. All the dirtying
162 * of non-transactional updates no goes through mark_inode_dirty*,
163 * which allows us to distinguish beteeen pure timestamp updates
164 * and i_size updates which need to be caught for fdatasync.
165 * After that also theck for the dirty state in the XFS inode, which
166 * might gets cleared when the inode gets written out via the AIL
167 * or xfs_iflush_cluster.
169 if (((inode->i_state & I_DIRTY_DATASYNC) ||
170 ((inode->i_state & I_DIRTY_SYNC) && !datasync)) &&
173 * Kick off a transaction to log the inode core to get the
174 * updates. The sync transaction will also force the log.
176 xfs_iunlock(ip, XFS_ILOCK_SHARED);
177 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
178 error = xfs_trans_reserve(tp, 0,
179 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
181 xfs_trans_cancel(tp, 0);
184 xfs_ilock(ip, XFS_ILOCK_EXCL);
187 * Note - it's possible that we might have pushed ourselves out
188 * of the way during trans_reserve which would flush the inode.
189 * But there's no guarantee that the inode buffer has actually
190 * gone out yet (it's delwri). Plus the buffer could be pinned
191 * anyway if it's part of an inode in another recent
192 * transaction. So we play it safe and fire off the
193 * transaction anyway.
195 xfs_trans_ijoin(tp, ip);
196 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
197 xfs_trans_set_sync(tp);
198 error = _xfs_trans_commit(tp, 0, &log_flushed);
200 xfs_iunlock(ip, XFS_ILOCK_EXCL);
203 * Timestamps/size haven't changed since last inode flush or
204 * inode transaction commit. That means either nothing got
205 * written or a transaction committed which caught the updates.
206 * If the latter happened and the transaction hasn't hit the
207 * disk yet, the inode will be still be pinned. If it is,
210 if (xfs_ipincount(ip)) {
211 error = _xfs_log_force_lsn(ip->i_mount,
212 ip->i_itemp->ili_last_lsn,
213 XFS_LOG_SYNC, &log_flushed);
215 xfs_iunlock(ip, XFS_ILOCK_SHARED);
218 if (ip->i_mount->m_flags & XFS_MOUNT_BARRIER) {
220 * If the log write didn't issue an ordered tag we need
221 * to flush the disk cache for the data device now.
224 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
227 * If this inode is on the RT dev we need to flush that
230 if (XFS_IS_REALTIME_INODE(ip))
231 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
240 const struct iovec *iovp,
241 unsigned long nr_segs,
244 struct file *file = iocb->ki_filp;
245 struct inode *inode = file->f_mapping->host;
246 struct xfs_inode *ip = XFS_I(inode);
247 struct xfs_mount *mp = ip->i_mount;
254 XFS_STATS_INC(xs_read_calls);
256 BUG_ON(iocb->ki_pos != pos);
258 if (unlikely(file->f_flags & O_DIRECT))
259 ioflags |= IO_ISDIRECT;
260 if (file->f_mode & FMODE_NOCMTIME)
263 /* START copy & waste from filemap.c */
264 for (seg = 0; seg < nr_segs; seg++) {
265 const struct iovec *iv = &iovp[seg];
268 * If any segment has a negative length, or the cumulative
269 * length ever wraps negative then return -EINVAL.
272 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
273 return XFS_ERROR(-EINVAL);
275 /* END copy & waste from filemap.c */
277 if (unlikely(ioflags & IO_ISDIRECT)) {
278 xfs_buftarg_t *target =
279 XFS_IS_REALTIME_INODE(ip) ?
280 mp->m_rtdev_targp : mp->m_ddev_targp;
281 if ((iocb->ki_pos & target->bt_smask) ||
282 (size & target->bt_smask)) {
283 if (iocb->ki_pos == ip->i_size)
285 return -XFS_ERROR(EINVAL);
289 n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
290 if (n <= 0 || size == 0)
296 if (XFS_FORCED_SHUTDOWN(mp))
299 if (unlikely(ioflags & IO_ISDIRECT)) {
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);
313 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
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_aio_write_isize_update(
361 ssize_t bytes_written)
363 struct xfs_inode *ip = XFS_I(inode);
364 xfs_fsize_t isize = i_size_read(inode);
366 if (bytes_written > 0)
367 XFS_STATS_ADD(xs_write_bytes, bytes_written);
369 if (unlikely(bytes_written < 0 && bytes_written != -EFAULT &&
373 if (*ppos > ip->i_size) {
374 xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
375 if (*ppos > ip->i_size)
377 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
382 * If this was a direct or synchronous I/O that failed (such as ENOSPC) then
383 * part of the I/O may have been written to disk before the error occured. In
384 * this case the on-disk file size may have been adjusted beyond the in-memory
385 * file size and now needs to be truncated back.
388 xfs_aio_write_newsize_update(
389 struct xfs_inode *ip)
391 if (ip->i_new_size) {
392 xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
394 if (ip->i_d.di_size > ip->i_size)
395 ip->i_d.di_size = ip->i_size;
396 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
401 * xfs_file_splice_write() does not use xfs_rw_ilock() because
402 * generic_file_splice_write() takes the i_mutex itself. This, in theory,
403 * couuld cause lock inversions between the aio_write path and the splice path
404 * if someone is doing concurrent splice(2) based writes and write(2) based
405 * writes to the same inode. The only real way to fix this is to re-implement
406 * the generic code here with correct locking orders.
409 xfs_file_splice_write(
410 struct pipe_inode_info *pipe,
411 struct file *outfilp,
416 struct inode *inode = outfilp->f_mapping->host;
417 struct xfs_inode *ip = XFS_I(inode);
418 xfs_fsize_t new_size;
422 XFS_STATS_INC(xs_write_calls);
424 if (outfilp->f_mode & FMODE_NOCMTIME)
427 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
430 xfs_ilock(ip, XFS_IOLOCK_EXCL);
432 new_size = *ppos + count;
434 xfs_ilock(ip, XFS_ILOCK_EXCL);
435 if (new_size > ip->i_size)
436 ip->i_new_size = new_size;
437 xfs_iunlock(ip, XFS_ILOCK_EXCL);
439 trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
441 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
443 xfs_aio_write_isize_update(inode, ppos, ret);
444 xfs_aio_write_newsize_update(ip);
445 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
450 * This routine is called to handle zeroing any space in the last
451 * block of the file that is beyond the EOF. We do this since the
452 * size is being increased without writing anything to that block
453 * and we don't want anyone to read the garbage on the disk.
455 STATIC int /* error (positive) */
461 xfs_fileoff_t last_fsb;
462 xfs_mount_t *mp = ip->i_mount;
467 xfs_bmbt_irec_t imap;
469 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
471 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
472 if (zero_offset == 0) {
474 * There are no extra bytes in the last block on disk to
480 last_fsb = XFS_B_TO_FSBT(mp, isize);
482 error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
489 * If the block underlying isize is just a hole, then there
490 * is nothing to zero.
492 if (imap.br_startblock == HOLESTARTBLOCK) {
496 * Zero the part of the last block beyond the EOF, and write it
497 * out sync. We need to drop the ilock while we do this so we
498 * don't deadlock when the buffer cache calls back to us.
500 xfs_iunlock(ip, XFS_ILOCK_EXCL);
502 zero_len = mp->m_sb.sb_blocksize - zero_offset;
503 if (isize + zero_len > offset)
504 zero_len = offset - isize;
505 error = xfs_iozero(ip, isize, zero_len);
507 xfs_ilock(ip, XFS_ILOCK_EXCL);
513 * Zero any on disk space between the current EOF and the new,
514 * larger EOF. This handles the normal case of zeroing the remainder
515 * of the last block in the file and the unusual case of zeroing blocks
516 * out beyond the size of the file. This second case only happens
517 * with fixed size extents and when the system crashes before the inode
518 * size was updated but after blocks were allocated. If fill is set,
519 * then any holes in the range are filled and zeroed. If not, the holes
520 * are left alone as holes.
523 int /* error (positive) */
526 xfs_off_t offset, /* starting I/O offset */
527 xfs_fsize_t isize) /* current inode size */
529 xfs_mount_t *mp = ip->i_mount;
530 xfs_fileoff_t start_zero_fsb;
531 xfs_fileoff_t end_zero_fsb;
532 xfs_fileoff_t zero_count_fsb;
533 xfs_fileoff_t last_fsb;
534 xfs_fileoff_t zero_off;
535 xfs_fsize_t zero_len;
538 xfs_bmbt_irec_t imap;
540 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
541 ASSERT(offset > isize);
544 * First handle zeroing the block on which isize resides.
545 * We only zero a part of that block so it is handled specially.
547 error = xfs_zero_last_block(ip, offset, isize);
549 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
554 * Calculate the range between the new size and the old
555 * where blocks needing to be zeroed may exist. To get the
556 * block where the last byte in the file currently resides,
557 * we need to subtract one from the size and truncate back
558 * to a block boundary. We subtract 1 in case the size is
559 * exactly on a block boundary.
561 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
562 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
563 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
564 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
565 if (last_fsb == end_zero_fsb) {
567 * The size was only incremented on its last block.
568 * We took care of that above, so just return.
573 ASSERT(start_zero_fsb <= end_zero_fsb);
574 while (start_zero_fsb <= end_zero_fsb) {
576 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
577 error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
578 0, NULL, 0, &imap, &nimaps, NULL);
580 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
585 if (imap.br_state == XFS_EXT_UNWRITTEN ||
586 imap.br_startblock == HOLESTARTBLOCK) {
588 * This loop handles initializing pages that were
589 * partially initialized by the code below this
590 * loop. It basically zeroes the part of the page
591 * that sits on a hole and sets the page as P_HOLE
592 * and calls remapf if it is a mapped file.
594 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
595 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
600 * There are blocks we need to zero.
601 * Drop the inode lock while we're doing the I/O.
602 * We'll still have the iolock to protect us.
604 xfs_iunlock(ip, XFS_ILOCK_EXCL);
606 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
607 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
609 if ((zero_off + zero_len) > offset)
610 zero_len = offset - zero_off;
612 error = xfs_iozero(ip, zero_off, zero_len);
617 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
618 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
620 xfs_ilock(ip, XFS_ILOCK_EXCL);
626 xfs_ilock(ip, XFS_ILOCK_EXCL);
632 * Common pre-write limit and setup checks.
634 * Returns with iolock held according to @iolock.
637 xfs_file_aio_write_checks(
643 struct inode *inode = file->f_mapping->host;
644 struct xfs_inode *ip = XFS_I(inode);
645 xfs_fsize_t new_size;
648 error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode));
650 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL | *iolock);
655 new_size = *pos + *count;
656 if (new_size > ip->i_size)
657 ip->i_new_size = new_size;
659 if (likely(!(file->f_mode & FMODE_NOCMTIME)))
660 file_update_time(file);
663 * If the offset is beyond the size of the file, we need to zero any
664 * blocks that fall between the existing EOF and the start of this
667 if (*pos > ip->i_size)
668 error = -xfs_zero_eof(ip, *pos, ip->i_size);
670 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
675 * If we're writing the file then make sure to clear the setuid and
676 * setgid bits if the process is not being run by root. This keeps
677 * people from modifying setuid and setgid binaries.
679 return file_remove_suid(file);
684 * xfs_file_dio_aio_write - handle direct IO writes
686 * Lock the inode appropriately to prepare for and issue a direct IO write.
687 * By separating it from the buffered write path we remove all the tricky to
688 * follow locking changes and looping.
690 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
691 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
692 * pages are flushed out.
694 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
695 * allowing them to be done in parallel with reads and other direct IO writes.
696 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
697 * needs to do sub-block zeroing and that requires serialisation against other
698 * direct IOs to the same block. In this case we need to serialise the
699 * submission of the unaligned IOs so that we don't get racing block zeroing in
700 * the dio layer. To avoid the problem with aio, we also need to wait for
701 * outstanding IOs to complete so that unwritten extent conversion is completed
702 * before we try to map the overlapping block. This is currently implemented by
703 * hitting it with a big hammer (i.e. xfs_ioend_wait()).
705 * Returns with locks held indicated by @iolock and errors indicated by
706 * negative return values.
709 xfs_file_dio_aio_write(
711 const struct iovec *iovp,
712 unsigned long nr_segs,
717 struct file *file = iocb->ki_filp;
718 struct address_space *mapping = file->f_mapping;
719 struct inode *inode = mapping->host;
720 struct xfs_inode *ip = XFS_I(inode);
721 struct xfs_mount *mp = ip->i_mount;
723 size_t count = ocount;
724 int unaligned_io = 0;
725 struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
726 mp->m_rtdev_targp : mp->m_ddev_targp;
729 if ((pos & target->bt_smask) || (count & target->bt_smask))
730 return -XFS_ERROR(EINVAL);
732 if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
735 if (unaligned_io || mapping->nrpages || pos > ip->i_size)
736 *iolock = XFS_IOLOCK_EXCL;
738 *iolock = XFS_IOLOCK_SHARED;
739 xfs_rw_ilock(ip, XFS_ILOCK_EXCL | *iolock);
741 ret = xfs_file_aio_write_checks(file, &pos, &count, iolock);
745 if (mapping->nrpages) {
746 WARN_ON(*iolock != XFS_IOLOCK_EXCL);
747 ret = -xfs_flushinval_pages(ip, (pos & PAGE_CACHE_MASK), -1,
754 * If we are doing unaligned IO, wait for all other IO to drain,
755 * otherwise demote the lock if we had to flush cached pages
759 else if (*iolock == XFS_IOLOCK_EXCL) {
760 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
761 *iolock = XFS_IOLOCK_SHARED;
764 trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
765 ret = generic_file_direct_write(iocb, iovp,
766 &nr_segs, pos, &iocb->ki_pos, count, ocount);
768 /* No fallback to buffered IO on errors for XFS. */
769 ASSERT(ret < 0 || ret == count);
774 xfs_file_buffered_aio_write(
776 const struct iovec *iovp,
777 unsigned long nr_segs,
782 struct file *file = iocb->ki_filp;
783 struct address_space *mapping = file->f_mapping;
784 struct inode *inode = mapping->host;
785 struct xfs_inode *ip = XFS_I(inode);
788 size_t count = ocount;
790 *iolock = XFS_IOLOCK_EXCL;
791 xfs_rw_ilock(ip, XFS_ILOCK_EXCL | *iolock);
793 ret = xfs_file_aio_write_checks(file, &pos, &count, iolock);
797 /* We can write back this queue in page reclaim */
798 current->backing_dev_info = mapping->backing_dev_info;
801 trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
802 ret = generic_file_buffered_write(iocb, iovp, nr_segs,
803 pos, &iocb->ki_pos, count, ret);
805 * if we just got an ENOSPC, flush the inode now we aren't holding any
806 * page locks and retry *once*
808 if (ret == -ENOSPC && !enospc) {
809 ret = -xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
815 current->backing_dev_info = NULL;
822 const struct iovec *iovp,
823 unsigned long nr_segs,
826 struct file *file = iocb->ki_filp;
827 struct address_space *mapping = file->f_mapping;
828 struct inode *inode = mapping->host;
829 struct xfs_inode *ip = XFS_I(inode);
834 XFS_STATS_INC(xs_write_calls);
836 BUG_ON(iocb->ki_pos != pos);
838 ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
845 xfs_wait_for_freeze(ip->i_mount, SB_FREEZE_WRITE);
847 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
850 if (unlikely(file->f_flags & O_DIRECT))
851 ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos,
854 ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
857 xfs_aio_write_isize_update(inode, &iocb->ki_pos, ret);
862 /* Handle various SYNC-type writes */
863 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
864 loff_t end = pos + ret - 1;
867 xfs_rw_iunlock(ip, iolock);
868 error = filemap_write_and_wait_range(mapping, pos, end);
869 xfs_rw_ilock(ip, iolock);
871 error2 = -xfs_file_fsync(file,
872 (file->f_flags & __O_SYNC) ? 0 : 1);
880 xfs_aio_write_newsize_update(ip);
881 xfs_rw_iunlock(ip, iolock);
890 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
892 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
902 struct xfs_inode *ip = XFS_I(inode);
906 error = xfs_file_open(inode, file);
911 * If there are any blocks, read-ahead block 0 as we're almost
912 * certain to have the next operation be a read there.
914 mode = xfs_ilock_map_shared(ip);
915 if (ip->i_d.di_nextents > 0)
916 xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
917 xfs_iunlock(ip, mode);
926 return -xfs_release(XFS_I(inode));
935 struct inode *inode = filp->f_path.dentry->d_inode;
936 xfs_inode_t *ip = XFS_I(inode);
941 * The Linux API doesn't pass down the total size of the buffer
942 * we read into down to the filesystem. With the filldir concept
943 * it's not needed for correct information, but the XFS dir2 leaf
944 * code wants an estimate of the buffer size to calculate it's
945 * readahead window and size the buffers used for mapping to
948 * Try to give it an estimate that's good enough, maybe at some
949 * point we can change the ->readdir prototype to include the
950 * buffer size. For now we use the current glibc buffer size.
952 bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
954 error = xfs_readdir(ip, dirent, bufsize,
955 (xfs_off_t *)&filp->f_pos, filldir);
964 struct vm_area_struct *vma)
966 vma->vm_ops = &xfs_file_vm_ops;
967 vma->vm_flags |= VM_CAN_NONLINEAR;
974 * mmap()d file has taken write protection fault and is being made
975 * writable. We can set the page state up correctly for a writable
976 * page, which means we can do correct delalloc accounting (ENOSPC
977 * checking!) and unwritten extent mapping.
981 struct vm_area_struct *vma,
982 struct vm_fault *vmf)
984 return block_page_mkwrite(vma, vmf, xfs_get_blocks);
987 const struct file_operations xfs_file_operations = {
988 .llseek = generic_file_llseek,
989 .read = do_sync_read,
990 .write = do_sync_write,
991 .aio_read = xfs_file_aio_read,
992 .aio_write = xfs_file_aio_write,
993 .splice_read = xfs_file_splice_read,
994 .splice_write = xfs_file_splice_write,
995 .unlocked_ioctl = xfs_file_ioctl,
997 .compat_ioctl = xfs_file_compat_ioctl,
999 .mmap = xfs_file_mmap,
1000 .open = xfs_file_open,
1001 .release = xfs_file_release,
1002 .fsync = xfs_file_fsync,
1005 const struct file_operations xfs_dir_file_operations = {
1006 .open = xfs_dir_open,
1007 .read = generic_read_dir,
1008 .readdir = xfs_file_readdir,
1009 .llseek = generic_file_llseek,
1010 .unlocked_ioctl = xfs_file_ioctl,
1011 #ifdef CONFIG_COMPAT
1012 .compat_ioctl = xfs_file_compat_ioctl,
1014 .fsync = xfs_file_fsync,
1017 static const struct vm_operations_struct xfs_file_vm_ops = {
1018 .fault = filemap_fault,
1019 .page_mkwrite = xfs_vm_page_mkwrite,