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