blob: 86d5dc2604642d45074f3876fb656116897e2ef7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * 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 Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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 Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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 Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000019#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
27#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_dinode.h"
30#include "xfs_inode.h"
Christoph Hellwigfd3200b2010-02-15 09:44:48 +000031#include "xfs_inode_item.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000032#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_error.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100034#include "xfs_vnodeops.h"
Christoph Hellwigf999a5b2008-11-28 14:23:32 +110035#include "xfs_da_btree.h"
Christoph Hellwigddcd8562008-12-03 07:55:34 -050036#include "xfs_ioctl.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000037#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <linux/dcache.h>
Christoph Hellwig2fe17c12011-01-14 13:07:43 +010040#include <linux/falloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040042static const struct vm_operations_struct xfs_file_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Christoph Hellwigdda35b82010-02-15 09:44:46 +000044/*
Dave Chinner487f84f2011-01-12 11:37:10 +110045 * 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 */
48static inline void
49xfs_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
58static inline void
59xfs_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
68static inline void
69xfs_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 Hellwigdda35b82010-02-15 09:44:46 +000079 * 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 */
88STATIC int
89xfs_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 Hellwig1da2f2d2011-10-02 14:25:16 +0000127/*
128 * Fsync operations on directories are much simpler than on regular files,
129 * as there is no file data to flush, and thus also no need for explicit
130 * cache flush operations, and there are no non-transaction metadata updates
131 * on directories either.
132 */
133STATIC int
134xfs_dir_fsync(
135 struct file *file,
136 loff_t start,
137 loff_t end,
138 int datasync)
139{
140 struct xfs_inode *ip = XFS_I(file->f_mapping->host);
141 struct xfs_mount *mp = ip->i_mount;
142 xfs_lsn_t lsn = 0;
143
144 trace_xfs_dir_fsync(ip);
145
146 xfs_ilock(ip, XFS_ILOCK_SHARED);
147 if (xfs_ipincount(ip))
148 lsn = ip->i_itemp->ili_last_lsn;
149 xfs_iunlock(ip, XFS_ILOCK_SHARED);
150
151 if (!lsn)
152 return 0;
153 return _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
154}
155
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000156STATIC int
157xfs_file_fsync(
158 struct file *file,
Josef Bacik02c24a82011-07-16 20:44:56 -0400159 loff_t start,
160 loff_t end,
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000161 int datasync)
162{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200163 struct inode *inode = file->f_mapping->host;
164 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000165 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000166 struct xfs_trans *tp;
167 int error = 0;
168 int log_flushed = 0;
Christoph Hellwigb1037052011-09-19 14:55:51 +0000169 xfs_lsn_t lsn = 0;
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000170
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000171 trace_xfs_file_fsync(ip);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000172
Josef Bacik02c24a82011-07-16 20:44:56 -0400173 error = filemap_write_and_wait_range(inode->i_mapping, start, end);
174 if (error)
175 return error;
176
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000177 if (XFS_FORCED_SHUTDOWN(mp))
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000178 return -XFS_ERROR(EIO);
179
180 xfs_iflags_clear(ip, XFS_ITRUNCATED);
181
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000182 if (mp->m_flags & XFS_MOUNT_BARRIER) {
183 /*
184 * If we have an RT and/or log subvolume we need to make sure
185 * to flush the write cache the device used for file data
186 * first. This is to ensure newly written file data make
187 * it to disk before logging the new inode size in case of
188 * an extending write.
189 */
190 if (XFS_IS_REALTIME_INODE(ip))
191 xfs_blkdev_issue_flush(mp->m_rtdev_targp);
192 else if (mp->m_logdev_targp != mp->m_ddev_targp)
193 xfs_blkdev_issue_flush(mp->m_ddev_targp);
194 }
195
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000196 /*
197 * We always need to make sure that the required inode state is safe on
198 * disk. The inode might be clean but we still might need to force the
199 * log because of committed transactions that haven't hit the disk yet.
200 * Likewise, there could be unflushed non-transactional changes to the
201 * inode core that have to go to disk and this requires us to issue
202 * a synchronous transaction to capture these changes correctly.
203 *
204 * This code relies on the assumption that if the i_update_core field
205 * of the inode is clear and the inode is unpinned then it is clean
206 * and no action is required.
207 */
208 xfs_ilock(ip, XFS_ILOCK_SHARED);
209
Christoph Hellwig66d834e2010-02-15 09:44:49 +0000210 /*
211 * First check if the VFS inode is marked dirty. All the dirtying
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800212 * of non-transactional updates do not go through mark_inode_dirty*,
213 * which allows us to distinguish between pure timestamp updates
Christoph Hellwig66d834e2010-02-15 09:44:49 +0000214 * and i_size updates which need to be caught for fdatasync.
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800215 * After that also check for the dirty state in the XFS inode, which
Christoph Hellwig66d834e2010-02-15 09:44:49 +0000216 * might gets cleared when the inode gets written out via the AIL
217 * or xfs_iflush_cluster.
218 */
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200219 if (((inode->i_state & I_DIRTY_DATASYNC) ||
220 ((inode->i_state & I_DIRTY_SYNC) && !datasync)) &&
Christoph Hellwig66d834e2010-02-15 09:44:49 +0000221 ip->i_update_core) {
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000222 /*
223 * Kick off a transaction to log the inode core to get the
224 * updates. The sync transaction will also force the log.
225 */
226 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000227 tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000228 error = xfs_trans_reserve(tp, 0,
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000229 XFS_FSYNC_TS_LOG_RES(mp), 0, 0, 0);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000230 if (error) {
231 xfs_trans_cancel(tp, 0);
232 return -error;
233 }
234 xfs_ilock(ip, XFS_ILOCK_EXCL);
235
236 /*
237 * Note - it's possible that we might have pushed ourselves out
238 * of the way during trans_reserve which would flush the inode.
239 * But there's no guarantee that the inode buffer has actually
240 * gone out yet (it's delwri). Plus the buffer could be pinned
241 * anyway if it's part of an inode in another recent
242 * transaction. So we play it safe and fire off the
243 * transaction anyway.
244 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000245 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000246 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwigb1037052011-09-19 14:55:51 +0000247 error = xfs_trans_commit(tp, 0);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000248
Christoph Hellwigb1037052011-09-19 14:55:51 +0000249 lsn = ip->i_itemp->ili_last_lsn;
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000250 xfs_iunlock(ip, XFS_ILOCK_EXCL);
251 } else {
252 /*
253 * Timestamps/size haven't changed since last inode flush or
254 * inode transaction commit. That means either nothing got
255 * written or a transaction committed which caught the updates.
256 * If the latter happened and the transaction hasn't hit the
257 * disk yet, the inode will be still be pinned. If it is,
258 * force the log.
259 */
Christoph Hellwigb1037052011-09-19 14:55:51 +0000260 if (xfs_ipincount(ip))
261 lsn = ip->i_itemp->ili_last_lsn;
Christoph Hellwig024910c2010-02-17 19:34:57 +0000262 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000263 }
264
Christoph Hellwigb1037052011-09-19 14:55:51 +0000265 if (!error && lsn)
266 error = _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, &log_flushed);
267
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000268 /*
269 * If we only have a single device, and the log force about was
270 * a no-op we might have to flush the data device cache here.
271 * This can only happen for fdatasync/O_DSYNC if we were overwriting
272 * an already allocated file and thus do not have any metadata to
273 * commit.
274 */
275 if ((mp->m_flags & XFS_MOUNT_BARRIER) &&
276 mp->m_logdev_targp == mp->m_ddev_targp &&
277 !XFS_IS_REALTIME_INODE(ip) &&
278 !log_flushed)
279 xfs_blkdev_issue_flush(mp->m_ddev_targp);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000280
281 return -error;
282}
283
Christoph Hellwig00258e32010-02-15 09:44:47 +0000284STATIC ssize_t
285xfs_file_aio_read(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000286 struct kiocb *iocb,
287 const struct iovec *iovp,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000288 unsigned long nr_segs,
289 loff_t pos)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000290{
291 struct file *file = iocb->ki_filp;
292 struct inode *inode = file->f_mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000293 struct xfs_inode *ip = XFS_I(inode);
294 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000295 size_t size = 0;
296 ssize_t ret = 0;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000297 int ioflags = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000298 xfs_fsize_t n;
299 unsigned long seg;
300
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000301 XFS_STATS_INC(xs_read_calls);
302
Christoph Hellwig00258e32010-02-15 09:44:47 +0000303 BUG_ON(iocb->ki_pos != pos);
304
305 if (unlikely(file->f_flags & O_DIRECT))
306 ioflags |= IO_ISDIRECT;
307 if (file->f_mode & FMODE_NOCMTIME)
308 ioflags |= IO_INVIS;
309
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000310 /* START copy & waste from filemap.c */
Christoph Hellwig00258e32010-02-15 09:44:47 +0000311 for (seg = 0; seg < nr_segs; seg++) {
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000312 const struct iovec *iv = &iovp[seg];
313
314 /*
315 * If any segment has a negative length, or the cumulative
316 * length ever wraps negative then return -EINVAL.
317 */
318 size += iv->iov_len;
319 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
320 return XFS_ERROR(-EINVAL);
321 }
322 /* END copy & waste from filemap.c */
323
324 if (unlikely(ioflags & IO_ISDIRECT)) {
325 xfs_buftarg_t *target =
326 XFS_IS_REALTIME_INODE(ip) ?
327 mp->m_rtdev_targp : mp->m_ddev_targp;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000328 if ((iocb->ki_pos & target->bt_smask) ||
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000329 (size & target->bt_smask)) {
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000330 if (iocb->ki_pos == i_size_read(inode))
Christoph Hellwig00258e32010-02-15 09:44:47 +0000331 return 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000332 return -XFS_ERROR(EINVAL);
333 }
334 }
335
Christoph Hellwig00258e32010-02-15 09:44:47 +0000336 n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
337 if (n <= 0 || size == 0)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000338 return 0;
339
340 if (n < size)
341 size = n;
342
343 if (XFS_FORCED_SHUTDOWN(mp))
344 return -EIO;
345
Dave Chinner0c38a252011-08-25 07:17:01 +0000346 /*
347 * Locking is a bit tricky here. If we take an exclusive lock
348 * for direct IO, we effectively serialise all new concurrent
349 * read IO to this file and block it behind IO that is currently in
350 * progress because IO in progress holds the IO lock shared. We only
351 * need to hold the lock exclusive to blow away the page cache, so
352 * only take lock exclusively if the page cache needs invalidation.
353 * This allows the normal direct IO case of no page cache pages to
354 * proceeed concurrently without serialisation.
355 */
356 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
357 if ((ioflags & IO_ISDIRECT) && inode->i_mapping->nrpages) {
358 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
Dave Chinner487f84f2011-01-12 11:37:10 +1100359 xfs_rw_ilock(ip, XFS_IOLOCK_EXCL);
360
Christoph Hellwig00258e32010-02-15 09:44:47 +0000361 if (inode->i_mapping->nrpages) {
362 ret = -xfs_flushinval_pages(ip,
363 (iocb->ki_pos & PAGE_CACHE_MASK),
364 -1, FI_REMAPF_LOCKED);
Dave Chinner487f84f2011-01-12 11:37:10 +1100365 if (ret) {
366 xfs_rw_iunlock(ip, XFS_IOLOCK_EXCL);
367 return ret;
368 }
Christoph Hellwig00258e32010-02-15 09:44:47 +0000369 }
Dave Chinner487f84f2011-01-12 11:37:10 +1100370 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
Dave Chinner0c38a252011-08-25 07:17:01 +0000371 }
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000372
Christoph Hellwig00258e32010-02-15 09:44:47 +0000373 trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000374
Christoph Hellwig00258e32010-02-15 09:44:47 +0000375 ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000376 if (ret > 0)
377 XFS_STATS_ADD(xs_read_bytes, ret);
378
Dave Chinner487f84f2011-01-12 11:37:10 +1100379 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000380 return ret;
381}
382
Christoph Hellwig00258e32010-02-15 09:44:47 +0000383STATIC ssize_t
384xfs_file_splice_read(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000385 struct file *infilp,
386 loff_t *ppos,
387 struct pipe_inode_info *pipe,
388 size_t count,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000389 unsigned int flags)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000390{
Christoph Hellwig00258e32010-02-15 09:44:47 +0000391 struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000392 int ioflags = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000393 ssize_t ret;
394
395 XFS_STATS_INC(xs_read_calls);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000396
397 if (infilp->f_mode & FMODE_NOCMTIME)
398 ioflags |= IO_INVIS;
399
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000400 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
401 return -EIO;
402
Dave Chinner487f84f2011-01-12 11:37:10 +1100403 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000404
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000405 trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
406
407 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
408 if (ret > 0)
409 XFS_STATS_ADD(xs_read_bytes, ret);
410
Dave Chinner487f84f2011-01-12 11:37:10 +1100411 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000412 return ret;
413}
414
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100415/*
416 * If this was a direct or synchronous I/O that failed (such as ENOSPC) then
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300417 * part of the I/O may have been written to disk before the error occurred. In
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100418 * this case the on-disk file size may have been adjusted beyond the in-memory
419 * file size and now needs to be truncated back.
420 */
421STATIC void
422xfs_aio_write_newsize_update(
Dave Chinner7271d242011-08-25 07:17:02 +0000423 struct xfs_inode *ip,
424 xfs_fsize_t new_size)
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100425{
Dave Chinner7271d242011-08-25 07:17:02 +0000426 if (new_size == ip->i_new_size) {
Dave Chinner487f84f2011-01-12 11:37:10 +1100427 xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner7271d242011-08-25 07:17:02 +0000428 if (new_size == ip->i_new_size)
429 ip->i_new_size = 0;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000430 if (ip->i_d.di_size > i_size_read(VFS_I(ip)))
431 ip->i_d.di_size = i_size_read(VFS_I(ip));
Dave Chinner487f84f2011-01-12 11:37:10 +1100432 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100433 }
434}
435
Dave Chinner487f84f2011-01-12 11:37:10 +1100436/*
437 * xfs_file_splice_write() does not use xfs_rw_ilock() because
438 * generic_file_splice_write() takes the i_mutex itself. This, in theory,
439 * couuld cause lock inversions between the aio_write path and the splice path
440 * if someone is doing concurrent splice(2) based writes and write(2) based
441 * writes to the same inode. The only real way to fix this is to re-implement
442 * the generic code here with correct locking orders.
443 */
Christoph Hellwig00258e32010-02-15 09:44:47 +0000444STATIC ssize_t
445xfs_file_splice_write(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000446 struct pipe_inode_info *pipe,
447 struct file *outfilp,
448 loff_t *ppos,
449 size_t count,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000450 unsigned int flags)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000451{
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000452 struct inode *inode = outfilp->f_mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000453 struct xfs_inode *ip = XFS_I(inode);
Dave Chinneredafb6d2011-01-11 10:14:06 +1100454 xfs_fsize_t new_size;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000455 int ioflags = 0;
456 ssize_t ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000457
458 XFS_STATS_INC(xs_write_calls);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000459
460 if (outfilp->f_mode & FMODE_NOCMTIME)
461 ioflags |= IO_INVIS;
462
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000463 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
464 return -EIO;
465
466 xfs_ilock(ip, XFS_IOLOCK_EXCL);
467
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000468 new_size = *ppos + count;
469
470 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000471 if (new_size > i_size_read(inode))
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000472 ip->i_new_size = new_size;
473 xfs_iunlock(ip, XFS_ILOCK_EXCL);
474
475 trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
476
477 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000478 if (ret > 0)
479 XFS_STATS_ADD(xs_write_bytes, ret);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000480
Dave Chinner7271d242011-08-25 07:17:02 +0000481 xfs_aio_write_newsize_update(ip, new_size);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000482 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
483 return ret;
484}
485
486/*
487 * This routine is called to handle zeroing any space in the last
488 * block of the file that is beyond the EOF. We do this since the
489 * size is being increased without writing anything to that block
490 * and we don't want anyone to read the garbage on the disk.
491 */
492STATIC int /* error (positive) */
493xfs_zero_last_block(
494 xfs_inode_t *ip,
495 xfs_fsize_t offset,
496 xfs_fsize_t isize)
497{
498 xfs_fileoff_t last_fsb;
499 xfs_mount_t *mp = ip->i_mount;
500 int nimaps;
501 int zero_offset;
502 int zero_len;
503 int error = 0;
504 xfs_bmbt_irec_t imap;
505
506 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
507
508 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
509 if (zero_offset == 0) {
510 /*
511 * There are no extra bytes in the last block on disk to
512 * zero, so return.
513 */
514 return 0;
515 }
516
517 last_fsb = XFS_B_TO_FSBT(mp, isize);
518 nimaps = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +0000519 error = xfs_bmapi_read(ip, last_fsb, 1, &imap, &nimaps, 0);
520 if (error)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000521 return error;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000522 ASSERT(nimaps > 0);
523 /*
524 * If the block underlying isize is just a hole, then there
525 * is nothing to zero.
526 */
527 if (imap.br_startblock == HOLESTARTBLOCK) {
528 return 0;
529 }
530 /*
531 * Zero the part of the last block beyond the EOF, and write it
532 * out sync. We need to drop the ilock while we do this so we
533 * don't deadlock when the buffer cache calls back to us.
534 */
535 xfs_iunlock(ip, XFS_ILOCK_EXCL);
536
537 zero_len = mp->m_sb.sb_blocksize - zero_offset;
538 if (isize + zero_len > offset)
539 zero_len = offset - isize;
540 error = xfs_iozero(ip, isize, zero_len);
541
542 xfs_ilock(ip, XFS_ILOCK_EXCL);
543 ASSERT(error >= 0);
544 return error;
545}
546
547/*
548 * Zero any on disk space between the current EOF and the new,
549 * larger EOF. This handles the normal case of zeroing the remainder
550 * of the last block in the file and the unusual case of zeroing blocks
551 * out beyond the size of the file. This second case only happens
552 * with fixed size extents and when the system crashes before the inode
553 * size was updated but after blocks were allocated. If fill is set,
554 * then any holes in the range are filled and zeroed. If not, the holes
555 * are left alone as holes.
556 */
557
558int /* error (positive) */
559xfs_zero_eof(
560 xfs_inode_t *ip,
561 xfs_off_t offset, /* starting I/O offset */
562 xfs_fsize_t isize) /* current inode size */
563{
564 xfs_mount_t *mp = ip->i_mount;
565 xfs_fileoff_t start_zero_fsb;
566 xfs_fileoff_t end_zero_fsb;
567 xfs_fileoff_t zero_count_fsb;
568 xfs_fileoff_t last_fsb;
569 xfs_fileoff_t zero_off;
570 xfs_fsize_t zero_len;
571 int nimaps;
572 int error = 0;
573 xfs_bmbt_irec_t imap;
574
575 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
576 ASSERT(offset > isize);
577
578 /*
579 * First handle zeroing the block on which isize resides.
580 * We only zero a part of that block so it is handled specially.
581 */
582 error = xfs_zero_last_block(ip, offset, isize);
583 if (error) {
584 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
585 return error;
586 }
587
588 /*
589 * Calculate the range between the new size and the old
590 * where blocks needing to be zeroed may exist. To get the
591 * block where the last byte in the file currently resides,
592 * we need to subtract one from the size and truncate back
593 * to a block boundary. We subtract 1 in case the size is
594 * exactly on a block boundary.
595 */
596 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
597 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
598 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
599 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
600 if (last_fsb == end_zero_fsb) {
601 /*
602 * The size was only incremented on its last block.
603 * We took care of that above, so just return.
604 */
605 return 0;
606 }
607
608 ASSERT(start_zero_fsb <= end_zero_fsb);
609 while (start_zero_fsb <= end_zero_fsb) {
610 nimaps = 1;
611 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +0000612 error = xfs_bmapi_read(ip, start_zero_fsb, zero_count_fsb,
613 &imap, &nimaps, 0);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000614 if (error) {
615 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
616 return error;
617 }
618 ASSERT(nimaps > 0);
619
620 if (imap.br_state == XFS_EXT_UNWRITTEN ||
621 imap.br_startblock == HOLESTARTBLOCK) {
622 /*
623 * This loop handles initializing pages that were
624 * partially initialized by the code below this
625 * loop. It basically zeroes the part of the page
626 * that sits on a hole and sets the page as P_HOLE
627 * and calls remapf if it is a mapped file.
628 */
629 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
630 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
631 continue;
632 }
633
634 /*
635 * There are blocks we need to zero.
636 * Drop the inode lock while we're doing the I/O.
637 * We'll still have the iolock to protect us.
638 */
639 xfs_iunlock(ip, XFS_ILOCK_EXCL);
640
641 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
642 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
643
644 if ((zero_off + zero_len) > offset)
645 zero_len = offset - zero_off;
646
647 error = xfs_iozero(ip, zero_off, zero_len);
648 if (error) {
649 goto out_lock;
650 }
651
652 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
653 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
654
655 xfs_ilock(ip, XFS_ILOCK_EXCL);
656 }
657
658 return 0;
659
660out_lock:
661 xfs_ilock(ip, XFS_ILOCK_EXCL);
662 ASSERT(error >= 0);
663 return error;
664}
665
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100666/*
Dave Chinner4d8d1582011-01-11 10:23:42 +1100667 * Common pre-write limit and setup checks.
668 *
669 * Returns with iolock held according to @iolock.
670 */
671STATIC ssize_t
672xfs_file_aio_write_checks(
673 struct file *file,
674 loff_t *pos,
675 size_t *count,
Dave Chinner7271d242011-08-25 07:17:02 +0000676 xfs_fsize_t *new_sizep,
Dave Chinner4d8d1582011-01-11 10:23:42 +1100677 int *iolock)
678{
679 struct inode *inode = file->f_mapping->host;
680 struct xfs_inode *ip = XFS_I(inode);
681 xfs_fsize_t new_size;
682 int error = 0;
683
Christoph Hellwigc58cb162011-08-27 14:42:53 +0000684 xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner7271d242011-08-25 07:17:02 +0000685 *new_sizep = 0;
686restart:
Dave Chinner4d8d1582011-01-11 10:23:42 +1100687 error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode));
688 if (error) {
689 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL | *iolock);
690 *iolock = 0;
691 return error;
692 }
693
Dave Chinner4d8d1582011-01-11 10:23:42 +1100694 if (likely(!(file->f_mode & FMODE_NOCMTIME)))
695 file_update_time(file);
696
697 /*
698 * If the offset is beyond the size of the file, we need to zero any
699 * blocks that fall between the existing EOF and the start of this
Dave Chinner7271d242011-08-25 07:17:02 +0000700 * write. There is no need to issue zeroing if another in-flght IO ends
701 * at or before this one If zeronig is needed and we are currently
702 * holding the iolock shared, we need to update it to exclusive which
703 * involves dropping all locks and relocking to maintain correct locking
704 * order. If we do this, restart the function to ensure all checks and
705 * values are still valid.
Dave Chinner4d8d1582011-01-11 10:23:42 +1100706 */
Dave Chinner7271d242011-08-25 07:17:02 +0000707 if ((ip->i_new_size && *pos > ip->i_new_size) ||
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000708 (!ip->i_new_size && *pos > i_size_read(inode))) {
Dave Chinner7271d242011-08-25 07:17:02 +0000709 if (*iolock == XFS_IOLOCK_SHARED) {
710 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL | *iolock);
711 *iolock = XFS_IOLOCK_EXCL;
712 xfs_rw_ilock(ip, XFS_ILOCK_EXCL | *iolock);
713 goto restart;
714 }
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000715 error = -xfs_zero_eof(ip, *pos, i_size_read(inode));
Dave Chinner7271d242011-08-25 07:17:02 +0000716 }
717
718 /*
719 * If this IO extends beyond EOF, we may need to update ip->i_new_size.
720 * We have already zeroed space beyond EOF (if necessary). Only update
721 * ip->i_new_size if this IO ends beyond any other in-flight writes.
722 */
723 new_size = *pos + *count;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000724 if (new_size > i_size_read(inode)) {
Dave Chinner7271d242011-08-25 07:17:02 +0000725 if (new_size > ip->i_new_size)
726 ip->i_new_size = new_size;
727 *new_sizep = new_size;
728 }
Dave Chinner4d8d1582011-01-11 10:23:42 +1100729
730 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
731 if (error)
732 return error;
733
734 /*
735 * If we're writing the file then make sure to clear the setuid and
736 * setgid bits if the process is not being run by root. This keeps
737 * people from modifying setuid and setgid binaries.
738 */
739 return file_remove_suid(file);
740
741}
742
743/*
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100744 * xfs_file_dio_aio_write - handle direct IO writes
745 *
746 * Lock the inode appropriately to prepare for and issue a direct IO write.
Dave Chinnereda77982011-01-11 10:22:40 +1100747 * By separating it from the buffered write path we remove all the tricky to
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100748 * follow locking changes and looping.
749 *
Dave Chinnereda77982011-01-11 10:22:40 +1100750 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
751 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
752 * pages are flushed out.
753 *
754 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
755 * allowing them to be done in parallel with reads and other direct IO writes.
756 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
757 * needs to do sub-block zeroing and that requires serialisation against other
758 * direct IOs to the same block. In this case we need to serialise the
759 * submission of the unaligned IOs so that we don't get racing block zeroing in
760 * the dio layer. To avoid the problem with aio, we also need to wait for
761 * outstanding IOs to complete so that unwritten extent conversion is completed
762 * before we try to map the overlapping block. This is currently implemented by
Christoph Hellwig4a06fd22011-08-23 08:28:13 +0000763 * hitting it with a big hammer (i.e. inode_dio_wait()).
Dave Chinnereda77982011-01-11 10:22:40 +1100764 *
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100765 * Returns with locks held indicated by @iolock and errors indicated by
766 * negative return values.
767 */
768STATIC ssize_t
769xfs_file_dio_aio_write(
770 struct kiocb *iocb,
771 const struct iovec *iovp,
772 unsigned long nr_segs,
773 loff_t pos,
774 size_t ocount,
Dave Chinner7271d242011-08-25 07:17:02 +0000775 xfs_fsize_t *new_size,
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100776 int *iolock)
777{
778 struct file *file = iocb->ki_filp;
779 struct address_space *mapping = file->f_mapping;
780 struct inode *inode = mapping->host;
781 struct xfs_inode *ip = XFS_I(inode);
782 struct xfs_mount *mp = ip->i_mount;
783 ssize_t ret = 0;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100784 size_t count = ocount;
Dave Chinnereda77982011-01-11 10:22:40 +1100785 int unaligned_io = 0;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100786 struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
787 mp->m_rtdev_targp : mp->m_ddev_targp;
788
789 *iolock = 0;
790 if ((pos & target->bt_smask) || (count & target->bt_smask))
791 return -XFS_ERROR(EINVAL);
792
Dave Chinnereda77982011-01-11 10:22:40 +1100793 if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
794 unaligned_io = 1;
795
Dave Chinner7271d242011-08-25 07:17:02 +0000796 /*
797 * We don't need to take an exclusive lock unless there page cache needs
798 * to be invalidated or unaligned IO is being executed. We don't need to
799 * consider the EOF extension case here because
800 * xfs_file_aio_write_checks() will relock the inode as necessary for
801 * EOF zeroing cases and fill out the new inode size as appropriate.
802 */
803 if (unaligned_io || mapping->nrpages)
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100804 *iolock = XFS_IOLOCK_EXCL;
805 else
806 *iolock = XFS_IOLOCK_SHARED;
Christoph Hellwigc58cb162011-08-27 14:42:53 +0000807 xfs_rw_ilock(ip, *iolock);
808
809 /*
810 * Recheck if there are cached pages that need invalidate after we got
811 * the iolock to protect against other threads adding new pages while
812 * we were waiting for the iolock.
813 */
814 if (mapping->nrpages && *iolock == XFS_IOLOCK_SHARED) {
815 xfs_rw_iunlock(ip, *iolock);
816 *iolock = XFS_IOLOCK_EXCL;
817 xfs_rw_ilock(ip, *iolock);
818 }
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100819
Dave Chinner7271d242011-08-25 07:17:02 +0000820 ret = xfs_file_aio_write_checks(file, &pos, &count, new_size, iolock);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100821 if (ret)
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100822 return ret;
823
824 if (mapping->nrpages) {
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100825 ret = -xfs_flushinval_pages(ip, (pos & PAGE_CACHE_MASK), -1,
826 FI_REMAPF_LOCKED);
827 if (ret)
828 return ret;
829 }
830
Dave Chinnereda77982011-01-11 10:22:40 +1100831 /*
832 * If we are doing unaligned IO, wait for all other IO to drain,
833 * otherwise demote the lock if we had to flush cached pages
834 */
835 if (unaligned_io)
Christoph Hellwig4a06fd22011-08-23 08:28:13 +0000836 inode_dio_wait(inode);
Dave Chinnereda77982011-01-11 10:22:40 +1100837 else if (*iolock == XFS_IOLOCK_EXCL) {
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100838 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
839 *iolock = XFS_IOLOCK_SHARED;
840 }
841
842 trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
843 ret = generic_file_direct_write(iocb, iovp,
844 &nr_segs, pos, &iocb->ki_pos, count, ocount);
845
846 /* No fallback to buffered IO on errors for XFS. */
847 ASSERT(ret < 0 || ret == count);
848 return ret;
849}
850
Christoph Hellwig00258e32010-02-15 09:44:47 +0000851STATIC ssize_t
Dave Chinner637bbc72011-01-11 10:17:30 +1100852xfs_file_buffered_aio_write(
853 struct kiocb *iocb,
854 const struct iovec *iovp,
855 unsigned long nr_segs,
856 loff_t pos,
857 size_t ocount,
Dave Chinner7271d242011-08-25 07:17:02 +0000858 xfs_fsize_t *new_size,
Dave Chinner637bbc72011-01-11 10:17:30 +1100859 int *iolock)
860{
861 struct file *file = iocb->ki_filp;
862 struct address_space *mapping = file->f_mapping;
863 struct inode *inode = mapping->host;
864 struct xfs_inode *ip = XFS_I(inode);
865 ssize_t ret;
866 int enospc = 0;
Dave Chinner637bbc72011-01-11 10:17:30 +1100867 size_t count = ocount;
868
869 *iolock = XFS_IOLOCK_EXCL;
Christoph Hellwigc58cb162011-08-27 14:42:53 +0000870 xfs_rw_ilock(ip, *iolock);
Dave Chinner637bbc72011-01-11 10:17:30 +1100871
Dave Chinner7271d242011-08-25 07:17:02 +0000872 ret = xfs_file_aio_write_checks(file, &pos, &count, new_size, iolock);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100873 if (ret)
Dave Chinner637bbc72011-01-11 10:17:30 +1100874 return ret;
875
876 /* We can write back this queue in page reclaim */
877 current->backing_dev_info = mapping->backing_dev_info;
878
879write_retry:
880 trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
881 ret = generic_file_buffered_write(iocb, iovp, nr_segs,
882 pos, &iocb->ki_pos, count, ret);
883 /*
884 * if we just got an ENOSPC, flush the inode now we aren't holding any
885 * page locks and retry *once*
886 */
887 if (ret == -ENOSPC && !enospc) {
888 ret = -xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
889 if (ret)
890 return ret;
891 enospc = 1;
892 goto write_retry;
893 }
894 current->backing_dev_info = NULL;
895 return ret;
896}
897
898STATIC ssize_t
Christoph Hellwig00258e32010-02-15 09:44:47 +0000899xfs_file_aio_write(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000900 struct kiocb *iocb,
901 const struct iovec *iovp,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000902 unsigned long nr_segs,
903 loff_t pos)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000904{
905 struct file *file = iocb->ki_filp;
906 struct address_space *mapping = file->f_mapping;
907 struct inode *inode = mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000908 struct xfs_inode *ip = XFS_I(inode);
Dave Chinner637bbc72011-01-11 10:17:30 +1100909 ssize_t ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000910 int iolock;
Dave Chinner637bbc72011-01-11 10:17:30 +1100911 size_t ocount = 0;
Dave Chinner7271d242011-08-25 07:17:02 +0000912 xfs_fsize_t new_size = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000913
914 XFS_STATS_INC(xs_write_calls);
915
Christoph Hellwig00258e32010-02-15 09:44:47 +0000916 BUG_ON(iocb->ki_pos != pos);
917
Dave Chinnera363f0c2011-01-11 10:13:53 +1100918 ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
919 if (ret)
920 return ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000921
Dave Chinner637bbc72011-01-11 10:17:30 +1100922 if (ocount == 0)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000923 return 0;
924
Dave Chinner637bbc72011-01-11 10:17:30 +1100925 xfs_wait_for_freeze(ip->i_mount, SB_FREEZE_WRITE);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000926
Dave Chinner637bbc72011-01-11 10:17:30 +1100927 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000928 return -EIO;
929
Dave Chinner637bbc72011-01-11 10:17:30 +1100930 if (unlikely(file->f_flags & O_DIRECT))
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100931 ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos,
Dave Chinner7271d242011-08-25 07:17:02 +0000932 ocount, &new_size, &iolock);
Dave Chinner637bbc72011-01-11 10:17:30 +1100933 else
934 ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
Dave Chinner7271d242011-08-25 07:17:02 +0000935 ocount, &new_size, &iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000936
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000937 if (ret <= 0)
Dave Chinner637bbc72011-01-11 10:17:30 +1100938 goto out_unlock;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000939
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000940 XFS_STATS_ADD(xs_write_bytes, ret);
941
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000942 /* Handle various SYNC-type writes */
943 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
944 loff_t end = pos + ret - 1;
Markus Trippelsdorf340a0a02011-07-24 14:03:30 +0200945 int error;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000946
Dave Chinner487f84f2011-01-12 11:37:10 +1100947 xfs_rw_iunlock(ip, iolock);
Markus Trippelsdorf340a0a02011-07-24 14:03:30 +0200948 error = xfs_file_fsync(file, pos, end,
Josef Bacik02c24a82011-07-16 20:44:56 -0400949 (file->f_flags & __O_SYNC) ? 0 : 1);
Dave Chinner487f84f2011-01-12 11:37:10 +1100950 xfs_rw_ilock(ip, iolock);
Markus Trippelsdorf340a0a02011-07-24 14:03:30 +0200951 if (error)
952 ret = error;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000953 }
954
Dave Chinner637bbc72011-01-11 10:17:30 +1100955out_unlock:
Dave Chinner7271d242011-08-25 07:17:02 +0000956 xfs_aio_write_newsize_update(ip, new_size);
Dave Chinner487f84f2011-01-12 11:37:10 +1100957 xfs_rw_iunlock(ip, iolock);
Dave Chinnera363f0c2011-01-11 10:13:53 +1100958 return ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000959}
960
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100961STATIC long
962xfs_file_fallocate(
963 struct file *file,
964 int mode,
965 loff_t offset,
966 loff_t len)
967{
968 struct inode *inode = file->f_path.dentry->d_inode;
969 long error;
970 loff_t new_size = 0;
971 xfs_flock64_t bf;
972 xfs_inode_t *ip = XFS_I(inode);
973 int cmd = XFS_IOC_RESVSP;
Dave Chinner82878892011-03-26 09:13:08 +1100974 int attr_flags = XFS_ATTR_NOLOCK;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100975
976 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
977 return -EOPNOTSUPP;
978
979 bf.l_whence = 0;
980 bf.l_start = offset;
981 bf.l_len = len;
982
983 xfs_ilock(ip, XFS_IOLOCK_EXCL);
984
985 if (mode & FALLOC_FL_PUNCH_HOLE)
986 cmd = XFS_IOC_UNRESVSP;
987
988 /* check the new inode size is valid before allocating */
989 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
990 offset + len > i_size_read(inode)) {
991 new_size = offset + len;
992 error = inode_newsize_ok(inode, new_size);
993 if (error)
994 goto out_unlock;
995 }
996
Dave Chinner82878892011-03-26 09:13:08 +1100997 if (file->f_flags & O_DSYNC)
998 attr_flags |= XFS_ATTR_SYNC;
999
1000 error = -xfs_change_file_space(ip, cmd, &bf, 0, attr_flags);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001001 if (error)
1002 goto out_unlock;
1003
1004 /* Change file size if needed */
1005 if (new_size) {
1006 struct iattr iattr;
1007
1008 iattr.ia_valid = ATTR_SIZE;
1009 iattr.ia_size = new_size;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02001010 error = -xfs_setattr_size(ip, &iattr, XFS_ATTR_NOLOCK);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001011 }
1012
1013out_unlock:
1014 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1015 return error;
1016}
1017
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +11001020xfs_file_open(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 struct inode *inode,
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001022 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001024 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return -EFBIG;
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001026 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
1027 return -EIO;
1028 return 0;
1029}
1030
1031STATIC int
1032xfs_dir_open(
1033 struct inode *inode,
1034 struct file *file)
1035{
1036 struct xfs_inode *ip = XFS_I(inode);
1037 int mode;
1038 int error;
1039
1040 error = xfs_file_open(inode, file);
1041 if (error)
1042 return error;
1043
1044 /*
1045 * If there are any blocks, read-ahead block 0 as we're almost
1046 * certain to have the next operation be a read there.
1047 */
1048 mode = xfs_ilock_map_shared(ip);
1049 if (ip->i_d.di_nextents > 0)
1050 xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
1051 xfs_iunlock(ip, mode);
1052 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +11001056xfs_file_release(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 struct inode *inode,
1058 struct file *filp)
1059{
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001060 return -xfs_release(XFS_I(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +11001064xfs_file_readdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct file *filp,
1066 void *dirent,
1067 filldir_t filldir)
1068{
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001069 struct inode *inode = filp->f_path.dentry->d_inode;
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001070 xfs_inode_t *ip = XFS_I(inode);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001071 int error;
1072 size_t bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001074 /*
1075 * The Linux API doesn't pass down the total size of the buffer
1076 * we read into down to the filesystem. With the filldir concept
1077 * it's not needed for correct information, but the XFS dir2 leaf
1078 * code wants an estimate of the buffer size to calculate it's
1079 * readahead window and size the buffers used for mapping to
1080 * physical blocks.
1081 *
1082 * Try to give it an estimate that's good enough, maybe at some
1083 * point we can change the ->readdir prototype to include the
Eric Sandeena9cc7992010-02-03 17:50:13 +00001084 * buffer size. For now we use the current glibc buffer size.
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001085 */
Eric Sandeena9cc7992010-02-03 17:50:13 +00001086 bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001088 error = xfs_readdir(ip, dirent, bufsize,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001089 (xfs_off_t *)&filp->f_pos, filldir);
1090 if (error)
1091 return -error;
1092 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +11001096xfs_file_mmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 struct file *filp,
1098 struct vm_area_struct *vma)
1099{
Nathan Scott3562fd42006-03-14 14:00:35 +11001100 vma->vm_ops = &xfs_file_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -07001101 vma->vm_flags |= VM_CAN_NONLINEAR;
Dean Roehrich6fac0cb2005-06-21 14:07:45 +10001102
Nathan Scottfbc14622006-06-09 14:52:13 +10001103 file_accessed(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return 0;
1105}
1106
David Chinner4f57dbc2007-07-19 16:28:17 +10001107/*
1108 * mmap()d file has taken write protection fault and is being made
1109 * writable. We can set the page state up correctly for a writable
1110 * page, which means we can do correct delalloc accounting (ENOSPC
1111 * checking!) and unwritten extent mapping.
1112 */
1113STATIC int
1114xfs_vm_page_mkwrite(
1115 struct vm_area_struct *vma,
Nick Pigginc2ec1752009-03-31 15:23:21 -07001116 struct vm_fault *vmf)
David Chinner4f57dbc2007-07-19 16:28:17 +10001117{
Nick Pigginc2ec1752009-03-31 15:23:21 -07001118 return block_page_mkwrite(vma, vmf, xfs_get_blocks);
David Chinner4f57dbc2007-07-19 16:28:17 +10001119}
1120
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001121const struct file_operations xfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 .llseek = generic_file_llseek,
1123 .read = do_sync_read,
Dean Roehrichbb3f7242005-09-02 15:43:05 +10001124 .write = do_sync_write,
Nathan Scott3562fd42006-03-14 14:00:35 +11001125 .aio_read = xfs_file_aio_read,
1126 .aio_write = xfs_file_aio_write,
Nathan Scott1b895842006-03-31 13:08:59 +10001127 .splice_read = xfs_file_splice_read,
1128 .splice_write = xfs_file_splice_write,
Nathan Scott3562fd42006-03-14 14:00:35 +11001129 .unlocked_ioctl = xfs_file_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +11001131 .compat_ioctl = xfs_file_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132#endif
Nathan Scott3562fd42006-03-14 14:00:35 +11001133 .mmap = xfs_file_mmap,
1134 .open = xfs_file_open,
1135 .release = xfs_file_release,
1136 .fsync = xfs_file_fsync,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001137 .fallocate = xfs_file_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138};
1139
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001140const struct file_operations xfs_dir_file_operations = {
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001141 .open = xfs_dir_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 .read = generic_read_dir,
Nathan Scott3562fd42006-03-14 14:00:35 +11001143 .readdir = xfs_file_readdir,
Al Viro59af1582008-08-24 07:24:41 -04001144 .llseek = generic_file_llseek,
Nathan Scott3562fd42006-03-14 14:00:35 +11001145 .unlocked_ioctl = xfs_file_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -07001146#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +11001147 .compat_ioctl = xfs_file_compat_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -07001148#endif
Christoph Hellwig1da2f2d2011-10-02 14:25:16 +00001149 .fsync = xfs_dir_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150};
1151
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001152static const struct vm_operations_struct xfs_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07001153 .fault = filemap_fault,
David Chinner4f57dbc2007-07-19 16:28:17 +10001154 .page_mkwrite = xfs_vm_page_mkwrite,
Dean Roehrich6fac0cb2005-06-21 14:07:45 +10001155};