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