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