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