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