| 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-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" | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [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_sb.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_ag.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_trans.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_mount.h" | 
|  | 27 | #include "xfs_bmap_btree.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_alloc.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "xfs_dinode.h" | 
|  | 30 | #include "xfs_inode.h" | 
| Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 31 | #include "xfs_inode_item.h" | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 32 | #include "xfs_bmap.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_error.h" | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 34 | #include "xfs_vnodeops.h" | 
| Christoph Hellwig | f999a5b | 2008-11-28 14:23:32 +1100 | [diff] [blame] | 35 | #include "xfs_da_btree.h" | 
| Christoph Hellwig | ddcd856 | 2008-12-03 07:55:34 -0500 | [diff] [blame] | 36 | #include "xfs_ioctl.h" | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 37 | #include "xfs_trace.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 |  | 
|  | 39 | #include <linux/dcache.h> | 
| Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 40 | #include <linux/falloc.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 |  | 
| Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 42 | static const struct vm_operations_struct xfs_file_vm_ops; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 |  | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 44 | /* | 
| Dave Chinner | 487f84f | 2011-01-12 11:37:10 +1100 | [diff] [blame] | 45 | * Locking primitives for read and write IO paths to ensure we consistently use | 
|  | 46 | * and order the inode->i_mutex, ip->i_lock and ip->i_iolock. | 
|  | 47 | */ | 
|  | 48 | static inline void | 
|  | 49 | xfs_rw_ilock( | 
|  | 50 | struct xfs_inode	*ip, | 
|  | 51 | int			type) | 
|  | 52 | { | 
|  | 53 | if (type & XFS_IOLOCK_EXCL) | 
|  | 54 | mutex_lock(&VFS_I(ip)->i_mutex); | 
|  | 55 | xfs_ilock(ip, type); | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | static inline void | 
|  | 59 | xfs_rw_iunlock( | 
|  | 60 | struct xfs_inode	*ip, | 
|  | 61 | int			type) | 
|  | 62 | { | 
|  | 63 | xfs_iunlock(ip, type); | 
|  | 64 | if (type & XFS_IOLOCK_EXCL) | 
|  | 65 | mutex_unlock(&VFS_I(ip)->i_mutex); | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | static inline void | 
|  | 69 | xfs_rw_ilock_demote( | 
|  | 70 | struct xfs_inode	*ip, | 
|  | 71 | int			type) | 
|  | 72 | { | 
|  | 73 | xfs_ilock_demote(ip, type); | 
|  | 74 | if (type & XFS_IOLOCK_EXCL) | 
|  | 75 | mutex_unlock(&VFS_I(ip)->i_mutex); | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | /* | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 79 | *	xfs_iozero | 
|  | 80 | * | 
|  | 81 | *	xfs_iozero clears the specified range of buffer supplied, | 
|  | 82 | *	and marks all the affected blocks as valid and modified.  If | 
|  | 83 | *	an affected block is not allocated, it will be allocated.  If | 
|  | 84 | *	an affected block is not completely overwritten, and is not | 
|  | 85 | *	valid before the operation, it will be read from disk before | 
|  | 86 | *	being partially zeroed. | 
|  | 87 | */ | 
|  | 88 | STATIC int | 
|  | 89 | xfs_iozero( | 
|  | 90 | struct xfs_inode	*ip,	/* inode			*/ | 
|  | 91 | loff_t			pos,	/* offset in file		*/ | 
|  | 92 | size_t			count)	/* size of data to zero		*/ | 
|  | 93 | { | 
|  | 94 | struct page		*page; | 
|  | 95 | struct address_space	*mapping; | 
|  | 96 | int			status; | 
|  | 97 |  | 
|  | 98 | mapping = VFS_I(ip)->i_mapping; | 
|  | 99 | do { | 
|  | 100 | unsigned offset, bytes; | 
|  | 101 | void *fsdata; | 
|  | 102 |  | 
|  | 103 | offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */ | 
|  | 104 | bytes = PAGE_CACHE_SIZE - offset; | 
|  | 105 | if (bytes > count) | 
|  | 106 | bytes = count; | 
|  | 107 |  | 
|  | 108 | status = pagecache_write_begin(NULL, mapping, pos, bytes, | 
|  | 109 | AOP_FLAG_UNINTERRUPTIBLE, | 
|  | 110 | &page, &fsdata); | 
|  | 111 | if (status) | 
|  | 112 | break; | 
|  | 113 |  | 
|  | 114 | zero_user(page, offset, bytes); | 
|  | 115 |  | 
|  | 116 | status = pagecache_write_end(NULL, mapping, pos, bytes, bytes, | 
|  | 117 | page, fsdata); | 
|  | 118 | WARN_ON(status <= 0); /* can't return less than zero! */ | 
|  | 119 | pos += bytes; | 
|  | 120 | count -= bytes; | 
|  | 121 | status = 0; | 
|  | 122 | } while (count); | 
|  | 123 |  | 
|  | 124 | return (-status); | 
|  | 125 | } | 
|  | 126 |  | 
| Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 127 | STATIC int | 
|  | 128 | xfs_file_fsync( | 
|  | 129 | struct file		*file, | 
| Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 130 | int			datasync) | 
|  | 131 | { | 
| Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 132 | struct inode		*inode = file->f_mapping->host; | 
|  | 133 | struct xfs_inode	*ip = XFS_I(inode); | 
| Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 134 | struct xfs_trans	*tp; | 
|  | 135 | int			error = 0; | 
|  | 136 | int			log_flushed = 0; | 
|  | 137 |  | 
| Christoph Hellwig | cca28fb | 2010-06-24 11:57:09 +1000 | [diff] [blame] | 138 | trace_xfs_file_fsync(ip); | 
| Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 139 |  | 
|  | 140 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) | 
|  | 141 | return -XFS_ERROR(EIO); | 
|  | 142 |  | 
|  | 143 | xfs_iflags_clear(ip, XFS_ITRUNCATED); | 
|  | 144 |  | 
| Christoph Hellwig | 37bc574 | 2010-04-20 17:00:59 +1000 | [diff] [blame] | 145 | xfs_ioend_wait(ip); | 
|  | 146 |  | 
| Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 147 | /* | 
|  | 148 | * We always need to make sure that the required inode state is safe on | 
|  | 149 | * disk.  The inode might be clean but we still might need to force the | 
|  | 150 | * log because of committed transactions that haven't hit the disk yet. | 
|  | 151 | * Likewise, there could be unflushed non-transactional changes to the | 
|  | 152 | * inode core that have to go to disk and this requires us to issue | 
|  | 153 | * a synchronous transaction to capture these changes correctly. | 
|  | 154 | * | 
|  | 155 | * This code relies on the assumption that if the i_update_core field | 
|  | 156 | * of the inode is clear and the inode is unpinned then it is clean | 
|  | 157 | * and no action is required. | 
|  | 158 | */ | 
|  | 159 | xfs_ilock(ip, XFS_ILOCK_SHARED); | 
|  | 160 |  | 
| Christoph Hellwig | 66d834e | 2010-02-15 09:44:49 +0000 | [diff] [blame] | 161 | /* | 
|  | 162 | * First check if the VFS inode is marked dirty.  All the dirtying | 
|  | 163 | * of non-transactional updates no goes through mark_inode_dirty*, | 
|  | 164 | * which allows us to distinguish beteeen pure timestamp updates | 
|  | 165 | * and i_size updates which need to be caught for fdatasync. | 
|  | 166 | * After that also theck for the dirty state in the XFS inode, which | 
|  | 167 | * might gets cleared when the inode gets written out via the AIL | 
|  | 168 | * or xfs_iflush_cluster. | 
|  | 169 | */ | 
| Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 170 | if (((inode->i_state & I_DIRTY_DATASYNC) || | 
|  | 171 | ((inode->i_state & I_DIRTY_SYNC) && !datasync)) && | 
| Christoph Hellwig | 66d834e | 2010-02-15 09:44:49 +0000 | [diff] [blame] | 172 | ip->i_update_core) { | 
| Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 173 | /* | 
|  | 174 | * Kick off a transaction to log the inode core to get the | 
|  | 175 | * updates.  The sync transaction will also force the log. | 
|  | 176 | */ | 
|  | 177 | xfs_iunlock(ip, XFS_ILOCK_SHARED); | 
|  | 178 | tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS); | 
|  | 179 | error = xfs_trans_reserve(tp, 0, | 
|  | 180 | XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0); | 
|  | 181 | if (error) { | 
|  | 182 | xfs_trans_cancel(tp, 0); | 
|  | 183 | return -error; | 
|  | 184 | } | 
|  | 185 | xfs_ilock(ip, XFS_ILOCK_EXCL); | 
|  | 186 |  | 
|  | 187 | /* | 
|  | 188 | * Note - it's possible that we might have pushed ourselves out | 
|  | 189 | * of the way during trans_reserve which would flush the inode. | 
|  | 190 | * But there's no guarantee that the inode buffer has actually | 
|  | 191 | * gone out yet (it's delwri).	Plus the buffer could be pinned | 
|  | 192 | * anyway if it's part of an inode in another recent | 
|  | 193 | * transaction.	 So we play it safe and fire off the | 
|  | 194 | * transaction anyway. | 
|  | 195 | */ | 
| Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 196 | xfs_trans_ijoin(tp, ip); | 
| Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 197 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | 
|  | 198 | xfs_trans_set_sync(tp); | 
|  | 199 | error = _xfs_trans_commit(tp, 0, &log_flushed); | 
|  | 200 |  | 
|  | 201 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | 
|  | 202 | } else { | 
|  | 203 | /* | 
|  | 204 | * Timestamps/size haven't changed since last inode flush or | 
|  | 205 | * inode transaction commit.  That means either nothing got | 
|  | 206 | * written or a transaction committed which caught the updates. | 
|  | 207 | * If the latter happened and the transaction hasn't hit the | 
|  | 208 | * disk yet, the inode will be still be pinned.  If it is, | 
|  | 209 | * force the log. | 
|  | 210 | */ | 
| Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 211 | if (xfs_ipincount(ip)) { | 
| Christoph Hellwig | 024910c | 2010-02-17 19:34:57 +0000 | [diff] [blame] | 212 | error = _xfs_log_force_lsn(ip->i_mount, | 
|  | 213 | ip->i_itemp->ili_last_lsn, | 
|  | 214 | XFS_LOG_SYNC, &log_flushed); | 
| Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 215 | } | 
| Christoph Hellwig | 024910c | 2010-02-17 19:34:57 +0000 | [diff] [blame] | 216 | xfs_iunlock(ip, XFS_ILOCK_SHARED); | 
| Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 217 | } | 
|  | 218 |  | 
|  | 219 | if (ip->i_mount->m_flags & XFS_MOUNT_BARRIER) { | 
|  | 220 | /* | 
|  | 221 | * If the log write didn't issue an ordered tag we need | 
|  | 222 | * to flush the disk cache for the data device now. | 
|  | 223 | */ | 
|  | 224 | if (!log_flushed) | 
|  | 225 | xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp); | 
|  | 226 |  | 
|  | 227 | /* | 
|  | 228 | * If this inode is on the RT dev we need to flush that | 
|  | 229 | * cache as well. | 
|  | 230 | */ | 
|  | 231 | if (XFS_IS_REALTIME_INODE(ip)) | 
|  | 232 | xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp); | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | return -error; | 
|  | 236 | } | 
|  | 237 |  | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 238 | STATIC ssize_t | 
|  | 239 | xfs_file_aio_read( | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 240 | struct kiocb		*iocb, | 
|  | 241 | const struct iovec	*iovp, | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 242 | unsigned long		nr_segs, | 
|  | 243 | loff_t			pos) | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 244 | { | 
|  | 245 | struct file		*file = iocb->ki_filp; | 
|  | 246 | struct inode		*inode = file->f_mapping->host; | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 247 | struct xfs_inode	*ip = XFS_I(inode); | 
|  | 248 | struct xfs_mount	*mp = ip->i_mount; | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 249 | size_t			size = 0; | 
|  | 250 | ssize_t			ret = 0; | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 251 | int			ioflags = 0; | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 252 | xfs_fsize_t		n; | 
|  | 253 | unsigned long		seg; | 
|  | 254 |  | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 255 | XFS_STATS_INC(xs_read_calls); | 
|  | 256 |  | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 257 | BUG_ON(iocb->ki_pos != pos); | 
|  | 258 |  | 
|  | 259 | if (unlikely(file->f_flags & O_DIRECT)) | 
|  | 260 | ioflags |= IO_ISDIRECT; | 
|  | 261 | if (file->f_mode & FMODE_NOCMTIME) | 
|  | 262 | ioflags |= IO_INVIS; | 
|  | 263 |  | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 264 | /* START copy & waste from filemap.c */ | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 265 | for (seg = 0; seg < nr_segs; seg++) { | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 266 | const struct iovec *iv = &iovp[seg]; | 
|  | 267 |  | 
|  | 268 | /* | 
|  | 269 | * If any segment has a negative length, or the cumulative | 
|  | 270 | * length ever wraps negative then return -EINVAL. | 
|  | 271 | */ | 
|  | 272 | size += iv->iov_len; | 
|  | 273 | if (unlikely((ssize_t)(size|iv->iov_len) < 0)) | 
|  | 274 | return XFS_ERROR(-EINVAL); | 
|  | 275 | } | 
|  | 276 | /* END copy & waste from filemap.c */ | 
|  | 277 |  | 
|  | 278 | if (unlikely(ioflags & IO_ISDIRECT)) { | 
|  | 279 | xfs_buftarg_t	*target = | 
|  | 280 | XFS_IS_REALTIME_INODE(ip) ? | 
|  | 281 | mp->m_rtdev_targp : mp->m_ddev_targp; | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 282 | if ((iocb->ki_pos & target->bt_smask) || | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 283 | (size & target->bt_smask)) { | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 284 | if (iocb->ki_pos == ip->i_size) | 
|  | 285 | return 0; | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 286 | return -XFS_ERROR(EINVAL); | 
|  | 287 | } | 
|  | 288 | } | 
|  | 289 |  | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 290 | n = XFS_MAXIOFFSET(mp) - iocb->ki_pos; | 
|  | 291 | if (n <= 0 || size == 0) | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 292 | return 0; | 
|  | 293 |  | 
|  | 294 | if (n < size) | 
|  | 295 | size = n; | 
|  | 296 |  | 
|  | 297 | if (XFS_FORCED_SHUTDOWN(mp)) | 
|  | 298 | return -EIO; | 
|  | 299 |  | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 300 | if (unlikely(ioflags & IO_ISDIRECT)) { | 
| Dave Chinner | 487f84f | 2011-01-12 11:37:10 +1100 | [diff] [blame] | 301 | xfs_rw_ilock(ip, XFS_IOLOCK_EXCL); | 
|  | 302 |  | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 303 | if (inode->i_mapping->nrpages) { | 
|  | 304 | ret = -xfs_flushinval_pages(ip, | 
|  | 305 | (iocb->ki_pos & PAGE_CACHE_MASK), | 
|  | 306 | -1, FI_REMAPF_LOCKED); | 
| Dave Chinner | 487f84f | 2011-01-12 11:37:10 +1100 | [diff] [blame] | 307 | if (ret) { | 
|  | 308 | xfs_rw_iunlock(ip, XFS_IOLOCK_EXCL); | 
|  | 309 | return ret; | 
|  | 310 | } | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 311 | } | 
| Dave Chinner | 487f84f | 2011-01-12 11:37:10 +1100 | [diff] [blame] | 312 | xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL); | 
|  | 313 | } else | 
|  | 314 | xfs_rw_ilock(ip, XFS_IOLOCK_SHARED); | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 315 |  | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 316 | trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags); | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 317 |  | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 318 | ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos); | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 319 | if (ret > 0) | 
|  | 320 | XFS_STATS_ADD(xs_read_bytes, ret); | 
|  | 321 |  | 
| Dave Chinner | 487f84f | 2011-01-12 11:37:10 +1100 | [diff] [blame] | 322 | xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED); | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 323 | return ret; | 
|  | 324 | } | 
|  | 325 |  | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 326 | STATIC ssize_t | 
|  | 327 | xfs_file_splice_read( | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 328 | struct file		*infilp, | 
|  | 329 | loff_t			*ppos, | 
|  | 330 | struct pipe_inode_info	*pipe, | 
|  | 331 | size_t			count, | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 332 | unsigned int		flags) | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 333 | { | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 334 | struct xfs_inode	*ip = XFS_I(infilp->f_mapping->host); | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 335 | int			ioflags = 0; | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 336 | ssize_t			ret; | 
|  | 337 |  | 
|  | 338 | XFS_STATS_INC(xs_read_calls); | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 339 |  | 
|  | 340 | if (infilp->f_mode & FMODE_NOCMTIME) | 
|  | 341 | ioflags |= IO_INVIS; | 
|  | 342 |  | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 343 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) | 
|  | 344 | return -EIO; | 
|  | 345 |  | 
| Dave Chinner | 487f84f | 2011-01-12 11:37:10 +1100 | [diff] [blame] | 346 | xfs_rw_ilock(ip, XFS_IOLOCK_SHARED); | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 347 |  | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 348 | trace_xfs_file_splice_read(ip, count, *ppos, ioflags); | 
|  | 349 |  | 
|  | 350 | ret = generic_file_splice_read(infilp, ppos, pipe, count, flags); | 
|  | 351 | if (ret > 0) | 
|  | 352 | XFS_STATS_ADD(xs_read_bytes, ret); | 
|  | 353 |  | 
| Dave Chinner | 487f84f | 2011-01-12 11:37:10 +1100 | [diff] [blame] | 354 | xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED); | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 355 | return ret; | 
|  | 356 | } | 
|  | 357 |  | 
| Dave Chinner | edafb6d | 2011-01-11 10:14:06 +1100 | [diff] [blame] | 358 | STATIC void | 
|  | 359 | xfs_aio_write_isize_update( | 
|  | 360 | struct inode	*inode, | 
|  | 361 | loff_t		*ppos, | 
|  | 362 | ssize_t		bytes_written) | 
|  | 363 | { | 
|  | 364 | struct xfs_inode	*ip = XFS_I(inode); | 
|  | 365 | xfs_fsize_t		isize = i_size_read(inode); | 
|  | 366 |  | 
|  | 367 | if (bytes_written > 0) | 
|  | 368 | XFS_STATS_ADD(xs_write_bytes, bytes_written); | 
|  | 369 |  | 
|  | 370 | if (unlikely(bytes_written < 0 && bytes_written != -EFAULT && | 
|  | 371 | *ppos > isize)) | 
|  | 372 | *ppos = isize; | 
|  | 373 |  | 
|  | 374 | if (*ppos > ip->i_size) { | 
| Dave Chinner | 487f84f | 2011-01-12 11:37:10 +1100 | [diff] [blame] | 375 | xfs_rw_ilock(ip, XFS_ILOCK_EXCL); | 
| Dave Chinner | edafb6d | 2011-01-11 10:14:06 +1100 | [diff] [blame] | 376 | if (*ppos > ip->i_size) | 
|  | 377 | ip->i_size = *ppos; | 
| Dave Chinner | 487f84f | 2011-01-12 11:37:10 +1100 | [diff] [blame] | 378 | xfs_rw_iunlock(ip, XFS_ILOCK_EXCL); | 
| Dave Chinner | edafb6d | 2011-01-11 10:14:06 +1100 | [diff] [blame] | 379 | } | 
|  | 380 | } | 
|  | 381 |  | 
| Dave Chinner | 4c5cfd1 | 2011-01-11 10:14:16 +1100 | [diff] [blame] | 382 | /* | 
|  | 383 | * If this was a direct or synchronous I/O that failed (such as ENOSPC) then | 
|  | 384 | * part of the I/O may have been written to disk before the error occured.  In | 
|  | 385 | * this case the on-disk file size may have been adjusted beyond the in-memory | 
|  | 386 | * file size and now needs to be truncated back. | 
|  | 387 | */ | 
|  | 388 | STATIC void | 
|  | 389 | xfs_aio_write_newsize_update( | 
|  | 390 | struct xfs_inode	*ip) | 
|  | 391 | { | 
|  | 392 | if (ip->i_new_size) { | 
| Dave Chinner | 487f84f | 2011-01-12 11:37:10 +1100 | [diff] [blame] | 393 | xfs_rw_ilock(ip, XFS_ILOCK_EXCL); | 
| Dave Chinner | 4c5cfd1 | 2011-01-11 10:14:16 +1100 | [diff] [blame] | 394 | ip->i_new_size = 0; | 
|  | 395 | if (ip->i_d.di_size > ip->i_size) | 
|  | 396 | ip->i_d.di_size = ip->i_size; | 
| Dave Chinner | 487f84f | 2011-01-12 11:37:10 +1100 | [diff] [blame] | 397 | xfs_rw_iunlock(ip, XFS_ILOCK_EXCL); | 
| Dave Chinner | 4c5cfd1 | 2011-01-11 10:14:16 +1100 | [diff] [blame] | 398 | } | 
|  | 399 | } | 
|  | 400 |  | 
| Dave Chinner | 487f84f | 2011-01-12 11:37:10 +1100 | [diff] [blame] | 401 | /* | 
|  | 402 | * xfs_file_splice_write() does not use xfs_rw_ilock() because | 
|  | 403 | * generic_file_splice_write() takes the i_mutex itself. This, in theory, | 
|  | 404 | * couuld cause lock inversions between the aio_write path and the splice path | 
|  | 405 | * if someone is doing concurrent splice(2) based writes and write(2) based | 
|  | 406 | * writes to the same inode. The only real way to fix this is to re-implement | 
|  | 407 | * the generic code here with correct locking orders. | 
|  | 408 | */ | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 409 | STATIC ssize_t | 
|  | 410 | xfs_file_splice_write( | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 411 | struct pipe_inode_info	*pipe, | 
|  | 412 | struct file		*outfilp, | 
|  | 413 | loff_t			*ppos, | 
|  | 414 | size_t			count, | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 415 | unsigned int		flags) | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 416 | { | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 417 | struct inode		*inode = outfilp->f_mapping->host; | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 418 | struct xfs_inode	*ip = XFS_I(inode); | 
| Dave Chinner | edafb6d | 2011-01-11 10:14:06 +1100 | [diff] [blame] | 419 | xfs_fsize_t		new_size; | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 420 | int			ioflags = 0; | 
|  | 421 | ssize_t			ret; | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 422 |  | 
|  | 423 | XFS_STATS_INC(xs_write_calls); | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 424 |  | 
|  | 425 | if (outfilp->f_mode & FMODE_NOCMTIME) | 
|  | 426 | ioflags |= IO_INVIS; | 
|  | 427 |  | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 428 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) | 
|  | 429 | return -EIO; | 
|  | 430 |  | 
|  | 431 | xfs_ilock(ip, XFS_IOLOCK_EXCL); | 
|  | 432 |  | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 433 | new_size = *ppos + count; | 
|  | 434 |  | 
|  | 435 | xfs_ilock(ip, XFS_ILOCK_EXCL); | 
|  | 436 | if (new_size > ip->i_size) | 
|  | 437 | ip->i_new_size = new_size; | 
|  | 438 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | 
|  | 439 |  | 
|  | 440 | trace_xfs_file_splice_write(ip, count, *ppos, ioflags); | 
|  | 441 |  | 
|  | 442 | ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags); | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 443 |  | 
| Dave Chinner | edafb6d | 2011-01-11 10:14:06 +1100 | [diff] [blame] | 444 | xfs_aio_write_isize_update(inode, ppos, ret); | 
| Dave Chinner | 4c5cfd1 | 2011-01-11 10:14:16 +1100 | [diff] [blame] | 445 | xfs_aio_write_newsize_update(ip); | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 446 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); | 
|  | 447 | return ret; | 
|  | 448 | } | 
|  | 449 |  | 
|  | 450 | /* | 
|  | 451 | * This routine is called to handle zeroing any space in the last | 
|  | 452 | * block of the file that is beyond the EOF.  We do this since the | 
|  | 453 | * size is being increased without writing anything to that block | 
|  | 454 | * and we don't want anyone to read the garbage on the disk. | 
|  | 455 | */ | 
|  | 456 | STATIC int				/* error (positive) */ | 
|  | 457 | xfs_zero_last_block( | 
|  | 458 | xfs_inode_t	*ip, | 
|  | 459 | xfs_fsize_t	offset, | 
|  | 460 | xfs_fsize_t	isize) | 
|  | 461 | { | 
|  | 462 | xfs_fileoff_t	last_fsb; | 
|  | 463 | xfs_mount_t	*mp = ip->i_mount; | 
|  | 464 | int		nimaps; | 
|  | 465 | int		zero_offset; | 
|  | 466 | int		zero_len; | 
|  | 467 | int		error = 0; | 
|  | 468 | xfs_bmbt_irec_t	imap; | 
|  | 469 |  | 
|  | 470 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); | 
|  | 471 |  | 
|  | 472 | zero_offset = XFS_B_FSB_OFFSET(mp, isize); | 
|  | 473 | if (zero_offset == 0) { | 
|  | 474 | /* | 
|  | 475 | * There are no extra bytes in the last block on disk to | 
|  | 476 | * zero, so return. | 
|  | 477 | */ | 
|  | 478 | return 0; | 
|  | 479 | } | 
|  | 480 |  | 
|  | 481 | last_fsb = XFS_B_TO_FSBT(mp, isize); | 
|  | 482 | nimaps = 1; | 
|  | 483 | error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap, | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 484 | &nimaps, NULL); | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 485 | if (error) { | 
|  | 486 | return error; | 
|  | 487 | } | 
|  | 488 | ASSERT(nimaps > 0); | 
|  | 489 | /* | 
|  | 490 | * If the block underlying isize is just a hole, then there | 
|  | 491 | * is nothing to zero. | 
|  | 492 | */ | 
|  | 493 | if (imap.br_startblock == HOLESTARTBLOCK) { | 
|  | 494 | return 0; | 
|  | 495 | } | 
|  | 496 | /* | 
|  | 497 | * Zero the part of the last block beyond the EOF, and write it | 
|  | 498 | * out sync.  We need to drop the ilock while we do this so we | 
|  | 499 | * don't deadlock when the buffer cache calls back to us. | 
|  | 500 | */ | 
|  | 501 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | 
|  | 502 |  | 
|  | 503 | zero_len = mp->m_sb.sb_blocksize - zero_offset; | 
|  | 504 | if (isize + zero_len > offset) | 
|  | 505 | zero_len = offset - isize; | 
|  | 506 | error = xfs_iozero(ip, isize, zero_len); | 
|  | 507 |  | 
|  | 508 | xfs_ilock(ip, XFS_ILOCK_EXCL); | 
|  | 509 | ASSERT(error >= 0); | 
|  | 510 | return error; | 
|  | 511 | } | 
|  | 512 |  | 
|  | 513 | /* | 
|  | 514 | * Zero any on disk space between the current EOF and the new, | 
|  | 515 | * larger EOF.  This handles the normal case of zeroing the remainder | 
|  | 516 | * of the last block in the file and the unusual case of zeroing blocks | 
|  | 517 | * out beyond the size of the file.  This second case only happens | 
|  | 518 | * with fixed size extents and when the system crashes before the inode | 
|  | 519 | * size was updated but after blocks were allocated.  If fill is set, | 
|  | 520 | * then any holes in the range are filled and zeroed.  If not, the holes | 
|  | 521 | * are left alone as holes. | 
|  | 522 | */ | 
|  | 523 |  | 
|  | 524 | int					/* error (positive) */ | 
|  | 525 | xfs_zero_eof( | 
|  | 526 | xfs_inode_t	*ip, | 
|  | 527 | xfs_off_t	offset,		/* starting I/O offset */ | 
|  | 528 | xfs_fsize_t	isize)		/* current inode size */ | 
|  | 529 | { | 
|  | 530 | xfs_mount_t	*mp = ip->i_mount; | 
|  | 531 | xfs_fileoff_t	start_zero_fsb; | 
|  | 532 | xfs_fileoff_t	end_zero_fsb; | 
|  | 533 | xfs_fileoff_t	zero_count_fsb; | 
|  | 534 | xfs_fileoff_t	last_fsb; | 
|  | 535 | xfs_fileoff_t	zero_off; | 
|  | 536 | xfs_fsize_t	zero_len; | 
|  | 537 | int		nimaps; | 
|  | 538 | int		error = 0; | 
|  | 539 | xfs_bmbt_irec_t	imap; | 
|  | 540 |  | 
|  | 541 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL)); | 
|  | 542 | ASSERT(offset > isize); | 
|  | 543 |  | 
|  | 544 | /* | 
|  | 545 | * First handle zeroing the block on which isize resides. | 
|  | 546 | * We only zero a part of that block so it is handled specially. | 
|  | 547 | */ | 
|  | 548 | error = xfs_zero_last_block(ip, offset, isize); | 
|  | 549 | if (error) { | 
|  | 550 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL)); | 
|  | 551 | return error; | 
|  | 552 | } | 
|  | 553 |  | 
|  | 554 | /* | 
|  | 555 | * Calculate the range between the new size and the old | 
|  | 556 | * where blocks needing to be zeroed may exist.  To get the | 
|  | 557 | * block where the last byte in the file currently resides, | 
|  | 558 | * we need to subtract one from the size and truncate back | 
|  | 559 | * to a block boundary.  We subtract 1 in case the size is | 
|  | 560 | * exactly on a block boundary. | 
|  | 561 | */ | 
|  | 562 | last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1; | 
|  | 563 | start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize); | 
|  | 564 | end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1); | 
|  | 565 | ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb); | 
|  | 566 | if (last_fsb == end_zero_fsb) { | 
|  | 567 | /* | 
|  | 568 | * The size was only incremented on its last block. | 
|  | 569 | * We took care of that above, so just return. | 
|  | 570 | */ | 
|  | 571 | return 0; | 
|  | 572 | } | 
|  | 573 |  | 
|  | 574 | ASSERT(start_zero_fsb <= end_zero_fsb); | 
|  | 575 | while (start_zero_fsb <= end_zero_fsb) { | 
|  | 576 | nimaps = 1; | 
|  | 577 | zero_count_fsb = end_zero_fsb - start_zero_fsb + 1; | 
|  | 578 | error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb, | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 579 | 0, NULL, 0, &imap, &nimaps, NULL); | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 580 | if (error) { | 
|  | 581 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL)); | 
|  | 582 | return error; | 
|  | 583 | } | 
|  | 584 | ASSERT(nimaps > 0); | 
|  | 585 |  | 
|  | 586 | if (imap.br_state == XFS_EXT_UNWRITTEN || | 
|  | 587 | imap.br_startblock == HOLESTARTBLOCK) { | 
|  | 588 | /* | 
|  | 589 | * This loop handles initializing pages that were | 
|  | 590 | * partially initialized by the code below this | 
|  | 591 | * loop. It basically zeroes the part of the page | 
|  | 592 | * that sits on a hole and sets the page as P_HOLE | 
|  | 593 | * and calls remapf if it is a mapped file. | 
|  | 594 | */ | 
|  | 595 | start_zero_fsb = imap.br_startoff + imap.br_blockcount; | 
|  | 596 | ASSERT(start_zero_fsb <= (end_zero_fsb + 1)); | 
|  | 597 | continue; | 
|  | 598 | } | 
|  | 599 |  | 
|  | 600 | /* | 
|  | 601 | * There are blocks we need to zero. | 
|  | 602 | * Drop the inode lock while we're doing the I/O. | 
|  | 603 | * We'll still have the iolock to protect us. | 
|  | 604 | */ | 
|  | 605 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | 
|  | 606 |  | 
|  | 607 | zero_off = XFS_FSB_TO_B(mp, start_zero_fsb); | 
|  | 608 | zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount); | 
|  | 609 |  | 
|  | 610 | if ((zero_off + zero_len) > offset) | 
|  | 611 | zero_len = offset - zero_off; | 
|  | 612 |  | 
|  | 613 | error = xfs_iozero(ip, zero_off, zero_len); | 
|  | 614 | if (error) { | 
|  | 615 | goto out_lock; | 
|  | 616 | } | 
|  | 617 |  | 
|  | 618 | start_zero_fsb = imap.br_startoff + imap.br_blockcount; | 
|  | 619 | ASSERT(start_zero_fsb <= (end_zero_fsb + 1)); | 
|  | 620 |  | 
|  | 621 | xfs_ilock(ip, XFS_ILOCK_EXCL); | 
|  | 622 | } | 
|  | 623 |  | 
|  | 624 | return 0; | 
|  | 625 |  | 
|  | 626 | out_lock: | 
|  | 627 | xfs_ilock(ip, XFS_ILOCK_EXCL); | 
|  | 628 | ASSERT(error >= 0); | 
|  | 629 | return error; | 
|  | 630 | } | 
|  | 631 |  | 
| Dave Chinner | f0d26e8 | 2011-01-11 10:15:36 +1100 | [diff] [blame] | 632 | /* | 
| Dave Chinner | 4d8d158 | 2011-01-11 10:23:42 +1100 | [diff] [blame] | 633 | * Common pre-write limit and setup checks. | 
|  | 634 | * | 
|  | 635 | * Returns with iolock held according to @iolock. | 
|  | 636 | */ | 
|  | 637 | STATIC ssize_t | 
|  | 638 | xfs_file_aio_write_checks( | 
|  | 639 | struct file		*file, | 
|  | 640 | loff_t			*pos, | 
|  | 641 | size_t			*count, | 
|  | 642 | int			*iolock) | 
|  | 643 | { | 
|  | 644 | struct inode		*inode = file->f_mapping->host; | 
|  | 645 | struct xfs_inode	*ip = XFS_I(inode); | 
|  | 646 | xfs_fsize_t		new_size; | 
|  | 647 | int			error = 0; | 
|  | 648 |  | 
|  | 649 | error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode)); | 
|  | 650 | if (error) { | 
|  | 651 | xfs_rw_iunlock(ip, XFS_ILOCK_EXCL | *iolock); | 
|  | 652 | *iolock = 0; | 
|  | 653 | return error; | 
|  | 654 | } | 
|  | 655 |  | 
|  | 656 | new_size = *pos + *count; | 
|  | 657 | if (new_size > ip->i_size) | 
|  | 658 | ip->i_new_size = new_size; | 
|  | 659 |  | 
|  | 660 | if (likely(!(file->f_mode & FMODE_NOCMTIME))) | 
|  | 661 | file_update_time(file); | 
|  | 662 |  | 
|  | 663 | /* | 
|  | 664 | * If the offset is beyond the size of the file, we need to zero any | 
|  | 665 | * blocks that fall between the existing EOF and the start of this | 
|  | 666 | * write. | 
|  | 667 | */ | 
|  | 668 | if (*pos > ip->i_size) | 
|  | 669 | error = -xfs_zero_eof(ip, *pos, ip->i_size); | 
|  | 670 |  | 
|  | 671 | xfs_rw_iunlock(ip, XFS_ILOCK_EXCL); | 
|  | 672 | if (error) | 
|  | 673 | return error; | 
|  | 674 |  | 
|  | 675 | /* | 
|  | 676 | * If we're writing the file then make sure to clear the setuid and | 
|  | 677 | * setgid bits if the process is not being run by root.  This keeps | 
|  | 678 | * people from modifying setuid and setgid binaries. | 
|  | 679 | */ | 
|  | 680 | return file_remove_suid(file); | 
|  | 681 |  | 
|  | 682 | } | 
|  | 683 |  | 
|  | 684 | /* | 
| Dave Chinner | f0d26e8 | 2011-01-11 10:15:36 +1100 | [diff] [blame] | 685 | * xfs_file_dio_aio_write - handle direct IO writes | 
|  | 686 | * | 
|  | 687 | * Lock the inode appropriately to prepare for and issue a direct IO write. | 
| Dave Chinner | eda7798 | 2011-01-11 10:22:40 +1100 | [diff] [blame] | 688 | * By separating it from the buffered write path we remove all the tricky to | 
| Dave Chinner | f0d26e8 | 2011-01-11 10:15:36 +1100 | [diff] [blame] | 689 | * follow locking changes and looping. | 
|  | 690 | * | 
| Dave Chinner | eda7798 | 2011-01-11 10:22:40 +1100 | [diff] [blame] | 691 | * If there are cached pages or we're extending the file, we need IOLOCK_EXCL | 
|  | 692 | * until we're sure the bytes at the new EOF have been zeroed and/or the cached | 
|  | 693 | * pages are flushed out. | 
|  | 694 | * | 
|  | 695 | * In most cases the direct IO writes will be done holding IOLOCK_SHARED | 
|  | 696 | * allowing them to be done in parallel with reads and other direct IO writes. | 
|  | 697 | * However, if the IO is not aligned to filesystem blocks, the direct IO layer | 
|  | 698 | * needs to do sub-block zeroing and that requires serialisation against other | 
|  | 699 | * direct IOs to the same block. In this case we need to serialise the | 
|  | 700 | * submission of the unaligned IOs so that we don't get racing block zeroing in | 
|  | 701 | * the dio layer.  To avoid the problem with aio, we also need to wait for | 
|  | 702 | * outstanding IOs to complete so that unwritten extent conversion is completed | 
|  | 703 | * before we try to map the overlapping block. This is currently implemented by | 
|  | 704 | * hitting it with a big hammer (i.e. xfs_ioend_wait()). | 
|  | 705 | * | 
| Dave Chinner | f0d26e8 | 2011-01-11 10:15:36 +1100 | [diff] [blame] | 706 | * Returns with locks held indicated by @iolock and errors indicated by | 
|  | 707 | * negative return values. | 
|  | 708 | */ | 
|  | 709 | STATIC ssize_t | 
|  | 710 | xfs_file_dio_aio_write( | 
|  | 711 | struct kiocb		*iocb, | 
|  | 712 | const struct iovec	*iovp, | 
|  | 713 | unsigned long		nr_segs, | 
|  | 714 | loff_t			pos, | 
|  | 715 | size_t			ocount, | 
|  | 716 | int			*iolock) | 
|  | 717 | { | 
|  | 718 | struct file		*file = iocb->ki_filp; | 
|  | 719 | struct address_space	*mapping = file->f_mapping; | 
|  | 720 | struct inode		*inode = mapping->host; | 
|  | 721 | struct xfs_inode	*ip = XFS_I(inode); | 
|  | 722 | struct xfs_mount	*mp = ip->i_mount; | 
|  | 723 | ssize_t			ret = 0; | 
| Dave Chinner | f0d26e8 | 2011-01-11 10:15:36 +1100 | [diff] [blame] | 724 | size_t			count = ocount; | 
| Dave Chinner | eda7798 | 2011-01-11 10:22:40 +1100 | [diff] [blame] | 725 | int			unaligned_io = 0; | 
| Dave Chinner | f0d26e8 | 2011-01-11 10:15:36 +1100 | [diff] [blame] | 726 | struct xfs_buftarg	*target = XFS_IS_REALTIME_INODE(ip) ? | 
|  | 727 | mp->m_rtdev_targp : mp->m_ddev_targp; | 
|  | 728 |  | 
|  | 729 | *iolock = 0; | 
|  | 730 | if ((pos & target->bt_smask) || (count & target->bt_smask)) | 
|  | 731 | return -XFS_ERROR(EINVAL); | 
|  | 732 |  | 
| Dave Chinner | eda7798 | 2011-01-11 10:22:40 +1100 | [diff] [blame] | 733 | if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask)) | 
|  | 734 | unaligned_io = 1; | 
|  | 735 |  | 
|  | 736 | if (unaligned_io || mapping->nrpages || pos > ip->i_size) | 
| Dave Chinner | f0d26e8 | 2011-01-11 10:15:36 +1100 | [diff] [blame] | 737 | *iolock = XFS_IOLOCK_EXCL; | 
|  | 738 | else | 
|  | 739 | *iolock = XFS_IOLOCK_SHARED; | 
|  | 740 | xfs_rw_ilock(ip, XFS_ILOCK_EXCL | *iolock); | 
|  | 741 |  | 
| Dave Chinner | 4d8d158 | 2011-01-11 10:23:42 +1100 | [diff] [blame] | 742 | ret = xfs_file_aio_write_checks(file, &pos, &count, iolock); | 
|  | 743 | if (ret) | 
| Dave Chinner | f0d26e8 | 2011-01-11 10:15:36 +1100 | [diff] [blame] | 744 | return ret; | 
|  | 745 |  | 
|  | 746 | if (mapping->nrpages) { | 
|  | 747 | WARN_ON(*iolock != XFS_IOLOCK_EXCL); | 
|  | 748 | ret = -xfs_flushinval_pages(ip, (pos & PAGE_CACHE_MASK), -1, | 
|  | 749 | FI_REMAPF_LOCKED); | 
|  | 750 | if (ret) | 
|  | 751 | return ret; | 
|  | 752 | } | 
|  | 753 |  | 
| Dave Chinner | eda7798 | 2011-01-11 10:22:40 +1100 | [diff] [blame] | 754 | /* | 
|  | 755 | * If we are doing unaligned IO, wait for all other IO to drain, | 
|  | 756 | * otherwise demote the lock if we had to flush cached pages | 
|  | 757 | */ | 
|  | 758 | if (unaligned_io) | 
|  | 759 | xfs_ioend_wait(ip); | 
|  | 760 | else if (*iolock == XFS_IOLOCK_EXCL) { | 
| Dave Chinner | f0d26e8 | 2011-01-11 10:15:36 +1100 | [diff] [blame] | 761 | xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL); | 
|  | 762 | *iolock = XFS_IOLOCK_SHARED; | 
|  | 763 | } | 
|  | 764 |  | 
|  | 765 | trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0); | 
|  | 766 | ret = generic_file_direct_write(iocb, iovp, | 
|  | 767 | &nr_segs, pos, &iocb->ki_pos, count, ocount); | 
|  | 768 |  | 
|  | 769 | /* No fallback to buffered IO on errors for XFS. */ | 
|  | 770 | ASSERT(ret < 0 || ret == count); | 
|  | 771 | return ret; | 
|  | 772 | } | 
|  | 773 |  | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 774 | STATIC ssize_t | 
| Dave Chinner | 637bbc7 | 2011-01-11 10:17:30 +1100 | [diff] [blame] | 775 | xfs_file_buffered_aio_write( | 
|  | 776 | struct kiocb		*iocb, | 
|  | 777 | const struct iovec	*iovp, | 
|  | 778 | unsigned long		nr_segs, | 
|  | 779 | loff_t			pos, | 
|  | 780 | size_t			ocount, | 
|  | 781 | int			*iolock) | 
|  | 782 | { | 
|  | 783 | struct file		*file = iocb->ki_filp; | 
|  | 784 | struct address_space	*mapping = file->f_mapping; | 
|  | 785 | struct inode		*inode = mapping->host; | 
|  | 786 | struct xfs_inode	*ip = XFS_I(inode); | 
|  | 787 | ssize_t			ret; | 
|  | 788 | int			enospc = 0; | 
| Dave Chinner | 637bbc7 | 2011-01-11 10:17:30 +1100 | [diff] [blame] | 789 | size_t			count = ocount; | 
|  | 790 |  | 
|  | 791 | *iolock = XFS_IOLOCK_EXCL; | 
|  | 792 | xfs_rw_ilock(ip, XFS_ILOCK_EXCL | *iolock); | 
|  | 793 |  | 
| Dave Chinner | 4d8d158 | 2011-01-11 10:23:42 +1100 | [diff] [blame] | 794 | ret = xfs_file_aio_write_checks(file, &pos, &count, iolock); | 
|  | 795 | if (ret) | 
| Dave Chinner | 637bbc7 | 2011-01-11 10:17:30 +1100 | [diff] [blame] | 796 | return ret; | 
|  | 797 |  | 
|  | 798 | /* We can write back this queue in page reclaim */ | 
|  | 799 | current->backing_dev_info = mapping->backing_dev_info; | 
|  | 800 |  | 
|  | 801 | write_retry: | 
|  | 802 | trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0); | 
|  | 803 | ret = generic_file_buffered_write(iocb, iovp, nr_segs, | 
|  | 804 | pos, &iocb->ki_pos, count, ret); | 
|  | 805 | /* | 
|  | 806 | * if we just got an ENOSPC, flush the inode now we aren't holding any | 
|  | 807 | * page locks and retry *once* | 
|  | 808 | */ | 
|  | 809 | if (ret == -ENOSPC && !enospc) { | 
|  | 810 | ret = -xfs_flush_pages(ip, 0, -1, 0, FI_NONE); | 
|  | 811 | if (ret) | 
|  | 812 | return ret; | 
|  | 813 | enospc = 1; | 
|  | 814 | goto write_retry; | 
|  | 815 | } | 
|  | 816 | current->backing_dev_info = NULL; | 
|  | 817 | return ret; | 
|  | 818 | } | 
|  | 819 |  | 
|  | 820 | STATIC ssize_t | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 821 | xfs_file_aio_write( | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 822 | struct kiocb		*iocb, | 
|  | 823 | const struct iovec	*iovp, | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 824 | unsigned long		nr_segs, | 
|  | 825 | loff_t			pos) | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 826 | { | 
|  | 827 | struct file		*file = iocb->ki_filp; | 
|  | 828 | struct address_space	*mapping = file->f_mapping; | 
|  | 829 | struct inode		*inode = mapping->host; | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 830 | struct xfs_inode	*ip = XFS_I(inode); | 
| Dave Chinner | 637bbc7 | 2011-01-11 10:17:30 +1100 | [diff] [blame] | 831 | ssize_t			ret; | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 832 | int			iolock; | 
| Dave Chinner | 637bbc7 | 2011-01-11 10:17:30 +1100 | [diff] [blame] | 833 | size_t			ocount = 0; | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 834 |  | 
|  | 835 | XFS_STATS_INC(xs_write_calls); | 
|  | 836 |  | 
| Christoph Hellwig | 00258e3 | 2010-02-15 09:44:47 +0000 | [diff] [blame] | 837 | BUG_ON(iocb->ki_pos != pos); | 
|  | 838 |  | 
| Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 839 | ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ); | 
|  | 840 | if (ret) | 
|  | 841 | return ret; | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 842 |  | 
| Dave Chinner | 637bbc7 | 2011-01-11 10:17:30 +1100 | [diff] [blame] | 843 | if (ocount == 0) | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 844 | return 0; | 
|  | 845 |  | 
| Dave Chinner | 637bbc7 | 2011-01-11 10:17:30 +1100 | [diff] [blame] | 846 | xfs_wait_for_freeze(ip->i_mount, SB_FREEZE_WRITE); | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 847 |  | 
| Dave Chinner | 637bbc7 | 2011-01-11 10:17:30 +1100 | [diff] [blame] | 848 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 849 | return -EIO; | 
|  | 850 |  | 
| Dave Chinner | 637bbc7 | 2011-01-11 10:17:30 +1100 | [diff] [blame] | 851 | if (unlikely(file->f_flags & O_DIRECT)) | 
| Dave Chinner | f0d26e8 | 2011-01-11 10:15:36 +1100 | [diff] [blame] | 852 | ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos, | 
|  | 853 | ocount, &iolock); | 
| Dave Chinner | 637bbc7 | 2011-01-11 10:17:30 +1100 | [diff] [blame] | 854 | else | 
|  | 855 | ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos, | 
|  | 856 | ocount, &iolock); | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 857 |  | 
| Dave Chinner | edafb6d | 2011-01-11 10:14:06 +1100 | [diff] [blame] | 858 | xfs_aio_write_isize_update(inode, &iocb->ki_pos, ret); | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 859 |  | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 860 | if (ret <= 0) | 
| Dave Chinner | 637bbc7 | 2011-01-11 10:17:30 +1100 | [diff] [blame] | 861 | goto out_unlock; | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 862 |  | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 863 | /* Handle various SYNC-type writes */ | 
|  | 864 | if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) { | 
|  | 865 | loff_t end = pos + ret - 1; | 
| Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 866 | int error, error2; | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 867 |  | 
| Dave Chinner | 487f84f | 2011-01-12 11:37:10 +1100 | [diff] [blame] | 868 | xfs_rw_iunlock(ip, iolock); | 
| Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 869 | error = filemap_write_and_wait_range(mapping, pos, end); | 
| Dave Chinner | 487f84f | 2011-01-12 11:37:10 +1100 | [diff] [blame] | 870 | xfs_rw_ilock(ip, iolock); | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 871 |  | 
| Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 872 | error2 = -xfs_file_fsync(file, | 
| Christoph Hellwig | fd3200b | 2010-02-15 09:44:48 +0000 | [diff] [blame] | 873 | (file->f_flags & __O_SYNC) ? 0 : 1); | 
| Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 874 | if (error) | 
|  | 875 | ret = error; | 
|  | 876 | else if (error2) | 
|  | 877 | ret = error2; | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 878 | } | 
|  | 879 |  | 
| Dave Chinner | 637bbc7 | 2011-01-11 10:17:30 +1100 | [diff] [blame] | 880 | out_unlock: | 
| Dave Chinner | 4c5cfd1 | 2011-01-11 10:14:16 +1100 | [diff] [blame] | 881 | xfs_aio_write_newsize_update(ip); | 
| Dave Chinner | 487f84f | 2011-01-12 11:37:10 +1100 | [diff] [blame] | 882 | xfs_rw_iunlock(ip, iolock); | 
| Dave Chinner | a363f0c | 2011-01-11 10:13:53 +1100 | [diff] [blame] | 883 | return ret; | 
| Christoph Hellwig | dda35b8 | 2010-02-15 09:44:46 +0000 | [diff] [blame] | 884 | } | 
|  | 885 |  | 
| Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 886 | STATIC long | 
|  | 887 | xfs_file_fallocate( | 
|  | 888 | struct file	*file, | 
|  | 889 | int		mode, | 
|  | 890 | loff_t		offset, | 
|  | 891 | loff_t		len) | 
|  | 892 | { | 
|  | 893 | struct inode	*inode = file->f_path.dentry->d_inode; | 
|  | 894 | long		error; | 
|  | 895 | loff_t		new_size = 0; | 
|  | 896 | xfs_flock64_t	bf; | 
|  | 897 | xfs_inode_t	*ip = XFS_I(inode); | 
|  | 898 | int		cmd = XFS_IOC_RESVSP; | 
| Dave Chinner | 8287889 | 2011-03-26 09:13:08 +1100 | [diff] [blame] | 899 | int		attr_flags = XFS_ATTR_NOLOCK; | 
| Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 900 |  | 
|  | 901 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) | 
|  | 902 | return -EOPNOTSUPP; | 
|  | 903 |  | 
|  | 904 | bf.l_whence = 0; | 
|  | 905 | bf.l_start = offset; | 
|  | 906 | bf.l_len = len; | 
|  | 907 |  | 
|  | 908 | xfs_ilock(ip, XFS_IOLOCK_EXCL); | 
|  | 909 |  | 
|  | 910 | if (mode & FALLOC_FL_PUNCH_HOLE) | 
|  | 911 | cmd = XFS_IOC_UNRESVSP; | 
|  | 912 |  | 
|  | 913 | /* check the new inode size is valid before allocating */ | 
|  | 914 | if (!(mode & FALLOC_FL_KEEP_SIZE) && | 
|  | 915 | offset + len > i_size_read(inode)) { | 
|  | 916 | new_size = offset + len; | 
|  | 917 | error = inode_newsize_ok(inode, new_size); | 
|  | 918 | if (error) | 
|  | 919 | goto out_unlock; | 
|  | 920 | } | 
|  | 921 |  | 
| Dave Chinner | 8287889 | 2011-03-26 09:13:08 +1100 | [diff] [blame] | 922 | if (file->f_flags & O_DSYNC) | 
|  | 923 | attr_flags |= XFS_ATTR_SYNC; | 
|  | 924 |  | 
|  | 925 | error = -xfs_change_file_space(ip, cmd, &bf, 0, attr_flags); | 
| Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 926 | if (error) | 
|  | 927 | goto out_unlock; | 
|  | 928 |  | 
|  | 929 | /* Change file size if needed */ | 
|  | 930 | if (new_size) { | 
|  | 931 | struct iattr iattr; | 
|  | 932 |  | 
|  | 933 | iattr.ia_valid = ATTR_SIZE; | 
|  | 934 | iattr.ia_size = new_size; | 
|  | 935 | error = -xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK); | 
|  | 936 | } | 
|  | 937 |  | 
|  | 938 | out_unlock: | 
|  | 939 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); | 
|  | 940 | return error; | 
|  | 941 | } | 
|  | 942 |  | 
|  | 943 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | STATIC int | 
| Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 945 | xfs_file_open( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | struct inode	*inode, | 
| Christoph Hellwig | f999a5b | 2008-11-28 14:23:32 +1100 | [diff] [blame] | 947 | struct file	*file) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | { | 
| Christoph Hellwig | f999a5b | 2008-11-28 14:23:32 +1100 | [diff] [blame] | 949 | if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | return -EFBIG; | 
| Christoph Hellwig | f999a5b | 2008-11-28 14:23:32 +1100 | [diff] [blame] | 951 | if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb))) | 
|  | 952 | return -EIO; | 
|  | 953 | return 0; | 
|  | 954 | } | 
|  | 955 |  | 
|  | 956 | STATIC int | 
|  | 957 | xfs_dir_open( | 
|  | 958 | struct inode	*inode, | 
|  | 959 | struct file	*file) | 
|  | 960 | { | 
|  | 961 | struct xfs_inode *ip = XFS_I(inode); | 
|  | 962 | int		mode; | 
|  | 963 | int		error; | 
|  | 964 |  | 
|  | 965 | error = xfs_file_open(inode, file); | 
|  | 966 | if (error) | 
|  | 967 | return error; | 
|  | 968 |  | 
|  | 969 | /* | 
|  | 970 | * If there are any blocks, read-ahead block 0 as we're almost | 
|  | 971 | * certain to have the next operation be a read there. | 
|  | 972 | */ | 
|  | 973 | mode = xfs_ilock_map_shared(ip); | 
|  | 974 | if (ip->i_d.di_nextents > 0) | 
|  | 975 | xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK); | 
|  | 976 | xfs_iunlock(ip, mode); | 
|  | 977 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | } | 
|  | 979 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | STATIC int | 
| Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 981 | xfs_file_release( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | struct inode	*inode, | 
|  | 983 | struct file	*filp) | 
|  | 984 | { | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 985 | return -xfs_release(XFS_I(inode)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | } | 
|  | 987 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | STATIC int | 
| Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 989 | xfs_file_readdir( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | struct file	*filp, | 
|  | 991 | void		*dirent, | 
|  | 992 | filldir_t	filldir) | 
|  | 993 | { | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 994 | struct inode	*inode = filp->f_path.dentry->d_inode; | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 995 | xfs_inode_t	*ip = XFS_I(inode); | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 996 | int		error; | 
|  | 997 | size_t		bufsize; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 |  | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 999 | /* | 
|  | 1000 | * The Linux API doesn't pass down the total size of the buffer | 
|  | 1001 | * we read into down to the filesystem.  With the filldir concept | 
|  | 1002 | * it's not needed for correct information, but the XFS dir2 leaf | 
|  | 1003 | * code wants an estimate of the buffer size to calculate it's | 
|  | 1004 | * readahead window and size the buffers used for mapping to | 
|  | 1005 | * physical blocks. | 
|  | 1006 | * | 
|  | 1007 | * Try to give it an estimate that's good enough, maybe at some | 
|  | 1008 | * point we can change the ->readdir prototype to include the | 
| Eric Sandeen | a9cc799 | 2010-02-03 17:50:13 +0000 | [diff] [blame] | 1009 | * buffer size.  For now we use the current glibc buffer size. | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 1010 | */ | 
| Eric Sandeen | a9cc799 | 2010-02-03 17:50:13 +0000 | [diff] [blame] | 1011 | bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 |  | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 1013 | error = xfs_readdir(ip, dirent, bufsize, | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 1014 | (xfs_off_t *)&filp->f_pos, filldir); | 
|  | 1015 | if (error) | 
|  | 1016 | return -error; | 
|  | 1017 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | } | 
|  | 1019 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | STATIC int | 
| Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 1021 | xfs_file_mmap( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | struct file	*filp, | 
|  | 1023 | struct vm_area_struct *vma) | 
|  | 1024 | { | 
| Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 1025 | vma->vm_ops = &xfs_file_vm_ops; | 
| Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1026 | vma->vm_flags |= VM_CAN_NONLINEAR; | 
| Dean Roehrich | 6fac0cb | 2005-06-21 14:07:45 +1000 | [diff] [blame] | 1027 |  | 
| Nathan Scott | fbc1462 | 2006-06-09 14:52:13 +1000 | [diff] [blame] | 1028 | file_accessed(filp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | return 0; | 
|  | 1030 | } | 
|  | 1031 |  | 
| David Chinner | 4f57dbc | 2007-07-19 16:28:17 +1000 | [diff] [blame] | 1032 | /* | 
|  | 1033 | * mmap()d file has taken write protection fault and is being made | 
|  | 1034 | * writable. We can set the page state up correctly for a writable | 
|  | 1035 | * page, which means we can do correct delalloc accounting (ENOSPC | 
|  | 1036 | * checking!) and unwritten extent mapping. | 
|  | 1037 | */ | 
|  | 1038 | STATIC int | 
|  | 1039 | xfs_vm_page_mkwrite( | 
|  | 1040 | struct vm_area_struct	*vma, | 
| Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 1041 | struct vm_fault		*vmf) | 
| David Chinner | 4f57dbc | 2007-07-19 16:28:17 +1000 | [diff] [blame] | 1042 | { | 
| Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 1043 | return block_page_mkwrite(vma, vmf, xfs_get_blocks); | 
| David Chinner | 4f57dbc | 2007-07-19 16:28:17 +1000 | [diff] [blame] | 1044 | } | 
|  | 1045 |  | 
| Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1046 | const struct file_operations xfs_file_operations = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | .llseek		= generic_file_llseek, | 
|  | 1048 | .read		= do_sync_read, | 
| Dean Roehrich | bb3f724 | 2005-09-02 15:43:05 +1000 | [diff] [blame] | 1049 | .write		= do_sync_write, | 
| Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 1050 | .aio_read	= xfs_file_aio_read, | 
|  | 1051 | .aio_write	= xfs_file_aio_write, | 
| Nathan Scott | 1b89584 | 2006-03-31 13:08:59 +1000 | [diff] [blame] | 1052 | .splice_read	= xfs_file_splice_read, | 
|  | 1053 | .splice_write	= xfs_file_splice_write, | 
| Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 1054 | .unlocked_ioctl	= xfs_file_ioctl, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | #ifdef CONFIG_COMPAT | 
| Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 1056 | .compat_ioctl	= xfs_file_compat_ioctl, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | #endif | 
| Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 1058 | .mmap		= xfs_file_mmap, | 
|  | 1059 | .open		= xfs_file_open, | 
|  | 1060 | .release	= xfs_file_release, | 
|  | 1061 | .fsync		= xfs_file_fsync, | 
| Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 1062 | .fallocate	= xfs_file_fallocate, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | }; | 
|  | 1064 |  | 
| Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1065 | const struct file_operations xfs_dir_file_operations = { | 
| Christoph Hellwig | f999a5b | 2008-11-28 14:23:32 +1100 | [diff] [blame] | 1066 | .open		= xfs_dir_open, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | .read		= generic_read_dir, | 
| Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 1068 | .readdir	= xfs_file_readdir, | 
| Al Viro | 59af158 | 2008-08-24 07:24:41 -0400 | [diff] [blame] | 1069 | .llseek		= generic_file_llseek, | 
| Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 1070 | .unlocked_ioctl	= xfs_file_ioctl, | 
| Nathan Scott | d387039 | 2005-05-06 06:44:46 -0700 | [diff] [blame] | 1071 | #ifdef CONFIG_COMPAT | 
| Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 1072 | .compat_ioctl	= xfs_file_compat_ioctl, | 
| Nathan Scott | d387039 | 2005-05-06 06:44:46 -0700 | [diff] [blame] | 1073 | #endif | 
| Nathan Scott | 3562fd4 | 2006-03-14 14:00:35 +1100 | [diff] [blame] | 1074 | .fsync		= xfs_file_fsync, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | }; | 
|  | 1076 |  | 
| Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 1077 | static const struct vm_operations_struct xfs_file_vm_ops = { | 
| Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 1078 | .fault		= filemap_fault, | 
| David Chinner | 4f57dbc | 2007-07-19 16:28:17 +1000 | [diff] [blame] | 1079 | .page_mkwrite	= xfs_vm_page_mkwrite, | 
| Dean Roehrich | 6fac0cb | 2005-06-21 14:07:45 +1000 | [diff] [blame] | 1080 | }; |