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