blob: b679198dcc01c7e279fe426b0f2c1f153e03c14d [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 Hellwigfd3200b2010-02-15 09:44:48 +0000127STATIC int
128xfs_file_fsync(
129 struct file *file,
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000130 int datasync)
131{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200132 struct inode *inode = file->f_mapping->host;
133 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000134 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000135 struct xfs_trans *tp;
136 int error = 0;
137 int log_flushed = 0;
138
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000139 trace_xfs_file_fsync(ip);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000140
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000141 if (XFS_FORCED_SHUTDOWN(mp))
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000142 return -XFS_ERROR(EIO);
143
144 xfs_iflags_clear(ip, XFS_ITRUNCATED);
145
Christoph Hellwig37bc5742010-04-20 17:00:59 +1000146 xfs_ioend_wait(ip);
147
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000148 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 Hellwigfd3200b2010-02-15 09:44:48 +0000162 /*
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 Hellwig66d834e2010-02-15 09:44:49 +0000176 /*
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 Hellwig7ea80852010-05-26 17:53:25 +0200185 if (((inode->i_state & I_DIRTY_DATASYNC) ||
186 ((inode->i_state & I_DIRTY_SYNC) && !datasync)) &&
Christoph Hellwig66d834e2010-02-15 09:44:49 +0000187 ip->i_update_core) {
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000188 /*
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 Hellwiga27a2632011-06-16 12:02:23 +0000193 tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000194 error = xfs_trans_reserve(tp, 0,
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000195 XFS_FSYNC_TS_LOG_RES(mp), 0, 0, 0);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000196 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 Hellwig898621d2010-06-24 11:36:58 +1000211 xfs_trans_ijoin(tp, ip);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000212 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 Hellwigfd3200b2010-02-15 09:44:48 +0000226 if (xfs_ipincount(ip)) {
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000227 error = _xfs_log_force_lsn(mp,
Christoph Hellwig024910c2010-02-17 19:34:57 +0000228 ip->i_itemp->ili_last_lsn,
229 XFS_LOG_SYNC, &log_flushed);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000230 }
Christoph Hellwig024910c2010-02-17 19:34:57 +0000231 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000232 }
233
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000234 /*
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 Hellwigfd3200b2010-02-15 09:44:48 +0000246
247 return -error;
248}
249
Christoph Hellwig00258e32010-02-15 09:44:47 +0000250STATIC ssize_t
251xfs_file_aio_read(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000252 struct kiocb *iocb,
253 const struct iovec *iovp,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000254 unsigned long nr_segs,
255 loff_t pos)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000256{
257 struct file *file = iocb->ki_filp;
258 struct inode *inode = file->f_mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000259 struct xfs_inode *ip = XFS_I(inode);
260 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000261 size_t size = 0;
262 ssize_t ret = 0;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000263 int ioflags = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000264 xfs_fsize_t n;
265 unsigned long seg;
266
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000267 XFS_STATS_INC(xs_read_calls);
268
Christoph Hellwig00258e32010-02-15 09:44:47 +0000269 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 Hellwigdda35b82010-02-15 09:44:46 +0000276 /* START copy & waste from filemap.c */
Christoph Hellwig00258e32010-02-15 09:44:47 +0000277 for (seg = 0; seg < nr_segs; seg++) {
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000278 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 Hellwig00258e32010-02-15 09:44:47 +0000294 if ((iocb->ki_pos & target->bt_smask) ||
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000295 (size & target->bt_smask)) {
Christoph Hellwig00258e32010-02-15 09:44:47 +0000296 if (iocb->ki_pos == ip->i_size)
297 return 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000298 return -XFS_ERROR(EINVAL);
299 }
300 }
301
Christoph Hellwig00258e32010-02-15 09:44:47 +0000302 n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
303 if (n <= 0 || size == 0)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000304 return 0;
305
306 if (n < size)
307 size = n;
308
309 if (XFS_FORCED_SHUTDOWN(mp))
310 return -EIO;
311
Dave Chinner16ba92e2011-11-19 13:13:40 -0500312 /*
313 * Locking is a bit tricky here. If we take an exclusive lock
314 * for direct IO, we effectively serialise all new concurrent
315 * read IO to this file and block it behind IO that is currently in
316 * progress because IO in progress holds the IO lock shared. We only
317 * need to hold the lock exclusive to blow away the page cache, so
318 * only take lock exclusively if the page cache needs invalidation.
319 * This allows the normal direct IO case of no page cache pages to
320 * proceeed concurrently without serialisation.
321 */
322 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
323 if ((ioflags & IO_ISDIRECT) && inode->i_mapping->nrpages) {
324 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
Dave Chinner487f84f2011-01-12 11:37:10 +1100325 xfs_rw_ilock(ip, XFS_IOLOCK_EXCL);
326
Christoph Hellwig00258e32010-02-15 09:44:47 +0000327 if (inode->i_mapping->nrpages) {
328 ret = -xfs_flushinval_pages(ip,
329 (iocb->ki_pos & PAGE_CACHE_MASK),
330 -1, FI_REMAPF_LOCKED);
Dave Chinner487f84f2011-01-12 11:37:10 +1100331 if (ret) {
332 xfs_rw_iunlock(ip, XFS_IOLOCK_EXCL);
333 return ret;
334 }
Christoph Hellwig00258e32010-02-15 09:44:47 +0000335 }
Dave Chinner487f84f2011-01-12 11:37:10 +1100336 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
Dave Chinner16ba92e2011-11-19 13:13:40 -0500337 }
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000338
Christoph Hellwig00258e32010-02-15 09:44:47 +0000339 trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000340
Christoph Hellwig00258e32010-02-15 09:44:47 +0000341 ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000342 if (ret > 0)
343 XFS_STATS_ADD(xs_read_bytes, ret);
344
Dave Chinner487f84f2011-01-12 11:37:10 +1100345 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000346 return ret;
347}
348
Christoph Hellwig00258e32010-02-15 09:44:47 +0000349STATIC ssize_t
350xfs_file_splice_read(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000351 struct file *infilp,
352 loff_t *ppos,
353 struct pipe_inode_info *pipe,
354 size_t count,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000355 unsigned int flags)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000356{
Christoph Hellwig00258e32010-02-15 09:44:47 +0000357 struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000358 int ioflags = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000359 ssize_t ret;
360
361 XFS_STATS_INC(xs_read_calls);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000362
363 if (infilp->f_mode & FMODE_NOCMTIME)
364 ioflags |= IO_INVIS;
365
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000366 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
367 return -EIO;
368
Dave Chinner487f84f2011-01-12 11:37:10 +1100369 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000370
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000371 trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
372
373 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
374 if (ret > 0)
375 XFS_STATS_ADD(xs_read_bytes, ret);
376
Dave Chinner487f84f2011-01-12 11:37:10 +1100377 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000378 return ret;
379}
380
Dave Chinneredafb6d2011-01-11 10:14:06 +1100381STATIC void
382xfs_aio_write_isize_update(
383 struct inode *inode,
384 loff_t *ppos,
385 ssize_t bytes_written)
386{
387 struct xfs_inode *ip = XFS_I(inode);
388 xfs_fsize_t isize = i_size_read(inode);
389
390 if (bytes_written > 0)
391 XFS_STATS_ADD(xs_write_bytes, bytes_written);
392
393 if (unlikely(bytes_written < 0 && bytes_written != -EFAULT &&
394 *ppos > isize))
395 *ppos = isize;
396
397 if (*ppos > ip->i_size) {
Dave Chinner487f84f2011-01-12 11:37:10 +1100398 xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinneredafb6d2011-01-11 10:14:06 +1100399 if (*ppos > ip->i_size)
400 ip->i_size = *ppos;
Dave Chinner487f84f2011-01-12 11:37:10 +1100401 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinneredafb6d2011-01-11 10:14:06 +1100402 }
403}
404
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100405/*
406 * If this was a direct or synchronous I/O that failed (such as ENOSPC) then
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300407 * part of the I/O may have been written to disk before the error occurred. In
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100408 * this case the on-disk file size may have been adjusted beyond the in-memory
409 * file size and now needs to be truncated back.
410 */
411STATIC void
412xfs_aio_write_newsize_update(
413 struct xfs_inode *ip)
414{
415 if (ip->i_new_size) {
Dave Chinner487f84f2011-01-12 11:37:10 +1100416 xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100417 ip->i_new_size = 0;
418 if (ip->i_d.di_size > ip->i_size)
419 ip->i_d.di_size = ip->i_size;
Dave Chinner487f84f2011-01-12 11:37:10 +1100420 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100421 }
422}
423
Dave Chinner487f84f2011-01-12 11:37:10 +1100424/*
425 * xfs_file_splice_write() does not use xfs_rw_ilock() because
426 * generic_file_splice_write() takes the i_mutex itself. This, in theory,
427 * couuld cause lock inversions between the aio_write path and the splice path
428 * if someone is doing concurrent splice(2) based writes and write(2) based
429 * writes to the same inode. The only real way to fix this is to re-implement
430 * the generic code here with correct locking orders.
431 */
Christoph Hellwig00258e32010-02-15 09:44:47 +0000432STATIC ssize_t
433xfs_file_splice_write(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000434 struct pipe_inode_info *pipe,
435 struct file *outfilp,
436 loff_t *ppos,
437 size_t count,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000438 unsigned int flags)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000439{
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000440 struct inode *inode = outfilp->f_mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000441 struct xfs_inode *ip = XFS_I(inode);
Dave Chinneredafb6d2011-01-11 10:14:06 +1100442 xfs_fsize_t new_size;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000443 int ioflags = 0;
444 ssize_t ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000445
446 XFS_STATS_INC(xs_write_calls);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000447
448 if (outfilp->f_mode & FMODE_NOCMTIME)
449 ioflags |= IO_INVIS;
450
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000451 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
452 return -EIO;
453
454 xfs_ilock(ip, XFS_IOLOCK_EXCL);
455
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000456 new_size = *ppos + count;
457
458 xfs_ilock(ip, XFS_ILOCK_EXCL);
459 if (new_size > ip->i_size)
460 ip->i_new_size = new_size;
461 xfs_iunlock(ip, XFS_ILOCK_EXCL);
462
463 trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
464
465 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000466
Dave Chinneredafb6d2011-01-11 10:14:06 +1100467 xfs_aio_write_isize_update(inode, ppos, ret);
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100468 xfs_aio_write_newsize_update(ip);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000469 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
470 return ret;
471}
472
473/*
474 * This routine is called to handle zeroing any space in the last
475 * block of the file that is beyond the EOF. We do this since the
476 * size is being increased without writing anything to that block
477 * and we don't want anyone to read the garbage on the disk.
478 */
479STATIC int /* error (positive) */
480xfs_zero_last_block(
481 xfs_inode_t *ip,
482 xfs_fsize_t offset,
483 xfs_fsize_t isize)
484{
485 xfs_fileoff_t last_fsb;
486 xfs_mount_t *mp = ip->i_mount;
487 int nimaps;
488 int zero_offset;
489 int zero_len;
490 int error = 0;
491 xfs_bmbt_irec_t imap;
492
493 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
494
495 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
496 if (zero_offset == 0) {
497 /*
498 * There are no extra bytes in the last block on disk to
499 * zero, so return.
500 */
501 return 0;
502 }
503
504 last_fsb = XFS_B_TO_FSBT(mp, isize);
505 nimaps = 1;
506 error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000507 &nimaps, NULL);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000508 if (error) {
509 return error;
510 }
511 ASSERT(nimaps > 0);
512 /*
513 * If the block underlying isize is just a hole, then there
514 * is nothing to zero.
515 */
516 if (imap.br_startblock == HOLESTARTBLOCK) {
517 return 0;
518 }
519 /*
520 * Zero the part of the last block beyond the EOF, and write it
521 * out sync. We need to drop the ilock while we do this so we
522 * don't deadlock when the buffer cache calls back to us.
523 */
524 xfs_iunlock(ip, XFS_ILOCK_EXCL);
525
526 zero_len = mp->m_sb.sb_blocksize - zero_offset;
527 if (isize + zero_len > offset)
528 zero_len = offset - isize;
529 error = xfs_iozero(ip, isize, zero_len);
530
531 xfs_ilock(ip, XFS_ILOCK_EXCL);
532 ASSERT(error >= 0);
533 return error;
534}
535
536/*
537 * Zero any on disk space between the current EOF and the new,
538 * larger EOF. This handles the normal case of zeroing the remainder
539 * of the last block in the file and the unusual case of zeroing blocks
540 * out beyond the size of the file. This second case only happens
541 * with fixed size extents and when the system crashes before the inode
542 * size was updated but after blocks were allocated. If fill is set,
543 * then any holes in the range are filled and zeroed. If not, the holes
544 * are left alone as holes.
545 */
546
547int /* error (positive) */
548xfs_zero_eof(
549 xfs_inode_t *ip,
550 xfs_off_t offset, /* starting I/O offset */
551 xfs_fsize_t isize) /* current inode size */
552{
553 xfs_mount_t *mp = ip->i_mount;
554 xfs_fileoff_t start_zero_fsb;
555 xfs_fileoff_t end_zero_fsb;
556 xfs_fileoff_t zero_count_fsb;
557 xfs_fileoff_t last_fsb;
558 xfs_fileoff_t zero_off;
559 xfs_fsize_t zero_len;
560 int nimaps;
561 int error = 0;
562 xfs_bmbt_irec_t imap;
563
564 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
565 ASSERT(offset > isize);
566
567 /*
568 * First handle zeroing the block on which isize resides.
569 * We only zero a part of that block so it is handled specially.
570 */
571 error = xfs_zero_last_block(ip, offset, isize);
572 if (error) {
573 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
574 return error;
575 }
576
577 /*
578 * Calculate the range between the new size and the old
579 * where blocks needing to be zeroed may exist. To get the
580 * block where the last byte in the file currently resides,
581 * we need to subtract one from the size and truncate back
582 * to a block boundary. We subtract 1 in case the size is
583 * exactly on a block boundary.
584 */
585 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
586 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
587 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
588 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
589 if (last_fsb == end_zero_fsb) {
590 /*
591 * The size was only incremented on its last block.
592 * We took care of that above, so just return.
593 */
594 return 0;
595 }
596
597 ASSERT(start_zero_fsb <= end_zero_fsb);
598 while (start_zero_fsb <= end_zero_fsb) {
599 nimaps = 1;
600 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
601 error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000602 0, NULL, 0, &imap, &nimaps, NULL);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000603 if (error) {
604 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
605 return error;
606 }
607 ASSERT(nimaps > 0);
608
609 if (imap.br_state == XFS_EXT_UNWRITTEN ||
610 imap.br_startblock == HOLESTARTBLOCK) {
611 /*
612 * This loop handles initializing pages that were
613 * partially initialized by the code below this
614 * loop. It basically zeroes the part of the page
615 * that sits on a hole and sets the page as P_HOLE
616 * and calls remapf if it is a mapped file.
617 */
618 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
619 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
620 continue;
621 }
622
623 /*
624 * There are blocks we need to zero.
625 * Drop the inode lock while we're doing the I/O.
626 * We'll still have the iolock to protect us.
627 */
628 xfs_iunlock(ip, XFS_ILOCK_EXCL);
629
630 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
631 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
632
633 if ((zero_off + zero_len) > offset)
634 zero_len = offset - zero_off;
635
636 error = xfs_iozero(ip, zero_off, zero_len);
637 if (error) {
638 goto out_lock;
639 }
640
641 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
642 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
643
644 xfs_ilock(ip, XFS_ILOCK_EXCL);
645 }
646
647 return 0;
648
649out_lock:
650 xfs_ilock(ip, XFS_ILOCK_EXCL);
651 ASSERT(error >= 0);
652 return error;
653}
654
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100655/*
Dave Chinner4d8d1582011-01-11 10:23:42 +1100656 * Common pre-write limit and setup checks.
657 *
658 * Returns with iolock held according to @iolock.
659 */
660STATIC ssize_t
661xfs_file_aio_write_checks(
662 struct file *file,
663 loff_t *pos,
664 size_t *count,
665 int *iolock)
666{
667 struct inode *inode = file->f_mapping->host;
668 struct xfs_inode *ip = XFS_I(inode);
669 xfs_fsize_t new_size;
670 int error = 0;
671
Christoph Hellwig3da97f92011-11-19 13:13:41 -0500672 xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100673 error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode));
674 if (error) {
675 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL | *iolock);
676 *iolock = 0;
677 return error;
678 }
679
680 new_size = *pos + *count;
681 if (new_size > ip->i_size)
682 ip->i_new_size = new_size;
683
684 if (likely(!(file->f_mode & FMODE_NOCMTIME)))
685 file_update_time(file);
686
687 /*
688 * If the offset is beyond the size of the file, we need to zero any
689 * blocks that fall between the existing EOF and the start of this
690 * write.
691 */
692 if (*pos > ip->i_size)
693 error = -xfs_zero_eof(ip, *pos, ip->i_size);
694
695 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
696 if (error)
697 return error;
698
699 /*
700 * If we're writing the file then make sure to clear the setuid and
701 * setgid bits if the process is not being run by root. This keeps
702 * people from modifying setuid and setgid binaries.
703 */
704 return file_remove_suid(file);
705
706}
707
708/*
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100709 * xfs_file_dio_aio_write - handle direct IO writes
710 *
711 * Lock the inode appropriately to prepare for and issue a direct IO write.
Dave Chinnereda77982011-01-11 10:22:40 +1100712 * By separating it from the buffered write path we remove all the tricky to
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100713 * follow locking changes and looping.
714 *
Dave Chinnereda77982011-01-11 10:22:40 +1100715 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
716 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
717 * pages are flushed out.
718 *
719 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
720 * allowing them to be done in parallel with reads and other direct IO writes.
721 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
722 * needs to do sub-block zeroing and that requires serialisation against other
723 * direct IOs to the same block. In this case we need to serialise the
724 * submission of the unaligned IOs so that we don't get racing block zeroing in
725 * the dio layer. To avoid the problem with aio, we also need to wait for
726 * outstanding IOs to complete so that unwritten extent conversion is completed
727 * before we try to map the overlapping block. This is currently implemented by
728 * hitting it with a big hammer (i.e. xfs_ioend_wait()).
729 *
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100730 * Returns with locks held indicated by @iolock and errors indicated by
731 * negative return values.
732 */
733STATIC ssize_t
734xfs_file_dio_aio_write(
735 struct kiocb *iocb,
736 const struct iovec *iovp,
737 unsigned long nr_segs,
738 loff_t pos,
739 size_t ocount,
740 int *iolock)
741{
742 struct file *file = iocb->ki_filp;
743 struct address_space *mapping = file->f_mapping;
744 struct inode *inode = mapping->host;
745 struct xfs_inode *ip = XFS_I(inode);
746 struct xfs_mount *mp = ip->i_mount;
747 ssize_t ret = 0;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100748 size_t count = ocount;
Dave Chinnereda77982011-01-11 10:22:40 +1100749 int unaligned_io = 0;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100750 struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
751 mp->m_rtdev_targp : mp->m_ddev_targp;
752
753 *iolock = 0;
754 if ((pos & target->bt_smask) || (count & target->bt_smask))
755 return -XFS_ERROR(EINVAL);
756
Dave Chinnereda77982011-01-11 10:22:40 +1100757 if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
758 unaligned_io = 1;
759
760 if (unaligned_io || mapping->nrpages || pos > ip->i_size)
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100761 *iolock = XFS_IOLOCK_EXCL;
762 else
763 *iolock = XFS_IOLOCK_SHARED;
Christoph Hellwig3da97f92011-11-19 13:13:41 -0500764 xfs_rw_ilock(ip, *iolock);
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100765
Dave Chinner4d8d1582011-01-11 10:23:42 +1100766 ret = xfs_file_aio_write_checks(file, &pos, &count, iolock);
767 if (ret)
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100768 return ret;
769
Christoph Hellwig3da97f92011-11-19 13:13:41 -0500770 /*
771 * Recheck if there are cached pages that need invalidate after we got
772 * the iolock to protect against other threads adding new pages while
773 * we were waiting for the iolock.
774 */
775 if (mapping->nrpages && *iolock == XFS_IOLOCK_SHARED) {
776 xfs_rw_iunlock(ip, *iolock);
777 *iolock = XFS_IOLOCK_EXCL;
778 xfs_rw_ilock(ip, *iolock);
779 }
780
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100781 if (mapping->nrpages) {
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100782 ret = -xfs_flushinval_pages(ip, (pos & PAGE_CACHE_MASK), -1,
783 FI_REMAPF_LOCKED);
784 if (ret)
785 return ret;
786 }
787
Dave Chinnereda77982011-01-11 10:22:40 +1100788 /*
789 * If we are doing unaligned IO, wait for all other IO to drain,
790 * otherwise demote the lock if we had to flush cached pages
791 */
792 if (unaligned_io)
793 xfs_ioend_wait(ip);
794 else if (*iolock == XFS_IOLOCK_EXCL) {
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100795 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
796 *iolock = XFS_IOLOCK_SHARED;
797 }
798
799 trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
800 ret = generic_file_direct_write(iocb, iovp,
801 &nr_segs, pos, &iocb->ki_pos, count, ocount);
802
803 /* No fallback to buffered IO on errors for XFS. */
804 ASSERT(ret < 0 || ret == count);
805 return ret;
806}
807
Christoph Hellwig00258e32010-02-15 09:44:47 +0000808STATIC ssize_t
Dave Chinner637bbc72011-01-11 10:17:30 +1100809xfs_file_buffered_aio_write(
810 struct kiocb *iocb,
811 const struct iovec *iovp,
812 unsigned long nr_segs,
813 loff_t pos,
814 size_t ocount,
815 int *iolock)
816{
817 struct file *file = iocb->ki_filp;
818 struct address_space *mapping = file->f_mapping;
819 struct inode *inode = mapping->host;
820 struct xfs_inode *ip = XFS_I(inode);
821 ssize_t ret;
822 int enospc = 0;
Dave Chinner637bbc72011-01-11 10:17:30 +1100823 size_t count = ocount;
824
825 *iolock = XFS_IOLOCK_EXCL;
Christoph Hellwig3da97f92011-11-19 13:13:41 -0500826 xfs_rw_ilock(ip, *iolock);
Dave Chinner637bbc72011-01-11 10:17:30 +1100827
Dave Chinner4d8d1582011-01-11 10:23:42 +1100828 ret = xfs_file_aio_write_checks(file, &pos, &count, iolock);
829 if (ret)
Dave Chinner637bbc72011-01-11 10:17:30 +1100830 return ret;
831
832 /* We can write back this queue in page reclaim */
833 current->backing_dev_info = mapping->backing_dev_info;
834
835write_retry:
836 trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
837 ret = generic_file_buffered_write(iocb, iovp, nr_segs,
838 pos, &iocb->ki_pos, count, ret);
839 /*
840 * if we just got an ENOSPC, flush the inode now we aren't holding any
841 * page locks and retry *once*
842 */
843 if (ret == -ENOSPC && !enospc) {
844 ret = -xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
845 if (ret)
846 return ret;
847 enospc = 1;
848 goto write_retry;
849 }
850 current->backing_dev_info = NULL;
851 return ret;
852}
853
854STATIC ssize_t
Christoph Hellwig00258e32010-02-15 09:44:47 +0000855xfs_file_aio_write(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000856 struct kiocb *iocb,
857 const struct iovec *iovp,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000858 unsigned long nr_segs,
859 loff_t pos)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000860{
861 struct file *file = iocb->ki_filp;
862 struct address_space *mapping = file->f_mapping;
863 struct inode *inode = mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000864 struct xfs_inode *ip = XFS_I(inode);
Dave Chinner637bbc72011-01-11 10:17:30 +1100865 ssize_t ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000866 int iolock;
Dave Chinner637bbc72011-01-11 10:17:30 +1100867 size_t ocount = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000868
869 XFS_STATS_INC(xs_write_calls);
870
Christoph Hellwig00258e32010-02-15 09:44:47 +0000871 BUG_ON(iocb->ki_pos != pos);
872
Dave Chinnera363f0c2011-01-11 10:13:53 +1100873 ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
874 if (ret)
875 return ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000876
Dave Chinner637bbc72011-01-11 10:17:30 +1100877 if (ocount == 0)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000878 return 0;
879
Dave Chinner637bbc72011-01-11 10:17:30 +1100880 xfs_wait_for_freeze(ip->i_mount, SB_FREEZE_WRITE);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000881
Dave Chinner637bbc72011-01-11 10:17:30 +1100882 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000883 return -EIO;
884
Dave Chinner637bbc72011-01-11 10:17:30 +1100885 if (unlikely(file->f_flags & O_DIRECT))
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100886 ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos,
887 ocount, &iolock);
Dave Chinner637bbc72011-01-11 10:17:30 +1100888 else
889 ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
890 ocount, &iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000891
Dave Chinneredafb6d2011-01-11 10:14:06 +1100892 xfs_aio_write_isize_update(inode, &iocb->ki_pos, ret);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000893
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000894 if (ret <= 0)
Dave Chinner637bbc72011-01-11 10:17:30 +1100895 goto out_unlock;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000896
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000897 /* Handle various SYNC-type writes */
898 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
899 loff_t end = pos + ret - 1;
Dave Chinnera363f0c2011-01-11 10:13:53 +1100900 int error, error2;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000901
Dave Chinner487f84f2011-01-12 11:37:10 +1100902 xfs_rw_iunlock(ip, iolock);
Dave Chinnera363f0c2011-01-11 10:13:53 +1100903 error = filemap_write_and_wait_range(mapping, pos, end);
Dave Chinner487f84f2011-01-12 11:37:10 +1100904 xfs_rw_ilock(ip, iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000905
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200906 error2 = -xfs_file_fsync(file,
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000907 (file->f_flags & __O_SYNC) ? 0 : 1);
Dave Chinnera363f0c2011-01-11 10:13:53 +1100908 if (error)
909 ret = error;
910 else if (error2)
911 ret = error2;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000912 }
913
Dave Chinner637bbc72011-01-11 10:17:30 +1100914out_unlock:
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100915 xfs_aio_write_newsize_update(ip);
Dave Chinner487f84f2011-01-12 11:37:10 +1100916 xfs_rw_iunlock(ip, iolock);
Dave Chinnera363f0c2011-01-11 10:13:53 +1100917 return ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000918}
919
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100920STATIC long
921xfs_file_fallocate(
922 struct file *file,
923 int mode,
924 loff_t offset,
925 loff_t len)
926{
927 struct inode *inode = file->f_path.dentry->d_inode;
928 long error;
929 loff_t new_size = 0;
930 xfs_flock64_t bf;
931 xfs_inode_t *ip = XFS_I(inode);
932 int cmd = XFS_IOC_RESVSP;
Dave Chinner82878892011-03-26 09:13:08 +1100933 int attr_flags = XFS_ATTR_NOLOCK;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100934
935 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
936 return -EOPNOTSUPP;
937
938 bf.l_whence = 0;
939 bf.l_start = offset;
940 bf.l_len = len;
941
942 xfs_ilock(ip, XFS_IOLOCK_EXCL);
943
944 if (mode & FALLOC_FL_PUNCH_HOLE)
945 cmd = XFS_IOC_UNRESVSP;
946
947 /* check the new inode size is valid before allocating */
948 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
949 offset + len > i_size_read(inode)) {
950 new_size = offset + len;
951 error = inode_newsize_ok(inode, new_size);
952 if (error)
953 goto out_unlock;
954 }
955
Dave Chinner82878892011-03-26 09:13:08 +1100956 if (file->f_flags & O_DSYNC)
957 attr_flags |= XFS_ATTR_SYNC;
958
959 error = -xfs_change_file_space(ip, cmd, &bf, 0, attr_flags);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100960 if (error)
961 goto out_unlock;
962
963 /* Change file size if needed */
964 if (new_size) {
965 struct iattr iattr;
966
967 iattr.ia_valid = ATTR_SIZE;
968 iattr.ia_size = new_size;
969 error = -xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK);
970 }
971
972out_unlock:
973 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
974 return error;
975}
976
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100979xfs_file_open(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 struct inode *inode,
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100981 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100983 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return -EFBIG;
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100985 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
986 return -EIO;
987 return 0;
988}
989
990STATIC int
991xfs_dir_open(
992 struct inode *inode,
993 struct file *file)
994{
995 struct xfs_inode *ip = XFS_I(inode);
996 int mode;
997 int error;
998
999 error = xfs_file_open(inode, file);
1000 if (error)
1001 return error;
1002
1003 /*
1004 * If there are any blocks, read-ahead block 0 as we're almost
1005 * certain to have the next operation be a read there.
1006 */
1007 mode = xfs_ilock_map_shared(ip);
1008 if (ip->i_d.di_nextents > 0)
1009 xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
1010 xfs_iunlock(ip, mode);
1011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +11001015xfs_file_release(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 struct inode *inode,
1017 struct file *filp)
1018{
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001019 return -xfs_release(XFS_I(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +11001023xfs_file_readdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 struct file *filp,
1025 void *dirent,
1026 filldir_t filldir)
1027{
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001028 struct inode *inode = filp->f_path.dentry->d_inode;
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001029 xfs_inode_t *ip = XFS_I(inode);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001030 int error;
1031 size_t bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001033 /*
1034 * The Linux API doesn't pass down the total size of the buffer
1035 * we read into down to the filesystem. With the filldir concept
1036 * it's not needed for correct information, but the XFS dir2 leaf
1037 * code wants an estimate of the buffer size to calculate it's
1038 * readahead window and size the buffers used for mapping to
1039 * physical blocks.
1040 *
1041 * Try to give it an estimate that's good enough, maybe at some
1042 * point we can change the ->readdir prototype to include the
Eric Sandeena9cc7992010-02-03 17:50:13 +00001043 * buffer size. For now we use the current glibc buffer size.
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001044 */
Eric Sandeena9cc7992010-02-03 17:50:13 +00001045 bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001047 error = xfs_readdir(ip, dirent, bufsize,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001048 (xfs_off_t *)&filp->f_pos, filldir);
1049 if (error)
1050 return -error;
1051 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +11001055xfs_file_mmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 struct file *filp,
1057 struct vm_area_struct *vma)
1058{
Nathan Scott3562fd42006-03-14 14:00:35 +11001059 vma->vm_ops = &xfs_file_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -07001060 vma->vm_flags |= VM_CAN_NONLINEAR;
Dean Roehrich6fac0cb2005-06-21 14:07:45 +10001061
Nathan Scottfbc14622006-06-09 14:52:13 +10001062 file_accessed(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return 0;
1064}
1065
David Chinner4f57dbc2007-07-19 16:28:17 +10001066/*
1067 * mmap()d file has taken write protection fault and is being made
1068 * writable. We can set the page state up correctly for a writable
1069 * page, which means we can do correct delalloc accounting (ENOSPC
1070 * checking!) and unwritten extent mapping.
1071 */
1072STATIC int
1073xfs_vm_page_mkwrite(
1074 struct vm_area_struct *vma,
Nick Pigginc2ec1752009-03-31 15:23:21 -07001075 struct vm_fault *vmf)
David Chinner4f57dbc2007-07-19 16:28:17 +10001076{
Nick Pigginc2ec1752009-03-31 15:23:21 -07001077 return block_page_mkwrite(vma, vmf, xfs_get_blocks);
David Chinner4f57dbc2007-07-19 16:28:17 +10001078}
1079
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001080const struct file_operations xfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 .llseek = generic_file_llseek,
1082 .read = do_sync_read,
Dean Roehrichbb3f7242005-09-02 15:43:05 +10001083 .write = do_sync_write,
Nathan Scott3562fd42006-03-14 14:00:35 +11001084 .aio_read = xfs_file_aio_read,
1085 .aio_write = xfs_file_aio_write,
Nathan Scott1b895842006-03-31 13:08:59 +10001086 .splice_read = xfs_file_splice_read,
1087 .splice_write = xfs_file_splice_write,
Nathan Scott3562fd42006-03-14 14:00:35 +11001088 .unlocked_ioctl = xfs_file_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +11001090 .compat_ioctl = xfs_file_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091#endif
Nathan Scott3562fd42006-03-14 14:00:35 +11001092 .mmap = xfs_file_mmap,
1093 .open = xfs_file_open,
1094 .release = xfs_file_release,
1095 .fsync = xfs_file_fsync,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001096 .fallocate = xfs_file_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097};
1098
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001099const struct file_operations xfs_dir_file_operations = {
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001100 .open = xfs_dir_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 .read = generic_read_dir,
Nathan Scott3562fd42006-03-14 14:00:35 +11001102 .readdir = xfs_file_readdir,
Al Viro59af1582008-08-24 07:24:41 -04001103 .llseek = generic_file_llseek,
Nathan Scott3562fd42006-03-14 14:00:35 +11001104 .unlocked_ioctl = xfs_file_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -07001105#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +11001106 .compat_ioctl = xfs_file_compat_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -07001107#endif
Nathan Scott3562fd42006-03-14 14:00:35 +11001108 .fsync = xfs_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109};
1110
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001111static const struct vm_operations_struct xfs_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07001112 .fault = filemap_fault,
David Chinner4f57dbc2007-07-19 16:28:17 +10001113 .page_mkwrite = xfs_vm_page_mkwrite,
Dean Roehrich6fac0cb2005-06-21 14:07:45 +10001114};