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