blob: 558543c146b3a0c0450dfc35b3fe94fef1259aff [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,
Josef Bacik02c24a82011-07-16 20:44:56 -0400130 loff_t start,
131 loff_t end,
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000132 int datasync)
133{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200134 struct inode *inode = file->f_mapping->host;
135 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000136 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000137 struct xfs_trans *tp;
138 int error = 0;
139 int log_flushed = 0;
140
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000141 trace_xfs_file_fsync(ip);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000142
Josef Bacik02c24a82011-07-16 20:44:56 -0400143 error = filemap_write_and_wait_range(inode->i_mapping, start, end);
144 if (error)
145 return error;
146
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000147 if (XFS_FORCED_SHUTDOWN(mp))
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000148 return -XFS_ERROR(EIO);
149
150 xfs_iflags_clear(ip, XFS_ITRUNCATED);
151
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000152 if (mp->m_flags & XFS_MOUNT_BARRIER) {
153 /*
154 * If we have an RT and/or log subvolume we need to make sure
155 * to flush the write cache the device used for file data
156 * first. This is to ensure newly written file data make
157 * it to disk before logging the new inode size in case of
158 * an extending write.
159 */
160 if (XFS_IS_REALTIME_INODE(ip))
161 xfs_blkdev_issue_flush(mp->m_rtdev_targp);
162 else if (mp->m_logdev_targp != mp->m_ddev_targp)
163 xfs_blkdev_issue_flush(mp->m_ddev_targp);
164 }
165
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000166 /*
167 * We always need to make sure that the required inode state is safe on
168 * disk. The inode might be clean but we still might need to force the
169 * log because of committed transactions that haven't hit the disk yet.
170 * Likewise, there could be unflushed non-transactional changes to the
171 * inode core that have to go to disk and this requires us to issue
172 * a synchronous transaction to capture these changes correctly.
173 *
174 * This code relies on the assumption that if the i_update_core field
175 * of the inode is clear and the inode is unpinned then it is clean
176 * and no action is required.
177 */
178 xfs_ilock(ip, XFS_ILOCK_SHARED);
179
Christoph Hellwig66d834e2010-02-15 09:44:49 +0000180 /*
181 * First check if the VFS inode is marked dirty. All the dirtying
182 * of non-transactional updates no goes through mark_inode_dirty*,
183 * which allows us to distinguish beteeen pure timestamp updates
184 * and i_size updates which need to be caught for fdatasync.
185 * After that also theck for the dirty state in the XFS inode, which
186 * might gets cleared when the inode gets written out via the AIL
187 * or xfs_iflush_cluster.
188 */
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200189 if (((inode->i_state & I_DIRTY_DATASYNC) ||
190 ((inode->i_state & I_DIRTY_SYNC) && !datasync)) &&
Christoph Hellwig66d834e2010-02-15 09:44:49 +0000191 ip->i_update_core) {
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000192 /*
193 * Kick off a transaction to log the inode core to get the
194 * updates. The sync transaction will also force the log.
195 */
196 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000197 tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000198 error = xfs_trans_reserve(tp, 0,
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000199 XFS_FSYNC_TS_LOG_RES(mp), 0, 0, 0);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000200 if (error) {
201 xfs_trans_cancel(tp, 0);
202 return -error;
203 }
204 xfs_ilock(ip, XFS_ILOCK_EXCL);
205
206 /*
207 * Note - it's possible that we might have pushed ourselves out
208 * of the way during trans_reserve which would flush the inode.
209 * But there's no guarantee that the inode buffer has actually
210 * gone out yet (it's delwri). Plus the buffer could be pinned
211 * anyway if it's part of an inode in another recent
212 * transaction. So we play it safe and fire off the
213 * transaction anyway.
214 */
Christoph Hellwig898621d2010-06-24 11:36:58 +1000215 xfs_trans_ijoin(tp, ip);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000216 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
217 xfs_trans_set_sync(tp);
218 error = _xfs_trans_commit(tp, 0, &log_flushed);
219
220 xfs_iunlock(ip, XFS_ILOCK_EXCL);
221 } else {
222 /*
223 * Timestamps/size haven't changed since last inode flush or
224 * inode transaction commit. That means either nothing got
225 * written or a transaction committed which caught the updates.
226 * If the latter happened and the transaction hasn't hit the
227 * disk yet, the inode will be still be pinned. If it is,
228 * force the log.
229 */
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000230 if (xfs_ipincount(ip)) {
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000231 error = _xfs_log_force_lsn(mp,
Christoph Hellwig024910c2010-02-17 19:34:57 +0000232 ip->i_itemp->ili_last_lsn,
233 XFS_LOG_SYNC, &log_flushed);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000234 }
Christoph Hellwig024910c2010-02-17 19:34:57 +0000235 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000236 }
237
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000238 /*
239 * If we only have a single device, and the log force about was
240 * a no-op we might have to flush the data device cache here.
241 * This can only happen for fdatasync/O_DSYNC if we were overwriting
242 * an already allocated file and thus do not have any metadata to
243 * commit.
244 */
245 if ((mp->m_flags & XFS_MOUNT_BARRIER) &&
246 mp->m_logdev_targp == mp->m_ddev_targp &&
247 !XFS_IS_REALTIME_INODE(ip) &&
248 !log_flushed)
249 xfs_blkdev_issue_flush(mp->m_ddev_targp);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000250
251 return -error;
252}
253
Christoph Hellwig00258e32010-02-15 09:44:47 +0000254STATIC ssize_t
255xfs_file_aio_read(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000256 struct kiocb *iocb,
257 const struct iovec *iovp,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000258 unsigned long nr_segs,
259 loff_t pos)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000260{
261 struct file *file = iocb->ki_filp;
262 struct inode *inode = file->f_mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000263 struct xfs_inode *ip = XFS_I(inode);
264 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000265 size_t size = 0;
266 ssize_t ret = 0;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000267 int ioflags = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000268 xfs_fsize_t n;
269 unsigned long seg;
270
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000271 XFS_STATS_INC(xs_read_calls);
272
Christoph Hellwig00258e32010-02-15 09:44:47 +0000273 BUG_ON(iocb->ki_pos != pos);
274
275 if (unlikely(file->f_flags & O_DIRECT))
276 ioflags |= IO_ISDIRECT;
277 if (file->f_mode & FMODE_NOCMTIME)
278 ioflags |= IO_INVIS;
279
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000280 /* START copy & waste from filemap.c */
Christoph Hellwig00258e32010-02-15 09:44:47 +0000281 for (seg = 0; seg < nr_segs; seg++) {
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000282 const struct iovec *iv = &iovp[seg];
283
284 /*
285 * If any segment has a negative length, or the cumulative
286 * length ever wraps negative then return -EINVAL.
287 */
288 size += iv->iov_len;
289 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
290 return XFS_ERROR(-EINVAL);
291 }
292 /* END copy & waste from filemap.c */
293
294 if (unlikely(ioflags & IO_ISDIRECT)) {
295 xfs_buftarg_t *target =
296 XFS_IS_REALTIME_INODE(ip) ?
297 mp->m_rtdev_targp : mp->m_ddev_targp;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000298 if ((iocb->ki_pos & target->bt_smask) ||
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000299 (size & target->bt_smask)) {
Christoph Hellwig00258e32010-02-15 09:44:47 +0000300 if (iocb->ki_pos == ip->i_size)
301 return 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000302 return -XFS_ERROR(EINVAL);
303 }
304 }
305
Christoph Hellwig00258e32010-02-15 09:44:47 +0000306 n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
307 if (n <= 0 || size == 0)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000308 return 0;
309
310 if (n < size)
311 size = n;
312
313 if (XFS_FORCED_SHUTDOWN(mp))
314 return -EIO;
315
Dave Chinner0c38a252011-08-25 07:17:01 +0000316 /*
317 * Locking is a bit tricky here. If we take an exclusive lock
318 * for direct IO, we effectively serialise all new concurrent
319 * read IO to this file and block it behind IO that is currently in
320 * progress because IO in progress holds the IO lock shared. We only
321 * need to hold the lock exclusive to blow away the page cache, so
322 * only take lock exclusively if the page cache needs invalidation.
323 * This allows the normal direct IO case of no page cache pages to
324 * proceeed concurrently without serialisation.
325 */
326 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
327 if ((ioflags & IO_ISDIRECT) && inode->i_mapping->nrpages) {
328 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
Dave Chinner487f84f2011-01-12 11:37:10 +1100329 xfs_rw_ilock(ip, XFS_IOLOCK_EXCL);
330
Christoph Hellwig00258e32010-02-15 09:44:47 +0000331 if (inode->i_mapping->nrpages) {
332 ret = -xfs_flushinval_pages(ip,
333 (iocb->ki_pos & PAGE_CACHE_MASK),
334 -1, FI_REMAPF_LOCKED);
Dave Chinner487f84f2011-01-12 11:37:10 +1100335 if (ret) {
336 xfs_rw_iunlock(ip, XFS_IOLOCK_EXCL);
337 return ret;
338 }
Christoph Hellwig00258e32010-02-15 09:44:47 +0000339 }
Dave Chinner487f84f2011-01-12 11:37:10 +1100340 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
Dave Chinner0c38a252011-08-25 07:17:01 +0000341 }
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000342
Christoph Hellwig00258e32010-02-15 09:44:47 +0000343 trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000344
Christoph Hellwig00258e32010-02-15 09:44:47 +0000345 ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000346 if (ret > 0)
347 XFS_STATS_ADD(xs_read_bytes, ret);
348
Dave Chinner487f84f2011-01-12 11:37:10 +1100349 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000350 return ret;
351}
352
Christoph Hellwig00258e32010-02-15 09:44:47 +0000353STATIC ssize_t
354xfs_file_splice_read(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000355 struct file *infilp,
356 loff_t *ppos,
357 struct pipe_inode_info *pipe,
358 size_t count,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000359 unsigned int flags)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000360{
Christoph Hellwig00258e32010-02-15 09:44:47 +0000361 struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000362 int ioflags = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000363 ssize_t ret;
364
365 XFS_STATS_INC(xs_read_calls);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000366
367 if (infilp->f_mode & FMODE_NOCMTIME)
368 ioflags |= IO_INVIS;
369
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000370 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
371 return -EIO;
372
Dave Chinner487f84f2011-01-12 11:37:10 +1100373 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000374
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000375 trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
376
377 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
378 if (ret > 0)
379 XFS_STATS_ADD(xs_read_bytes, ret);
380
Dave Chinner487f84f2011-01-12 11:37:10 +1100381 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000382 return ret;
383}
384
Dave Chinneredafb6d2011-01-11 10:14:06 +1100385STATIC void
386xfs_aio_write_isize_update(
387 struct inode *inode,
388 loff_t *ppos,
389 ssize_t bytes_written)
390{
391 struct xfs_inode *ip = XFS_I(inode);
392 xfs_fsize_t isize = i_size_read(inode);
393
394 if (bytes_written > 0)
395 XFS_STATS_ADD(xs_write_bytes, bytes_written);
396
397 if (unlikely(bytes_written < 0 && bytes_written != -EFAULT &&
398 *ppos > isize))
399 *ppos = isize;
400
401 if (*ppos > ip->i_size) {
Dave Chinner487f84f2011-01-12 11:37:10 +1100402 xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinneredafb6d2011-01-11 10:14:06 +1100403 if (*ppos > ip->i_size)
404 ip->i_size = *ppos;
Dave Chinner487f84f2011-01-12 11:37:10 +1100405 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinneredafb6d2011-01-11 10:14:06 +1100406 }
407}
408
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100409/*
410 * If this was a direct or synchronous I/O that failed (such as ENOSPC) then
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300411 * part of the I/O may have been written to disk before the error occurred. In
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100412 * this case the on-disk file size may have been adjusted beyond the in-memory
413 * file size and now needs to be truncated back.
414 */
415STATIC void
416xfs_aio_write_newsize_update(
Dave Chinner7271d242011-08-25 07:17:02 +0000417 struct xfs_inode *ip,
418 xfs_fsize_t new_size)
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100419{
Dave Chinner7271d242011-08-25 07:17:02 +0000420 if (new_size == ip->i_new_size) {
Dave Chinner487f84f2011-01-12 11:37:10 +1100421 xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner7271d242011-08-25 07:17:02 +0000422 if (new_size == ip->i_new_size)
423 ip->i_new_size = 0;
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100424 if (ip->i_d.di_size > ip->i_size)
425 ip->i_d.di_size = ip->i_size;
Dave Chinner487f84f2011-01-12 11:37:10 +1100426 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100427 }
428}
429
Dave Chinner487f84f2011-01-12 11:37:10 +1100430/*
431 * xfs_file_splice_write() does not use xfs_rw_ilock() because
432 * generic_file_splice_write() takes the i_mutex itself. This, in theory,
433 * couuld cause lock inversions between the aio_write path and the splice path
434 * if someone is doing concurrent splice(2) based writes and write(2) based
435 * writes to the same inode. The only real way to fix this is to re-implement
436 * the generic code here with correct locking orders.
437 */
Christoph Hellwig00258e32010-02-15 09:44:47 +0000438STATIC ssize_t
439xfs_file_splice_write(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000440 struct pipe_inode_info *pipe,
441 struct file *outfilp,
442 loff_t *ppos,
443 size_t count,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000444 unsigned int flags)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000445{
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000446 struct inode *inode = outfilp->f_mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000447 struct xfs_inode *ip = XFS_I(inode);
Dave Chinneredafb6d2011-01-11 10:14:06 +1100448 xfs_fsize_t new_size;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000449 int ioflags = 0;
450 ssize_t ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000451
452 XFS_STATS_INC(xs_write_calls);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000453
454 if (outfilp->f_mode & FMODE_NOCMTIME)
455 ioflags |= IO_INVIS;
456
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000457 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
458 return -EIO;
459
460 xfs_ilock(ip, XFS_IOLOCK_EXCL);
461
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000462 new_size = *ppos + count;
463
464 xfs_ilock(ip, XFS_ILOCK_EXCL);
465 if (new_size > ip->i_size)
466 ip->i_new_size = new_size;
467 xfs_iunlock(ip, XFS_ILOCK_EXCL);
468
469 trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
470
471 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000472
Dave Chinneredafb6d2011-01-11 10:14:06 +1100473 xfs_aio_write_isize_update(inode, ppos, ret);
Dave Chinner7271d242011-08-25 07:17:02 +0000474 xfs_aio_write_newsize_update(ip, new_size);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000475 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
476 return ret;
477}
478
479/*
480 * This routine is called to handle zeroing any space in the last
481 * block of the file that is beyond the EOF. We do this since the
482 * size is being increased without writing anything to that block
483 * and we don't want anyone to read the garbage on the disk.
484 */
485STATIC int /* error (positive) */
486xfs_zero_last_block(
487 xfs_inode_t *ip,
488 xfs_fsize_t offset,
489 xfs_fsize_t isize)
490{
491 xfs_fileoff_t last_fsb;
492 xfs_mount_t *mp = ip->i_mount;
493 int nimaps;
494 int zero_offset;
495 int zero_len;
496 int error = 0;
497 xfs_bmbt_irec_t imap;
498
499 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
500
501 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
502 if (zero_offset == 0) {
503 /*
504 * There are no extra bytes in the last block on disk to
505 * zero, so return.
506 */
507 return 0;
508 }
509
510 last_fsb = XFS_B_TO_FSBT(mp, isize);
511 nimaps = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +0000512 error = xfs_bmapi_read(ip, last_fsb, 1, &imap, &nimaps, 0);
513 if (error)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000514 return error;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000515 ASSERT(nimaps > 0);
516 /*
517 * If the block underlying isize is just a hole, then there
518 * is nothing to zero.
519 */
520 if (imap.br_startblock == HOLESTARTBLOCK) {
521 return 0;
522 }
523 /*
524 * Zero the part of the last block beyond the EOF, and write it
525 * out sync. We need to drop the ilock while we do this so we
526 * don't deadlock when the buffer cache calls back to us.
527 */
528 xfs_iunlock(ip, XFS_ILOCK_EXCL);
529
530 zero_len = mp->m_sb.sb_blocksize - zero_offset;
531 if (isize + zero_len > offset)
532 zero_len = offset - isize;
533 error = xfs_iozero(ip, isize, zero_len);
534
535 xfs_ilock(ip, XFS_ILOCK_EXCL);
536 ASSERT(error >= 0);
537 return error;
538}
539
540/*
541 * Zero any on disk space between the current EOF and the new,
542 * larger EOF. This handles the normal case of zeroing the remainder
543 * of the last block in the file and the unusual case of zeroing blocks
544 * out beyond the size of the file. This second case only happens
545 * with fixed size extents and when the system crashes before the inode
546 * size was updated but after blocks were allocated. If fill is set,
547 * then any holes in the range are filled and zeroed. If not, the holes
548 * are left alone as holes.
549 */
550
551int /* error (positive) */
552xfs_zero_eof(
553 xfs_inode_t *ip,
554 xfs_off_t offset, /* starting I/O offset */
555 xfs_fsize_t isize) /* current inode size */
556{
557 xfs_mount_t *mp = ip->i_mount;
558 xfs_fileoff_t start_zero_fsb;
559 xfs_fileoff_t end_zero_fsb;
560 xfs_fileoff_t zero_count_fsb;
561 xfs_fileoff_t last_fsb;
562 xfs_fileoff_t zero_off;
563 xfs_fsize_t zero_len;
564 int nimaps;
565 int error = 0;
566 xfs_bmbt_irec_t imap;
567
568 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
569 ASSERT(offset > isize);
570
571 /*
572 * First handle zeroing the block on which isize resides.
573 * We only zero a part of that block so it is handled specially.
574 */
575 error = xfs_zero_last_block(ip, offset, isize);
576 if (error) {
577 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
578 return error;
579 }
580
581 /*
582 * Calculate the range between the new size and the old
583 * where blocks needing to be zeroed may exist. To get the
584 * block where the last byte in the file currently resides,
585 * we need to subtract one from the size and truncate back
586 * to a block boundary. We subtract 1 in case the size is
587 * exactly on a block boundary.
588 */
589 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
590 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
591 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
592 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
593 if (last_fsb == end_zero_fsb) {
594 /*
595 * The size was only incremented on its last block.
596 * We took care of that above, so just return.
597 */
598 return 0;
599 }
600
601 ASSERT(start_zero_fsb <= end_zero_fsb);
602 while (start_zero_fsb <= end_zero_fsb) {
603 nimaps = 1;
604 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +0000605 error = xfs_bmapi_read(ip, start_zero_fsb, zero_count_fsb,
606 &imap, &nimaps, 0);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000607 if (error) {
608 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
609 return error;
610 }
611 ASSERT(nimaps > 0);
612
613 if (imap.br_state == XFS_EXT_UNWRITTEN ||
614 imap.br_startblock == HOLESTARTBLOCK) {
615 /*
616 * This loop handles initializing pages that were
617 * partially initialized by the code below this
618 * loop. It basically zeroes the part of the page
619 * that sits on a hole and sets the page as P_HOLE
620 * and calls remapf if it is a mapped file.
621 */
622 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
623 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
624 continue;
625 }
626
627 /*
628 * There are blocks we need to zero.
629 * Drop the inode lock while we're doing the I/O.
630 * We'll still have the iolock to protect us.
631 */
632 xfs_iunlock(ip, XFS_ILOCK_EXCL);
633
634 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
635 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
636
637 if ((zero_off + zero_len) > offset)
638 zero_len = offset - zero_off;
639
640 error = xfs_iozero(ip, zero_off, zero_len);
641 if (error) {
642 goto out_lock;
643 }
644
645 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
646 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
647
648 xfs_ilock(ip, XFS_ILOCK_EXCL);
649 }
650
651 return 0;
652
653out_lock:
654 xfs_ilock(ip, XFS_ILOCK_EXCL);
655 ASSERT(error >= 0);
656 return error;
657}
658
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100659/*
Dave Chinner4d8d1582011-01-11 10:23:42 +1100660 * Common pre-write limit and setup checks.
661 *
662 * Returns with iolock held according to @iolock.
663 */
664STATIC ssize_t
665xfs_file_aio_write_checks(
666 struct file *file,
667 loff_t *pos,
668 size_t *count,
Dave Chinner7271d242011-08-25 07:17:02 +0000669 xfs_fsize_t *new_sizep,
Dave Chinner4d8d1582011-01-11 10:23:42 +1100670 int *iolock)
671{
672 struct inode *inode = file->f_mapping->host;
673 struct xfs_inode *ip = XFS_I(inode);
674 xfs_fsize_t new_size;
675 int error = 0;
676
Christoph Hellwigc58cb162011-08-27 14:42:53 +0000677 xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner7271d242011-08-25 07:17:02 +0000678 *new_sizep = 0;
679restart:
Dave Chinner4d8d1582011-01-11 10:23:42 +1100680 error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode));
681 if (error) {
682 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL | *iolock);
683 *iolock = 0;
684 return error;
685 }
686
Dave Chinner4d8d1582011-01-11 10:23:42 +1100687 if (likely(!(file->f_mode & FMODE_NOCMTIME)))
688 file_update_time(file);
689
690 /*
691 * If the offset is beyond the size of the file, we need to zero any
692 * blocks that fall between the existing EOF and the start of this
Dave Chinner7271d242011-08-25 07:17:02 +0000693 * write. There is no need to issue zeroing if another in-flght IO ends
694 * at or before this one If zeronig is needed and we are currently
695 * holding the iolock shared, we need to update it to exclusive which
696 * involves dropping all locks and relocking to maintain correct locking
697 * order. If we do this, restart the function to ensure all checks and
698 * values are still valid.
Dave Chinner4d8d1582011-01-11 10:23:42 +1100699 */
Dave Chinner7271d242011-08-25 07:17:02 +0000700 if ((ip->i_new_size && *pos > ip->i_new_size) ||
701 (!ip->i_new_size && *pos > ip->i_size)) {
702 if (*iolock == XFS_IOLOCK_SHARED) {
703 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL | *iolock);
704 *iolock = XFS_IOLOCK_EXCL;
705 xfs_rw_ilock(ip, XFS_ILOCK_EXCL | *iolock);
706 goto restart;
707 }
Dave Chinner4d8d1582011-01-11 10:23:42 +1100708 error = -xfs_zero_eof(ip, *pos, ip->i_size);
Dave Chinner7271d242011-08-25 07:17:02 +0000709 }
710
711 /*
712 * If this IO extends beyond EOF, we may need to update ip->i_new_size.
713 * We have already zeroed space beyond EOF (if necessary). Only update
714 * ip->i_new_size if this IO ends beyond any other in-flight writes.
715 */
716 new_size = *pos + *count;
717 if (new_size > ip->i_size) {
718 if (new_size > ip->i_new_size)
719 ip->i_new_size = new_size;
720 *new_sizep = new_size;
721 }
Dave Chinner4d8d1582011-01-11 10:23:42 +1100722
723 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
724 if (error)
725 return error;
726
727 /*
728 * If we're writing the file then make sure to clear the setuid and
729 * setgid bits if the process is not being run by root. This keeps
730 * people from modifying setuid and setgid binaries.
731 */
732 return file_remove_suid(file);
733
734}
735
736/*
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100737 * xfs_file_dio_aio_write - handle direct IO writes
738 *
739 * Lock the inode appropriately to prepare for and issue a direct IO write.
Dave Chinnereda77982011-01-11 10:22:40 +1100740 * By separating it from the buffered write path we remove all the tricky to
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100741 * follow locking changes and looping.
742 *
Dave Chinnereda77982011-01-11 10:22:40 +1100743 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
744 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
745 * pages are flushed out.
746 *
747 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
748 * allowing them to be done in parallel with reads and other direct IO writes.
749 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
750 * needs to do sub-block zeroing and that requires serialisation against other
751 * direct IOs to the same block. In this case we need to serialise the
752 * submission of the unaligned IOs so that we don't get racing block zeroing in
753 * the dio layer. To avoid the problem with aio, we also need to wait for
754 * outstanding IOs to complete so that unwritten extent conversion is completed
755 * before we try to map the overlapping block. This is currently implemented by
Christoph Hellwig4a06fd22011-08-23 08:28:13 +0000756 * hitting it with a big hammer (i.e. inode_dio_wait()).
Dave Chinnereda77982011-01-11 10:22:40 +1100757 *
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100758 * Returns with locks held indicated by @iolock and errors indicated by
759 * negative return values.
760 */
761STATIC ssize_t
762xfs_file_dio_aio_write(
763 struct kiocb *iocb,
764 const struct iovec *iovp,
765 unsigned long nr_segs,
766 loff_t pos,
767 size_t ocount,
Dave Chinner7271d242011-08-25 07:17:02 +0000768 xfs_fsize_t *new_size,
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100769 int *iolock)
770{
771 struct file *file = iocb->ki_filp;
772 struct address_space *mapping = file->f_mapping;
773 struct inode *inode = mapping->host;
774 struct xfs_inode *ip = XFS_I(inode);
775 struct xfs_mount *mp = ip->i_mount;
776 ssize_t ret = 0;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100777 size_t count = ocount;
Dave Chinnereda77982011-01-11 10:22:40 +1100778 int unaligned_io = 0;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100779 struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
780 mp->m_rtdev_targp : mp->m_ddev_targp;
781
782 *iolock = 0;
783 if ((pos & target->bt_smask) || (count & target->bt_smask))
784 return -XFS_ERROR(EINVAL);
785
Dave Chinnereda77982011-01-11 10:22:40 +1100786 if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
787 unaligned_io = 1;
788
Dave Chinner7271d242011-08-25 07:17:02 +0000789 /*
790 * We don't need to take an exclusive lock unless there page cache needs
791 * to be invalidated or unaligned IO is being executed. We don't need to
792 * consider the EOF extension case here because
793 * xfs_file_aio_write_checks() will relock the inode as necessary for
794 * EOF zeroing cases and fill out the new inode size as appropriate.
795 */
796 if (unaligned_io || mapping->nrpages)
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100797 *iolock = XFS_IOLOCK_EXCL;
798 else
799 *iolock = XFS_IOLOCK_SHARED;
Christoph Hellwigc58cb162011-08-27 14:42:53 +0000800 xfs_rw_ilock(ip, *iolock);
801
802 /*
803 * Recheck if there are cached pages that need invalidate after we got
804 * the iolock to protect against other threads adding new pages while
805 * we were waiting for the iolock.
806 */
807 if (mapping->nrpages && *iolock == XFS_IOLOCK_SHARED) {
808 xfs_rw_iunlock(ip, *iolock);
809 *iolock = XFS_IOLOCK_EXCL;
810 xfs_rw_ilock(ip, *iolock);
811 }
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100812
Dave Chinner7271d242011-08-25 07:17:02 +0000813 ret = xfs_file_aio_write_checks(file, &pos, &count, new_size, iolock);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100814 if (ret)
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100815 return ret;
816
817 if (mapping->nrpages) {
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100818 ret = -xfs_flushinval_pages(ip, (pos & PAGE_CACHE_MASK), -1,
819 FI_REMAPF_LOCKED);
820 if (ret)
821 return ret;
822 }
823
Dave Chinnereda77982011-01-11 10:22:40 +1100824 /*
825 * If we are doing unaligned IO, wait for all other IO to drain,
826 * otherwise demote the lock if we had to flush cached pages
827 */
828 if (unaligned_io)
Christoph Hellwig4a06fd22011-08-23 08:28:13 +0000829 inode_dio_wait(inode);
Dave Chinnereda77982011-01-11 10:22:40 +1100830 else if (*iolock == XFS_IOLOCK_EXCL) {
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100831 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
832 *iolock = XFS_IOLOCK_SHARED;
833 }
834
835 trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
836 ret = generic_file_direct_write(iocb, iovp,
837 &nr_segs, pos, &iocb->ki_pos, count, ocount);
838
839 /* No fallback to buffered IO on errors for XFS. */
840 ASSERT(ret < 0 || ret == count);
841 return ret;
842}
843
Christoph Hellwig00258e32010-02-15 09:44:47 +0000844STATIC ssize_t
Dave Chinner637bbc72011-01-11 10:17:30 +1100845xfs_file_buffered_aio_write(
846 struct kiocb *iocb,
847 const struct iovec *iovp,
848 unsigned long nr_segs,
849 loff_t pos,
850 size_t ocount,
Dave Chinner7271d242011-08-25 07:17:02 +0000851 xfs_fsize_t *new_size,
Dave Chinner637bbc72011-01-11 10:17:30 +1100852 int *iolock)
853{
854 struct file *file = iocb->ki_filp;
855 struct address_space *mapping = file->f_mapping;
856 struct inode *inode = mapping->host;
857 struct xfs_inode *ip = XFS_I(inode);
858 ssize_t ret;
859 int enospc = 0;
Dave Chinner637bbc72011-01-11 10:17:30 +1100860 size_t count = ocount;
861
862 *iolock = XFS_IOLOCK_EXCL;
Christoph Hellwigc58cb162011-08-27 14:42:53 +0000863 xfs_rw_ilock(ip, *iolock);
Dave Chinner637bbc72011-01-11 10:17:30 +1100864
Dave Chinner7271d242011-08-25 07:17:02 +0000865 ret = xfs_file_aio_write_checks(file, &pos, &count, new_size, iolock);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100866 if (ret)
Dave Chinner637bbc72011-01-11 10:17:30 +1100867 return ret;
868
869 /* We can write back this queue in page reclaim */
870 current->backing_dev_info = mapping->backing_dev_info;
871
872write_retry:
873 trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
874 ret = generic_file_buffered_write(iocb, iovp, nr_segs,
875 pos, &iocb->ki_pos, count, ret);
876 /*
877 * if we just got an ENOSPC, flush the inode now we aren't holding any
878 * page locks and retry *once*
879 */
880 if (ret == -ENOSPC && !enospc) {
881 ret = -xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
882 if (ret)
883 return ret;
884 enospc = 1;
885 goto write_retry;
886 }
887 current->backing_dev_info = NULL;
888 return ret;
889}
890
891STATIC ssize_t
Christoph Hellwig00258e32010-02-15 09:44:47 +0000892xfs_file_aio_write(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000893 struct kiocb *iocb,
894 const struct iovec *iovp,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000895 unsigned long nr_segs,
896 loff_t pos)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000897{
898 struct file *file = iocb->ki_filp;
899 struct address_space *mapping = file->f_mapping;
900 struct inode *inode = mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000901 struct xfs_inode *ip = XFS_I(inode);
Dave Chinner637bbc72011-01-11 10:17:30 +1100902 ssize_t ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000903 int iolock;
Dave Chinner637bbc72011-01-11 10:17:30 +1100904 size_t ocount = 0;
Dave Chinner7271d242011-08-25 07:17:02 +0000905 xfs_fsize_t new_size = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000906
907 XFS_STATS_INC(xs_write_calls);
908
Christoph Hellwig00258e32010-02-15 09:44:47 +0000909 BUG_ON(iocb->ki_pos != pos);
910
Dave Chinnera363f0c2011-01-11 10:13:53 +1100911 ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
912 if (ret)
913 return ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000914
Dave Chinner637bbc72011-01-11 10:17:30 +1100915 if (ocount == 0)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000916 return 0;
917
Dave Chinner637bbc72011-01-11 10:17:30 +1100918 xfs_wait_for_freeze(ip->i_mount, SB_FREEZE_WRITE);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000919
Dave Chinner637bbc72011-01-11 10:17:30 +1100920 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000921 return -EIO;
922
Dave Chinner637bbc72011-01-11 10:17:30 +1100923 if (unlikely(file->f_flags & O_DIRECT))
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100924 ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos,
Dave Chinner7271d242011-08-25 07:17:02 +0000925 ocount, &new_size, &iolock);
Dave Chinner637bbc72011-01-11 10:17:30 +1100926 else
927 ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
Dave Chinner7271d242011-08-25 07:17:02 +0000928 ocount, &new_size, &iolock);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000929
Dave Chinneredafb6d2011-01-11 10:14:06 +1100930 xfs_aio_write_isize_update(inode, &iocb->ki_pos, ret);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000931
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000932 if (ret <= 0)
Dave Chinner637bbc72011-01-11 10:17:30 +1100933 goto out_unlock;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000934
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000935 /* Handle various SYNC-type writes */
936 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
937 loff_t end = pos + ret - 1;
Markus Trippelsdorf340a0a02011-07-24 14:03:30 +0200938 int error;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000939
Dave Chinner487f84f2011-01-12 11:37:10 +1100940 xfs_rw_iunlock(ip, iolock);
Markus Trippelsdorf340a0a02011-07-24 14:03:30 +0200941 error = xfs_file_fsync(file, pos, end,
Josef Bacik02c24a82011-07-16 20:44:56 -0400942 (file->f_flags & __O_SYNC) ? 0 : 1);
Dave Chinner487f84f2011-01-12 11:37:10 +1100943 xfs_rw_ilock(ip, iolock);
Markus Trippelsdorf340a0a02011-07-24 14:03:30 +0200944 if (error)
945 ret = error;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000946 }
947
Dave Chinner637bbc72011-01-11 10:17:30 +1100948out_unlock:
Dave Chinner7271d242011-08-25 07:17:02 +0000949 xfs_aio_write_newsize_update(ip, new_size);
Dave Chinner487f84f2011-01-12 11:37:10 +1100950 xfs_rw_iunlock(ip, iolock);
Dave Chinnera363f0c2011-01-11 10:13:53 +1100951 return ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000952}
953
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100954STATIC long
955xfs_file_fallocate(
956 struct file *file,
957 int mode,
958 loff_t offset,
959 loff_t len)
960{
961 struct inode *inode = file->f_path.dentry->d_inode;
962 long error;
963 loff_t new_size = 0;
964 xfs_flock64_t bf;
965 xfs_inode_t *ip = XFS_I(inode);
966 int cmd = XFS_IOC_RESVSP;
Dave Chinner82878892011-03-26 09:13:08 +1100967 int attr_flags = XFS_ATTR_NOLOCK;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100968
969 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
970 return -EOPNOTSUPP;
971
972 bf.l_whence = 0;
973 bf.l_start = offset;
974 bf.l_len = len;
975
976 xfs_ilock(ip, XFS_IOLOCK_EXCL);
977
978 if (mode & FALLOC_FL_PUNCH_HOLE)
979 cmd = XFS_IOC_UNRESVSP;
980
981 /* check the new inode size is valid before allocating */
982 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
983 offset + len > i_size_read(inode)) {
984 new_size = offset + len;
985 error = inode_newsize_ok(inode, new_size);
986 if (error)
987 goto out_unlock;
988 }
989
Dave Chinner82878892011-03-26 09:13:08 +1100990 if (file->f_flags & O_DSYNC)
991 attr_flags |= XFS_ATTR_SYNC;
992
993 error = -xfs_change_file_space(ip, cmd, &bf, 0, attr_flags);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100994 if (error)
995 goto out_unlock;
996
997 /* Change file size if needed */
998 if (new_size) {
999 struct iattr iattr;
1000
1001 iattr.ia_valid = ATTR_SIZE;
1002 iattr.ia_size = new_size;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02001003 error = -xfs_setattr_size(ip, &iattr, XFS_ATTR_NOLOCK);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001004 }
1005
1006out_unlock:
1007 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1008 return error;
1009}
1010
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +11001013xfs_file_open(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 struct inode *inode,
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001015 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001017 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return -EFBIG;
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001019 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
1020 return -EIO;
1021 return 0;
1022}
1023
1024STATIC int
1025xfs_dir_open(
1026 struct inode *inode,
1027 struct file *file)
1028{
1029 struct xfs_inode *ip = XFS_I(inode);
1030 int mode;
1031 int error;
1032
1033 error = xfs_file_open(inode, file);
1034 if (error)
1035 return error;
1036
1037 /*
1038 * If there are any blocks, read-ahead block 0 as we're almost
1039 * certain to have the next operation be a read there.
1040 */
1041 mode = xfs_ilock_map_shared(ip);
1042 if (ip->i_d.di_nextents > 0)
1043 xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
1044 xfs_iunlock(ip, mode);
1045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +11001049xfs_file_release(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 struct inode *inode,
1051 struct file *filp)
1052{
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001053 return -xfs_release(XFS_I(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +11001057xfs_file_readdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 struct file *filp,
1059 void *dirent,
1060 filldir_t filldir)
1061{
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001062 struct inode *inode = filp->f_path.dentry->d_inode;
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001063 xfs_inode_t *ip = XFS_I(inode);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001064 int error;
1065 size_t bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001067 /*
1068 * The Linux API doesn't pass down the total size of the buffer
1069 * we read into down to the filesystem. With the filldir concept
1070 * it's not needed for correct information, but the XFS dir2 leaf
1071 * code wants an estimate of the buffer size to calculate it's
1072 * readahead window and size the buffers used for mapping to
1073 * physical blocks.
1074 *
1075 * Try to give it an estimate that's good enough, maybe at some
1076 * point we can change the ->readdir prototype to include the
Eric Sandeena9cc7992010-02-03 17:50:13 +00001077 * buffer size. For now we use the current glibc buffer size.
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001078 */
Eric Sandeena9cc7992010-02-03 17:50:13 +00001079 bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001081 error = xfs_readdir(ip, dirent, bufsize,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001082 (xfs_off_t *)&filp->f_pos, filldir);
1083 if (error)
1084 return -error;
1085 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +11001089xfs_file_mmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 struct file *filp,
1091 struct vm_area_struct *vma)
1092{
Nathan Scott3562fd42006-03-14 14:00:35 +11001093 vma->vm_ops = &xfs_file_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -07001094 vma->vm_flags |= VM_CAN_NONLINEAR;
Dean Roehrich6fac0cb2005-06-21 14:07:45 +10001095
Nathan Scottfbc14622006-06-09 14:52:13 +10001096 file_accessed(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return 0;
1098}
1099
David Chinner4f57dbc2007-07-19 16:28:17 +10001100/*
1101 * mmap()d file has taken write protection fault and is being made
1102 * writable. We can set the page state up correctly for a writable
1103 * page, which means we can do correct delalloc accounting (ENOSPC
1104 * checking!) and unwritten extent mapping.
1105 */
1106STATIC int
1107xfs_vm_page_mkwrite(
1108 struct vm_area_struct *vma,
Nick Pigginc2ec1752009-03-31 15:23:21 -07001109 struct vm_fault *vmf)
David Chinner4f57dbc2007-07-19 16:28:17 +10001110{
Nick Pigginc2ec1752009-03-31 15:23:21 -07001111 return block_page_mkwrite(vma, vmf, xfs_get_blocks);
David Chinner4f57dbc2007-07-19 16:28:17 +10001112}
1113
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001114const struct file_operations xfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 .llseek = generic_file_llseek,
1116 .read = do_sync_read,
Dean Roehrichbb3f7242005-09-02 15:43:05 +10001117 .write = do_sync_write,
Nathan Scott3562fd42006-03-14 14:00:35 +11001118 .aio_read = xfs_file_aio_read,
1119 .aio_write = xfs_file_aio_write,
Nathan Scott1b895842006-03-31 13:08:59 +10001120 .splice_read = xfs_file_splice_read,
1121 .splice_write = xfs_file_splice_write,
Nathan Scott3562fd42006-03-14 14:00:35 +11001122 .unlocked_ioctl = xfs_file_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +11001124 .compat_ioctl = xfs_file_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125#endif
Nathan Scott3562fd42006-03-14 14:00:35 +11001126 .mmap = xfs_file_mmap,
1127 .open = xfs_file_open,
1128 .release = xfs_file_release,
1129 .fsync = xfs_file_fsync,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001130 .fallocate = xfs_file_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131};
1132
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001133const struct file_operations xfs_dir_file_operations = {
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001134 .open = xfs_dir_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 .read = generic_read_dir,
Nathan Scott3562fd42006-03-14 14:00:35 +11001136 .readdir = xfs_file_readdir,
Al Viro59af1582008-08-24 07:24:41 -04001137 .llseek = generic_file_llseek,
Nathan Scott3562fd42006-03-14 14:00:35 +11001138 .unlocked_ioctl = xfs_file_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -07001139#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +11001140 .compat_ioctl = xfs_file_compat_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -07001141#endif
Nathan Scott3562fd42006-03-14 14:00:35 +11001142 .fsync = xfs_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143};
1144
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001145static const struct vm_operations_struct xfs_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07001146 .fault = filemap_fault,
David Chinner4f57dbc2007-07-19 16:28:17 +10001147 .page_mkwrite = xfs_vm_page_mkwrite,
Dean Roehrich6fac0cb2005-06-21 14:07:45 +10001148};