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