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