Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 20 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 22 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "xfs_trans.h" |
| 24 | #include "xfs_sb.h" |
| 25 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_dir2.h" |
| 27 | #include "xfs_alloc.h" |
| 28 | #include "xfs_dmapi.h" |
| 29 | #include "xfs_quota.h" |
| 30 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_dinode.h" |
| 37 | #include "xfs_inode.h" |
| 38 | #include "xfs_bmap.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 39 | #include "xfs_btree.h" |
| 40 | #include "xfs_ialloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include "xfs_rtalloc.h" |
| 42 | #include "xfs_error.h" |
| 43 | #include "xfs_itable.h" |
| 44 | #include "xfs_rw.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include "xfs_attr.h" |
| 46 | #include "xfs_inode_item.h" |
| 47 | #include "xfs_buf_item.h" |
| 48 | #include "xfs_utils.h" |
| 49 | #include "xfs_iomap.h" |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 50 | #include "xfs_vnodeops.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 51 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | #include <linux/capability.h> |
| 54 | #include <linux/writeback.h> |
| 55 | |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* |
| 58 | * xfs_iozero |
| 59 | * |
| 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. |
| 66 | */ |
| 67 | STATIC int |
| 68 | xfs_iozero( |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 69 | struct xfs_inode *ip, /* inode */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | loff_t pos, /* offset in file */ |
Lachlan McIlroy | 6816016 | 2007-02-10 18:36:47 +1100 | [diff] [blame] | 71 | size_t count) /* size of data to zero */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | struct page *page; |
| 74 | struct address_space *mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | int status; |
| 76 | |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 77 | mapping = VFS_I(ip)->i_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | do { |
Nick Piggin | d79689c | 2007-10-16 01:25:06 -0700 | [diff] [blame] | 79 | unsigned offset, bytes; |
| 80 | void *fsdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | bytes = PAGE_CACHE_SIZE - offset; |
| 84 | if (bytes > count) |
| 85 | bytes = count; |
| 86 | |
Nick Piggin | d79689c | 2007-10-16 01:25:06 -0700 | [diff] [blame] | 87 | status = pagecache_write_begin(NULL, mapping, pos, bytes, |
| 88 | AOP_FLAG_UNINTERRUPTIBLE, |
| 89 | &page, &fsdata); |
David Chinner | e7ff6ae | 2007-02-10 18:37:46 +1100 | [diff] [blame] | 90 | if (status) |
Nick Piggin | d79689c | 2007-10-16 01:25:06 -0700 | [diff] [blame] | 91 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 93 | zero_user(page, offset, bytes); |
David Chinner | e7ff6ae | 2007-02-10 18:37:46 +1100 | [diff] [blame] | 94 | |
Nick Piggin | d79689c | 2007-10-16 01:25:06 -0700 | [diff] [blame] | 95 | status = pagecache_write_end(NULL, mapping, pos, bytes, bytes, |
| 96 | page, fsdata); |
| 97 | WARN_ON(status <= 0); /* can't return less than zero! */ |
| 98 | pos += bytes; |
| 99 | count -= bytes; |
| 100 | status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } while (count); |
| 102 | |
| 103 | return (-status); |
| 104 | } |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | ssize_t /* bytes read, or (-) error */ |
| 107 | xfs_read( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 108 | xfs_inode_t *ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | struct kiocb *iocb, |
| 110 | const struct iovec *iovp, |
| 111 | unsigned int segs, |
| 112 | loff_t *offset, |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 113 | int ioflags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
| 115 | struct file *file = iocb->ki_filp; |
| 116 | struct inode *inode = file->f_mapping->host; |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 117 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | size_t size = 0; |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 119 | ssize_t ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | xfs_fsize_t n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | unsigned long seg; |
| 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
| 124 | XFS_STATS_INC(xs_read_calls); |
| 125 | |
| 126 | /* START copy & waste from filemap.c */ |
| 127 | for (seg = 0; seg < segs; seg++) { |
| 128 | const struct iovec *iv = &iovp[seg]; |
| 129 | |
| 130 | /* |
| 131 | * If any segment has a negative length, or the cumulative |
| 132 | * length ever wraps negative then return -EINVAL. |
| 133 | */ |
| 134 | size += iv->iov_len; |
| 135 | if (unlikely((ssize_t)(size|iv->iov_len) < 0)) |
| 136 | return XFS_ERROR(-EINVAL); |
| 137 | } |
| 138 | /* END copy & waste from filemap.c */ |
| 139 | |
| 140 | if (unlikely(ioflags & IO_ISDIRECT)) { |
| 141 | xfs_buftarg_t *target = |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 142 | XFS_IS_REALTIME_INODE(ip) ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | mp->m_rtdev_targp : mp->m_ddev_targp; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 144 | if ((*offset & target->bt_smask) || |
| 145 | (size & target->bt_smask)) { |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 146 | if (*offset == ip->i_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | return (0); |
| 148 | } |
| 149 | return -XFS_ERROR(EINVAL); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | n = XFS_MAXIOFFSET(mp) - *offset; |
| 154 | if ((n <= 0) || (size == 0)) |
| 155 | return 0; |
| 156 | |
| 157 | if (n < size) |
| 158 | size = n; |
| 159 | |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 160 | if (XFS_FORCED_SHUTDOWN(mp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
| 163 | if (unlikely(ioflags & IO_ISDIRECT)) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 164 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
| 166 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 167 | if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) { |
Dean Roehrich | e1a40fa | 2005-06-22 10:20:44 +1000 | [diff] [blame] | 168 | int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags); |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 169 | int iolock = XFS_IOLOCK_SHARED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 171 | ret = -XFS_SEND_DATA(mp, DM_EVENT_READ, ip, *offset, size, |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 172 | dmflags, &iolock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | if (ret) { |
| 174 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
Lachlan McIlroy | 721259b | 2006-09-07 14:27:05 +1000 | [diff] [blame] | 175 | if (unlikely(ioflags & IO_ISDIRECT)) |
| 176 | mutex_unlock(&inode->i_mutex); |
| 177 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Nathan Scott | edcd4bc | 2006-09-28 11:05:33 +1000 | [diff] [blame] | 181 | if (unlikely(ioflags & IO_ISDIRECT)) { |
Christoph Hellwig | dcf49cc | 2008-03-06 13:46:37 +1100 | [diff] [blame] | 182 | if (inode->i_mapping->nrpages) |
Dave Chinner | 2e65609 | 2008-11-28 14:23:33 +1100 | [diff] [blame] | 183 | ret = -xfs_flushinval_pages(ip, (*offset & PAGE_CACHE_MASK), |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 184 | -1, FI_REMAPF_LOCKED); |
Lachlan McIlroy | 721259b | 2006-09-07 14:27:05 +1000 | [diff] [blame] | 185 | mutex_unlock(&inode->i_mutex); |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 186 | if (ret) { |
| 187 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
| 188 | return ret; |
| 189 | } |
Nathan Scott | edcd4bc | 2006-09-28 11:05:33 +1000 | [diff] [blame] | 190 | } |
Lachlan McIlroy | 721259b | 2006-09-07 14:27:05 +1000 | [diff] [blame] | 191 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 192 | trace_xfs_file_read(ip, size, *offset, ioflags); |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 193 | |
| 194 | iocb->ki_pos = *offset; |
| 195 | ret = generic_file_aio_read(iocb, iovp, segs, *offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | if (ret > 0) |
| 197 | XFS_STATS_ADD(xs_read_bytes, ret); |
| 198 | |
| 199 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | ssize_t |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 204 | xfs_splice_read( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 205 | xfs_inode_t *ip, |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 206 | struct file *infilp, |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 207 | loff_t *ppos, |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 208 | struct pipe_inode_info *pipe, |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 209 | size_t count, |
| 210 | int flags, |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 211 | int ioflags) |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 212 | { |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 213 | xfs_mount_t *mp = ip->i_mount; |
| 214 | ssize_t ret; |
| 215 | |
| 216 | XFS_STATS_INC(xs_read_calls); |
| 217 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 218 | return -EIO; |
| 219 | |
| 220 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
| 221 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 222 | if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) { |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 223 | int iolock = XFS_IOLOCK_SHARED; |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 224 | int error; |
| 225 | |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 226 | error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, *ppos, count, |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 227 | FILP_DELAY_FLAG(infilp), &iolock); |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 228 | if (error) { |
| 229 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
| 230 | return -error; |
| 231 | } |
| 232 | } |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 233 | |
| 234 | trace_xfs_file_splice_read(ip, count, *ppos, ioflags); |
| 235 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 236 | ret = generic_file_splice_read(infilp, ppos, pipe, count, flags); |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 237 | if (ret > 0) |
| 238 | XFS_STATS_ADD(xs_read_bytes, ret); |
| 239 | |
| 240 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | ssize_t |
| 245 | xfs_splice_write( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 246 | xfs_inode_t *ip, |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 247 | struct pipe_inode_info *pipe, |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 248 | struct file *outfilp, |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 249 | loff_t *ppos, |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 250 | size_t count, |
| 251 | int flags, |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 252 | int ioflags) |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 253 | { |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 254 | xfs_mount_t *mp = ip->i_mount; |
| 255 | ssize_t ret; |
David Chinner | 0a8d17d | 2006-09-07 14:27:15 +1000 | [diff] [blame] | 256 | struct inode *inode = outfilp->f_mapping->host; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 257 | xfs_fsize_t isize, new_size; |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 258 | |
| 259 | XFS_STATS_INC(xs_write_calls); |
| 260 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 261 | return -EIO; |
| 262 | |
| 263 | xfs_ilock(ip, XFS_IOLOCK_EXCL); |
| 264 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 265 | if (DM_EVENT_ENABLED(ip, DM_EVENT_WRITE) && !(ioflags & IO_INVIS)) { |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 266 | int iolock = XFS_IOLOCK_EXCL; |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 267 | int error; |
| 268 | |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 269 | error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, *ppos, count, |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 270 | FILP_DELAY_FLAG(outfilp), &iolock); |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 271 | if (error) { |
| 272 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 273 | return -error; |
| 274 | } |
| 275 | } |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 276 | |
| 277 | new_size = *ppos + count; |
| 278 | |
| 279 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 280 | if (new_size > ip->i_size) |
Christoph Hellwig | 613d704 | 2007-10-11 17:44:08 +1000 | [diff] [blame] | 281 | ip->i_new_size = new_size; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 282 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 283 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 284 | trace_xfs_file_splice_write(ip, count, *ppos, ioflags); |
| 285 | |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 286 | ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags); |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 287 | if (ret > 0) |
| 288 | XFS_STATS_ADD(xs_write_bytes, ret); |
| 289 | |
David Chinner | 0a8d17d | 2006-09-07 14:27:15 +1000 | [diff] [blame] | 290 | isize = i_size_read(inode); |
| 291 | if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize)) |
| 292 | *ppos = isize; |
| 293 | |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 294 | if (*ppos > ip->i_size) { |
David Chinner | 0a8d17d | 2006-09-07 14:27:15 +1000 | [diff] [blame] | 295 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 296 | if (*ppos > ip->i_size) |
| 297 | ip->i_size = *ppos; |
| 298 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 299 | } |
| 300 | |
Christoph Hellwig | 613d704 | 2007-10-11 17:44:08 +1000 | [diff] [blame] | 301 | if (ip->i_new_size) { |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 302 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 613d704 | 2007-10-11 17:44:08 +1000 | [diff] [blame] | 303 | ip->i_new_size = 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 304 | if (ip->i_d.di_size > ip->i_size) |
| 305 | ip->i_d.di_size = ip->i_size; |
David Chinner | 0a8d17d | 2006-09-07 14:27:15 +1000 | [diff] [blame] | 306 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 307 | } |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 308 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | /* |
| 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. |
| 317 | */ |
| 318 | STATIC int /* error (positive) */ |
| 319 | xfs_zero_last_block( |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 320 | xfs_inode_t *ip, |
Lachlan McIlroy | 6816016 | 2007-02-10 18:36:47 +1100 | [diff] [blame] | 321 | xfs_fsize_t offset, |
| 322 | xfs_fsize_t isize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | { |
| 324 | xfs_fileoff_t last_fsb; |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 325 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | int nimaps; |
| 327 | int zero_offset; |
| 328 | int zero_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | int error = 0; |
| 330 | xfs_bmbt_irec_t imap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 332 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Eric Sandeen | 24ee808 | 2006-01-11 15:34:32 +1100 | [diff] [blame] | 334 | zero_offset = XFS_B_FSB_OFFSET(mp, isize); |
| 335 | if (zero_offset == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | /* |
| 337 | * There are no extra bytes in the last block on disk to |
| 338 | * zero, so return. |
| 339 | */ |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | last_fsb = XFS_B_TO_FSBT(mp, isize); |
| 344 | nimaps = 1; |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 345 | error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 346 | &nimaps, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | if (error) { |
| 348 | return error; |
| 349 | } |
| 350 | ASSERT(nimaps > 0); |
| 351 | /* |
| 352 | * If the block underlying isize is just a hole, then there |
| 353 | * is nothing to zero. |
| 354 | */ |
| 355 | if (imap.br_startblock == HOLESTARTBLOCK) { |
| 356 | return 0; |
| 357 | } |
| 358 | /* |
| 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. |
| 362 | */ |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 363 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 364 | |
Eric Sandeen | 24ee808 | 2006-01-11 15:34:32 +1100 | [diff] [blame] | 365 | zero_len = mp->m_sb.sb_blocksize - zero_offset; |
Lachlan McIlroy | 6816016 | 2007-02-10 18:36:47 +1100 | [diff] [blame] | 366 | if (isize + zero_len > offset) |
| 367 | zero_len = offset - isize; |
| 368 | error = xfs_iozero(ip, isize, zero_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 370 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | ASSERT(error >= 0); |
| 372 | return error; |
| 373 | } |
| 374 | |
| 375 | /* |
| 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. |
| 384 | */ |
| 385 | |
| 386 | int /* error (positive) */ |
| 387 | xfs_zero_eof( |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 388 | xfs_inode_t *ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | xfs_off_t offset, /* starting I/O offset */ |
Lachlan McIlroy | 6816016 | 2007-02-10 18:36:47 +1100 | [diff] [blame] | 390 | xfs_fsize_t isize) /* current inode size */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | { |
Christoph Hellwig | 613d704 | 2007-10-11 17:44:08 +1000 | [diff] [blame] | 392 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | xfs_fileoff_t start_zero_fsb; |
| 394 | xfs_fileoff_t end_zero_fsb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | xfs_fileoff_t zero_count_fsb; |
| 396 | xfs_fileoff_t last_fsb; |
Lachlan McIlroy | 6816016 | 2007-02-10 18:36:47 +1100 | [diff] [blame] | 397 | xfs_fileoff_t zero_off; |
| 398 | xfs_fsize_t zero_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | int nimaps; |
| 400 | int error = 0; |
| 401 | xfs_bmbt_irec_t imap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 403 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL)); |
Eric Sandeen | 24ee808 | 2006-01-11 15:34:32 +1100 | [diff] [blame] | 404 | ASSERT(offset > isize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | /* |
| 407 | * First handle zeroing the block on which isize resides. |
| 408 | * We only zero a part of that block so it is handled specially. |
| 409 | */ |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 410 | error = xfs_zero_last_block(ip, offset, isize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | if (error) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 412 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | return error; |
| 414 | } |
| 415 | |
| 416 | /* |
| 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. |
| 423 | */ |
| 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) { |
| 429 | /* |
| 430 | * The size was only incremented on its last block. |
| 431 | * We took care of that above, so just return. |
| 432 | */ |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | ASSERT(start_zero_fsb <= end_zero_fsb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | while (start_zero_fsb <= end_zero_fsb) { |
| 438 | nimaps = 1; |
| 439 | zero_count_fsb = end_zero_fsb - start_zero_fsb + 1; |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 440 | error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 441 | 0, NULL, 0, &imap, &nimaps, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | if (error) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 443 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | return error; |
| 445 | } |
| 446 | ASSERT(nimaps > 0); |
| 447 | |
| 448 | if (imap.br_state == XFS_EXT_UNWRITTEN || |
| 449 | imap.br_startblock == HOLESTARTBLOCK) { |
| 450 | /* |
| 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. |
| 456 | */ |
Eric Sandeen | 24ee808 | 2006-01-11 15:34:32 +1100 | [diff] [blame] | 457 | start_zero_fsb = imap.br_startoff + imap.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | ASSERT(start_zero_fsb <= (end_zero_fsb + 1)); |
| 459 | continue; |
| 460 | } |
| 461 | |
| 462 | /* |
Nathan Scott | 3d80ede | 2006-06-09 14:57:30 +1000 | [diff] [blame] | 463 | * There are blocks we need to zero. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | * Drop the inode lock while we're doing the I/O. |
| 465 | * We'll still have the iolock to protect us. |
| 466 | */ |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 467 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
Lachlan McIlroy | 6816016 | 2007-02-10 18:36:47 +1100 | [diff] [blame] | 469 | zero_off = XFS_FSB_TO_B(mp, start_zero_fsb); |
| 470 | zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount); |
| 471 | |
| 472 | if ((zero_off + zero_len) > offset) |
| 473 | zero_len = offset - zero_off; |
| 474 | |
| 475 | error = xfs_iozero(ip, zero_off, zero_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | if (error) { |
| 477 | goto out_lock; |
| 478 | } |
| 479 | |
Nathan Scott | 3d80ede | 2006-06-09 14:57:30 +1000 | [diff] [blame] | 480 | start_zero_fsb = imap.br_startoff + imap.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | ASSERT(start_zero_fsb <= (end_zero_fsb + 1)); |
| 482 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 483 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | return 0; |
| 487 | |
| 488 | out_lock: |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 489 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | ASSERT(error >= 0); |
| 491 | return error; |
| 492 | } |
| 493 | |
| 494 | ssize_t /* bytes written, or (-) error */ |
| 495 | xfs_write( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 496 | struct xfs_inode *xip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | struct kiocb *iocb, |
| 498 | const struct iovec *iovp, |
| 499 | unsigned int nsegs, |
| 500 | loff_t *offset, |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 501 | int ioflags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | { |
| 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | xfs_mount_t *mp; |
| 508 | ssize_t ret = 0, error = 0; |
| 509 | xfs_fsize_t isize, new_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | int iolock; |
| 511 | int eventsent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | size_t ocount = 0, count; |
| 513 | loff_t pos; |
Lachlan McIlroy | 2a32963 | 2007-05-08 13:49:39 +1000 | [diff] [blame] | 514 | int need_i_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
| 516 | XFS_STATS_INC(xs_write_calls); |
| 517 | |
Dmitriy Monakhov | 0ceb331 | 2007-05-08 00:23:02 -0700 | [diff] [blame] | 518 | error = generic_segment_checks(iovp, &segs, &ocount, VERIFY_READ); |
| 519 | if (error) |
| 520 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
| 522 | count = ocount; |
| 523 | pos = *offset; |
| 524 | |
| 525 | if (count == 0) |
| 526 | return 0; |
| 527 | |
Christoph Hellwig | 613d704 | 2007-10-11 17:44:08 +1000 | [diff] [blame] | 528 | mp = xip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
Christoph Hellwig | b267ce9 | 2007-08-30 17:21:30 +1000 | [diff] [blame] | 530 | xfs_wait_for_freeze(mp, SB_FREEZE_WRITE); |
Nathan Scott | 34327e1 | 2006-06-09 17:11:55 +1000 | [diff] [blame] | 531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 533 | return -EIO; |
| 534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | relock: |
Lachlan McIlroy | 2a32963 | 2007-05-08 13:49:39 +1000 | [diff] [blame] | 536 | if (ioflags & IO_ISDIRECT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | iolock = XFS_IOLOCK_SHARED; |
Lachlan McIlroy | 2a32963 | 2007-05-08 13:49:39 +1000 | [diff] [blame] | 538 | need_i_mutex = 0; |
| 539 | } else { |
| 540 | iolock = XFS_IOLOCK_EXCL; |
Lachlan McIlroy | 2a32963 | 2007-05-08 13:49:39 +1000 | [diff] [blame] | 541 | need_i_mutex = 1; |
| 542 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | xfs_ilock(xip, XFS_ILOCK_EXCL|iolock); |
| 546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | start: |
| 548 | error = -generic_write_checks(file, &pos, &count, |
| 549 | S_ISBLK(inode->i_mode)); |
| 550 | if (error) { |
| 551 | xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock); |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 552 | goto out_unlock_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 555 | if ((DM_EVENT_ENABLED(xip, DM_EVENT_WRITE) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | !(ioflags & IO_INVIS) && !eventsent)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | int dmflags = FILP_DELAY_FLAG(file); |
| 558 | |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 559 | if (need_i_mutex) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 560 | dmflags |= DM_FLAGS_IMUX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
| 562 | xfs_iunlock(xip, XFS_ILOCK_EXCL); |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 563 | error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, xip, |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 564 | pos, count, dmflags, &iolock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | if (error) { |
Lachlan McIlroy | 71dfd5a | 2007-05-08 13:50:12 +1000 | [diff] [blame] | 566 | goto out_unlock_internal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
| 568 | xfs_ilock(xip, XFS_ILOCK_EXCL); |
| 569 | eventsent = 1; |
| 570 | |
| 571 | /* |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 572 | * The iolock was dropped and reacquired in XFS_SEND_DATA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
| 577 | */ |
Lachlan McIlroy | 71dfd5a | 2007-05-08 13:50:12 +1000 | [diff] [blame] | 578 | if ((file->f_flags & O_APPEND) && pos != xip->i_size) |
| 579 | goto start; |
| 580 | } |
| 581 | |
Lachlan McIlroy | 2a32963 | 2007-05-08 13:49:39 +1000 | [diff] [blame] | 582 | if (ioflags & IO_ISDIRECT) { |
| 583 | xfs_buftarg_t *target = |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 584 | XFS_IS_REALTIME_INODE(xip) ? |
Lachlan McIlroy | 2a32963 | 2007-05-08 13:49:39 +1000 | [diff] [blame] | 585 | mp->m_rtdev_targp : mp->m_ddev_targp; |
| 586 | |
| 587 | if ((pos & target->bt_smask) || (count & target->bt_smask)) { |
| 588 | xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock); |
| 589 | return XFS_ERROR(-EINVAL); |
| 590 | } |
| 591 | |
Christoph Hellwig | dcf49cc | 2008-03-06 13:46:37 +1100 | [diff] [blame] | 592 | if (!need_i_mutex && (mapping->nrpages || pos > xip->i_size)) { |
Lachlan McIlroy | 2a32963 | 2007-05-08 13:49:39 +1000 | [diff] [blame] | 593 | xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock); |
| 594 | iolock = XFS_IOLOCK_EXCL; |
Lachlan McIlroy | 2a32963 | 2007-05-08 13:49:39 +1000 | [diff] [blame] | 595 | need_i_mutex = 1; |
| 596 | mutex_lock(&inode->i_mutex); |
| 597 | xfs_ilock(xip, XFS_ILOCK_EXCL|iolock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | goto start; |
| 599 | } |
| 600 | } |
| 601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | new_size = pos + count; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 603 | if (new_size > xip->i_size) |
Christoph Hellwig | 613d704 | 2007-10-11 17:44:08 +1000 | [diff] [blame] | 604 | xip->i_new_size = new_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
Christoph Hellwig | 2234d54 | 2008-12-03 12:20:22 +0100 | [diff] [blame] | 606 | if (likely(!(ioflags & IO_INVIS))) |
Christoph Hellwig | f9581b1 | 2009-10-06 20:29:26 +0000 | [diff] [blame] | 607 | file_update_time(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
| 609 | /* |
| 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 ... |
| 613 | * |
| 614 | * If there is a page where the previous size lands, we need |
| 615 | * to zero it out up to the new size. |
| 616 | */ |
| 617 | |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 618 | if (pos > xip->i_size) { |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 619 | error = xfs_zero_eof(xip, pos, xip->i_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | if (error) { |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 621 | xfs_iunlock(xip, XFS_ILOCK_EXCL); |
| 622 | goto out_unlock_internal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | xfs_iunlock(xip, XFS_ILOCK_EXCL); |
| 626 | |
| 627 | /* |
| 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 |
| 631 | * setgid binaries. |
| 632 | */ |
Christoph Hellwig | 8718551 | 2010-02-03 19:43:31 +0000 | [diff] [blame^] | 633 | error = -file_remove_suid(file); |
| 634 | if (unlikely(error)) |
| 635 | goto out_unlock_internal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | /* We can write back this queue in page reclaim */ |
| 638 | current->backing_dev_info = mapping->backing_dev_info; |
| 639 | |
| 640 | if ((ioflags & IO_ISDIRECT)) { |
Christoph Hellwig | dcf49cc | 2008-03-06 13:46:37 +1100 | [diff] [blame] | 641 | if (mapping->nrpages) { |
Lachlan McIlroy | 2a32963 | 2007-05-08 13:49:39 +1000 | [diff] [blame] | 642 | WARN_ON(need_i_mutex == 0); |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 643 | error = xfs_flushinval_pages(xip, |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 644 | (pos & PAGE_CACHE_MASK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | -1, FI_REMAPF_LOCKED); |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 646 | if (error) |
| 647 | goto out_unlock_internal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 650 | if (need_i_mutex) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | /* demote the lock now the cached pages are gone */ |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 652 | xfs_ilock_demote(xip, XFS_IOLOCK_EXCL); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 653 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
| 655 | iolock = XFS_IOLOCK_SHARED; |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 656 | need_i_mutex = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 659 | trace_xfs_file_direct_write(xip, count, *offset, ioflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | ret = generic_file_direct_write(iocb, iovp, |
| 661 | &segs, pos, offset, count, ocount); |
| 662 | |
| 663 | /* |
| 664 | * direct-io write to a hole: fall through to buffered I/O |
| 665 | * for completing the rest of the request. |
| 666 | */ |
| 667 | if (ret >= 0 && ret != count) { |
| 668 | XFS_STATS_ADD(xs_write_bytes, ret); |
| 669 | |
| 670 | pos += ret; |
| 671 | count -= ret; |
| 672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | ioflags &= ~IO_ISDIRECT; |
| 674 | xfs_iunlock(xip, iolock); |
| 675 | goto relock; |
| 676 | } |
| 677 | } else { |
Dave Chinner | 5825294 | 2009-04-06 18:45:44 +0200 | [diff] [blame] | 678 | int enospc = 0; |
| 679 | ssize_t ret2 = 0; |
| 680 | |
| 681 | write_retry: |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 682 | trace_xfs_file_buffered_write(xip, count, *offset, ioflags); |
Dave Chinner | 5825294 | 2009-04-06 18:45:44 +0200 | [diff] [blame] | 683 | ret2 = generic_file_buffered_write(iocb, iovp, segs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | pos, offset, count, ret); |
Dave Chinner | 5825294 | 2009-04-06 18:45:44 +0200 | [diff] [blame] | 685 | /* |
| 686 | * if we just got an ENOSPC, flush the inode now we |
| 687 | * aren't holding any page locks and retry *once* |
| 688 | */ |
| 689 | if (ret2 == -ENOSPC && !enospc) { |
| 690 | error = xfs_flush_pages(xip, 0, -1, 0, FI_NONE); |
| 691 | if (error) |
| 692 | goto out_unlock_internal; |
| 693 | enospc = 1; |
| 694 | goto write_retry; |
| 695 | } |
| 696 | ret = ret2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | current->backing_dev_info = NULL; |
| 700 | |
Lachlan McIlroy | 2505115 | 2008-12-24 14:07:32 +1100 | [diff] [blame] | 701 | isize = i_size_read(inode); |
| 702 | if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize)) |
| 703 | *offset = isize; |
| 704 | |
| 705 | if (*offset > xip->i_size) { |
| 706 | xfs_ilock(xip, XFS_ILOCK_EXCL); |
| 707 | if (*offset > xip->i_size) |
| 708 | xip->i_size = *offset; |
| 709 | xfs_iunlock(xip, XFS_ILOCK_EXCL); |
| 710 | } |
| 711 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 712 | if (ret == -ENOSPC && |
| 713 | DM_EVENT_ENABLED(xip, DM_EVENT_NOSPACE) && !(ioflags & IO_INVIS)) { |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 714 | xfs_iunlock(xip, iolock); |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 715 | if (need_i_mutex) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 716 | mutex_unlock(&inode->i_mutex); |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 717 | error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, xip, |
| 718 | DM_RIGHT_NULL, xip, DM_RIGHT_NULL, NULL, NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | 0, 0, 0); /* Delay flag intentionally unused */ |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 720 | if (need_i_mutex) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 721 | mutex_lock(&inode->i_mutex); |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 722 | xfs_ilock(xip, iolock); |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 723 | if (error) |
| 724 | goto out_unlock_internal; |
Lachlan McIlroy | 2505115 | 2008-12-24 14:07:32 +1100 | [diff] [blame] | 725 | goto start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | error = -ret; |
| 729 | if (ret <= 0) |
| 730 | goto out_unlock_internal; |
| 731 | |
| 732 | XFS_STATS_ADD(xs_write_bytes, ret); |
| 733 | |
| 734 | /* Handle various SYNC-type writes */ |
Christoph Hellwig | 6b2f3d1 | 2009-10-27 11:05:28 +0100 | [diff] [blame] | 735 | if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) { |
Christoph Hellwig | 13e6d5c | 2009-08-31 21:00:31 -0300 | [diff] [blame] | 736 | loff_t end = pos + ret - 1; |
Lachlan McIlroy | 5903c49 | 2007-09-14 15:22:08 +1000 | [diff] [blame] | 737 | int error2; |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 738 | |
| 739 | xfs_iunlock(xip, iolock); |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 740 | if (need_i_mutex) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 741 | mutex_unlock(&inode->i_mutex); |
Christoph Hellwig | 13e6d5c | 2009-08-31 21:00:31 -0300 | [diff] [blame] | 742 | |
| 743 | error2 = filemap_write_and_wait_range(mapping, pos, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | if (!error) |
Lachlan McIlroy | 5903c49 | 2007-09-14 15:22:08 +1000 | [diff] [blame] | 745 | error = error2; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 746 | if (need_i_mutex) |
| 747 | mutex_lock(&inode->i_mutex); |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 748 | xfs_ilock(xip, iolock); |
Christoph Hellwig | 13e6d5c | 2009-08-31 21:00:31 -0300 | [diff] [blame] | 749 | |
| 750 | error2 = xfs_fsync(xip); |
Lachlan McIlroy | 5903c49 | 2007-09-14 15:22:08 +1000 | [diff] [blame] | 751 | if (!error) |
| 752 | error = error2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | out_unlock_internal: |
Christoph Hellwig | 613d704 | 2007-10-11 17:44:08 +1000 | [diff] [blame] | 756 | if (xip->i_new_size) { |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 757 | xfs_ilock(xip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 613d704 | 2007-10-11 17:44:08 +1000 | [diff] [blame] | 758 | xip->i_new_size = 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 759 | /* |
| 760 | * If this was a direct or synchronous I/O that failed (such |
| 761 | * as ENOSPC) then part of the I/O may have been written to |
| 762 | * disk before the error occured. In this case the on-disk |
| 763 | * file size may have been adjusted beyond the in-memory file |
| 764 | * size and now needs to be truncated back. |
| 765 | */ |
| 766 | if (xip->i_d.di_size > xip->i_size) |
| 767 | xip->i_d.di_size = xip->i_size; |
| 768 | xfs_iunlock(xip, XFS_ILOCK_EXCL); |
| 769 | } |
Christoph Hellwig | 126468b | 2008-03-06 13:44:57 +1100 | [diff] [blame] | 770 | xfs_iunlock(xip, iolock); |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 771 | out_unlock_mutex: |
| 772 | if (need_i_mutex) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 773 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | return -error; |
| 775 | } |
| 776 | |
| 777 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | * If the underlying (data/log/rt) device is readonly, there are some |
| 779 | * operations that cannot proceed. |
| 780 | */ |
| 781 | int |
| 782 | xfs_dev_is_read_only( |
| 783 | xfs_mount_t *mp, |
| 784 | char *message) |
| 785 | { |
| 786 | if (xfs_readonly_buftarg(mp->m_ddev_targp) || |
| 787 | xfs_readonly_buftarg(mp->m_logdev_targp) || |
| 788 | (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) { |
| 789 | cmn_err(CE_NOTE, |
| 790 | "XFS: %s required on read-only device.", message); |
| 791 | cmn_err(CE_NOTE, |
| 792 | "XFS: write access unavailable, cannot proceed."); |
| 793 | return EROFS; |
| 794 | } |
| 795 | return 0; |
| 796 | } |