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-2003,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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [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_trans.h" |
| 24 | #include "xfs_sb.h" |
| 25 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_dir2.h" |
| 27 | #include "xfs_alloc.h" |
| 28 | #include "xfs_dmapi.h" |
| 29 | #include "xfs_quota.h" |
| 30 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_dinode.h" |
| 37 | #include "xfs_inode.h" |
| 38 | #include "xfs_bmap.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 39 | #include "xfs_btree.h" |
| 40 | #include "xfs_ialloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include "xfs_rtalloc.h" |
| 42 | #include "xfs_error.h" |
| 43 | #include "xfs_itable.h" |
| 44 | #include "xfs_rw.h" |
| 45 | #include "xfs_acl.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include "xfs_attr.h" |
| 47 | #include "xfs_inode_item.h" |
| 48 | #include "xfs_buf_item.h" |
| 49 | #include "xfs_utils.h" |
| 50 | #include "xfs_iomap.h" |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 51 | #include "xfs_vnodeops.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | #include <linux/capability.h> |
| 54 | #include <linux/writeback.h> |
| 55 | |
| 56 | |
| 57 | #if defined(XFS_RW_TRACE) |
| 58 | void |
| 59 | xfs_rw_enter_trace( |
| 60 | int tag, |
| 61 | xfs_iocore_t *io, |
| 62 | void *data, |
| 63 | size_t segs, |
| 64 | loff_t offset, |
| 65 | int ioflags) |
| 66 | { |
| 67 | xfs_inode_t *ip = XFS_IO_INODE(io); |
| 68 | |
| 69 | if (ip->i_rwtrace == NULL) |
| 70 | return; |
| 71 | ktrace_enter(ip->i_rwtrace, |
| 72 | (void *)(unsigned long)tag, |
| 73 | (void *)ip, |
| 74 | (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)), |
| 75 | (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)), |
| 76 | (void *)data, |
| 77 | (void *)((unsigned long)segs), |
| 78 | (void *)((unsigned long)((offset >> 32) & 0xffffffff)), |
| 79 | (void *)((unsigned long)(offset & 0xffffffff)), |
| 80 | (void *)((unsigned long)ioflags), |
| 81 | (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)), |
| 82 | (void *)((unsigned long)(io->io_new_size & 0xffffffff)), |
Yingping Lu | f1fdc84 | 2006-03-22 12:44:15 +1100 | [diff] [blame] | 83 | (void *)((unsigned long)current_pid()), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | (void *)NULL, |
| 85 | (void *)NULL, |
| 86 | (void *)NULL, |
| 87 | (void *)NULL); |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | xfs_inval_cached_trace( |
| 92 | xfs_iocore_t *io, |
| 93 | xfs_off_t offset, |
| 94 | xfs_off_t len, |
| 95 | xfs_off_t first, |
| 96 | xfs_off_t last) |
| 97 | { |
| 98 | xfs_inode_t *ip = XFS_IO_INODE(io); |
| 99 | |
| 100 | if (ip->i_rwtrace == NULL) |
| 101 | return; |
| 102 | ktrace_enter(ip->i_rwtrace, |
| 103 | (void *)(__psint_t)XFS_INVAL_CACHED, |
| 104 | (void *)ip, |
| 105 | (void *)((unsigned long)((offset >> 32) & 0xffffffff)), |
| 106 | (void *)((unsigned long)(offset & 0xffffffff)), |
| 107 | (void *)((unsigned long)((len >> 32) & 0xffffffff)), |
| 108 | (void *)((unsigned long)(len & 0xffffffff)), |
| 109 | (void *)((unsigned long)((first >> 32) & 0xffffffff)), |
| 110 | (void *)((unsigned long)(first & 0xffffffff)), |
| 111 | (void *)((unsigned long)((last >> 32) & 0xffffffff)), |
| 112 | (void *)((unsigned long)(last & 0xffffffff)), |
Yingping Lu | f1fdc84 | 2006-03-22 12:44:15 +1100 | [diff] [blame] | 113 | (void *)((unsigned long)current_pid()), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | (void *)NULL, |
| 115 | (void *)NULL, |
| 116 | (void *)NULL, |
| 117 | (void *)NULL, |
| 118 | (void *)NULL); |
| 119 | } |
| 120 | #endif |
| 121 | |
| 122 | /* |
| 123 | * xfs_iozero |
| 124 | * |
| 125 | * xfs_iozero clears the specified range of buffer supplied, |
| 126 | * and marks all the affected blocks as valid and modified. If |
| 127 | * an affected block is not allocated, it will be allocated. If |
| 128 | * an affected block is not completely overwritten, and is not |
| 129 | * valid before the operation, it will be read from disk before |
| 130 | * being partially zeroed. |
| 131 | */ |
| 132 | STATIC int |
| 133 | xfs_iozero( |
| 134 | struct inode *ip, /* inode */ |
| 135 | loff_t pos, /* offset in file */ |
Lachlan McIlroy | 6816016 | 2007-02-10 18:36:47 +1100 | [diff] [blame] | 136 | size_t count) /* size of data to zero */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
| 138 | unsigned bytes; |
| 139 | struct page *page; |
| 140 | struct address_space *mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | int status; |
| 142 | |
| 143 | mapping = ip->i_mapping; |
| 144 | do { |
| 145 | unsigned long index, offset; |
| 146 | |
| 147 | offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */ |
| 148 | index = pos >> PAGE_CACHE_SHIFT; |
| 149 | bytes = PAGE_CACHE_SIZE - offset; |
| 150 | if (bytes > count) |
| 151 | bytes = count; |
| 152 | |
| 153 | status = -ENOMEM; |
| 154 | page = grab_cache_page(mapping, index); |
| 155 | if (!page) |
| 156 | break; |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | status = mapping->a_ops->prepare_write(NULL, page, offset, |
| 159 | offset + bytes); |
David Chinner | e7ff6ae | 2007-02-10 18:37:46 +1100 | [diff] [blame] | 160 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Christoph Hellwig | 700716c | 2007-05-24 15:27:17 +1000 | [diff] [blame] | 163 | zero_user_page(page, offset, bytes, KM_USER0); |
David Chinner | e7ff6ae | 2007-02-10 18:37:46 +1100 | [diff] [blame] | 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | status = mapping->a_ops->commit_write(NULL, page, offset, |
| 166 | offset + bytes); |
| 167 | if (!status) { |
| 168 | pos += bytes; |
| 169 | count -= bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | unlock_page(page); |
| 174 | page_cache_release(page); |
| 175 | if (status) |
| 176 | break; |
| 177 | } while (count); |
| 178 | |
| 179 | return (-status); |
| 180 | } |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | ssize_t /* bytes read, or (-) error */ |
| 183 | xfs_read( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 184 | xfs_inode_t *ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | struct kiocb *iocb, |
| 186 | const struct iovec *iovp, |
| 187 | unsigned int segs, |
| 188 | loff_t *offset, |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 189 | int ioflags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
| 191 | struct file *file = iocb->ki_filp; |
| 192 | struct inode *inode = file->f_mapping->host; |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 193 | bhv_vnode_t *vp = XFS_ITOV(ip); |
| 194 | xfs_mount_t *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | size_t size = 0; |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 196 | ssize_t ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | xfs_fsize_t n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | unsigned long seg; |
| 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | XFS_STATS_INC(xs_read_calls); |
| 202 | |
| 203 | /* START copy & waste from filemap.c */ |
| 204 | for (seg = 0; seg < segs; seg++) { |
| 205 | const struct iovec *iv = &iovp[seg]; |
| 206 | |
| 207 | /* |
| 208 | * If any segment has a negative length, or the cumulative |
| 209 | * length ever wraps negative then return -EINVAL. |
| 210 | */ |
| 211 | size += iv->iov_len; |
| 212 | if (unlikely((ssize_t)(size|iv->iov_len) < 0)) |
| 213 | return XFS_ERROR(-EINVAL); |
| 214 | } |
| 215 | /* END copy & waste from filemap.c */ |
| 216 | |
| 217 | if (unlikely(ioflags & IO_ISDIRECT)) { |
| 218 | xfs_buftarg_t *target = |
| 219 | (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ? |
| 220 | mp->m_rtdev_targp : mp->m_ddev_targp; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 221 | if ((*offset & target->bt_smask) || |
| 222 | (size & target->bt_smask)) { |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 223 | if (*offset == ip->i_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | return (0); |
| 225 | } |
| 226 | return -XFS_ERROR(EINVAL); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | n = XFS_MAXIOFFSET(mp) - *offset; |
| 231 | if ((n <= 0) || (size == 0)) |
| 232 | return 0; |
| 233 | |
| 234 | if (n < size) |
| 235 | size = n; |
| 236 | |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 237 | if (XFS_FORCED_SHUTDOWN(mp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
| 240 | if (unlikely(ioflags & IO_ISDIRECT)) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 241 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
| 243 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 244 | if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) { |
Nathan Scott | 8285fb5 | 2006-06-09 17:07:12 +1000 | [diff] [blame] | 245 | bhv_vrwlock_t locktype = VRWLOCK_READ; |
Dean Roehrich | e1a40fa | 2005-06-22 10:20:44 +1000 | [diff] [blame] | 246 | int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 248 | ret = -XFS_SEND_DATA(mp, DM_EVENT_READ, vp, *offset, size, |
Dean Roehrich | e1a40fa | 2005-06-22 10:20:44 +1000 | [diff] [blame] | 249 | dmflags, &locktype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | if (ret) { |
| 251 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
Lachlan McIlroy | 721259b | 2006-09-07 14:27:05 +1000 | [diff] [blame] | 252 | if (unlikely(ioflags & IO_ISDIRECT)) |
| 253 | mutex_unlock(&inode->i_mutex); |
| 254 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Nathan Scott | edcd4bc | 2006-09-28 11:05:33 +1000 | [diff] [blame] | 258 | if (unlikely(ioflags & IO_ISDIRECT)) { |
| 259 | if (VN_CACHED(vp)) |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 260 | ret = bhv_vop_flushinval_pages(vp, ctooff(offtoct(*offset)), |
Nathan Scott | edcd4bc | 2006-09-28 11:05:33 +1000 | [diff] [blame] | 261 | -1, FI_REMAPF_LOCKED); |
Lachlan McIlroy | 721259b | 2006-09-07 14:27:05 +1000 | [diff] [blame] | 262 | mutex_unlock(&inode->i_mutex); |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 263 | if (ret) { |
| 264 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
| 265 | return ret; |
| 266 | } |
Nathan Scott | edcd4bc | 2006-09-28 11:05:33 +1000 | [diff] [blame] | 267 | } |
Lachlan McIlroy | 721259b | 2006-09-07 14:27:05 +1000 | [diff] [blame] | 268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | xfs_rw_enter_trace(XFS_READ_ENTER, &ip->i_iocore, |
| 270 | (void *)iovp, segs, *offset, ioflags); |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 271 | |
| 272 | iocb->ki_pos = *offset; |
| 273 | ret = generic_file_aio_read(iocb, iovp, segs, *offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO)) |
| 275 | ret = wait_on_sync_kiocb(iocb); |
| 276 | if (ret > 0) |
| 277 | XFS_STATS_ADD(xs_read_bytes, ret); |
| 278 | |
| 279 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | ssize_t |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 284 | xfs_splice_read( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 285 | xfs_inode_t *ip, |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 286 | struct file *infilp, |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 287 | loff_t *ppos, |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 288 | struct pipe_inode_info *pipe, |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 289 | size_t count, |
| 290 | int flags, |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 291 | int ioflags) |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 292 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 293 | bhv_vnode_t *vp = XFS_ITOV(ip); |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 294 | xfs_mount_t *mp = ip->i_mount; |
| 295 | ssize_t ret; |
| 296 | |
| 297 | XFS_STATS_INC(xs_read_calls); |
| 298 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 299 | return -EIO; |
| 300 | |
| 301 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
| 302 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 303 | if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) { |
Nathan Scott | 8285fb5 | 2006-06-09 17:07:12 +1000 | [diff] [blame] | 304 | bhv_vrwlock_t locktype = VRWLOCK_READ; |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 305 | int error; |
| 306 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 307 | error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, *ppos, count, |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 308 | FILP_DELAY_FLAG(infilp), &locktype); |
| 309 | if (error) { |
| 310 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
| 311 | return -error; |
| 312 | } |
| 313 | } |
| 314 | xfs_rw_enter_trace(XFS_SPLICE_READ_ENTER, &ip->i_iocore, |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 315 | pipe, count, *ppos, ioflags); |
| 316 | ret = generic_file_splice_read(infilp, ppos, pipe, count, flags); |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 317 | if (ret > 0) |
| 318 | XFS_STATS_ADD(xs_read_bytes, ret); |
| 319 | |
| 320 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | ssize_t |
| 325 | xfs_splice_write( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 326 | xfs_inode_t *ip, |
Ingo Molnar | 3a326a2 | 2006-04-10 15:18:35 +0200 | [diff] [blame] | 327 | struct pipe_inode_info *pipe, |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 328 | struct file *outfilp, |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 329 | loff_t *ppos, |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 330 | size_t count, |
| 331 | int flags, |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 332 | int ioflags) |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 333 | { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 334 | bhv_vnode_t *vp = XFS_ITOV(ip); |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 335 | xfs_mount_t *mp = ip->i_mount; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 336 | xfs_iocore_t *io = &ip->i_iocore; |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 337 | ssize_t ret; |
David Chinner | 0a8d17d | 2006-09-07 14:27:15 +1000 | [diff] [blame] | 338 | struct inode *inode = outfilp->f_mapping->host; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 339 | xfs_fsize_t isize, new_size; |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 340 | |
| 341 | XFS_STATS_INC(xs_write_calls); |
| 342 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 343 | return -EIO; |
| 344 | |
| 345 | xfs_ilock(ip, XFS_IOLOCK_EXCL); |
| 346 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 347 | if (DM_EVENT_ENABLED(ip, DM_EVENT_WRITE) && !(ioflags & IO_INVIS)) { |
Nathan Scott | 8285fb5 | 2006-06-09 17:07:12 +1000 | [diff] [blame] | 348 | bhv_vrwlock_t locktype = VRWLOCK_WRITE; |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 349 | int error; |
| 350 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 351 | error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp, *ppos, count, |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 352 | FILP_DELAY_FLAG(outfilp), &locktype); |
| 353 | if (error) { |
| 354 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 355 | return -error; |
| 356 | } |
| 357 | } |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 358 | |
| 359 | new_size = *ppos + count; |
| 360 | |
| 361 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 362 | if (new_size > ip->i_size) |
| 363 | io->io_new_size = new_size; |
| 364 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 365 | |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 366 | xfs_rw_enter_trace(XFS_SPLICE_WRITE_ENTER, &ip->i_iocore, |
Jens Axboe | cbb7e57 | 2006-04-11 14:57:50 +0200 | [diff] [blame] | 367 | pipe, count, *ppos, ioflags); |
| 368 | ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags); |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 369 | if (ret > 0) |
| 370 | XFS_STATS_ADD(xs_write_bytes, ret); |
| 371 | |
David Chinner | 0a8d17d | 2006-09-07 14:27:15 +1000 | [diff] [blame] | 372 | isize = i_size_read(inode); |
| 373 | if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize)) |
| 374 | *ppos = isize; |
| 375 | |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 376 | if (*ppos > ip->i_size) { |
David Chinner | 0a8d17d | 2006-09-07 14:27:15 +1000 | [diff] [blame] | 377 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 378 | if (*ppos > ip->i_size) |
| 379 | ip->i_size = *ppos; |
| 380 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 381 | } |
| 382 | |
| 383 | if (io->io_new_size) { |
| 384 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 385 | io->io_new_size = 0; |
| 386 | if (ip->i_d.di_size > ip->i_size) |
| 387 | ip->i_d.di_size = ip->i_size; |
David Chinner | 0a8d17d | 2006-09-07 14:27:15 +1000 | [diff] [blame] | 388 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 389 | } |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 390 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | return ret; |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * This routine is called to handle zeroing any space in the last |
| 396 | * block of the file that is beyond the EOF. We do this since the |
| 397 | * size is being increased without writing anything to that block |
| 398 | * and we don't want anyone to read the garbage on the disk. |
| 399 | */ |
| 400 | STATIC int /* error (positive) */ |
| 401 | xfs_zero_last_block( |
| 402 | struct inode *ip, |
| 403 | xfs_iocore_t *io, |
Lachlan McIlroy | 6816016 | 2007-02-10 18:36:47 +1100 | [diff] [blame] | 404 | xfs_fsize_t offset, |
| 405 | xfs_fsize_t isize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | { |
| 407 | xfs_fileoff_t last_fsb; |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 408 | xfs_mount_t *mp = io->io_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | int nimaps; |
| 410 | int zero_offset; |
| 411 | int zero_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | int error = 0; |
| 413 | xfs_bmbt_irec_t imap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
| 415 | ASSERT(ismrlocked(io->io_lock, MR_UPDATE) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
Eric Sandeen | 24ee808 | 2006-01-11 15:34:32 +1100 | [diff] [blame] | 417 | zero_offset = XFS_B_FSB_OFFSET(mp, isize); |
| 418 | if (zero_offset == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | /* |
| 420 | * There are no extra bytes in the last block on disk to |
| 421 | * zero, so return. |
| 422 | */ |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | last_fsb = XFS_B_TO_FSBT(mp, isize); |
| 427 | nimaps = 1; |
| 428 | error = XFS_BMAPI(mp, NULL, io, last_fsb, 1, 0, NULL, 0, &imap, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 429 | &nimaps, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | if (error) { |
| 431 | return error; |
| 432 | } |
| 433 | ASSERT(nimaps > 0); |
| 434 | /* |
| 435 | * If the block underlying isize is just a hole, then there |
| 436 | * is nothing to zero. |
| 437 | */ |
| 438 | if (imap.br_startblock == HOLESTARTBLOCK) { |
| 439 | return 0; |
| 440 | } |
| 441 | /* |
| 442 | * Zero the part of the last block beyond the EOF, and write it |
| 443 | * out sync. We need to drop the ilock while we do this so we |
| 444 | * don't deadlock when the buffer cache calls back to us. |
| 445 | */ |
| 446 | XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD); |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 447 | |
Eric Sandeen | 24ee808 | 2006-01-11 15:34:32 +1100 | [diff] [blame] | 448 | zero_len = mp->m_sb.sb_blocksize - zero_offset; |
Lachlan McIlroy | 6816016 | 2007-02-10 18:36:47 +1100 | [diff] [blame] | 449 | if (isize + zero_len > offset) |
| 450 | zero_len = offset - isize; |
| 451 | error = xfs_iozero(ip, isize, zero_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
| 453 | XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD); |
| 454 | ASSERT(error >= 0); |
| 455 | return error; |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Zero any on disk space between the current EOF and the new, |
| 460 | * larger EOF. This handles the normal case of zeroing the remainder |
| 461 | * of the last block in the file and the unusual case of zeroing blocks |
| 462 | * out beyond the size of the file. This second case only happens |
| 463 | * with fixed size extents and when the system crashes before the inode |
| 464 | * size was updated but after blocks were allocated. If fill is set, |
| 465 | * then any holes in the range are filled and zeroed. If not, the holes |
| 466 | * are left alone as holes. |
| 467 | */ |
| 468 | |
| 469 | int /* error (positive) */ |
| 470 | xfs_zero_eof( |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 471 | bhv_vnode_t *vp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | xfs_iocore_t *io, |
| 473 | xfs_off_t offset, /* starting I/O offset */ |
Lachlan McIlroy | 6816016 | 2007-02-10 18:36:47 +1100 | [diff] [blame] | 474 | xfs_fsize_t isize) /* current inode size */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | { |
Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 476 | struct inode *ip = vn_to_inode(vp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | xfs_fileoff_t start_zero_fsb; |
| 478 | xfs_fileoff_t end_zero_fsb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | xfs_fileoff_t zero_count_fsb; |
| 480 | xfs_fileoff_t last_fsb; |
Lachlan McIlroy | 6816016 | 2007-02-10 18:36:47 +1100 | [diff] [blame] | 481 | xfs_fileoff_t zero_off; |
| 482 | xfs_fsize_t zero_len; |
Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 483 | xfs_mount_t *mp = io->io_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | int nimaps; |
| 485 | int error = 0; |
| 486 | xfs_bmbt_irec_t imap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
| 488 | ASSERT(ismrlocked(io->io_lock, MR_UPDATE)); |
| 489 | ASSERT(ismrlocked(io->io_iolock, MR_UPDATE)); |
Eric Sandeen | 24ee808 | 2006-01-11 15:34:32 +1100 | [diff] [blame] | 490 | ASSERT(offset > isize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | /* |
| 493 | * First handle zeroing the block on which isize resides. |
| 494 | * We only zero a part of that block so it is handled specially. |
| 495 | */ |
Lachlan McIlroy | 6816016 | 2007-02-10 18:36:47 +1100 | [diff] [blame] | 496 | error = xfs_zero_last_block(ip, io, offset, isize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | if (error) { |
| 498 | ASSERT(ismrlocked(io->io_lock, MR_UPDATE)); |
| 499 | ASSERT(ismrlocked(io->io_iolock, MR_UPDATE)); |
| 500 | return error; |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Calculate the range between the new size and the old |
| 505 | * where blocks needing to be zeroed may exist. To get the |
| 506 | * block where the last byte in the file currently resides, |
| 507 | * we need to subtract one from the size and truncate back |
| 508 | * to a block boundary. We subtract 1 in case the size is |
| 509 | * exactly on a block boundary. |
| 510 | */ |
| 511 | last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1; |
| 512 | start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize); |
| 513 | end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1); |
| 514 | ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb); |
| 515 | if (last_fsb == end_zero_fsb) { |
| 516 | /* |
| 517 | * The size was only incremented on its last block. |
| 518 | * We took care of that above, so just return. |
| 519 | */ |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | ASSERT(start_zero_fsb <= end_zero_fsb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | while (start_zero_fsb <= end_zero_fsb) { |
| 525 | nimaps = 1; |
| 526 | zero_count_fsb = end_zero_fsb - start_zero_fsb + 1; |
| 527 | error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 528 | 0, NULL, 0, &imap, &nimaps, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | if (error) { |
| 530 | ASSERT(ismrlocked(io->io_lock, MR_UPDATE)); |
| 531 | ASSERT(ismrlocked(io->io_iolock, MR_UPDATE)); |
| 532 | return error; |
| 533 | } |
| 534 | ASSERT(nimaps > 0); |
| 535 | |
| 536 | if (imap.br_state == XFS_EXT_UNWRITTEN || |
| 537 | imap.br_startblock == HOLESTARTBLOCK) { |
| 538 | /* |
| 539 | * This loop handles initializing pages that were |
| 540 | * partially initialized by the code below this |
| 541 | * loop. It basically zeroes the part of the page |
| 542 | * that sits on a hole and sets the page as P_HOLE |
| 543 | * and calls remapf if it is a mapped file. |
| 544 | */ |
Eric Sandeen | 24ee808 | 2006-01-11 15:34:32 +1100 | [diff] [blame] | 545 | start_zero_fsb = imap.br_startoff + imap.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | ASSERT(start_zero_fsb <= (end_zero_fsb + 1)); |
| 547 | continue; |
| 548 | } |
| 549 | |
| 550 | /* |
Nathan Scott | 3d80ede | 2006-06-09 14:57:30 +1000 | [diff] [blame] | 551 | * There are blocks we need to zero. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | * Drop the inode lock while we're doing the I/O. |
| 553 | * We'll still have the iolock to protect us. |
| 554 | */ |
| 555 | XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD); |
| 556 | |
Lachlan McIlroy | 6816016 | 2007-02-10 18:36:47 +1100 | [diff] [blame] | 557 | zero_off = XFS_FSB_TO_B(mp, start_zero_fsb); |
| 558 | zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount); |
| 559 | |
| 560 | if ((zero_off + zero_len) > offset) |
| 561 | zero_len = offset - zero_off; |
| 562 | |
| 563 | error = xfs_iozero(ip, zero_off, zero_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | if (error) { |
| 565 | goto out_lock; |
| 566 | } |
| 567 | |
Nathan Scott | 3d80ede | 2006-06-09 14:57:30 +1000 | [diff] [blame] | 568 | start_zero_fsb = imap.br_startoff + imap.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | ASSERT(start_zero_fsb <= (end_zero_fsb + 1)); |
| 570 | |
| 571 | XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD); |
| 572 | } |
| 573 | |
| 574 | return 0; |
| 575 | |
| 576 | out_lock: |
| 577 | |
| 578 | XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD); |
| 579 | ASSERT(error >= 0); |
| 580 | return error; |
| 581 | } |
| 582 | |
| 583 | ssize_t /* bytes written, or (-) error */ |
| 584 | xfs_write( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 585 | struct xfs_inode *xip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | struct kiocb *iocb, |
| 587 | const struct iovec *iovp, |
| 588 | unsigned int nsegs, |
| 589 | loff_t *offset, |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 590 | int ioflags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | { |
| 592 | struct file *file = iocb->ki_filp; |
| 593 | struct address_space *mapping = file->f_mapping; |
| 594 | struct inode *inode = mapping->host; |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 595 | bhv_vnode_t *vp = XFS_ITOV(xip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | unsigned long segs = nsegs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | xfs_mount_t *mp; |
| 598 | ssize_t ret = 0, error = 0; |
| 599 | xfs_fsize_t isize, new_size; |
| 600 | xfs_iocore_t *io; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | int iolock; |
| 602 | int eventsent = 0; |
Nathan Scott | 8285fb5 | 2006-06-09 17:07:12 +1000 | [diff] [blame] | 603 | bhv_vrwlock_t locktype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | size_t ocount = 0, count; |
| 605 | loff_t pos; |
Lachlan McIlroy | 2a32963 | 2007-05-08 13:49:39 +1000 | [diff] [blame] | 606 | int need_i_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
| 608 | XFS_STATS_INC(xs_write_calls); |
| 609 | |
Dmitriy Monakhov | 0ceb331 | 2007-05-08 00:23:02 -0700 | [diff] [blame] | 610 | error = generic_segment_checks(iovp, &segs, &ocount, VERIFY_READ); |
| 611 | if (error) |
| 612 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
| 614 | count = ocount; |
| 615 | pos = *offset; |
| 616 | |
| 617 | if (count == 0) |
| 618 | return 0; |
| 619 | |
| 620 | io = &xip->i_iocore; |
| 621 | mp = io->io_mount; |
| 622 | |
Nathan Scott | 34327e1 | 2006-06-09 17:11:55 +1000 | [diff] [blame] | 623 | vfs_wait_for_freeze(vp->v_vfsp, SB_FREEZE_WRITE); |
| 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 626 | return -EIO; |
| 627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | relock: |
Lachlan McIlroy | 2a32963 | 2007-05-08 13:49:39 +1000 | [diff] [blame] | 629 | if (ioflags & IO_ISDIRECT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | iolock = XFS_IOLOCK_SHARED; |
| 631 | locktype = VRWLOCK_WRITE_DIRECT; |
Lachlan McIlroy | 2a32963 | 2007-05-08 13:49:39 +1000 | [diff] [blame] | 632 | need_i_mutex = 0; |
| 633 | } else { |
| 634 | iolock = XFS_IOLOCK_EXCL; |
| 635 | locktype = VRWLOCK_WRITE; |
| 636 | need_i_mutex = 1; |
| 637 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | xfs_ilock(xip, XFS_ILOCK_EXCL|iolock); |
| 641 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | start: |
| 643 | error = -generic_write_checks(file, &pos, &count, |
| 644 | S_ISBLK(inode->i_mode)); |
| 645 | if (error) { |
| 646 | xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock); |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 647 | goto out_unlock_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 650 | if ((DM_EVENT_ENABLED(xip, DM_EVENT_WRITE) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | !(ioflags & IO_INVIS) && !eventsent)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | int dmflags = FILP_DELAY_FLAG(file); |
| 653 | |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 654 | if (need_i_mutex) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 655 | dmflags |= DM_FLAGS_IMUX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
| 657 | xfs_iunlock(xip, XFS_ILOCK_EXCL); |
| 658 | error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp, |
| 659 | pos, count, |
| 660 | dmflags, &locktype); |
| 661 | if (error) { |
Lachlan McIlroy | 71dfd5a | 2007-05-08 13:50:12 +1000 | [diff] [blame] | 662 | goto out_unlock_internal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | } |
| 664 | xfs_ilock(xip, XFS_ILOCK_EXCL); |
| 665 | eventsent = 1; |
| 666 | |
| 667 | /* |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 668 | * The iolock was dropped and reacquired in XFS_SEND_DATA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | * so we have to recheck the size when appending. |
| 670 | * We will only "goto start;" once, since having sent the |
| 671 | * event prevents another call to XFS_SEND_DATA, which is |
| 672 | * what allows the size to change in the first place. |
| 673 | */ |
Lachlan McIlroy | 71dfd5a | 2007-05-08 13:50:12 +1000 | [diff] [blame] | 674 | if ((file->f_flags & O_APPEND) && pos != xip->i_size) |
| 675 | goto start; |
| 676 | } |
| 677 | |
Lachlan McIlroy | 2a32963 | 2007-05-08 13:49:39 +1000 | [diff] [blame] | 678 | if (ioflags & IO_ISDIRECT) { |
| 679 | xfs_buftarg_t *target = |
| 680 | (xip->i_d.di_flags & XFS_DIFLAG_REALTIME) ? |
| 681 | mp->m_rtdev_targp : mp->m_ddev_targp; |
| 682 | |
| 683 | if ((pos & target->bt_smask) || (count & target->bt_smask)) { |
| 684 | xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock); |
| 685 | return XFS_ERROR(-EINVAL); |
| 686 | } |
| 687 | |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 688 | if (!need_i_mutex && (VN_CACHED(vp) || pos > xip->i_size)) { |
Lachlan McIlroy | 2a32963 | 2007-05-08 13:49:39 +1000 | [diff] [blame] | 689 | xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock); |
| 690 | iolock = XFS_IOLOCK_EXCL; |
| 691 | locktype = VRWLOCK_WRITE; |
| 692 | need_i_mutex = 1; |
| 693 | mutex_lock(&inode->i_mutex); |
| 694 | xfs_ilock(xip, XFS_ILOCK_EXCL|iolock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | goto start; |
| 696 | } |
| 697 | } |
| 698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | new_size = pos + count; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 700 | if (new_size > xip->i_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | io->io_new_size = new_size; |
| 702 | |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 703 | if (likely(!(ioflags & IO_INVIS))) { |
Christoph Hellwig | 870f481 | 2006-01-09 20:52:01 -0800 | [diff] [blame] | 704 | file_update_time(file); |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 705 | xfs_ichgtime_fast(xip, inode, |
| 706 | XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | /* |
| 710 | * If the offset is beyond the size of the file, we have a couple |
| 711 | * of things to do. First, if there is already space allocated |
| 712 | * we need to either create holes or zero the disk or ... |
| 713 | * |
| 714 | * If there is a page where the previous size lands, we need |
| 715 | * to zero it out up to the new size. |
| 716 | */ |
| 717 | |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 718 | if (pos > xip->i_size) { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 719 | error = xfs_zero_eof(vp, io, pos, xip->i_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | if (error) { |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 721 | xfs_iunlock(xip, XFS_ILOCK_EXCL); |
| 722 | goto out_unlock_internal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | xfs_iunlock(xip, XFS_ILOCK_EXCL); |
| 726 | |
| 727 | /* |
| 728 | * If we're writing the file then make sure to clear the |
| 729 | * setuid and setgid bits if the process is not being run |
| 730 | * by root. This keeps people from modifying setuid and |
| 731 | * setgid binaries. |
| 732 | */ |
| 733 | |
| 734 | if (((xip->i_d.di_mode & S_ISUID) || |
| 735 | ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) == |
| 736 | (S_ISGID | S_IXGRP))) && |
| 737 | !capable(CAP_FSETID)) { |
| 738 | error = xfs_write_clear_setuid(xip); |
| 739 | if (likely(!error)) |
Josef "Jeff" Sipek | e678fb0 | 2006-12-08 02:36:49 -0800 | [diff] [blame] | 740 | error = -remove_suid(file->f_path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | if (unlikely(error)) { |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 742 | goto out_unlock_internal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
| 746 | retry: |
| 747 | /* We can write back this queue in page reclaim */ |
| 748 | current->backing_dev_info = mapping->backing_dev_info; |
| 749 | |
| 750 | if ((ioflags & IO_ISDIRECT)) { |
Lachlan McIlroy | 2a32963 | 2007-05-08 13:49:39 +1000 | [diff] [blame] | 751 | if (VN_CACHED(vp)) { |
| 752 | WARN_ON(need_i_mutex == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | xfs_inval_cached_trace(io, pos, -1, |
| 754 | ctooff(offtoct(pos)), -1); |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 755 | error = bhv_vop_flushinval_pages(vp, ctooff(offtoct(pos)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | -1, FI_REMAPF_LOCKED); |
Lachlan McIlroy | d3cf209 | 2007-05-08 13:49:27 +1000 | [diff] [blame] | 757 | if (error) |
| 758 | goto out_unlock_internal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 761 | if (need_i_mutex) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | /* demote the lock now the cached pages are gone */ |
| 763 | XFS_ILOCK_DEMOTE(mp, io, XFS_IOLOCK_EXCL); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 764 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
| 766 | iolock = XFS_IOLOCK_SHARED; |
| 767 | locktype = VRWLOCK_WRITE_DIRECT; |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 768 | need_i_mutex = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | xfs_rw_enter_trace(XFS_DIOWR_ENTER, io, (void *)iovp, segs, |
| 772 | *offset, ioflags); |
| 773 | ret = generic_file_direct_write(iocb, iovp, |
| 774 | &segs, pos, offset, count, ocount); |
| 775 | |
| 776 | /* |
| 777 | * direct-io write to a hole: fall through to buffered I/O |
| 778 | * for completing the rest of the request. |
| 779 | */ |
| 780 | if (ret >= 0 && ret != count) { |
| 781 | XFS_STATS_ADD(xs_write_bytes, ret); |
| 782 | |
| 783 | pos += ret; |
| 784 | count -= ret; |
| 785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | ioflags &= ~IO_ISDIRECT; |
| 787 | xfs_iunlock(xip, iolock); |
| 788 | goto relock; |
| 789 | } |
| 790 | } else { |
| 791 | xfs_rw_enter_trace(XFS_WRITE_ENTER, io, (void *)iovp, segs, |
| 792 | *offset, ioflags); |
| 793 | ret = generic_file_buffered_write(iocb, iovp, segs, |
| 794 | pos, offset, count, ret); |
| 795 | } |
| 796 | |
| 797 | current->backing_dev_info = NULL; |
| 798 | |
| 799 | if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO)) |
| 800 | ret = wait_on_sync_kiocb(iocb); |
| 801 | |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 802 | if (ret == -ENOSPC && |
| 803 | DM_EVENT_ENABLED(xip, DM_EVENT_NOSPACE) && !(ioflags & IO_INVIS)) { |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 804 | xfs_rwunlock(xip, locktype); |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 805 | if (need_i_mutex) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 806 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp, |
| 808 | DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL, |
| 809 | 0, 0, 0); /* Delay flag intentionally unused */ |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 810 | if (need_i_mutex) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 811 | mutex_lock(&inode->i_mutex); |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 812 | xfs_rwlock(xip, locktype); |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 813 | if (error) |
| 814 | goto out_unlock_internal; |
| 815 | pos = xip->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | ret = 0; |
| 817 | goto retry; |
| 818 | } |
| 819 | |
Yingping Lu | 68bdb6e | 2006-01-11 15:38:31 +1100 | [diff] [blame] | 820 | isize = i_size_read(inode); |
| 821 | if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize)) |
| 822 | *offset = isize; |
| 823 | |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 824 | if (*offset > xip->i_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | xfs_ilock(xip, XFS_ILOCK_EXCL); |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 826 | if (*offset > xip->i_size) |
| 827 | xip->i_size = *offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | xfs_iunlock(xip, XFS_ILOCK_EXCL); |
| 829 | } |
| 830 | |
| 831 | error = -ret; |
| 832 | if (ret <= 0) |
| 833 | goto out_unlock_internal; |
| 834 | |
| 835 | XFS_STATS_ADD(xs_write_bytes, ret); |
| 836 | |
| 837 | /* Handle various SYNC-type writes */ |
| 838 | if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) { |
Nathan Scott | 1e69dd0 | 2006-06-19 08:39:53 +1000 | [diff] [blame] | 839 | error = xfs_write_sync_logforce(mp, xip); |
| 840 | if (error) |
| 841 | goto out_unlock_internal; |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 842 | |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 843 | xfs_rwunlock(xip, locktype); |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 844 | if (need_i_mutex) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 845 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | |
| 847 | error = sync_page_range(inode, mapping, pos, ret); |
| 848 | if (!error) |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 849 | error = -ret; |
| 850 | if (need_i_mutex) |
| 851 | mutex_lock(&inode->i_mutex); |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 852 | xfs_rwlock(xip, locktype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | out_unlock_internal: |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 856 | if (io->io_new_size) { |
| 857 | xfs_ilock(xip, XFS_ILOCK_EXCL); |
| 858 | io->io_new_size = 0; |
| 859 | /* |
| 860 | * If this was a direct or synchronous I/O that failed (such |
| 861 | * as ENOSPC) then part of the I/O may have been written to |
| 862 | * disk before the error occured. In this case the on-disk |
| 863 | * file size may have been adjusted beyond the in-memory file |
| 864 | * size and now needs to be truncated back. |
| 865 | */ |
| 866 | if (xip->i_d.di_size > xip->i_size) |
| 867 | xip->i_d.di_size = xip->i_size; |
| 868 | xfs_iunlock(xip, XFS_ILOCK_EXCL); |
| 869 | } |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 870 | xfs_rwunlock(xip, locktype); |
Nathan Scott | a13828b | 2006-03-17 17:26:14 +1100 | [diff] [blame] | 871 | out_unlock_mutex: |
| 872 | if (need_i_mutex) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 873 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | return -error; |
| 875 | } |
| 876 | |
| 877 | /* |
| 878 | * All xfs metadata buffers except log state machine buffers |
| 879 | * get this attached as their b_bdstrat callback function. |
| 880 | * This is so that we can catch a buffer |
| 881 | * after prematurely unpinning it to forcibly shutdown the filesystem. |
| 882 | */ |
| 883 | int |
| 884 | xfs_bdstrat_cb(struct xfs_buf *bp) |
| 885 | { |
| 886 | xfs_mount_t *mp; |
| 887 | |
| 888 | mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *); |
| 889 | if (!XFS_FORCED_SHUTDOWN(mp)) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 890 | xfs_buf_iorequest(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | return 0; |
| 892 | } else { |
| 893 | xfs_buftrace("XFS__BDSTRAT IOERROR", bp); |
| 894 | /* |
| 895 | * Metadata write that didn't get logged but |
| 896 | * written delayed anyway. These aren't associated |
| 897 | * with a transaction, and can be ignored. |
| 898 | */ |
| 899 | if (XFS_BUF_IODONE_FUNC(bp) == NULL && |
| 900 | (XFS_BUF_ISREAD(bp)) == 0) |
| 901 | return (xfs_bioerror_relse(bp)); |
| 902 | else |
| 903 | return (xfs_bioerror(bp)); |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | |
| 908 | int |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame^] | 909 | xfs_bmap( |
| 910 | xfs_inode_t *ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | xfs_off_t offset, |
| 912 | ssize_t count, |
| 913 | int flags, |
| 914 | xfs_iomap_t *iomapp, |
| 915 | int *niomaps) |
| 916 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | xfs_iocore_t *io = &ip->i_iocore; |
| 918 | |
| 919 | ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG); |
| 920 | ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) == |
| 921 | ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0)); |
| 922 | |
| 923 | return xfs_iomap(io, offset, count, flags, iomapp, niomaps); |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * Wrapper around bdstrat so that we can stop data |
| 928 | * from going to disk in case we are shutting down the filesystem. |
| 929 | * Typically user data goes thru this path; one of the exceptions |
| 930 | * is the superblock. |
| 931 | */ |
| 932 | int |
| 933 | xfsbdstrat( |
| 934 | struct xfs_mount *mp, |
| 935 | struct xfs_buf *bp) |
| 936 | { |
| 937 | ASSERT(mp); |
| 938 | if (!XFS_FORCED_SHUTDOWN(mp)) { |
| 939 | /* Grio redirection would go here |
| 940 | * if (XFS_BUF_IS_GRIO(bp)) { |
| 941 | */ |
| 942 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 943 | xfs_buf_iorequest(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | xfs_buftrace("XFSBDSTRAT IOERROR", bp); |
| 948 | return (xfs_bioerror_relse(bp)); |
| 949 | } |
| 950 | |
| 951 | /* |
| 952 | * If the underlying (data/log/rt) device is readonly, there are some |
| 953 | * operations that cannot proceed. |
| 954 | */ |
| 955 | int |
| 956 | xfs_dev_is_read_only( |
| 957 | xfs_mount_t *mp, |
| 958 | char *message) |
| 959 | { |
| 960 | if (xfs_readonly_buftarg(mp->m_ddev_targp) || |
| 961 | xfs_readonly_buftarg(mp->m_logdev_targp) || |
| 962 | (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) { |
| 963 | cmn_err(CE_NOTE, |
| 964 | "XFS: %s required on read-only device.", message); |
| 965 | cmn_err(CE_NOTE, |
| 966 | "XFS: write access unavailable, cannot proceed."); |
| 967 | return EROFS; |
| 968 | } |
| 969 | return 0; |
| 970 | } |