2 * Copyright (c) 2000-2003,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
23 #include "xfs_trans.h"
27 #include "xfs_alloc.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_quota.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
39 #include "xfs_btree.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_error.h"
43 #include "xfs_itable.h"
46 #include "xfs_inode_item.h"
47 #include "xfs_buf_item.h"
48 #include "xfs_utils.h"
49 #include "xfs_iomap.h"
50 #include "xfs_vnodeops.h"
51 #include "xfs_trace.h"
53 #include <linux/capability.h>
54 #include <linux/writeback.h>
60 * xfs_iozero clears the specified range of buffer supplied,
61 * and marks all the affected blocks as valid and modified. If
62 * an affected block is not allocated, it will be allocated. If
63 * an affected block is not completely overwritten, and is not
64 * valid before the operation, it will be read from disk before
65 * being partially zeroed.
69 struct xfs_inode *ip, /* inode */
70 loff_t pos, /* offset in file */
71 size_t count) /* size of data to zero */
74 struct address_space *mapping;
77 mapping = VFS_I(ip)->i_mapping;
79 unsigned offset, bytes;
82 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
83 bytes = PAGE_CACHE_SIZE - offset;
87 status = pagecache_write_begin(NULL, mapping, pos, bytes,
88 AOP_FLAG_UNINTERRUPTIBLE,
93 zero_user(page, offset, bytes);
95 status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
97 WARN_ON(status <= 0); /* can't return less than zero! */
106 ssize_t /* bytes read, or (-) error */
110 const struct iovec *iovp,
115 struct file *file = iocb->ki_filp;
116 struct inode *inode = file->f_mapping->host;
117 xfs_mount_t *mp = ip->i_mount;
124 XFS_STATS_INC(xs_read_calls);
126 /* START copy & waste from filemap.c */
127 for (seg = 0; seg < segs; seg++) {
128 const struct iovec *iv = &iovp[seg];
131 * If any segment has a negative length, or the cumulative
132 * length ever wraps negative then return -EINVAL.
135 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
136 return XFS_ERROR(-EINVAL);
138 /* END copy & waste from filemap.c */
140 if (unlikely(ioflags & IO_ISDIRECT)) {
141 xfs_buftarg_t *target =
142 XFS_IS_REALTIME_INODE(ip) ?
143 mp->m_rtdev_targp : mp->m_ddev_targp;
144 if ((*offset & target->bt_smask) ||
145 (size & target->bt_smask)) {
146 if (*offset == ip->i_size) {
149 return -XFS_ERROR(EINVAL);
153 n = XFS_MAXIOFFSET(mp) - *offset;
154 if ((n <= 0) || (size == 0))
160 if (XFS_FORCED_SHUTDOWN(mp))
163 if (unlikely(ioflags & IO_ISDIRECT))
164 mutex_lock(&inode->i_mutex);
165 xfs_ilock(ip, XFS_IOLOCK_SHARED);
167 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
168 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
169 int iolock = XFS_IOLOCK_SHARED;
171 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ, ip, *offset, size,
174 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
175 if (unlikely(ioflags & IO_ISDIRECT))
176 mutex_unlock(&inode->i_mutex);
181 if (unlikely(ioflags & IO_ISDIRECT)) {
182 if (inode->i_mapping->nrpages)
183 ret = -xfs_flushinval_pages(ip, (*offset & PAGE_CACHE_MASK),
184 -1, FI_REMAPF_LOCKED);
185 mutex_unlock(&inode->i_mutex);
187 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
192 trace_xfs_file_read(ip, size, *offset, ioflags);
194 iocb->ki_pos = *offset;
195 ret = generic_file_aio_read(iocb, iovp, segs, *offset);
197 XFS_STATS_ADD(xs_read_bytes, ret);
199 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
208 struct pipe_inode_info *pipe,
213 xfs_mount_t *mp = ip->i_mount;
216 XFS_STATS_INC(xs_read_calls);
217 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
220 xfs_ilock(ip, XFS_IOLOCK_SHARED);
222 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
223 int iolock = XFS_IOLOCK_SHARED;
226 error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, *ppos, count,
227 FILP_DELAY_FLAG(infilp), &iolock);
229 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
234 trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
236 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
238 XFS_STATS_ADD(xs_read_bytes, ret);
240 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
247 struct pipe_inode_info *pipe,
248 struct file *outfilp,
254 xfs_mount_t *mp = ip->i_mount;
256 struct inode *inode = outfilp->f_mapping->host;
257 xfs_fsize_t isize, new_size;
259 XFS_STATS_INC(xs_write_calls);
260 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
263 xfs_ilock(ip, XFS_IOLOCK_EXCL);
265 if (DM_EVENT_ENABLED(ip, DM_EVENT_WRITE) && !(ioflags & IO_INVIS)) {
266 int iolock = XFS_IOLOCK_EXCL;
269 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, *ppos, count,
270 FILP_DELAY_FLAG(outfilp), &iolock);
272 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
277 new_size = *ppos + count;
279 xfs_ilock(ip, XFS_ILOCK_EXCL);
280 if (new_size > ip->i_size)
281 ip->i_new_size = new_size;
282 xfs_iunlock(ip, XFS_ILOCK_EXCL);
284 trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
286 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
288 XFS_STATS_ADD(xs_write_bytes, ret);
290 isize = i_size_read(inode);
291 if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
294 if (*ppos > ip->i_size) {
295 xfs_ilock(ip, XFS_ILOCK_EXCL);
296 if (*ppos > ip->i_size)
298 xfs_iunlock(ip, XFS_ILOCK_EXCL);
301 if (ip->i_new_size) {
302 xfs_ilock(ip, XFS_ILOCK_EXCL);
304 if (ip->i_d.di_size > ip->i_size)
305 ip->i_d.di_size = ip->i_size;
306 xfs_iunlock(ip, XFS_ILOCK_EXCL);
308 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
313 * This routine is called to handle zeroing any space in the last
314 * block of the file that is beyond the EOF. We do this since the
315 * size is being increased without writing anything to that block
316 * and we don't want anyone to read the garbage on the disk.
318 STATIC int /* error (positive) */
324 xfs_fileoff_t last_fsb;
325 xfs_mount_t *mp = ip->i_mount;
330 xfs_bmbt_irec_t imap;
332 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
334 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
335 if (zero_offset == 0) {
337 * There are no extra bytes in the last block on disk to
343 last_fsb = XFS_B_TO_FSBT(mp, isize);
345 error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
346 &nimaps, NULL, NULL);
352 * If the block underlying isize is just a hole, then there
353 * is nothing to zero.
355 if (imap.br_startblock == HOLESTARTBLOCK) {
359 * Zero the part of the last block beyond the EOF, and write it
360 * out sync. We need to drop the ilock while we do this so we
361 * don't deadlock when the buffer cache calls back to us.
363 xfs_iunlock(ip, XFS_ILOCK_EXCL);
365 zero_len = mp->m_sb.sb_blocksize - zero_offset;
366 if (isize + zero_len > offset)
367 zero_len = offset - isize;
368 error = xfs_iozero(ip, isize, zero_len);
370 xfs_ilock(ip, XFS_ILOCK_EXCL);
376 * Zero any on disk space between the current EOF and the new,
377 * larger EOF. This handles the normal case of zeroing the remainder
378 * of the last block in the file and the unusual case of zeroing blocks
379 * out beyond the size of the file. This second case only happens
380 * with fixed size extents and when the system crashes before the inode
381 * size was updated but after blocks were allocated. If fill is set,
382 * then any holes in the range are filled and zeroed. If not, the holes
383 * are left alone as holes.
386 int /* error (positive) */
389 xfs_off_t offset, /* starting I/O offset */
390 xfs_fsize_t isize) /* current inode size */
392 xfs_mount_t *mp = ip->i_mount;
393 xfs_fileoff_t start_zero_fsb;
394 xfs_fileoff_t end_zero_fsb;
395 xfs_fileoff_t zero_count_fsb;
396 xfs_fileoff_t last_fsb;
397 xfs_fileoff_t zero_off;
398 xfs_fsize_t zero_len;
401 xfs_bmbt_irec_t imap;
403 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
404 ASSERT(offset > isize);
407 * First handle zeroing the block on which isize resides.
408 * We only zero a part of that block so it is handled specially.
410 error = xfs_zero_last_block(ip, offset, isize);
412 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
417 * Calculate the range between the new size and the old
418 * where blocks needing to be zeroed may exist. To get the
419 * block where the last byte in the file currently resides,
420 * we need to subtract one from the size and truncate back
421 * to a block boundary. We subtract 1 in case the size is
422 * exactly on a block boundary.
424 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
425 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
426 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
427 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
428 if (last_fsb == end_zero_fsb) {
430 * The size was only incremented on its last block.
431 * We took care of that above, so just return.
436 ASSERT(start_zero_fsb <= end_zero_fsb);
437 while (start_zero_fsb <= end_zero_fsb) {
439 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
440 error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
441 0, NULL, 0, &imap, &nimaps, NULL, NULL);
443 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
448 if (imap.br_state == XFS_EXT_UNWRITTEN ||
449 imap.br_startblock == HOLESTARTBLOCK) {
451 * This loop handles initializing pages that were
452 * partially initialized by the code below this
453 * loop. It basically zeroes the part of the page
454 * that sits on a hole and sets the page as P_HOLE
455 * and calls remapf if it is a mapped file.
457 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
458 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
463 * There are blocks we need to zero.
464 * Drop the inode lock while we're doing the I/O.
465 * We'll still have the iolock to protect us.
467 xfs_iunlock(ip, XFS_ILOCK_EXCL);
469 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
470 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
472 if ((zero_off + zero_len) > offset)
473 zero_len = offset - zero_off;
475 error = xfs_iozero(ip, zero_off, zero_len);
480 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
481 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
483 xfs_ilock(ip, XFS_ILOCK_EXCL);
489 xfs_ilock(ip, XFS_ILOCK_EXCL);
494 ssize_t /* bytes written, or (-) error */
496 struct xfs_inode *xip,
498 const struct iovec *iovp,
503 struct file *file = iocb->ki_filp;
504 struct address_space *mapping = file->f_mapping;
505 struct inode *inode = mapping->host;
506 unsigned long segs = nsegs;
508 ssize_t ret = 0, error = 0;
509 xfs_fsize_t isize, new_size;
512 size_t ocount = 0, count;
516 XFS_STATS_INC(xs_write_calls);
518 error = generic_segment_checks(iovp, &segs, &ocount, VERIFY_READ);
530 xfs_wait_for_freeze(mp, SB_FREEZE_WRITE);
532 if (XFS_FORCED_SHUTDOWN(mp))
536 if (ioflags & IO_ISDIRECT) {
537 iolock = XFS_IOLOCK_SHARED;
540 iolock = XFS_IOLOCK_EXCL;
542 mutex_lock(&inode->i_mutex);
545 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
548 error = -generic_write_checks(file, &pos, &count,
549 S_ISBLK(inode->i_mode));
551 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
552 goto out_unlock_mutex;
555 if ((DM_EVENT_ENABLED(xip, DM_EVENT_WRITE) &&
556 !(ioflags & IO_INVIS) && !eventsent)) {
557 int dmflags = FILP_DELAY_FLAG(file);
560 dmflags |= DM_FLAGS_IMUX;
562 xfs_iunlock(xip, XFS_ILOCK_EXCL);
563 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, xip,
564 pos, count, dmflags, &iolock);
566 goto out_unlock_internal;
568 xfs_ilock(xip, XFS_ILOCK_EXCL);
572 * The iolock was dropped and reacquired in XFS_SEND_DATA
573 * so we have to recheck the size when appending.
574 * We will only "goto start;" once, since having sent the
575 * event prevents another call to XFS_SEND_DATA, which is
576 * what allows the size to change in the first place.
578 if ((file->f_flags & O_APPEND) && pos != xip->i_size)
582 if (ioflags & IO_ISDIRECT) {
583 xfs_buftarg_t *target =
584 XFS_IS_REALTIME_INODE(xip) ?
585 mp->m_rtdev_targp : mp->m_ddev_targp;
587 if ((pos & target->bt_smask) || (count & target->bt_smask)) {
588 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
589 return XFS_ERROR(-EINVAL);
592 if (!need_i_mutex && (mapping->nrpages || pos > xip->i_size)) {
593 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
594 iolock = XFS_IOLOCK_EXCL;
596 mutex_lock(&inode->i_mutex);
597 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
602 new_size = pos + count;
603 if (new_size > xip->i_size)
604 xip->i_new_size = new_size;
606 if (likely(!(ioflags & IO_INVIS)))
607 file_update_time(file);
610 * If the offset is beyond the size of the file, we have a couple
611 * of things to do. First, if there is already space allocated
612 * we need to either create holes or zero the disk or ...
614 * If there is a page where the previous size lands, we need
615 * to zero it out up to the new size.
618 if (pos > xip->i_size) {
619 error = xfs_zero_eof(xip, pos, xip->i_size);
621 xfs_iunlock(xip, XFS_ILOCK_EXCL);
622 goto out_unlock_internal;
625 xfs_iunlock(xip, XFS_ILOCK_EXCL);
628 * If we're writing the file then make sure to clear the
629 * setuid and setgid bits if the process is not being run
630 * by root. This keeps people from modifying setuid and
634 if (((xip->i_d.di_mode & S_ISUID) ||
635 ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
636 (S_ISGID | S_IXGRP))) &&
637 !capable(CAP_FSETID)) {
638 error = xfs_write_clear_setuid(xip);
640 error = -file_remove_suid(file);
641 if (unlikely(error)) {
642 goto out_unlock_internal;
646 /* We can write back this queue in page reclaim */
647 current->backing_dev_info = mapping->backing_dev_info;
649 if ((ioflags & IO_ISDIRECT)) {
650 if (mapping->nrpages) {
651 WARN_ON(need_i_mutex == 0);
652 error = xfs_flushinval_pages(xip,
653 (pos & PAGE_CACHE_MASK),
654 -1, FI_REMAPF_LOCKED);
656 goto out_unlock_internal;
660 /* demote the lock now the cached pages are gone */
661 xfs_ilock_demote(xip, XFS_IOLOCK_EXCL);
662 mutex_unlock(&inode->i_mutex);
664 iolock = XFS_IOLOCK_SHARED;
668 trace_xfs_file_direct_write(xip, count, *offset, ioflags);
669 ret = generic_file_direct_write(iocb, iovp,
670 &segs, pos, offset, count, ocount);
673 * direct-io write to a hole: fall through to buffered I/O
674 * for completing the rest of the request.
676 if (ret >= 0 && ret != count) {
677 XFS_STATS_ADD(xs_write_bytes, ret);
682 ioflags &= ~IO_ISDIRECT;
683 xfs_iunlock(xip, iolock);
691 trace_xfs_file_buffered_write(xip, count, *offset, ioflags);
692 ret2 = generic_file_buffered_write(iocb, iovp, segs,
693 pos, offset, count, ret);
695 * if we just got an ENOSPC, flush the inode now we
696 * aren't holding any page locks and retry *once*
698 if (ret2 == -ENOSPC && !enospc) {
699 error = xfs_flush_pages(xip, 0, -1, 0, FI_NONE);
701 goto out_unlock_internal;
708 current->backing_dev_info = NULL;
710 isize = i_size_read(inode);
711 if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
714 if (*offset > xip->i_size) {
715 xfs_ilock(xip, XFS_ILOCK_EXCL);
716 if (*offset > xip->i_size)
717 xip->i_size = *offset;
718 xfs_iunlock(xip, XFS_ILOCK_EXCL);
721 if (ret == -ENOSPC &&
722 DM_EVENT_ENABLED(xip, DM_EVENT_NOSPACE) && !(ioflags & IO_INVIS)) {
723 xfs_iunlock(xip, iolock);
725 mutex_unlock(&inode->i_mutex);
726 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, xip,
727 DM_RIGHT_NULL, xip, DM_RIGHT_NULL, NULL, NULL,
728 0, 0, 0); /* Delay flag intentionally unused */
730 mutex_lock(&inode->i_mutex);
731 xfs_ilock(xip, iolock);
733 goto out_unlock_internal;
739 goto out_unlock_internal;
741 XFS_STATS_ADD(xs_write_bytes, ret);
743 /* Handle various SYNC-type writes */
744 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
745 loff_t end = pos + ret - 1;
748 xfs_iunlock(xip, iolock);
750 mutex_unlock(&inode->i_mutex);
752 error2 = filemap_write_and_wait_range(mapping, pos, end);
756 mutex_lock(&inode->i_mutex);
757 xfs_ilock(xip, iolock);
759 error2 = xfs_fsync(xip);
765 if (xip->i_new_size) {
766 xfs_ilock(xip, XFS_ILOCK_EXCL);
769 * If this was a direct or synchronous I/O that failed (such
770 * as ENOSPC) then part of the I/O may have been written to
771 * disk before the error occured. In this case the on-disk
772 * file size may have been adjusted beyond the in-memory file
773 * size and now needs to be truncated back.
775 if (xip->i_d.di_size > xip->i_size)
776 xip->i_d.di_size = xip->i_size;
777 xfs_iunlock(xip, XFS_ILOCK_EXCL);
779 xfs_iunlock(xip, iolock);
782 mutex_unlock(&inode->i_mutex);
787 * All xfs metadata buffers except log state machine buffers
788 * get this attached as their b_bdstrat callback function.
789 * This is so that we can catch a buffer
790 * after prematurely unpinning it to forcibly shutdown the filesystem.
793 xfs_bdstrat_cb(struct xfs_buf *bp)
795 if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
796 trace_xfs_bdstrat_shut(bp, _RET_IP_);
798 * Metadata write that didn't get logged but
799 * written delayed anyway. These aren't associated
800 * with a transaction, and can be ignored.
802 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
803 (XFS_BUF_ISREAD(bp)) == 0)
804 return (xfs_bioerror_relse(bp));
806 return (xfs_bioerror(bp));
809 xfs_buf_iorequest(bp);
814 * Wrapper around bdstrat so that we can stop data from going to disk in case
815 * we are shutting down the filesystem. Typically user data goes thru this
816 * path; one of the exceptions is the superblock.
820 struct xfs_mount *mp,
824 if (!XFS_FORCED_SHUTDOWN(mp)) {
825 xfs_buf_iorequest(bp);
829 trace_xfs_bdstrat_shut(bp, _RET_IP_);
830 xfs_bioerror_relse(bp);
834 * If the underlying (data/log/rt) device is readonly, there are some
835 * operations that cannot proceed.
838 xfs_dev_is_read_only(
842 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
843 xfs_readonly_buftarg(mp->m_logdev_targp) ||
844 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
846 "XFS: %s required on read-only device.", message);
848 "XFS: write access unavailable, cannot proceed.");