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
26 #include "xfs_trans.h"
27 #include "xfs_dmapi.h"
28 #include "xfs_mount.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_alloc_btree.h"
31 #include "xfs_ialloc_btree.h"
32 #include "xfs_alloc.h"
33 #include "xfs_btree.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
38 #include "xfs_inode_item.h"
40 #include "xfs_error.h"
42 #include "xfs_vnodeops.h"
43 #include "xfs_da_btree.h"
44 #include "xfs_ioctl.h"
45 #include "xfs_trace.h"
47 #include <linux/dcache.h>
49 static const struct vm_operations_struct xfs_file_vm_ops;
54 * xfs_iozero clears the specified range of buffer supplied,
55 * and marks all the affected blocks as valid and modified. If
56 * an affected block is not allocated, it will be allocated. If
57 * an affected block is not completely overwritten, and is not
58 * valid before the operation, it will be read from disk before
59 * being partially zeroed.
63 struct xfs_inode *ip, /* inode */
64 loff_t pos, /* offset in file */
65 size_t count) /* size of data to zero */
68 struct address_space *mapping;
71 mapping = VFS_I(ip)->i_mapping;
73 unsigned offset, bytes;
76 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
77 bytes = PAGE_CACHE_SIZE - offset;
81 status = pagecache_write_begin(NULL, mapping, pos, bytes,
82 AOP_FLAG_UNINTERRUPTIBLE,
87 zero_user(page, offset, bytes);
89 status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
91 WARN_ON(status <= 0); /* can't return less than zero! */
103 struct dentry *dentry,
106 struct xfs_inode *ip = XFS_I(dentry->d_inode);
107 struct xfs_trans *tp;
111 xfs_itrace_entry(ip);
113 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
114 return -XFS_ERROR(EIO);
116 xfs_iflags_clear(ip, XFS_ITRUNCATED);
119 * We always need to make sure that the required inode state is safe on
120 * disk. The inode might be clean but we still might need to force the
121 * log because of committed transactions that haven't hit the disk yet.
122 * Likewise, there could be unflushed non-transactional changes to the
123 * inode core that have to go to disk and this requires us to issue
124 * a synchronous transaction to capture these changes correctly.
126 * This code relies on the assumption that if the i_update_core field
127 * of the inode is clear and the inode is unpinned then it is clean
128 * and no action is required.
130 xfs_ilock(ip, XFS_ILOCK_SHARED);
133 * First check if the VFS inode is marked dirty. All the dirtying
134 * of non-transactional updates no goes through mark_inode_dirty*,
135 * which allows us to distinguish beteeen pure timestamp updates
136 * and i_size updates which need to be caught for fdatasync.
137 * After that also theck for the dirty state in the XFS inode, which
138 * might gets cleared when the inode gets written out via the AIL
139 * or xfs_iflush_cluster.
141 if (((dentry->d_inode->i_state & I_DIRTY_DATASYNC) ||
142 ((dentry->d_inode->i_state & I_DIRTY_SYNC) && !datasync)) &&
145 * Kick off a transaction to log the inode core to get the
146 * updates. The sync transaction will also force the log.
148 xfs_iunlock(ip, XFS_ILOCK_SHARED);
149 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
150 error = xfs_trans_reserve(tp, 0,
151 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
153 xfs_trans_cancel(tp, 0);
156 xfs_ilock(ip, XFS_ILOCK_EXCL);
159 * Note - it's possible that we might have pushed ourselves out
160 * of the way during trans_reserve which would flush the inode.
161 * But there's no guarantee that the inode buffer has actually
162 * gone out yet (it's delwri). Plus the buffer could be pinned
163 * anyway if it's part of an inode in another recent
164 * transaction. So we play it safe and fire off the
165 * transaction anyway.
167 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
168 xfs_trans_ihold(tp, ip);
169 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
170 xfs_trans_set_sync(tp);
171 error = _xfs_trans_commit(tp, 0, &log_flushed);
173 xfs_iunlock(ip, XFS_ILOCK_EXCL);
176 * Timestamps/size haven't changed since last inode flush or
177 * inode transaction commit. That means either nothing got
178 * written or a transaction committed which caught the updates.
179 * If the latter happened and the transaction hasn't hit the
180 * disk yet, the inode will be still be pinned. If it is,
183 if (xfs_ipincount(ip)) {
184 error = _xfs_log_force_lsn(ip->i_mount,
185 ip->i_itemp->ili_last_lsn,
186 XFS_LOG_SYNC, &log_flushed);
188 xfs_iunlock(ip, XFS_ILOCK_SHARED);
191 if (ip->i_mount->m_flags & XFS_MOUNT_BARRIER) {
193 * If the log write didn't issue an ordered tag we need
194 * to flush the disk cache for the data device now.
197 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
200 * If this inode is on the RT dev we need to flush that
203 if (XFS_IS_REALTIME_INODE(ip))
204 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
213 const struct iovec *iovp,
214 unsigned long nr_segs,
217 struct file *file = iocb->ki_filp;
218 struct inode *inode = file->f_mapping->host;
219 struct xfs_inode *ip = XFS_I(inode);
220 struct xfs_mount *mp = ip->i_mount;
227 XFS_STATS_INC(xs_read_calls);
229 BUG_ON(iocb->ki_pos != pos);
231 if (unlikely(file->f_flags & O_DIRECT))
232 ioflags |= IO_ISDIRECT;
233 if (file->f_mode & FMODE_NOCMTIME)
236 /* START copy & waste from filemap.c */
237 for (seg = 0; seg < nr_segs; seg++) {
238 const struct iovec *iv = &iovp[seg];
241 * If any segment has a negative length, or the cumulative
242 * length ever wraps negative then return -EINVAL.
245 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
246 return XFS_ERROR(-EINVAL);
248 /* END copy & waste from filemap.c */
250 if (unlikely(ioflags & IO_ISDIRECT)) {
251 xfs_buftarg_t *target =
252 XFS_IS_REALTIME_INODE(ip) ?
253 mp->m_rtdev_targp : mp->m_ddev_targp;
254 if ((iocb->ki_pos & target->bt_smask) ||
255 (size & target->bt_smask)) {
256 if (iocb->ki_pos == ip->i_size)
258 return -XFS_ERROR(EINVAL);
262 n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
263 if (n <= 0 || size == 0)
269 if (XFS_FORCED_SHUTDOWN(mp))
272 if (unlikely(ioflags & IO_ISDIRECT))
273 mutex_lock(&inode->i_mutex);
274 xfs_ilock(ip, XFS_IOLOCK_SHARED);
276 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
277 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
278 int iolock = XFS_IOLOCK_SHARED;
280 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ, ip, iocb->ki_pos, size,
283 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
284 if (unlikely(ioflags & IO_ISDIRECT))
285 mutex_unlock(&inode->i_mutex);
290 if (unlikely(ioflags & IO_ISDIRECT)) {
291 if (inode->i_mapping->nrpages) {
292 ret = -xfs_flushinval_pages(ip,
293 (iocb->ki_pos & PAGE_CACHE_MASK),
294 -1, FI_REMAPF_LOCKED);
296 mutex_unlock(&inode->i_mutex);
298 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
303 trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags);
305 ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos);
307 XFS_STATS_ADD(xs_read_bytes, ret);
309 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
314 xfs_file_splice_read(
317 struct pipe_inode_info *pipe,
321 struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
322 struct xfs_mount *mp = ip->i_mount;
326 XFS_STATS_INC(xs_read_calls);
328 if (infilp->f_mode & FMODE_NOCMTIME)
331 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
334 xfs_ilock(ip, XFS_IOLOCK_SHARED);
336 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
337 int iolock = XFS_IOLOCK_SHARED;
340 error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, *ppos, count,
341 FILP_DELAY_FLAG(infilp), &iolock);
343 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
348 trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
350 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
352 XFS_STATS_ADD(xs_read_bytes, ret);
354 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
359 xfs_file_splice_write(
360 struct pipe_inode_info *pipe,
361 struct file *outfilp,
366 struct inode *inode = outfilp->f_mapping->host;
367 struct xfs_inode *ip = XFS_I(inode);
368 struct xfs_mount *mp = ip->i_mount;
369 xfs_fsize_t isize, new_size;
373 XFS_STATS_INC(xs_write_calls);
375 if (outfilp->f_mode & FMODE_NOCMTIME)
378 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
381 xfs_ilock(ip, XFS_IOLOCK_EXCL);
383 if (DM_EVENT_ENABLED(ip, DM_EVENT_WRITE) && !(ioflags & IO_INVIS)) {
384 int iolock = XFS_IOLOCK_EXCL;
387 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, *ppos, count,
388 FILP_DELAY_FLAG(outfilp), &iolock);
390 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
395 new_size = *ppos + count;
397 xfs_ilock(ip, XFS_ILOCK_EXCL);
398 if (new_size > ip->i_size)
399 ip->i_new_size = new_size;
400 xfs_iunlock(ip, XFS_ILOCK_EXCL);
402 trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
404 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
406 XFS_STATS_ADD(xs_write_bytes, ret);
408 isize = i_size_read(inode);
409 if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
412 if (*ppos > ip->i_size) {
413 xfs_ilock(ip, XFS_ILOCK_EXCL);
414 if (*ppos > ip->i_size)
416 xfs_iunlock(ip, XFS_ILOCK_EXCL);
419 if (ip->i_new_size) {
420 xfs_ilock(ip, XFS_ILOCK_EXCL);
422 if (ip->i_d.di_size > ip->i_size)
423 ip->i_d.di_size = ip->i_size;
424 xfs_iunlock(ip, XFS_ILOCK_EXCL);
426 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
431 * This routine is called to handle zeroing any space in the last
432 * block of the file that is beyond the EOF. We do this since the
433 * size is being increased without writing anything to that block
434 * and we don't want anyone to read the garbage on the disk.
436 STATIC int /* error (positive) */
442 xfs_fileoff_t last_fsb;
443 xfs_mount_t *mp = ip->i_mount;
448 xfs_bmbt_irec_t imap;
450 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
452 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
453 if (zero_offset == 0) {
455 * There are no extra bytes in the last block on disk to
461 last_fsb = XFS_B_TO_FSBT(mp, isize);
463 error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
464 &nimaps, NULL, NULL);
470 * If the block underlying isize is just a hole, then there
471 * is nothing to zero.
473 if (imap.br_startblock == HOLESTARTBLOCK) {
477 * Zero the part of the last block beyond the EOF, and write it
478 * out sync. We need to drop the ilock while we do this so we
479 * don't deadlock when the buffer cache calls back to us.
481 xfs_iunlock(ip, XFS_ILOCK_EXCL);
483 zero_len = mp->m_sb.sb_blocksize - zero_offset;
484 if (isize + zero_len > offset)
485 zero_len = offset - isize;
486 error = xfs_iozero(ip, isize, zero_len);
488 xfs_ilock(ip, XFS_ILOCK_EXCL);
494 * Zero any on disk space between the current EOF and the new,
495 * larger EOF. This handles the normal case of zeroing the remainder
496 * of the last block in the file and the unusual case of zeroing blocks
497 * out beyond the size of the file. This second case only happens
498 * with fixed size extents and when the system crashes before the inode
499 * size was updated but after blocks were allocated. If fill is set,
500 * then any holes in the range are filled and zeroed. If not, the holes
501 * are left alone as holes.
504 int /* error (positive) */
507 xfs_off_t offset, /* starting I/O offset */
508 xfs_fsize_t isize) /* current inode size */
510 xfs_mount_t *mp = ip->i_mount;
511 xfs_fileoff_t start_zero_fsb;
512 xfs_fileoff_t end_zero_fsb;
513 xfs_fileoff_t zero_count_fsb;
514 xfs_fileoff_t last_fsb;
515 xfs_fileoff_t zero_off;
516 xfs_fsize_t zero_len;
519 xfs_bmbt_irec_t imap;
521 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
522 ASSERT(offset > isize);
525 * First handle zeroing the block on which isize resides.
526 * We only zero a part of that block so it is handled specially.
528 error = xfs_zero_last_block(ip, offset, isize);
530 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
535 * Calculate the range between the new size and the old
536 * where blocks needing to be zeroed may exist. To get the
537 * block where the last byte in the file currently resides,
538 * we need to subtract one from the size and truncate back
539 * to a block boundary. We subtract 1 in case the size is
540 * exactly on a block boundary.
542 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
543 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
544 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
545 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
546 if (last_fsb == end_zero_fsb) {
548 * The size was only incremented on its last block.
549 * We took care of that above, so just return.
554 ASSERT(start_zero_fsb <= end_zero_fsb);
555 while (start_zero_fsb <= end_zero_fsb) {
557 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
558 error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
559 0, NULL, 0, &imap, &nimaps, NULL, NULL);
561 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
566 if (imap.br_state == XFS_EXT_UNWRITTEN ||
567 imap.br_startblock == HOLESTARTBLOCK) {
569 * This loop handles initializing pages that were
570 * partially initialized by the code below this
571 * loop. It basically zeroes the part of the page
572 * that sits on a hole and sets the page as P_HOLE
573 * and calls remapf if it is a mapped file.
575 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
576 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
581 * There are blocks we need to zero.
582 * Drop the inode lock while we're doing the I/O.
583 * We'll still have the iolock to protect us.
585 xfs_iunlock(ip, XFS_ILOCK_EXCL);
587 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
588 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
590 if ((zero_off + zero_len) > offset)
591 zero_len = offset - zero_off;
593 error = xfs_iozero(ip, zero_off, zero_len);
598 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
599 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
601 xfs_ilock(ip, XFS_ILOCK_EXCL);
607 xfs_ilock(ip, XFS_ILOCK_EXCL);
615 const struct iovec *iovp,
616 unsigned long nr_segs,
619 struct file *file = iocb->ki_filp;
620 struct address_space *mapping = file->f_mapping;
621 struct inode *inode = mapping->host;
622 struct xfs_inode *ip = XFS_I(inode);
623 struct xfs_mount *mp = ip->i_mount;
624 ssize_t ret = 0, error = 0;
626 xfs_fsize_t isize, new_size;
629 size_t ocount = 0, count;
632 XFS_STATS_INC(xs_write_calls);
634 BUG_ON(iocb->ki_pos != pos);
636 if (unlikely(file->f_flags & O_DIRECT))
637 ioflags |= IO_ISDIRECT;
638 if (file->f_mode & FMODE_NOCMTIME)
641 error = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
649 xfs_wait_for_freeze(mp, SB_FREEZE_WRITE);
651 if (XFS_FORCED_SHUTDOWN(mp))
655 if (ioflags & IO_ISDIRECT) {
656 iolock = XFS_IOLOCK_SHARED;
659 iolock = XFS_IOLOCK_EXCL;
661 mutex_lock(&inode->i_mutex);
664 xfs_ilock(ip, XFS_ILOCK_EXCL|iolock);
667 error = -generic_write_checks(file, &pos, &count,
668 S_ISBLK(inode->i_mode));
670 xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock);
671 goto out_unlock_mutex;
674 if ((DM_EVENT_ENABLED(ip, DM_EVENT_WRITE) &&
675 !(ioflags & IO_INVIS) && !eventsent)) {
676 int dmflags = FILP_DELAY_FLAG(file);
679 dmflags |= DM_FLAGS_IMUX;
681 xfs_iunlock(ip, XFS_ILOCK_EXCL);
682 error = XFS_SEND_DATA(ip->i_mount, DM_EVENT_WRITE, ip,
683 pos, count, dmflags, &iolock);
685 goto out_unlock_internal;
687 xfs_ilock(ip, XFS_ILOCK_EXCL);
691 * The iolock was dropped and reacquired in XFS_SEND_DATA
692 * so we have to recheck the size when appending.
693 * We will only "goto start;" once, since having sent the
694 * event prevents another call to XFS_SEND_DATA, which is
695 * what allows the size to change in the first place.
697 if ((file->f_flags & O_APPEND) && pos != ip->i_size)
701 if (ioflags & IO_ISDIRECT) {
702 xfs_buftarg_t *target =
703 XFS_IS_REALTIME_INODE(ip) ?
704 mp->m_rtdev_targp : mp->m_ddev_targp;
706 if ((pos & target->bt_smask) || (count & target->bt_smask)) {
707 xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock);
708 return XFS_ERROR(-EINVAL);
711 if (!need_i_mutex && (mapping->nrpages || pos > ip->i_size)) {
712 xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock);
713 iolock = XFS_IOLOCK_EXCL;
715 mutex_lock(&inode->i_mutex);
716 xfs_ilock(ip, XFS_ILOCK_EXCL|iolock);
721 new_size = pos + count;
722 if (new_size > ip->i_size)
723 ip->i_new_size = new_size;
725 if (likely(!(ioflags & IO_INVIS)))
726 file_update_time(file);
729 * If the offset is beyond the size of the file, we have a couple
730 * of things to do. First, if there is already space allocated
731 * we need to either create holes or zero the disk or ...
733 * If there is a page where the previous size lands, we need
734 * to zero it out up to the new size.
737 if (pos > ip->i_size) {
738 error = xfs_zero_eof(ip, pos, ip->i_size);
740 xfs_iunlock(ip, XFS_ILOCK_EXCL);
741 goto out_unlock_internal;
744 xfs_iunlock(ip, XFS_ILOCK_EXCL);
747 * If we're writing the file then make sure to clear the
748 * setuid and setgid bits if the process is not being run
749 * by root. This keeps people from modifying setuid and
752 error = -file_remove_suid(file);
754 goto out_unlock_internal;
756 /* We can write back this queue in page reclaim */
757 current->backing_dev_info = mapping->backing_dev_info;
759 if ((ioflags & IO_ISDIRECT)) {
760 if (mapping->nrpages) {
761 WARN_ON(need_i_mutex == 0);
762 error = xfs_flushinval_pages(ip,
763 (pos & PAGE_CACHE_MASK),
764 -1, FI_REMAPF_LOCKED);
766 goto out_unlock_internal;
770 /* demote the lock now the cached pages are gone */
771 xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
772 mutex_unlock(&inode->i_mutex);
774 iolock = XFS_IOLOCK_SHARED;
778 trace_xfs_file_direct_write(ip, count, iocb->ki_pos, ioflags);
779 ret = generic_file_direct_write(iocb, iovp,
780 &nr_segs, pos, &iocb->ki_pos, count, ocount);
783 * direct-io write to a hole: fall through to buffered I/O
784 * for completing the rest of the request.
786 if (ret >= 0 && ret != count) {
787 XFS_STATS_ADD(xs_write_bytes, ret);
792 ioflags &= ~IO_ISDIRECT;
793 xfs_iunlock(ip, iolock);
801 trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, ioflags);
802 ret2 = 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
806 * aren't holding any page locks and retry *once*
808 if (ret2 == -ENOSPC && !enospc) {
809 error = xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
811 goto out_unlock_internal;
818 current->backing_dev_info = NULL;
820 isize = i_size_read(inode);
821 if (unlikely(ret < 0 && ret != -EFAULT && iocb->ki_pos > isize))
822 iocb->ki_pos = isize;
824 if (iocb->ki_pos > ip->i_size) {
825 xfs_ilock(ip, XFS_ILOCK_EXCL);
826 if (iocb->ki_pos > ip->i_size)
827 ip->i_size = iocb->ki_pos;
828 xfs_iunlock(ip, XFS_ILOCK_EXCL);
831 if (ret == -ENOSPC &&
832 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE) && !(ioflags & IO_INVIS)) {
833 xfs_iunlock(ip, iolock);
835 mutex_unlock(&inode->i_mutex);
836 error = XFS_SEND_NAMESP(ip->i_mount, DM_EVENT_NOSPACE, ip,
837 DM_RIGHT_NULL, ip, DM_RIGHT_NULL, NULL, NULL,
838 0, 0, 0); /* Delay flag intentionally unused */
840 mutex_lock(&inode->i_mutex);
841 xfs_ilock(ip, iolock);
843 goto out_unlock_internal;
849 goto out_unlock_internal;
851 XFS_STATS_ADD(xs_write_bytes, ret);
853 /* Handle various SYNC-type writes */
854 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
855 loff_t end = pos + ret - 1;
858 xfs_iunlock(ip, iolock);
860 mutex_unlock(&inode->i_mutex);
862 error2 = filemap_write_and_wait_range(mapping, pos, end);
866 mutex_lock(&inode->i_mutex);
867 xfs_ilock(ip, iolock);
869 error2 = -xfs_file_fsync(file, file->f_path.dentry,
870 (file->f_flags & __O_SYNC) ? 0 : 1);
876 if (ip->i_new_size) {
877 xfs_ilock(ip, XFS_ILOCK_EXCL);
880 * If this was a direct or synchronous I/O that failed (such
881 * as ENOSPC) then part of the I/O may have been written to
882 * disk before the error occured. In this case the on-disk
883 * file size may have been adjusted beyond the in-memory file
884 * size and now needs to be truncated back.
886 if (ip->i_d.di_size > ip->i_size)
887 ip->i_d.di_size = ip->i_size;
888 xfs_iunlock(ip, XFS_ILOCK_EXCL);
890 xfs_iunlock(ip, iolock);
893 mutex_unlock(&inode->i_mutex);
902 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
904 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
914 struct xfs_inode *ip = XFS_I(inode);
918 error = xfs_file_open(inode, file);
923 * If there are any blocks, read-ahead block 0 as we're almost
924 * certain to have the next operation be a read there.
926 mode = xfs_ilock_map_shared(ip);
927 if (ip->i_d.di_nextents > 0)
928 xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
929 xfs_iunlock(ip, mode);
938 return -xfs_release(XFS_I(inode));
947 struct inode *inode = filp->f_path.dentry->d_inode;
948 xfs_inode_t *ip = XFS_I(inode);
953 * The Linux API doesn't pass down the total size of the buffer
954 * we read into down to the filesystem. With the filldir concept
955 * it's not needed for correct information, but the XFS dir2 leaf
956 * code wants an estimate of the buffer size to calculate it's
957 * readahead window and size the buffers used for mapping to
960 * Try to give it an estimate that's good enough, maybe at some
961 * point we can change the ->readdir prototype to include the
962 * buffer size. For now we use the current glibc buffer size.
964 bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
966 error = xfs_readdir(ip, dirent, bufsize,
967 (xfs_off_t *)&filp->f_pos, filldir);
976 struct vm_area_struct *vma)
978 vma->vm_ops = &xfs_file_vm_ops;
979 vma->vm_flags |= VM_CAN_NONLINEAR;
986 * mmap()d file has taken write protection fault and is being made
987 * writable. We can set the page state up correctly for a writable
988 * page, which means we can do correct delalloc accounting (ENOSPC
989 * checking!) and unwritten extent mapping.
993 struct vm_area_struct *vma,
994 struct vm_fault *vmf)
996 return block_page_mkwrite(vma, vmf, xfs_get_blocks);
999 const struct file_operations xfs_file_operations = {
1000 .llseek = generic_file_llseek,
1001 .read = do_sync_read,
1002 .write = do_sync_write,
1003 .aio_read = xfs_file_aio_read,
1004 .aio_write = xfs_file_aio_write,
1005 .splice_read = xfs_file_splice_read,
1006 .splice_write = xfs_file_splice_write,
1007 .unlocked_ioctl = xfs_file_ioctl,
1008 #ifdef CONFIG_COMPAT
1009 .compat_ioctl = xfs_file_compat_ioctl,
1011 .mmap = xfs_file_mmap,
1012 .open = xfs_file_open,
1013 .release = xfs_file_release,
1014 .fsync = xfs_file_fsync,
1015 #ifdef HAVE_FOP_OPEN_EXEC
1016 .open_exec = xfs_file_open_exec,
1020 const struct file_operations xfs_dir_file_operations = {
1021 .open = xfs_dir_open,
1022 .read = generic_read_dir,
1023 .readdir = xfs_file_readdir,
1024 .llseek = generic_file_llseek,
1025 .unlocked_ioctl = xfs_file_ioctl,
1026 #ifdef CONFIG_COMPAT
1027 .compat_ioctl = xfs_file_compat_ioctl,
1029 .fsync = xfs_file_fsync,
1032 static const struct vm_operations_struct xfs_file_vm_ops = {
1033 .fault = filemap_fault,
1034 .page_mkwrite = xfs_vm_page_mkwrite,