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