2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 * fs/xfs/linux/xfs_lrw.c (Linux Read Write stuff)
42 #include "xfs_trans.h"
47 #include "xfs_alloc.h"
48 #include "xfs_dmapi.h"
49 #include "xfs_quota.h"
50 #include "xfs_mount.h"
51 #include "xfs_alloc_btree.h"
52 #include "xfs_bmap_btree.h"
53 #include "xfs_ialloc_btree.h"
54 #include "xfs_btree.h"
55 #include "xfs_ialloc.h"
56 #include "xfs_attr_sf.h"
57 #include "xfs_dir_sf.h"
58 #include "xfs_dir2_sf.h"
59 #include "xfs_dinode.h"
60 #include "xfs_inode.h"
63 #include "xfs_rtalloc.h"
64 #include "xfs_error.h"
65 #include "xfs_itable.h"
71 #include "xfs_inode_item.h"
72 #include "xfs_buf_item.h"
73 #include "xfs_utils.h"
75 #include <linux/capability.h>
81 * xfs_iozero clears the specified range of buffer supplied,
82 * and marks all the affected blocks as valid and modified. If
83 * an affected block is not allocated, it will be allocated. If
84 * an affected block is not completely overwritten, and is not
85 * valid before the operation, it will be read from disk before
86 * being partially zeroed.
90 struct inode *ip, /* inode */
91 loff_t pos, /* offset in file */
92 size_t count, /* size of data to zero */
93 loff_t end_size) /* max file size to set */
97 struct address_space *mapping;
101 mapping = ip->i_mapping;
103 unsigned long index, offset;
105 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
106 index = pos >> PAGE_CACHE_SHIFT;
107 bytes = PAGE_CACHE_SIZE - offset;
112 page = grab_cache_page(mapping, index);
117 status = mapping->a_ops->prepare_write(NULL, page, offset,
123 memset((void *) (kaddr + offset), 0, bytes);
124 flush_dcache_page(page);
125 status = mapping->a_ops->commit_write(NULL, page, offset,
130 if (pos > ip->i_size)
131 ip->i_size = pos < end_size ? pos : end_size;
137 page_cache_release(page);
145 ssize_t /* bytes read, or (-) error */
149 const struct iovec *iovp,
161 int direct = (filp->f_flags & O_DIRECT);
162 int invisible = (filp->f_mode & FINVIS);
164 ip = XFS_BHVTOI(bdp);
165 vp = BHV_TO_VNODE(bdp);
167 vn_trace_entry(vp, "xfs_read", (inst_t *)__return_address);
169 XFS_STATS_INC(xfsstats.xs_read_calls);
171 /* START copy & waste from filemap.c */
172 for (seg = 0; seg < segs; seg++) {
173 const struct iovec *iv = &iovp[seg];
176 * If any segment has a negative length, or the cumulative
177 * length ever wraps negative then return -EINVAL.
180 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
181 return XFS_ERROR(-EINVAL);
182 if (direct) { /* XFS specific check */
183 if ((__psint_t)iv->iov_base & BBMASK) {
184 if (*offp == ip->i_d.di_size)
186 return XFS_ERROR(-EINVAL);
189 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
192 return XFS_ERROR(-EFAULT);
196 /* END copy & waste from filemap.c */
199 if ((*offp & mp->m_blockmask) ||
200 (size & mp->m_blockmask)) {
201 if (*offp == ip->i_d.di_size) {
204 return -XFS_ERROR(EINVAL);
208 n = XFS_MAX_FILE_OFFSET - *offp;
209 if ((n <= 0) || (size == 0))
215 if (XFS_FORCED_SHUTDOWN(mp)) {
219 xfs_ilock(ip, XFS_IOLOCK_SHARED);
221 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ) && !invisible) {
223 vrwlock_t locktype = VRWLOCK_READ;
225 error = XFS_SEND_DATA(mp, DM_EVENT_READ, bdp, *offp, size,
226 FILP_DELAY_FLAG(filp), &locktype);
228 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
233 ret = generic_file_readv(filp, iovp, segs, offp);
234 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
236 XFS_STATS_ADD(xfsstats.xs_read_bytes, ret);
239 xfs_ichgtime(ip, XFS_ICHGTIME_ACC);
259 int invisible = (filp->f_mode & FINVIS);
261 ip = XFS_BHVTOI(bdp);
262 vp = BHV_TO_VNODE(bdp);
264 vn_trace_entry(vp, "xfs_sendfile", (inst_t *)__return_address);
266 XFS_STATS_INC(xfsstats.xs_read_calls);
268 n = XFS_MAX_FILE_OFFSET - *offp;
269 if ((n <= 0) || (count == 0))
275 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
278 xfs_ilock(ip, XFS_IOLOCK_SHARED);
279 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ) && !invisible) {
280 vrwlock_t locktype = VRWLOCK_READ;
283 error = XFS_SEND_DATA(mp, DM_EVENT_READ, bdp, *offp, count,
284 FILP_DELAY_FLAG(filp), &locktype);
286 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
290 ret = generic_file_sendfile(filp, offp, count, actor, target);
291 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
293 XFS_STATS_ADD(xfsstats.xs_read_bytes, ret);
295 xfs_ichgtime(ip, XFS_ICHGTIME_ACC);
300 * This routine is called to handle zeroing any space in the last
301 * block of the file that is beyond the EOF. We do this since the
302 * size is being increased without writing anything to that block
303 * and we don't want anyone to read the garbage on the disk.
305 STATIC int /* error (positive) */
311 xfs_fsize_t end_size)
313 xfs_fileoff_t last_fsb;
318 int isize_fsb_offset;
320 xfs_bmbt_irec_t imap;
324 ASSERT(ismrlocked(io->io_lock, MR_UPDATE) != 0);
325 ASSERT(offset > isize);
329 isize_fsb_offset = XFS_B_FSB_OFFSET(mp, isize);
330 if (isize_fsb_offset == 0) {
332 * There are no extra bytes in the last block on disk to
338 last_fsb = XFS_B_TO_FSBT(mp, isize);
340 error = XFS_BMAPI(mp, NULL, io, last_fsb, 1, 0, NULL, 0, &imap,
347 * If the block underlying isize is just a hole, then there
348 * is nothing to zero.
350 if (imap.br_startblock == HOLESTARTBLOCK) {
354 * Zero the part of the last block beyond the EOF, and write it
355 * out sync. We need to drop the ilock while we do this so we
356 * don't deadlock when the buffer cache calls back to us.
358 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
359 loff = XFS_FSB_TO_B(mp, last_fsb);
360 lsize = XFS_FSB_TO_B(mp, 1);
362 zero_offset = isize_fsb_offset;
363 zero_len = mp->m_sb.sb_blocksize - isize_fsb_offset;
365 error = xfs_iozero(ip, loff + zero_offset, zero_len, end_size);
367 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
373 * Zero any on disk space between the current EOF and the new,
374 * larger EOF. This handles the normal case of zeroing the remainder
375 * of the last block in the file and the unusual case of zeroing blocks
376 * out beyond the size of the file. This second case only happens
377 * with fixed size extents and when the system crashes before the inode
378 * size was updated but after blocks were allocated. If fill is set,
379 * then any holes in the range are filled and zeroed. If not, the holes
380 * are left alone as holes.
383 int /* error (positive) */
387 xfs_off_t offset, /* starting I/O offset */
388 xfs_fsize_t isize, /* current inode size */
389 xfs_fsize_t end_size) /* terminal inode size */
391 struct inode *ip = LINVFS_GET_IP(vp);
392 xfs_fileoff_t start_zero_fsb;
393 xfs_fileoff_t end_zero_fsb;
394 xfs_fileoff_t prev_zero_fsb;
395 xfs_fileoff_t zero_count_fsb;
396 xfs_fileoff_t last_fsb;
397 xfs_extlen_t buf_len_fsb;
398 xfs_extlen_t prev_zero_count;
402 xfs_bmbt_irec_t imap;
406 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
407 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
412 * First handle zeroing the block on which isize resides.
413 * We only zero a part of that block so it is handled specially.
415 error = xfs_zero_last_block(ip, io, offset, isize, end_size);
417 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
418 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
423 * Calculate the range between the new size and the old
424 * where blocks needing to be zeroed may exist. To get the
425 * block where the last byte in the file currently resides,
426 * we need to subtract one from the size and truncate back
427 * to a block boundary. We subtract 1 in case the size is
428 * exactly on a block boundary.
430 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
431 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
432 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
433 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
434 if (last_fsb == end_zero_fsb) {
436 * The size was only incremented on its last block.
437 * We took care of that above, so just return.
442 ASSERT(start_zero_fsb <= end_zero_fsb);
443 prev_zero_fsb = NULLFILEOFF;
445 while (start_zero_fsb <= end_zero_fsb) {
447 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
448 error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb,
449 0, NULL, 0, &imap, &nimaps, NULL);
451 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
452 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
457 if (imap.br_startblock == HOLESTARTBLOCK) {
459 * This loop handles initializing pages that were
460 * partially initialized by the code below this
461 * loop. It basically zeroes the part of the page
462 * that sits on a hole and sets the page as P_HOLE
463 * and calls remapf if it is a mapped file.
465 prev_zero_fsb = NULLFILEOFF;
467 start_zero_fsb = imap.br_startoff +
469 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
474 * There are blocks in the range requested.
475 * Zero them a single write at a time. We actually
476 * don't zero the entire range returned if it is
477 * too big and simply loop around to get the rest.
478 * That is not the most efficient thing to do, but it
479 * is simple and this path should not be exercised often.
481 buf_len_fsb = XFS_FILBLKS_MIN(imap.br_blockcount,
482 mp->m_writeio_blocks << 8);
484 * Drop the inode lock while we're doing the I/O.
485 * We'll still have the iolock to protect us.
487 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
489 loff = XFS_FSB_TO_B(mp, start_zero_fsb);
490 lsize = XFS_FSB_TO_B(mp, buf_len_fsb);
492 error = xfs_iozero(ip, loff, lsize, end_size);
498 prev_zero_fsb = start_zero_fsb;
499 prev_zero_count = buf_len_fsb;
500 start_zero_fsb = imap.br_startoff + buf_len_fsb;
501 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
503 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
510 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
515 ssize_t /* bytes written, or (-) error */
519 const struct iovec *iovp,
529 xfs_fsize_t isize, new_size;
530 xfs_fsize_t n, limit = XFS_MAX_FILE_OFFSET;
535 int direct = (file->f_flags & O_DIRECT);
536 int invisible = (file->f_mode & FINVIS);
540 XFS_STATS_INC(xfsstats.xs_write_calls);
542 vp = BHV_TO_VNODE(bdp);
543 vn_trace_entry(vp, "xfs_write", (inst_t *)__return_address);
544 xip = XFS_BHVTOI(bdp);
546 /* START copy & waste from filemap.c */
547 for (seg = 0; seg < segs; seg++) {
548 const struct iovec *iv = &iovp[seg];
551 * If any segment has a negative length, or the cumulative
552 * length ever wraps negative then return -EINVAL.
555 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
556 return XFS_ERROR(-EINVAL);
557 if (direct) { /* XFS specific check */
558 if ((__psint_t)iv->iov_base & BBMASK)
559 return XFS_ERROR(-EINVAL);
561 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
564 return XFS_ERROR(-EFAULT);
568 /* END copy & waste from filemap.c */
573 io = &(xip->i_iocore);
576 xfs_check_frozen(mp, bdp, XFS_FREEZE_WRITE);
578 if (XFS_FORCED_SHUTDOWN(xip->i_mount)) {
583 if ((*offset & mp->m_blockmask) ||
584 (size & mp->m_blockmask)) {
585 return XFS_ERROR(-EINVAL);
587 iolock = XFS_IOLOCK_SHARED;
588 locktype = VRWLOCK_WRITE_DIRECT;
590 iolock = XFS_IOLOCK_EXCL;
591 locktype = VRWLOCK_WRITE;
594 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
595 isize = xip->i_d.di_size;
597 if (file->f_flags & O_APPEND)
603 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
610 new_size = *offset + size;
611 if (new_size > isize) {
612 io->io_new_size = new_size;
615 if ((DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_WRITE) &&
616 !invisible && !eventsent)) {
617 loff_t savedsize = *offset;
619 xfs_iunlock(xip, XFS_ILOCK_EXCL);
620 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, bdp,
622 FILP_DELAY_FLAG(file), &locktype);
624 xfs_iunlock(xip, iolock);
627 xfs_ilock(xip, XFS_ILOCK_EXCL);
631 * The iolock was dropped and reaquired in XFS_SEND_DATA
632 * so we have to recheck the size when appending.
633 * We will only "goto start;" once, since having sent the
634 * event prevents another call to XFS_SEND_DATA, which is
635 * what allows the size to change in the first place.
637 if ((file->f_flags & O_APPEND) &&
638 savedsize != xip->i_d.di_size) {
639 *offset = isize = xip->i_d.di_size;
645 * On Linux, generic_file_write updates the times even if
646 * no data is copied in so long as the write had a size.
648 * We must update xfs' times since revalidate will overcopy xfs.
650 if (size && !invisible)
651 xfs_ichgtime(xip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
654 * If the offset is beyond the size of the file, we have a couple
655 * of things to do. First, if there is already space allocated
656 * we need to either create holes or zero the disk or ...
658 * If there is a page where the previous size lands, we need
659 * to zero it out up to the new size.
662 if (!direct && (*offset > isize && isize)) {
663 error = xfs_zero_eof(BHV_TO_VNODE(bdp), io, *offset,
664 isize, *offset + size);
666 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
670 xfs_iunlock(xip, XFS_ILOCK_EXCL);
673 * If we're writing the file then make sure to clear the
674 * setuid and setgid bits if the process is not being run
675 * by root. This keeps people from modifying setuid and
679 if (((xip->i_d.di_mode & ISUID) ||
680 ((xip->i_d.di_mode & (ISGID | (IEXEC >> 3))) ==
681 (ISGID | (IEXEC >> 3)))) &&
682 !capable(CAP_FSETID)) {
683 error = xfs_write_clear_setuid(xip);
685 xfs_iunlock(xip, iolock);
692 xfs_inval_cached_pages(vp, &xip->i_iocore, *offset, 1, 1);
695 ret = generic_file_write_nolock(file, iovp, segs, offset);
697 if ((ret == -ENOSPC) &&
698 DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_NOSPACE) && !invisible) {
700 xfs_rwunlock(bdp, locktype);
701 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, bdp,
702 DM_RIGHT_NULL, bdp, DM_RIGHT_NULL, NULL, NULL,
703 0, 0, 0); /* Delay flag intentionally unused */
706 xfs_rwlock(bdp, locktype);
707 *offset = xip->i_d.di_size;
713 xfs_rwunlock(bdp, locktype);
717 XFS_STATS_ADD(xfsstats.xs_write_bytes, ret);
719 if (*offset > xip->i_d.di_size) {
720 xfs_ilock(xip, XFS_ILOCK_EXCL);
721 if (*offset > xip->i_d.di_size) {
722 struct inode *inode = LINVFS_GET_IP(vp);
724 inode->i_size = xip->i_d.di_size = *offset;
725 xip->i_update_core = 1;
726 xip->i_update_size = 1;
728 xfs_iunlock(xip, XFS_ILOCK_EXCL);
731 /* Handle various SYNC-type writes */
732 if ((file->f_flags & O_SYNC) || IS_SYNC(file->f_dentry->d_inode)) {
735 * If we're treating this as O_DSYNC and we have not updated the
736 * size, force the log.
739 if (!(mp->m_flags & XFS_MOUNT_OSYNCISOSYNC)
740 && !(xip->i_update_size)) {
742 * If an allocation transaction occurred
743 * without extending the size, then we have to force
744 * the log up the proper point to ensure that the
745 * allocation is permanent. We can't count on
746 * the fact that buffered writes lock out direct I/O
747 * writes - the direct I/O write could have extended
748 * the size nontransactionally, then finished before
749 * we started. xfs_write_file will think that the file
750 * didn't grow but the update isn't safe unless the
751 * size change is logged.
753 * Force the log if we've committed a transaction
754 * against the inode or if someone else has and
755 * the commit record hasn't gone to disk (e.g.
756 * the inode is pinned). This guarantees that
757 * all changes affecting the inode are permanent
761 xfs_inode_log_item_t *iip;
765 if (iip && iip->ili_last_lsn) {
766 lsn = iip->ili_last_lsn;
767 xfs_log_force(mp, lsn,
768 XFS_LOG_FORCE | XFS_LOG_SYNC);
769 } else if (xfs_ipincount(xip) > 0) {
770 xfs_log_force(mp, (xfs_lsn_t)0,
771 XFS_LOG_FORCE | XFS_LOG_SYNC);
778 * O_SYNC or O_DSYNC _with_ a size update are handled
781 * If the write was synchronous then we need to make
782 * sure that the inode modification time is permanent.
783 * We'll have updated the timestamp above, so here
784 * we use a synchronous transaction to log the inode.
785 * It's not fast, but it's necessary.
787 * If this a dsync write and the size got changed
788 * non-transactionally, then we need to ensure that
789 * the size change gets logged in a synchronous
793 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITE_SYNC);
794 if ((error = xfs_trans_reserve(tp, 0,
795 XFS_SWRITE_LOG_RES(mp),
797 /* Transaction reserve failed */
798 xfs_trans_cancel(tp, 0);
800 /* Transaction reserve successful */
801 xfs_ilock(xip, XFS_ILOCK_EXCL);
802 xfs_trans_ijoin(tp, xip, XFS_ILOCK_EXCL);
803 xfs_trans_ihold(tp, xip);
804 xfs_trans_log_inode(tp, xip, XFS_ILOG_CORE);
805 xfs_trans_set_sync(tp);
806 error = xfs_trans_commit(tp, 0, (xfs_lsn_t)0);
807 xfs_iunlock(xip, XFS_ILOCK_EXCL);
810 } /* (ioflags & O_SYNC) */
812 xfs_rwunlock(bdp, locktype);
817 * All xfs metadata buffers except log state machine buffers
818 * get this attached as their b_bdstrat callback function.
819 * This is so that we can catch a buffer
820 * after prematurely unpinning it to forcibly shutdown the filesystem.
823 xfs_bdstrat_cb(struct xfs_buf *bp)
827 mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
828 if (!XFS_FORCED_SHUTDOWN(mp)) {
829 pagebuf_iorequest(bp);
832 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
834 * Metadata write that didn't get logged but
835 * written delayed anyway. These aren't associated
836 * with a transaction, and can be ignored.
838 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
839 (XFS_BUF_ISREAD(bp)) == 0)
840 return (xfs_bioerror_relse(bp));
842 return (xfs_bioerror(bp));
848 xfs_bmap(bhv_desc_t *bdp,
852 page_buf_bmap_t *pbmapp,
855 xfs_inode_t *ip = XFS_BHVTOI(bdp);
856 xfs_iocore_t *io = &ip->i_iocore;
858 ASSERT((ip->i_d.di_mode & IFMT) == IFREG);
859 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
860 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
862 return xfs_iomap(io, offset, count, flags, pbmapp, npbmaps);
866 * Wrapper around bdstrat so that we can stop data
867 * from going to disk in case we are shutting down the filesystem.
868 * Typically user data goes thru this path; one of the exceptions
873 struct xfs_mount *mp,
877 if (!XFS_FORCED_SHUTDOWN(mp)) {
878 /* Grio redirection would go here
879 * if (XFS_BUF_IS_GRIO(bp)) {
882 pagebuf_iorequest(bp);
886 xfs_buftrace("XFSBDSTRAT IOERROR", bp);
887 return (xfs_bioerror_relse(bp));
891 * If the underlying (data/log/rt) device is readonly, there are some
892 * operations that cannot proceed.
895 xfs_dev_is_read_only(
899 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
900 xfs_readonly_buftarg(mp->m_logdev_targp) ||
901 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
903 "XFS: %s required on read-only device.", message);
905 "XFS: write access unavailable, cannot proceed.");