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-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" |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [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_sb.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_mount.h" |
| 27 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_alloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "xfs_dinode.h" |
| 30 | #include "xfs_inode.h" |
Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 31 | #include "xfs_inode_item.h" |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 32 | #include "xfs_bmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_error.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 34 | #include "xfs_vnodeops.h" |
Christoph Hellwig | f999a5b | 2008-11-28 14:23:32 +1100 | [diff] [blame] | 35 | #include "xfs_da_btree.h" |
Christoph Hellwig | ddcd856 | 2008-12-03 07:55:34 -0500 | [diff] [blame] | 36 | #include "xfs_ioctl.h" |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 37 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | #include <linux/dcache.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 41 | static const struct vm_operations_struct xfs_file_vm_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 43 | /* |
| 44 | * xfs_iozero |
| 45 | * |
| 46 | * xfs_iozero clears the specified range of buffer supplied, |
| 47 | * and marks all the affected blocks as valid and modified. If |
| 48 | * an affected block is not allocated, it will be allocated. If |
| 49 | * an affected block is not completely overwritten, and is not |
| 50 | * valid before the operation, it will be read from disk before |
| 51 | * being partially zeroed. |
| 52 | */ |
| 53 | STATIC int |
| 54 | xfs_iozero( |
| 55 | struct xfs_inode *ip, /* inode */ |
| 56 | loff_t pos, /* offset in file */ |
| 57 | size_t count) /* size of data to zero */ |
| 58 | { |
| 59 | struct page *page; |
| 60 | struct address_space *mapping; |
| 61 | int status; |
| 62 | |
| 63 | mapping = VFS_I(ip)->i_mapping; |
| 64 | do { |
| 65 | unsigned offset, bytes; |
| 66 | void *fsdata; |
| 67 | |
| 68 | offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */ |
| 69 | bytes = PAGE_CACHE_SIZE - offset; |
| 70 | if (bytes > count) |
| 71 | bytes = count; |
| 72 | |
| 73 | status = pagecache_write_begin(NULL, mapping, pos, bytes, |
| 74 | AOP_FLAG_UNINTERRUPTIBLE, |
| 75 | &page, &fsdata); |
| 76 | if (status) |
| 77 | break; |
| 78 | |
| 79 | zero_user(page, offset, bytes); |
| 80 | |
| 81 | status = pagecache_write_end(NULL, mapping, pos, bytes, bytes, |
| 82 | page, fsdata); |
| 83 | WARN_ON(status <= 0); /* can't return less than zero! */ |
| 84 | pos += bytes; |
| 85 | count -= bytes; |
| 86 | status = 0; |
| 87 | } while (count); |
| 88 | |
| 89 | return (-status); |
| 90 | } |
| 91 | |
Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 92 | STATIC int |
| 93 | xfs_file_fsync( |
| 94 | struct file *file, |
Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 95 | int datasync) |
| 96 | { |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 97 | struct inode *inode = file->f_mapping->host; |
| 98 | struct xfs_inode *ip = XFS_I(inode); |
Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 99 | struct xfs_trans *tp; |
| 100 | int error = 0; |
| 101 | int log_flushed = 0; |
| 102 | |
Christoph Hellwig | cca28fb | 2010-06-24 11:57:09 +1000 | [diff] [blame] | 103 | trace_xfs_file_fsync(ip); |
Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 104 | |
| 105 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 106 | return -XFS_ERROR(EIO); |
| 107 | |
| 108 | xfs_iflags_clear(ip, XFS_ITRUNCATED); |
| 109 | |
Christoph Hellwig | 37bc574 | 2010-04-20 17:00:59 +1000 | [diff] [blame] | 110 | xfs_ioend_wait(ip); |
| 111 | |
Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 112 | /* |
| 113 | * We always need to make sure that the required inode state is safe on |
| 114 | * disk. The inode might be clean but we still might need to force the |
| 115 | * log because of committed transactions that haven't hit the disk yet. |
| 116 | * Likewise, there could be unflushed non-transactional changes to the |
| 117 | * inode core that have to go to disk and this requires us to issue |
| 118 | * a synchronous transaction to capture these changes correctly. |
| 119 | * |
| 120 | * This code relies on the assumption that if the i_update_core field |
| 121 | * of the inode is clear and the inode is unpinned then it is clean |
| 122 | * and no action is required. |
| 123 | */ |
| 124 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
| 125 | |
Christoph Hellwig | 66d834e | 2010-02-15 09:44:49 +0000 | [diff] [blame] | 126 | /* |
| 127 | * First check if the VFS inode is marked dirty. All the dirtying |
| 128 | * of non-transactional updates no goes through mark_inode_dirty*, |
| 129 | * which allows us to distinguish beteeen pure timestamp updates |
| 130 | * and i_size updates which need to be caught for fdatasync. |
| 131 | * After that also theck for the dirty state in the XFS inode, which |
| 132 | * might gets cleared when the inode gets written out via the AIL |
| 133 | * or xfs_iflush_cluster. |
| 134 | */ |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 135 | if (((inode->i_state & I_DIRTY_DATASYNC) || |
| 136 | ((inode->i_state & I_DIRTY_SYNC) && !datasync)) && |
Christoph Hellwig | 66d834e | 2010-02-15 09:44:49 +0000 | [diff] [blame] | 137 | ip->i_update_core) { |
Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 138 | /* |
| 139 | * Kick off a transaction to log the inode core to get the |
| 140 | * updates. The sync transaction will also force the log. |
| 141 | */ |
| 142 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 143 | tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS); |
| 144 | error = xfs_trans_reserve(tp, 0, |
| 145 | XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0); |
| 146 | if (error) { |
| 147 | xfs_trans_cancel(tp, 0); |
| 148 | return -error; |
| 149 | } |
| 150 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 151 | |
| 152 | /* |
| 153 | * Note - it's possible that we might have pushed ourselves out |
| 154 | * of the way during trans_reserve which would flush the inode. |
| 155 | * But there's no guarantee that the inode buffer has actually |
| 156 | * gone out yet (it's delwri). Plus the buffer could be pinned |
| 157 | * anyway if it's part of an inode in another recent |
| 158 | * transaction. So we play it safe and fire off the |
| 159 | * transaction anyway. |
| 160 | */ |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 161 | xfs_trans_ijoin(tp, ip); |
Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 162 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 163 | xfs_trans_set_sync(tp); |
| 164 | error = _xfs_trans_commit(tp, 0, &log_flushed); |
| 165 | |
| 166 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 167 | } else { |
| 168 | /* |
| 169 | * Timestamps/size haven't changed since last inode flush or |
| 170 | * inode transaction commit. That means either nothing got |
| 171 | * written or a transaction committed which caught the updates. |
| 172 | * If the latter happened and the transaction hasn't hit the |
| 173 | * disk yet, the inode will be still be pinned. If it is, |
| 174 | * force the log. |
| 175 | */ |
Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 176 | if (xfs_ipincount(ip)) { |
Christoph Hellwig | 024910c | 2010-02-17 19:34:57 +0000 | [diff] [blame] | 177 | error = _xfs_log_force_lsn(ip->i_mount, |
| 178 | ip->i_itemp->ili_last_lsn, |
| 179 | XFS_LOG_SYNC, &log_flushed); |
Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 180 | } |
Christoph Hellwig | 024910c | 2010-02-17 19:34:57 +0000 | [diff] [blame] | 181 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | if (ip->i_mount->m_flags & XFS_MOUNT_BARRIER) { |
| 185 | /* |
| 186 | * If the log write didn't issue an ordered tag we need |
| 187 | * to flush the disk cache for the data device now. |
| 188 | */ |
| 189 | if (!log_flushed) |
| 190 | xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp); |
| 191 | |
| 192 | /* |
| 193 | * If this inode is on the RT dev we need to flush that |
| 194 | * cache as well. |
| 195 | */ |
| 196 | if (XFS_IS_REALTIME_INODE(ip)) |
| 197 | xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp); |
| 198 | } |
| 199 | |
| 200 | return -error; |
| 201 | } |
| 202 | |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 203 | STATIC ssize_t |
| 204 | xfs_file_aio_read( |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 205 | struct kiocb *iocb, |
| 206 | const struct iovec *iovp, |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 207 | unsigned long nr_segs, |
| 208 | loff_t pos) |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 209 | { |
| 210 | struct file *file = iocb->ki_filp; |
| 211 | struct inode *inode = file->f_mapping->host; |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 212 | struct xfs_inode *ip = XFS_I(inode); |
| 213 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 214 | size_t size = 0; |
| 215 | ssize_t ret = 0; |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 216 | int ioflags = 0; |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 217 | xfs_fsize_t n; |
| 218 | unsigned long seg; |
| 219 | |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 220 | XFS_STATS_INC(xs_read_calls); |
| 221 | |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 222 | BUG_ON(iocb->ki_pos != pos); |
| 223 | |
| 224 | if (unlikely(file->f_flags & O_DIRECT)) |
| 225 | ioflags |= IO_ISDIRECT; |
| 226 | if (file->f_mode & FMODE_NOCMTIME) |
| 227 | ioflags |= IO_INVIS; |
| 228 | |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 229 | /* START copy & waste from filemap.c */ |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 230 | for (seg = 0; seg < nr_segs; seg++) { |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 231 | const struct iovec *iv = &iovp[seg]; |
| 232 | |
| 233 | /* |
| 234 | * If any segment has a negative length, or the cumulative |
| 235 | * length ever wraps negative then return -EINVAL. |
| 236 | */ |
| 237 | size += iv->iov_len; |
| 238 | if (unlikely((ssize_t)(size|iv->iov_len) < 0)) |
| 239 | return XFS_ERROR(-EINVAL); |
| 240 | } |
| 241 | /* END copy & waste from filemap.c */ |
| 242 | |
| 243 | if (unlikely(ioflags & IO_ISDIRECT)) { |
| 244 | xfs_buftarg_t *target = |
| 245 | XFS_IS_REALTIME_INODE(ip) ? |
| 246 | mp->m_rtdev_targp : mp->m_ddev_targp; |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 247 | if ((iocb->ki_pos & target->bt_smask) || |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 248 | (size & target->bt_smask)) { |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 249 | if (iocb->ki_pos == ip->i_size) |
| 250 | return 0; |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 251 | return -XFS_ERROR(EINVAL); |
| 252 | } |
| 253 | } |
| 254 | |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 255 | n = XFS_MAXIOFFSET(mp) - iocb->ki_pos; |
| 256 | if (n <= 0 || size == 0) |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 257 | return 0; |
| 258 | |
| 259 | if (n < size) |
| 260 | size = n; |
| 261 | |
| 262 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 263 | return -EIO; |
| 264 | |
| 265 | if (unlikely(ioflags & IO_ISDIRECT)) |
| 266 | mutex_lock(&inode->i_mutex); |
| 267 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
| 268 | |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 269 | if (unlikely(ioflags & IO_ISDIRECT)) { |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 270 | if (inode->i_mapping->nrpages) { |
| 271 | ret = -xfs_flushinval_pages(ip, |
| 272 | (iocb->ki_pos & PAGE_CACHE_MASK), |
| 273 | -1, FI_REMAPF_LOCKED); |
| 274 | } |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 275 | mutex_unlock(&inode->i_mutex); |
| 276 | if (ret) { |
| 277 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
| 278 | return ret; |
| 279 | } |
| 280 | } |
| 281 | |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 282 | trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 283 | |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 284 | ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 285 | if (ret > 0) |
| 286 | XFS_STATS_ADD(xs_read_bytes, ret); |
| 287 | |
| 288 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
| 289 | return ret; |
| 290 | } |
| 291 | |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 292 | STATIC ssize_t |
| 293 | xfs_file_splice_read( |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 294 | struct file *infilp, |
| 295 | loff_t *ppos, |
| 296 | struct pipe_inode_info *pipe, |
| 297 | size_t count, |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 298 | unsigned int flags) |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 299 | { |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 300 | struct xfs_inode *ip = XFS_I(infilp->f_mapping->host); |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 301 | int ioflags = 0; |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 302 | ssize_t ret; |
| 303 | |
| 304 | XFS_STATS_INC(xs_read_calls); |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 305 | |
| 306 | if (infilp->f_mode & FMODE_NOCMTIME) |
| 307 | ioflags |= IO_INVIS; |
| 308 | |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 309 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 310 | return -EIO; |
| 311 | |
| 312 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
| 313 | |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 314 | trace_xfs_file_splice_read(ip, count, *ppos, ioflags); |
| 315 | |
| 316 | ret = generic_file_splice_read(infilp, ppos, pipe, count, flags); |
| 317 | if (ret > 0) |
| 318 | XFS_STATS_ADD(xs_read_bytes, ret); |
| 319 | |
| 320 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
| 321 | return ret; |
| 322 | } |
| 323 | |
Dave Chinner | edafb6d | 2011-01-11 10:14:06 +1100 | [diff] [blame] | 324 | STATIC void |
| 325 | xfs_aio_write_isize_update( |
| 326 | struct inode *inode, |
| 327 | loff_t *ppos, |
| 328 | ssize_t bytes_written) |
| 329 | { |
| 330 | struct xfs_inode *ip = XFS_I(inode); |
| 331 | xfs_fsize_t isize = i_size_read(inode); |
| 332 | |
| 333 | if (bytes_written > 0) |
| 334 | XFS_STATS_ADD(xs_write_bytes, bytes_written); |
| 335 | |
| 336 | if (unlikely(bytes_written < 0 && bytes_written != -EFAULT && |
| 337 | *ppos > isize)) |
| 338 | *ppos = isize; |
| 339 | |
| 340 | if (*ppos > ip->i_size) { |
| 341 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 342 | if (*ppos > ip->i_size) |
| 343 | ip->i_size = *ppos; |
| 344 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 345 | } |
| 346 | } |
| 347 | |
Dave Chinner | 4c5cfd1 | 2011-01-11 10:14:16 +1100 | [diff] [blame^] | 348 | /* |
| 349 | * If this was a direct or synchronous I/O that failed (such as ENOSPC) then |
| 350 | * part of the I/O may have been written to disk before the error occured. In |
| 351 | * this case the on-disk file size may have been adjusted beyond the in-memory |
| 352 | * file size and now needs to be truncated back. |
| 353 | */ |
| 354 | STATIC void |
| 355 | xfs_aio_write_newsize_update( |
| 356 | struct xfs_inode *ip) |
| 357 | { |
| 358 | if (ip->i_new_size) { |
| 359 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 360 | ip->i_new_size = 0; |
| 361 | if (ip->i_d.di_size > ip->i_size) |
| 362 | ip->i_d.di_size = ip->i_size; |
| 363 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 364 | } |
| 365 | } |
| 366 | |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 367 | STATIC ssize_t |
| 368 | xfs_file_splice_write( |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 369 | struct pipe_inode_info *pipe, |
| 370 | struct file *outfilp, |
| 371 | loff_t *ppos, |
| 372 | size_t count, |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 373 | unsigned int flags) |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 374 | { |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 375 | struct inode *inode = outfilp->f_mapping->host; |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 376 | struct xfs_inode *ip = XFS_I(inode); |
Dave Chinner | edafb6d | 2011-01-11 10:14:06 +1100 | [diff] [blame] | 377 | xfs_fsize_t new_size; |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 378 | int ioflags = 0; |
| 379 | ssize_t ret; |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 380 | |
| 381 | XFS_STATS_INC(xs_write_calls); |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 382 | |
| 383 | if (outfilp->f_mode & FMODE_NOCMTIME) |
| 384 | ioflags |= IO_INVIS; |
| 385 | |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 386 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 387 | return -EIO; |
| 388 | |
| 389 | xfs_ilock(ip, XFS_IOLOCK_EXCL); |
| 390 | |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 391 | new_size = *ppos + count; |
| 392 | |
| 393 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 394 | if (new_size > ip->i_size) |
| 395 | ip->i_new_size = new_size; |
| 396 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 397 | |
| 398 | trace_xfs_file_splice_write(ip, count, *ppos, ioflags); |
| 399 | |
| 400 | ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 401 | |
Dave Chinner | edafb6d | 2011-01-11 10:14:06 +1100 | [diff] [blame] | 402 | xfs_aio_write_isize_update(inode, ppos, ret); |
Dave Chinner | 4c5cfd1 | 2011-01-11 10:14:16 +1100 | [diff] [blame^] | 403 | xfs_aio_write_newsize_update(ip); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 404 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 405 | return ret; |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * This routine is called to handle zeroing any space in the last |
| 410 | * block of the file that is beyond the EOF. We do this since the |
| 411 | * size is being increased without writing anything to that block |
| 412 | * and we don't want anyone to read the garbage on the disk. |
| 413 | */ |
| 414 | STATIC int /* error (positive) */ |
| 415 | xfs_zero_last_block( |
| 416 | xfs_inode_t *ip, |
| 417 | xfs_fsize_t offset, |
| 418 | xfs_fsize_t isize) |
| 419 | { |
| 420 | xfs_fileoff_t last_fsb; |
| 421 | xfs_mount_t *mp = ip->i_mount; |
| 422 | int nimaps; |
| 423 | int zero_offset; |
| 424 | int zero_len; |
| 425 | int error = 0; |
| 426 | xfs_bmbt_irec_t imap; |
| 427 | |
| 428 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 429 | |
| 430 | zero_offset = XFS_B_FSB_OFFSET(mp, isize); |
| 431 | if (zero_offset == 0) { |
| 432 | /* |
| 433 | * There are no extra bytes in the last block on disk to |
| 434 | * zero, so return. |
| 435 | */ |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | last_fsb = XFS_B_TO_FSBT(mp, isize); |
| 440 | nimaps = 1; |
| 441 | error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap, |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 442 | &nimaps, NULL); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 443 | if (error) { |
| 444 | return error; |
| 445 | } |
| 446 | ASSERT(nimaps > 0); |
| 447 | /* |
| 448 | * If the block underlying isize is just a hole, then there |
| 449 | * is nothing to zero. |
| 450 | */ |
| 451 | if (imap.br_startblock == HOLESTARTBLOCK) { |
| 452 | return 0; |
| 453 | } |
| 454 | /* |
| 455 | * Zero the part of the last block beyond the EOF, and write it |
| 456 | * out sync. We need to drop the ilock while we do this so we |
| 457 | * don't deadlock when the buffer cache calls back to us. |
| 458 | */ |
| 459 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 460 | |
| 461 | zero_len = mp->m_sb.sb_blocksize - zero_offset; |
| 462 | if (isize + zero_len > offset) |
| 463 | zero_len = offset - isize; |
| 464 | error = xfs_iozero(ip, isize, zero_len); |
| 465 | |
| 466 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 467 | ASSERT(error >= 0); |
| 468 | return error; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | * Zero any on disk space between the current EOF and the new, |
| 473 | * larger EOF. This handles the normal case of zeroing the remainder |
| 474 | * of the last block in the file and the unusual case of zeroing blocks |
| 475 | * out beyond the size of the file. This second case only happens |
| 476 | * with fixed size extents and when the system crashes before the inode |
| 477 | * size was updated but after blocks were allocated. If fill is set, |
| 478 | * then any holes in the range are filled and zeroed. If not, the holes |
| 479 | * are left alone as holes. |
| 480 | */ |
| 481 | |
| 482 | int /* error (positive) */ |
| 483 | xfs_zero_eof( |
| 484 | xfs_inode_t *ip, |
| 485 | xfs_off_t offset, /* starting I/O offset */ |
| 486 | xfs_fsize_t isize) /* current inode size */ |
| 487 | { |
| 488 | xfs_mount_t *mp = ip->i_mount; |
| 489 | xfs_fileoff_t start_zero_fsb; |
| 490 | xfs_fileoff_t end_zero_fsb; |
| 491 | xfs_fileoff_t zero_count_fsb; |
| 492 | xfs_fileoff_t last_fsb; |
| 493 | xfs_fileoff_t zero_off; |
| 494 | xfs_fsize_t zero_len; |
| 495 | int nimaps; |
| 496 | int error = 0; |
| 497 | xfs_bmbt_irec_t imap; |
| 498 | |
| 499 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL)); |
| 500 | ASSERT(offset > isize); |
| 501 | |
| 502 | /* |
| 503 | * First handle zeroing the block on which isize resides. |
| 504 | * We only zero a part of that block so it is handled specially. |
| 505 | */ |
| 506 | error = xfs_zero_last_block(ip, offset, isize); |
| 507 | if (error) { |
| 508 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL)); |
| 509 | return error; |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * Calculate the range between the new size and the old |
| 514 | * where blocks needing to be zeroed may exist. To get the |
| 515 | * block where the last byte in the file currently resides, |
| 516 | * we need to subtract one from the size and truncate back |
| 517 | * to a block boundary. We subtract 1 in case the size is |
| 518 | * exactly on a block boundary. |
| 519 | */ |
| 520 | last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1; |
| 521 | start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize); |
| 522 | end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1); |
| 523 | ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb); |
| 524 | if (last_fsb == end_zero_fsb) { |
| 525 | /* |
| 526 | * The size was only incremented on its last block. |
| 527 | * We took care of that above, so just return. |
| 528 | */ |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | ASSERT(start_zero_fsb <= end_zero_fsb); |
| 533 | while (start_zero_fsb <= end_zero_fsb) { |
| 534 | nimaps = 1; |
| 535 | zero_count_fsb = end_zero_fsb - start_zero_fsb + 1; |
| 536 | error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb, |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 537 | 0, NULL, 0, &imap, &nimaps, NULL); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 538 | if (error) { |
| 539 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL)); |
| 540 | return error; |
| 541 | } |
| 542 | ASSERT(nimaps > 0); |
| 543 | |
| 544 | if (imap.br_state == XFS_EXT_UNWRITTEN || |
| 545 | imap.br_startblock == HOLESTARTBLOCK) { |
| 546 | /* |
| 547 | * This loop handles initializing pages that were |
| 548 | * partially initialized by the code below this |
| 549 | * loop. It basically zeroes the part of the page |
| 550 | * that sits on a hole and sets the page as P_HOLE |
| 551 | * and calls remapf if it is a mapped file. |
| 552 | */ |
| 553 | start_zero_fsb = imap.br_startoff + imap.br_blockcount; |
| 554 | ASSERT(start_zero_fsb <= (end_zero_fsb + 1)); |
| 555 | continue; |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * There are blocks we need to zero. |
| 560 | * Drop the inode lock while we're doing the I/O. |
| 561 | * We'll still have the iolock to protect us. |
| 562 | */ |
| 563 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 564 | |
| 565 | zero_off = XFS_FSB_TO_B(mp, start_zero_fsb); |
| 566 | zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount); |
| 567 | |
| 568 | if ((zero_off + zero_len) > offset) |
| 569 | zero_len = offset - zero_off; |
| 570 | |
| 571 | error = xfs_iozero(ip, zero_off, zero_len); |
| 572 | if (error) { |
| 573 | goto out_lock; |
| 574 | } |
| 575 | |
| 576 | start_zero_fsb = imap.br_startoff + imap.br_blockcount; |
| 577 | ASSERT(start_zero_fsb <= (end_zero_fsb + 1)); |
| 578 | |
| 579 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 580 | } |
| 581 | |
| 582 | return 0; |
| 583 | |
| 584 | out_lock: |
| 585 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 586 | ASSERT(error >= 0); |
| 587 | return error; |
| 588 | } |
| 589 | |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 590 | STATIC ssize_t |
| 591 | xfs_file_aio_write( |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 592 | struct kiocb *iocb, |
| 593 | const struct iovec *iovp, |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 594 | unsigned long nr_segs, |
| 595 | loff_t pos) |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 596 | { |
| 597 | struct file *file = iocb->ki_filp; |
| 598 | struct address_space *mapping = file->f_mapping; |
| 599 | struct inode *inode = mapping->host; |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 600 | struct xfs_inode *ip = XFS_I(inode); |
| 601 | struct xfs_mount *mp = ip->i_mount; |
Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 602 | ssize_t ret = 0; |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 603 | int ioflags = 0; |
Dave Chinner | edafb6d | 2011-01-11 10:14:06 +1100 | [diff] [blame] | 604 | xfs_fsize_t new_size; |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 605 | int iolock; |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 606 | size_t ocount = 0, count; |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 607 | int need_i_mutex; |
| 608 | |
| 609 | XFS_STATS_INC(xs_write_calls); |
| 610 | |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 611 | BUG_ON(iocb->ki_pos != pos); |
| 612 | |
| 613 | if (unlikely(file->f_flags & O_DIRECT)) |
| 614 | ioflags |= IO_ISDIRECT; |
| 615 | if (file->f_mode & FMODE_NOCMTIME) |
| 616 | ioflags |= IO_INVIS; |
| 617 | |
Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 618 | ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ); |
| 619 | if (ret) |
| 620 | return ret; |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 621 | |
| 622 | count = ocount; |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 623 | if (count == 0) |
| 624 | return 0; |
| 625 | |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 626 | xfs_wait_for_freeze(mp, SB_FREEZE_WRITE); |
| 627 | |
| 628 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 629 | return -EIO; |
| 630 | |
| 631 | relock: |
| 632 | if (ioflags & IO_ISDIRECT) { |
| 633 | iolock = XFS_IOLOCK_SHARED; |
| 634 | need_i_mutex = 0; |
| 635 | } else { |
| 636 | iolock = XFS_IOLOCK_EXCL; |
| 637 | need_i_mutex = 1; |
| 638 | mutex_lock(&inode->i_mutex); |
| 639 | } |
| 640 | |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 641 | xfs_ilock(ip, XFS_ILOCK_EXCL|iolock); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 642 | |
| 643 | start: |
Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 644 | ret = generic_write_checks(file, &pos, &count, |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 645 | S_ISBLK(inode->i_mode)); |
Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 646 | if (ret) { |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 647 | xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 648 | goto out_unlock_mutex; |
| 649 | } |
| 650 | |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 651 | if (ioflags & IO_ISDIRECT) { |
| 652 | xfs_buftarg_t *target = |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 653 | XFS_IS_REALTIME_INODE(ip) ? |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 654 | mp->m_rtdev_targp : mp->m_ddev_targp; |
| 655 | |
| 656 | if ((pos & target->bt_smask) || (count & target->bt_smask)) { |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 657 | xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 658 | return XFS_ERROR(-EINVAL); |
| 659 | } |
| 660 | |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 661 | if (!need_i_mutex && (mapping->nrpages || pos > ip->i_size)) { |
| 662 | xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 663 | iolock = XFS_IOLOCK_EXCL; |
| 664 | need_i_mutex = 1; |
| 665 | mutex_lock(&inode->i_mutex); |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 666 | xfs_ilock(ip, XFS_ILOCK_EXCL|iolock); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 667 | goto start; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | new_size = pos + count; |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 672 | if (new_size > ip->i_size) |
| 673 | ip->i_new_size = new_size; |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 674 | |
| 675 | if (likely(!(ioflags & IO_INVIS))) |
| 676 | file_update_time(file); |
| 677 | |
| 678 | /* |
| 679 | * If the offset is beyond the size of the file, we have a couple |
| 680 | * of things to do. First, if there is already space allocated |
| 681 | * we need to either create holes or zero the disk or ... |
| 682 | * |
| 683 | * If there is a page where the previous size lands, we need |
| 684 | * to zero it out up to the new size. |
| 685 | */ |
| 686 | |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 687 | if (pos > ip->i_size) { |
Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 688 | ret = -xfs_zero_eof(ip, pos, ip->i_size); |
| 689 | if (ret) { |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 690 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 691 | goto out_unlock_internal; |
| 692 | } |
| 693 | } |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 694 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 695 | |
| 696 | /* |
| 697 | * If we're writing the file then make sure to clear the |
| 698 | * setuid and setgid bits if the process is not being run |
| 699 | * by root. This keeps people from modifying setuid and |
| 700 | * setgid binaries. |
| 701 | */ |
Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 702 | ret = file_remove_suid(file); |
| 703 | if (unlikely(ret)) |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 704 | goto out_unlock_internal; |
| 705 | |
| 706 | /* We can write back this queue in page reclaim */ |
| 707 | current->backing_dev_info = mapping->backing_dev_info; |
| 708 | |
| 709 | if ((ioflags & IO_ISDIRECT)) { |
| 710 | if (mapping->nrpages) { |
| 711 | WARN_ON(need_i_mutex == 0); |
Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 712 | ret = -xfs_flushinval_pages(ip, |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 713 | (pos & PAGE_CACHE_MASK), |
| 714 | -1, FI_REMAPF_LOCKED); |
Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 715 | if (ret) |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 716 | goto out_unlock_internal; |
| 717 | } |
| 718 | |
| 719 | if (need_i_mutex) { |
| 720 | /* demote the lock now the cached pages are gone */ |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 721 | xfs_ilock_demote(ip, XFS_IOLOCK_EXCL); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 722 | mutex_unlock(&inode->i_mutex); |
| 723 | |
| 724 | iolock = XFS_IOLOCK_SHARED; |
| 725 | need_i_mutex = 0; |
| 726 | } |
| 727 | |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 728 | trace_xfs_file_direct_write(ip, count, iocb->ki_pos, ioflags); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 729 | ret = generic_file_direct_write(iocb, iovp, |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 730 | &nr_segs, pos, &iocb->ki_pos, count, ocount); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 731 | |
| 732 | /* |
| 733 | * direct-io write to a hole: fall through to buffered I/O |
| 734 | * for completing the rest of the request. |
| 735 | */ |
| 736 | if (ret >= 0 && ret != count) { |
| 737 | XFS_STATS_ADD(xs_write_bytes, ret); |
| 738 | |
| 739 | pos += ret; |
| 740 | count -= ret; |
| 741 | |
| 742 | ioflags &= ~IO_ISDIRECT; |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 743 | xfs_iunlock(ip, iolock); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 744 | goto relock; |
| 745 | } |
| 746 | } else { |
| 747 | int enospc = 0; |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 748 | |
| 749 | write_retry: |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 750 | trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, ioflags); |
Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 751 | ret = generic_file_buffered_write(iocb, iovp, nr_segs, |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 752 | pos, &iocb->ki_pos, count, ret); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 753 | /* |
| 754 | * if we just got an ENOSPC, flush the inode now we |
| 755 | * aren't holding any page locks and retry *once* |
| 756 | */ |
Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 757 | if (ret == -ENOSPC && !enospc) { |
| 758 | ret = xfs_flush_pages(ip, 0, -1, 0, FI_NONE); |
| 759 | if (ret) |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 760 | goto out_unlock_internal; |
| 761 | enospc = 1; |
| 762 | goto write_retry; |
| 763 | } |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | current->backing_dev_info = NULL; |
| 767 | |
Dave Chinner | edafb6d | 2011-01-11 10:14:06 +1100 | [diff] [blame] | 768 | xfs_aio_write_isize_update(inode, &iocb->ki_pos, ret); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 769 | |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 770 | if (ret <= 0) |
| 771 | goto out_unlock_internal; |
| 772 | |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 773 | /* Handle various SYNC-type writes */ |
| 774 | if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) { |
| 775 | loff_t end = pos + ret - 1; |
Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 776 | int error, error2; |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 777 | |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 778 | xfs_iunlock(ip, iolock); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 779 | if (need_i_mutex) |
| 780 | mutex_unlock(&inode->i_mutex); |
| 781 | |
Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 782 | error = filemap_write_and_wait_range(mapping, pos, end); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 783 | if (need_i_mutex) |
| 784 | mutex_lock(&inode->i_mutex); |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 785 | xfs_ilock(ip, iolock); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 786 | |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 787 | error2 = -xfs_file_fsync(file, |
Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 788 | (file->f_flags & __O_SYNC) ? 0 : 1); |
Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 789 | if (error) |
| 790 | ret = error; |
| 791 | else if (error2) |
| 792 | ret = error2; |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | out_unlock_internal: |
Dave Chinner | 4c5cfd1 | 2011-01-11 10:14:16 +1100 | [diff] [blame^] | 796 | xfs_aio_write_newsize_update(ip); |
Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 797 | xfs_iunlock(ip, iolock); |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 798 | out_unlock_mutex: |
| 799 | if (need_i_mutex) |
| 800 | mutex_unlock(&inode->i_mutex); |
Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 801 | return ret; |
Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | STATIC int |
Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 805 | xfs_file_open( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | struct inode *inode, |
Christoph Hellwig | f999a5b | 2008-11-28 14:23:32 +1100 | [diff] [blame] | 807 | struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | { |
Christoph Hellwig | f999a5b | 2008-11-28 14:23:32 +1100 | [diff] [blame] | 809 | if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | return -EFBIG; |
Christoph Hellwig | f999a5b | 2008-11-28 14:23:32 +1100 | [diff] [blame] | 811 | if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb))) |
| 812 | return -EIO; |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | STATIC int |
| 817 | xfs_dir_open( |
| 818 | struct inode *inode, |
| 819 | struct file *file) |
| 820 | { |
| 821 | struct xfs_inode *ip = XFS_I(inode); |
| 822 | int mode; |
| 823 | int error; |
| 824 | |
| 825 | error = xfs_file_open(inode, file); |
| 826 | if (error) |
| 827 | return error; |
| 828 | |
| 829 | /* |
| 830 | * If there are any blocks, read-ahead block 0 as we're almost |
| 831 | * certain to have the next operation be a read there. |
| 832 | */ |
| 833 | mode = xfs_ilock_map_shared(ip); |
| 834 | if (ip->i_d.di_nextents > 0) |
| 835 | xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK); |
| 836 | xfs_iunlock(ip, mode); |
| 837 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | STATIC int |
Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 841 | xfs_file_release( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | struct inode *inode, |
| 843 | struct file *filp) |
| 844 | { |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 845 | return -xfs_release(XFS_I(inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | STATIC int |
Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 849 | xfs_file_readdir( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | struct file *filp, |
| 851 | void *dirent, |
| 852 | filldir_t filldir) |
| 853 | { |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 854 | struct inode *inode = filp->f_path.dentry->d_inode; |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 855 | xfs_inode_t *ip = XFS_I(inode); |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 856 | int error; |
| 857 | size_t bufsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 859 | /* |
| 860 | * The Linux API doesn't pass down the total size of the buffer |
| 861 | * we read into down to the filesystem. With the filldir concept |
| 862 | * it's not needed for correct information, but the XFS dir2 leaf |
| 863 | * code wants an estimate of the buffer size to calculate it's |
| 864 | * readahead window and size the buffers used for mapping to |
| 865 | * physical blocks. |
| 866 | * |
| 867 | * Try to give it an estimate that's good enough, maybe at some |
| 868 | * point we can change the ->readdir prototype to include the |
Eric Sandeen | a9cc799 | 2010-02-03 17:50:13 +0000 | [diff] [blame] | 869 | * buffer size. For now we use the current glibc buffer size. |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 870 | */ |
Eric Sandeen | a9cc799 | 2010-02-03 17:50:13 +0000 | [diff] [blame] | 871 | bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 873 | error = xfs_readdir(ip, dirent, bufsize, |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 874 | (xfs_off_t *)&filp->f_pos, filldir); |
| 875 | if (error) |
| 876 | return -error; |
| 877 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | STATIC int |
Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 881 | xfs_file_mmap( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | struct file *filp, |
| 883 | struct vm_area_struct *vma) |
| 884 | { |
Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 885 | vma->vm_ops = &xfs_file_vm_ops; |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 886 | vma->vm_flags |= VM_CAN_NONLINEAR; |
Dean Roehrich | 6fac0cb | 2005-06-21 14:07:45 +1000 | [diff] [blame] | 887 | |
Nathan Scott | fbc1462 | 2006-06-09 14:52:13 +1000 | [diff] [blame] | 888 | file_accessed(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | return 0; |
| 890 | } |
| 891 | |
David Chinner | 4f57dbc | 2007-07-19 16:28:17 +1000 | [diff] [blame] | 892 | /* |
| 893 | * mmap()d file has taken write protection fault and is being made |
| 894 | * writable. We can set the page state up correctly for a writable |
| 895 | * page, which means we can do correct delalloc accounting (ENOSPC |
| 896 | * checking!) and unwritten extent mapping. |
| 897 | */ |
| 898 | STATIC int |
| 899 | xfs_vm_page_mkwrite( |
| 900 | struct vm_area_struct *vma, |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 901 | struct vm_fault *vmf) |
David Chinner | 4f57dbc | 2007-07-19 16:28:17 +1000 | [diff] [blame] | 902 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 903 | return block_page_mkwrite(vma, vmf, xfs_get_blocks); |
David Chinner | 4f57dbc | 2007-07-19 16:28:17 +1000 | [diff] [blame] | 904 | } |
| 905 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 906 | const struct file_operations xfs_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | .llseek = generic_file_llseek, |
| 908 | .read = do_sync_read, |
Dean Roehrich | bb3f724 | 2005-09-02 15:43:05 +1000 | [diff] [blame] | 909 | .write = do_sync_write, |
Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 910 | .aio_read = xfs_file_aio_read, |
| 911 | .aio_write = xfs_file_aio_write, |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 912 | .splice_read = xfs_file_splice_read, |
| 913 | .splice_write = xfs_file_splice_write, |
Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 914 | .unlocked_ioctl = xfs_file_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | #ifdef CONFIG_COMPAT |
Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 916 | .compat_ioctl = xfs_file_compat_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | #endif |
Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 918 | .mmap = xfs_file_mmap, |
| 919 | .open = xfs_file_open, |
| 920 | .release = xfs_file_release, |
| 921 | .fsync = xfs_file_fsync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | }; |
| 923 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 924 | const struct file_operations xfs_dir_file_operations = { |
Christoph Hellwig | f999a5b | 2008-11-28 14:23:32 +1100 | [diff] [blame] | 925 | .open = xfs_dir_open, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | .read = generic_read_dir, |
Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 927 | .readdir = xfs_file_readdir, |
Al Viro | 59af158 | 2008-08-24 07:24:41 -0400 | [diff] [blame] | 928 | .llseek = generic_file_llseek, |
Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 929 | .unlocked_ioctl = xfs_file_ioctl, |
Nathan Scott | d387039 | 2005-05-06 06:44:46 -0700 | [diff] [blame] | 930 | #ifdef CONFIG_COMPAT |
Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 931 | .compat_ioctl = xfs_file_compat_ioctl, |
Nathan Scott | d387039 | 2005-05-06 06:44:46 -0700 | [diff] [blame] | 932 | #endif |
Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 933 | .fsync = xfs_file_fsync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | }; |
| 935 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 936 | static const struct vm_operations_struct xfs_file_vm_ops = { |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 937 | .fault = filemap_fault, |
David Chinner | 4f57dbc | 2007-07-19 16:28:17 +1000 | [diff] [blame] | 938 | .page_mkwrite = xfs_vm_page_mkwrite, |
Dean Roehrich | 6fac0cb | 2005-06-21 14:07:45 +1000 | [diff] [blame] | 939 | }; |