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