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