blob: 3537c8d0af48e7e175dca6880d5df6f1ae71dcfc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000019#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
27#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_dinode.h"
30#include "xfs_inode.h"
Christoph Hellwigfd3200b2010-02-15 09:44:48 +000031#include "xfs_inode_item.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000032#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_error.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100034#include "xfs_vnodeops.h"
Christoph Hellwigf999a5b2008-11-28 14:23:32 +110035#include "xfs_da_btree.h"
Christoph Hellwigddcd8562008-12-03 07:55:34 -050036#include "xfs_ioctl.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000037#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <linux/dcache.h>
Christoph Hellwig2fe17c12011-01-14 13:07:43 +010040#include <linux/falloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040042static const struct vm_operations_struct xfs_file_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Christoph Hellwigdda35b82010-02-15 09:44:46 +000044/*
Dave Chinner487f84f2011-01-12 11:37:10 +110045 * Locking primitives for read and write IO paths to ensure we consistently use
46 * and order the inode->i_mutex, ip->i_lock and ip->i_iolock.
47 */
48static inline void
49xfs_rw_ilock(
50 struct xfs_inode *ip,
51 int type)
52{
53 if (type & XFS_IOLOCK_EXCL)
54 mutex_lock(&VFS_I(ip)->i_mutex);
55 xfs_ilock(ip, type);
56}
57
58static inline void
59xfs_rw_iunlock(
60 struct xfs_inode *ip,
61 int type)
62{
63 xfs_iunlock(ip, type);
64 if (type & XFS_IOLOCK_EXCL)
65 mutex_unlock(&VFS_I(ip)->i_mutex);
66}
67
68static inline void
69xfs_rw_ilock_demote(
70 struct xfs_inode *ip,
71 int type)
72{
73 xfs_ilock_demote(ip, type);
74 if (type & XFS_IOLOCK_EXCL)
75 mutex_unlock(&VFS_I(ip)->i_mutex);
76}
77
78/*
Christoph Hellwigdda35b82010-02-15 09:44:46 +000079 * xfs_iozero
80 *
81 * xfs_iozero clears the specified range of buffer supplied,
82 * and marks all the affected blocks as valid and modified. If
83 * an affected block is not allocated, it will be allocated. If
84 * an affected block is not completely overwritten, and is not
85 * valid before the operation, it will be read from disk before
86 * being partially zeroed.
87 */
88STATIC int
89xfs_iozero(
90 struct xfs_inode *ip, /* inode */
91 loff_t pos, /* offset in file */
92 size_t count) /* size of data to zero */
93{
94 struct page *page;
95 struct address_space *mapping;
96 int status;
97
98 mapping = VFS_I(ip)->i_mapping;
99 do {
100 unsigned offset, bytes;
101 void *fsdata;
102
103 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
104 bytes = PAGE_CACHE_SIZE - offset;
105 if (bytes > count)
106 bytes = count;
107
108 status = pagecache_write_begin(NULL, mapping, pos, bytes,
109 AOP_FLAG_UNINTERRUPTIBLE,
110 &page, &fsdata);
111 if (status)
112 break;
113
114 zero_user(page, offset, bytes);
115
116 status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
117 page, fsdata);
118 WARN_ON(status <= 0); /* can't return less than zero! */
119 pos += bytes;
120 count -= bytes;
121 status = 0;
122 } while (count);
123
124 return (-status);
125}
126
Christoph Hellwig1da2f2d2011-10-02 14:25:16 +0000127/*
128 * Fsync operations on directories are much simpler than on regular files,
129 * as there is no file data to flush, and thus also no need for explicit
130 * cache flush operations, and there are no non-transaction metadata updates
131 * on directories either.
132 */
133STATIC int
134xfs_dir_fsync(
135 struct file *file,
136 loff_t start,
137 loff_t end,
138 int datasync)
139{
140 struct xfs_inode *ip = XFS_I(file->f_mapping->host);
141 struct xfs_mount *mp = ip->i_mount;
142 xfs_lsn_t lsn = 0;
143
144 trace_xfs_dir_fsync(ip);
145
146 xfs_ilock(ip, XFS_ILOCK_SHARED);
147 if (xfs_ipincount(ip))
148 lsn = ip->i_itemp->ili_last_lsn;
149 xfs_iunlock(ip, XFS_ILOCK_SHARED);
150
151 if (!lsn)
152 return 0;
153 return _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
154}
155
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000156STATIC int
157xfs_file_fsync(
158 struct file *file,
Josef Bacik02c24a82011-07-16 20:44:56 -0400159 loff_t start,
160 loff_t end,
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000161 int datasync)
162{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200163 struct inode *inode = file->f_mapping->host;
164 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000165 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000166 int error = 0;
167 int log_flushed = 0;
Christoph Hellwigb1037052011-09-19 14:55:51 +0000168 xfs_lsn_t lsn = 0;
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000169
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000170 trace_xfs_file_fsync(ip);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000171
Josef Bacik02c24a82011-07-16 20:44:56 -0400172 error = filemap_write_and_wait_range(inode->i_mapping, start, end);
173 if (error)
174 return error;
175
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000176 if (XFS_FORCED_SHUTDOWN(mp))
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000177 return -XFS_ERROR(EIO);
178
179 xfs_iflags_clear(ip, XFS_ITRUNCATED);
180
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000181 if (mp->m_flags & XFS_MOUNT_BARRIER) {
182 /*
183 * If we have an RT and/or log subvolume we need to make sure
184 * to flush the write cache the device used for file data
185 * first. This is to ensure newly written file data make
186 * it to disk before logging the new inode size in case of
187 * an extending write.
188 */
189 if (XFS_IS_REALTIME_INODE(ip))
190 xfs_blkdev_issue_flush(mp->m_rtdev_targp);
191 else if (mp->m_logdev_targp != mp->m_ddev_targp)
192 xfs_blkdev_issue_flush(mp->m_ddev_targp);
193 }
194
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000195 /*
Christoph Hellwig8a9c9982012-02-29 09:53:52 +0000196 * All metadata updates are logged, which means that we just have
197 * to flush the log up to the latest LSN that touched the inode.
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000198 */
199 xfs_ilock(ip, XFS_ILOCK_SHARED);
Christoph Hellwig8f639dd2012-02-29 09:53:55 +0000200 if (xfs_ipincount(ip)) {
201 if (!datasync ||
202 (ip->i_itemp->ili_fields & ~XFS_ILOG_TIMESTAMP))
203 lsn = ip->i_itemp->ili_last_lsn;
204 }
Christoph Hellwig8a9c9982012-02-29 09:53:52 +0000205 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000206
Christoph Hellwig8a9c9982012-02-29 09:53:52 +0000207 if (lsn)
Christoph Hellwigb1037052011-09-19 14:55:51 +0000208 error = _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, &log_flushed);
209
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000210 /*
211 * If we only have a single device, and the log force about was
212 * a no-op we might have to flush the data device cache here.
213 * This can only happen for fdatasync/O_DSYNC if we were overwriting
214 * an already allocated file and thus do not have any metadata to
215 * commit.
216 */
217 if ((mp->m_flags & XFS_MOUNT_BARRIER) &&
218 mp->m_logdev_targp == mp->m_ddev_targp &&
219 !XFS_IS_REALTIME_INODE(ip) &&
220 !log_flushed)
221 xfs_blkdev_issue_flush(mp->m_ddev_targp);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000222
223 return -error;
224}
225
Christoph Hellwig00258e32010-02-15 09:44:47 +0000226STATIC ssize_t
227xfs_file_aio_read(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000228 struct kiocb *iocb,
229 const struct iovec *iovp,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000230 unsigned long nr_segs,
231 loff_t pos)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000232{
233 struct file *file = iocb->ki_filp;
234 struct inode *inode = file->f_mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000235 struct xfs_inode *ip = XFS_I(inode);
236 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000237 size_t size = 0;
238 ssize_t ret = 0;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000239 int ioflags = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000240 xfs_fsize_t n;
241 unsigned long seg;
242
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000243 XFS_STATS_INC(xs_read_calls);
244
Christoph Hellwig00258e32010-02-15 09:44:47 +0000245 BUG_ON(iocb->ki_pos != pos);
246
247 if (unlikely(file->f_flags & O_DIRECT))
248 ioflags |= IO_ISDIRECT;
249 if (file->f_mode & FMODE_NOCMTIME)
250 ioflags |= IO_INVIS;
251
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000252 /* START copy & waste from filemap.c */
Christoph Hellwig00258e32010-02-15 09:44:47 +0000253 for (seg = 0; seg < nr_segs; seg++) {
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000254 const struct iovec *iv = &iovp[seg];
255
256 /*
257 * If any segment has a negative length, or the cumulative
258 * length ever wraps negative then return -EINVAL.
259 */
260 size += iv->iov_len;
261 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
262 return XFS_ERROR(-EINVAL);
263 }
264 /* END copy & waste from filemap.c */
265
266 if (unlikely(ioflags & IO_ISDIRECT)) {
267 xfs_buftarg_t *target =
268 XFS_IS_REALTIME_INODE(ip) ?
269 mp->m_rtdev_targp : mp->m_ddev_targp;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000270 if ((iocb->ki_pos & target->bt_smask) ||
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000271 (size & target->bt_smask)) {
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000272 if (iocb->ki_pos == i_size_read(inode))
Christoph Hellwig00258e32010-02-15 09:44:47 +0000273 return 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000274 return -XFS_ERROR(EINVAL);
275 }
276 }
277
Christoph Hellwig00258e32010-02-15 09:44:47 +0000278 n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
279 if (n <= 0 || size == 0)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000280 return 0;
281
282 if (n < size)
283 size = n;
284
285 if (XFS_FORCED_SHUTDOWN(mp))
286 return -EIO;
287
Dave Chinner0c38a252011-08-25 07:17:01 +0000288 /*
289 * Locking is a bit tricky here. If we take an exclusive lock
290 * for direct IO, we effectively serialise all new concurrent
291 * read IO to this file and block it behind IO that is currently in
292 * progress because IO in progress holds the IO lock shared. We only
293 * need to hold the lock exclusive to blow away the page cache, so
294 * only take lock exclusively if the page cache needs invalidation.
295 * This allows the normal direct IO case of no page cache pages to
296 * proceeed concurrently without serialisation.
297 */
298 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
299 if ((ioflags & IO_ISDIRECT) && inode->i_mapping->nrpages) {
300 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
Dave Chinner487f84f2011-01-12 11:37:10 +1100301 xfs_rw_ilock(ip, XFS_IOLOCK_EXCL);
302
Christoph Hellwig00258e32010-02-15 09:44:47 +0000303 if (inode->i_mapping->nrpages) {
304 ret = -xfs_flushinval_pages(ip,
305 (iocb->ki_pos & PAGE_CACHE_MASK),
306 -1, FI_REMAPF_LOCKED);
Dave Chinner487f84f2011-01-12 11:37:10 +1100307 if (ret) {
308 xfs_rw_iunlock(ip, XFS_IOLOCK_EXCL);
309 return ret;
310 }
Christoph Hellwig00258e32010-02-15 09:44:47 +0000311 }
Dave Chinner487f84f2011-01-12 11:37:10 +1100312 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
Dave Chinner0c38a252011-08-25 07:17:01 +0000313 }
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000314
Christoph Hellwig00258e32010-02-15 09:44:47 +0000315 trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000316
Christoph Hellwig00258e32010-02-15 09:44:47 +0000317 ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000318 if (ret > 0)
319 XFS_STATS_ADD(xs_read_bytes, ret);
320
Dave Chinner487f84f2011-01-12 11:37:10 +1100321 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000322 return ret;
323}
324
Christoph Hellwig00258e32010-02-15 09:44:47 +0000325STATIC ssize_t
326xfs_file_splice_read(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000327 struct file *infilp,
328 loff_t *ppos,
329 struct pipe_inode_info *pipe,
330 size_t count,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000331 unsigned int flags)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000332{
Christoph Hellwig00258e32010-02-15 09:44:47 +0000333 struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000334 int ioflags = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000335 ssize_t ret;
336
337 XFS_STATS_INC(xs_read_calls);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000338
339 if (infilp->f_mode & FMODE_NOCMTIME)
340 ioflags |= IO_INVIS;
341
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000342 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
343 return -EIO;
344
Dave Chinner487f84f2011-01-12 11:37:10 +1100345 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000346
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000347 trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
348
349 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
350 if (ret > 0)
351 XFS_STATS_ADD(xs_read_bytes, ret);
352
Dave Chinner487f84f2011-01-12 11:37:10 +1100353 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000354 return ret;
355}
356
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100357/*
Dave Chinner487f84f2011-01-12 11:37:10 +1100358 * xfs_file_splice_write() does not use xfs_rw_ilock() because
359 * generic_file_splice_write() takes the i_mutex itself. This, in theory,
360 * couuld cause lock inversions between the aio_write path and the splice path
361 * if someone is doing concurrent splice(2) based writes and write(2) based
362 * writes to the same inode. The only real way to fix this is to re-implement
363 * the generic code here with correct locking orders.
364 */
Christoph Hellwig00258e32010-02-15 09:44:47 +0000365STATIC ssize_t
366xfs_file_splice_write(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000367 struct pipe_inode_info *pipe,
368 struct file *outfilp,
369 loff_t *ppos,
370 size_t count,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000371 unsigned int flags)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000372{
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000373 struct inode *inode = outfilp->f_mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000374 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000375 int ioflags = 0;
376 ssize_t ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000377
378 XFS_STATS_INC(xs_write_calls);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000379
380 if (outfilp->f_mode & FMODE_NOCMTIME)
381 ioflags |= IO_INVIS;
382
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000383 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
384 return -EIO;
385
386 xfs_ilock(ip, XFS_IOLOCK_EXCL);
387
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000388 trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
389
390 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000391 if (ret > 0)
392 XFS_STATS_ADD(xs_write_bytes, ret);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000393
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000394 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
395 return ret;
396}
397
398/*
399 * This routine is called to handle zeroing any space in the last
400 * block of the file that is beyond the EOF. We do this since the
401 * size is being increased without writing anything to that block
402 * and we don't want anyone to read the garbage on the disk.
403 */
404STATIC int /* error (positive) */
405xfs_zero_last_block(
406 xfs_inode_t *ip,
407 xfs_fsize_t offset,
408 xfs_fsize_t isize)
409{
410 xfs_fileoff_t last_fsb;
411 xfs_mount_t *mp = ip->i_mount;
412 int nimaps;
413 int zero_offset;
414 int zero_len;
415 int error = 0;
416 xfs_bmbt_irec_t imap;
417
418 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
419
420 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
421 if (zero_offset == 0) {
422 /*
423 * There are no extra bytes in the last block on disk to
424 * zero, so return.
425 */
426 return 0;
427 }
428
429 last_fsb = XFS_B_TO_FSBT(mp, isize);
430 nimaps = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +0000431 error = xfs_bmapi_read(ip, last_fsb, 1, &imap, &nimaps, 0);
432 if (error)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000433 return error;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000434 ASSERT(nimaps > 0);
435 /*
436 * If the block underlying isize is just a hole, then there
437 * is nothing to zero.
438 */
439 if (imap.br_startblock == HOLESTARTBLOCK) {
440 return 0;
441 }
442 /*
443 * Zero the part of the last block beyond the EOF, and write it
444 * out sync. We need to drop the ilock while we do this so we
445 * don't deadlock when the buffer cache calls back to us.
446 */
447 xfs_iunlock(ip, XFS_ILOCK_EXCL);
448
449 zero_len = mp->m_sb.sb_blocksize - zero_offset;
450 if (isize + zero_len > offset)
451 zero_len = offset - isize;
452 error = xfs_iozero(ip, isize, zero_len);
453
454 xfs_ilock(ip, XFS_ILOCK_EXCL);
455 ASSERT(error >= 0);
456 return error;
457}
458
459/*
460 * Zero any on disk space between the current EOF and the new,
461 * larger EOF. This handles the normal case of zeroing the remainder
462 * of the last block in the file and the unusual case of zeroing blocks
463 * out beyond the size of the file. This second case only happens
464 * with fixed size extents and when the system crashes before the inode
465 * size was updated but after blocks were allocated. If fill is set,
466 * then any holes in the range are filled and zeroed. If not, the holes
467 * are left alone as holes.
468 */
469
470int /* error (positive) */
471xfs_zero_eof(
472 xfs_inode_t *ip,
473 xfs_off_t offset, /* starting I/O offset */
474 xfs_fsize_t isize) /* current inode size */
475{
476 xfs_mount_t *mp = ip->i_mount;
477 xfs_fileoff_t start_zero_fsb;
478 xfs_fileoff_t end_zero_fsb;
479 xfs_fileoff_t zero_count_fsb;
480 xfs_fileoff_t last_fsb;
481 xfs_fileoff_t zero_off;
482 xfs_fsize_t zero_len;
483 int nimaps;
484 int error = 0;
485 xfs_bmbt_irec_t imap;
486
487 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
488 ASSERT(offset > isize);
489
490 /*
491 * First handle zeroing the block on which isize resides.
492 * We only zero a part of that block so it is handled specially.
493 */
494 error = xfs_zero_last_block(ip, offset, isize);
495 if (error) {
496 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
497 return error;
498 }
499
500 /*
501 * Calculate the range between the new size and the old
502 * where blocks needing to be zeroed may exist. To get the
503 * block where the last byte in the file currently resides,
504 * we need to subtract one from the size and truncate back
505 * to a block boundary. We subtract 1 in case the size is
506 * exactly on a block boundary.
507 */
508 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
509 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
510 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
511 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
512 if (last_fsb == end_zero_fsb) {
513 /*
514 * The size was only incremented on its last block.
515 * We took care of that above, so just return.
516 */
517 return 0;
518 }
519
520 ASSERT(start_zero_fsb <= end_zero_fsb);
521 while (start_zero_fsb <= end_zero_fsb) {
522 nimaps = 1;
523 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +0000524 error = xfs_bmapi_read(ip, start_zero_fsb, zero_count_fsb,
525 &imap, &nimaps, 0);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000526 if (error) {
527 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
528 return error;
529 }
530 ASSERT(nimaps > 0);
531
532 if (imap.br_state == XFS_EXT_UNWRITTEN ||
533 imap.br_startblock == HOLESTARTBLOCK) {
534 /*
535 * This loop handles initializing pages that were
536 * partially initialized by the code below this
537 * loop. It basically zeroes the part of the page
538 * that sits on a hole and sets the page as P_HOLE
539 * and calls remapf if it is a mapped file.
540 */
541 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
542 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
543 continue;
544 }
545
546 /*
547 * There are blocks we need to zero.
548 * Drop the inode lock while we're doing the I/O.
549 * We'll still have the iolock to protect us.
550 */
551 xfs_iunlock(ip, XFS_ILOCK_EXCL);
552
553 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
554 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
555
556 if ((zero_off + zero_len) > offset)
557 zero_len = offset - zero_off;
558
559 error = xfs_iozero(ip, zero_off, zero_len);
560 if (error) {
561 goto out_lock;
562 }
563
564 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
565 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
566
567 xfs_ilock(ip, XFS_ILOCK_EXCL);
568 }
569
570 return 0;
571
572out_lock:
573 xfs_ilock(ip, XFS_ILOCK_EXCL);
574 ASSERT(error >= 0);
575 return error;
576}
577
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100578/*
Dave Chinner4d8d1582011-01-11 10:23:42 +1100579 * Common pre-write limit and setup checks.
580 *
Christoph Hellwig5bf1f262011-12-18 20:00:13 +0000581 * Called with the iolocked held either shared and exclusive according to
582 * @iolock, and returns with it held. Might upgrade the iolock to exclusive
583 * if called for a direct write beyond i_size.
Dave Chinner4d8d1582011-01-11 10:23:42 +1100584 */
585STATIC ssize_t
586xfs_file_aio_write_checks(
587 struct file *file,
588 loff_t *pos,
589 size_t *count,
590 int *iolock)
591{
592 struct inode *inode = file->f_mapping->host;
593 struct xfs_inode *ip = XFS_I(inode);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100594 int error = 0;
595
Dave Chinner7271d242011-08-25 07:17:02 +0000596restart:
Dave Chinner4d8d1582011-01-11 10:23:42 +1100597 error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode));
Christoph Hellwig467f7892012-03-27 10:34:47 -0400598 if (error)
Dave Chinner4d8d1582011-01-11 10:23:42 +1100599 return error;
Dave Chinner4d8d1582011-01-11 10:23:42 +1100600
Dave Chinner4d8d1582011-01-11 10:23:42 +1100601 /*
602 * If the offset is beyond the size of the file, we need to zero any
603 * blocks that fall between the existing EOF and the start of this
Christoph Hellwig2813d682011-12-18 20:00:12 +0000604 * write. If zeroing is needed and we are currently holding the
Christoph Hellwig467f7892012-03-27 10:34:47 -0400605 * iolock shared, we need to update it to exclusive which implies
606 * having to redo all checks before.
Dave Chinner4d8d1582011-01-11 10:23:42 +1100607 */
Christoph Hellwig2813d682011-12-18 20:00:12 +0000608 if (*pos > i_size_read(inode)) {
Dave Chinner7271d242011-08-25 07:17:02 +0000609 if (*iolock == XFS_IOLOCK_SHARED) {
Christoph Hellwig467f7892012-03-27 10:34:47 -0400610 xfs_rw_iunlock(ip, *iolock);
Dave Chinner7271d242011-08-25 07:17:02 +0000611 *iolock = XFS_IOLOCK_EXCL;
Christoph Hellwig467f7892012-03-27 10:34:47 -0400612 xfs_rw_ilock(ip, *iolock);
Dave Chinner7271d242011-08-25 07:17:02 +0000613 goto restart;
614 }
Christoph Hellwig467f7892012-03-27 10:34:47 -0400615 xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000616 error = -xfs_zero_eof(ip, *pos, i_size_read(inode));
Christoph Hellwig467f7892012-03-27 10:34:47 -0400617 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
618 if (error)
619 return error;
Dave Chinner7271d242011-08-25 07:17:02 +0000620 }
Dave Chinner4d8d1582011-01-11 10:23:42 +1100621
622 /*
Christoph Hellwig8a9c9982012-02-29 09:53:52 +0000623 * Updating the timestamps will grab the ilock again from
624 * xfs_fs_dirty_inode, so we have to call it after dropping the
625 * lock above. Eventually we should look into a way to avoid
626 * the pointless lock roundtrip.
627 */
628 if (likely(!(file->f_mode & FMODE_NOCMTIME)))
629 file_update_time(file);
630
631 /*
Dave Chinner4d8d1582011-01-11 10:23:42 +1100632 * If we're writing the file then make sure to clear the setuid and
633 * setgid bits if the process is not being run by root. This keeps
634 * people from modifying setuid and setgid binaries.
635 */
636 return file_remove_suid(file);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100637}
638
639/*
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100640 * xfs_file_dio_aio_write - handle direct IO writes
641 *
642 * Lock the inode appropriately to prepare for and issue a direct IO write.
Dave Chinnereda77982011-01-11 10:22:40 +1100643 * By separating it from the buffered write path we remove all the tricky to
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100644 * follow locking changes and looping.
645 *
Dave Chinnereda77982011-01-11 10:22:40 +1100646 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
647 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
648 * pages are flushed out.
649 *
650 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
651 * allowing them to be done in parallel with reads and other direct IO writes.
652 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
653 * needs to do sub-block zeroing and that requires serialisation against other
654 * direct IOs to the same block. In this case we need to serialise the
655 * submission of the unaligned IOs so that we don't get racing block zeroing in
656 * the dio layer. To avoid the problem with aio, we also need to wait for
657 * outstanding IOs to complete so that unwritten extent conversion is completed
658 * before we try to map the overlapping block. This is currently implemented by
Christoph Hellwig4a06fd22011-08-23 08:28:13 +0000659 * hitting it with a big hammer (i.e. inode_dio_wait()).
Dave Chinnereda77982011-01-11 10:22:40 +1100660 *
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100661 * Returns with locks held indicated by @iolock and errors indicated by
662 * negative return values.
663 */
664STATIC ssize_t
665xfs_file_dio_aio_write(
666 struct kiocb *iocb,
667 const struct iovec *iovp,
668 unsigned long nr_segs,
669 loff_t pos,
Christoph Hellwigd0606462011-12-18 20:00:14 +0000670 size_t ocount)
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100671{
672 struct file *file = iocb->ki_filp;
673 struct address_space *mapping = file->f_mapping;
674 struct inode *inode = mapping->host;
675 struct xfs_inode *ip = XFS_I(inode);
676 struct xfs_mount *mp = ip->i_mount;
677 ssize_t ret = 0;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100678 size_t count = ocount;
Dave Chinnereda77982011-01-11 10:22:40 +1100679 int unaligned_io = 0;
Christoph Hellwigd0606462011-12-18 20:00:14 +0000680 int iolock;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100681 struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
682 mp->m_rtdev_targp : mp->m_ddev_targp;
683
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100684 if ((pos & target->bt_smask) || (count & target->bt_smask))
685 return -XFS_ERROR(EINVAL);
686
Dave Chinnereda77982011-01-11 10:22:40 +1100687 if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
688 unaligned_io = 1;
689
Dave Chinner7271d242011-08-25 07:17:02 +0000690 /*
691 * We don't need to take an exclusive lock unless there page cache needs
692 * to be invalidated or unaligned IO is being executed. We don't need to
693 * consider the EOF extension case here because
694 * xfs_file_aio_write_checks() will relock the inode as necessary for
695 * EOF zeroing cases and fill out the new inode size as appropriate.
696 */
697 if (unaligned_io || mapping->nrpages)
Christoph Hellwigd0606462011-12-18 20:00:14 +0000698 iolock = XFS_IOLOCK_EXCL;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100699 else
Christoph Hellwigd0606462011-12-18 20:00:14 +0000700 iolock = XFS_IOLOCK_SHARED;
701 xfs_rw_ilock(ip, iolock);
Christoph Hellwigc58cb162011-08-27 14:42:53 +0000702
703 /*
704 * Recheck if there are cached pages that need invalidate after we got
705 * the iolock to protect against other threads adding new pages while
706 * we were waiting for the iolock.
707 */
Christoph Hellwigd0606462011-12-18 20:00:14 +0000708 if (mapping->nrpages && iolock == XFS_IOLOCK_SHARED) {
709 xfs_rw_iunlock(ip, iolock);
710 iolock = XFS_IOLOCK_EXCL;
711 xfs_rw_ilock(ip, iolock);
Christoph Hellwigc58cb162011-08-27 14:42:53 +0000712 }
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100713
Christoph Hellwigd0606462011-12-18 20:00:14 +0000714 ret = xfs_file_aio_write_checks(file, &pos, &count, &iolock);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100715 if (ret)
Christoph Hellwigd0606462011-12-18 20:00:14 +0000716 goto out;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100717
718 if (mapping->nrpages) {
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100719 ret = -xfs_flushinval_pages(ip, (pos & PAGE_CACHE_MASK), -1,
720 FI_REMAPF_LOCKED);
721 if (ret)
Christoph Hellwigd0606462011-12-18 20:00:14 +0000722 goto out;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100723 }
724
Dave Chinnereda77982011-01-11 10:22:40 +1100725 /*
726 * If we are doing unaligned IO, wait for all other IO to drain,
727 * otherwise demote the lock if we had to flush cached pages
728 */
729 if (unaligned_io)
Christoph Hellwig4a06fd22011-08-23 08:28:13 +0000730 inode_dio_wait(inode);
Christoph Hellwigd0606462011-12-18 20:00:14 +0000731 else if (iolock == XFS_IOLOCK_EXCL) {
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100732 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigd0606462011-12-18 20:00:14 +0000733 iolock = XFS_IOLOCK_SHARED;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100734 }
735
736 trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
737 ret = generic_file_direct_write(iocb, iovp,
738 &nr_segs, pos, &iocb->ki_pos, count, ocount);
739
Christoph Hellwigd0606462011-12-18 20:00:14 +0000740out:
741 xfs_rw_iunlock(ip, iolock);
742
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100743 /* No fallback to buffered IO on errors for XFS. */
744 ASSERT(ret < 0 || ret == count);
745 return ret;
746}
747
Christoph Hellwig00258e32010-02-15 09:44:47 +0000748STATIC ssize_t
Dave Chinner637bbc72011-01-11 10:17:30 +1100749xfs_file_buffered_aio_write(
750 struct kiocb *iocb,
751 const struct iovec *iovp,
752 unsigned long nr_segs,
753 loff_t pos,
Christoph Hellwigd0606462011-12-18 20:00:14 +0000754 size_t ocount)
Dave Chinner637bbc72011-01-11 10:17:30 +1100755{
756 struct file *file = iocb->ki_filp;
757 struct address_space *mapping = file->f_mapping;
758 struct inode *inode = mapping->host;
759 struct xfs_inode *ip = XFS_I(inode);
760 ssize_t ret;
761 int enospc = 0;
Christoph Hellwigd0606462011-12-18 20:00:14 +0000762 int iolock = XFS_IOLOCK_EXCL;
Dave Chinner637bbc72011-01-11 10:17:30 +1100763 size_t count = ocount;
764
Christoph Hellwigd0606462011-12-18 20:00:14 +0000765 xfs_rw_ilock(ip, iolock);
Dave Chinner637bbc72011-01-11 10:17:30 +1100766
Christoph Hellwigd0606462011-12-18 20:00:14 +0000767 ret = xfs_file_aio_write_checks(file, &pos, &count, &iolock);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100768 if (ret)
Christoph Hellwigd0606462011-12-18 20:00:14 +0000769 goto out;
Dave Chinner637bbc72011-01-11 10:17:30 +1100770
771 /* We can write back this queue in page reclaim */
772 current->backing_dev_info = mapping->backing_dev_info;
773
774write_retry:
775 trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
776 ret = generic_file_buffered_write(iocb, iovp, nr_segs,
777 pos, &iocb->ki_pos, count, ret);
778 /*
779 * if we just got an ENOSPC, flush the inode now we aren't holding any
780 * page locks and retry *once*
781 */
782 if (ret == -ENOSPC && !enospc) {
Dave Chinner637bbc72011-01-11 10:17:30 +1100783 enospc = 1;
Christoph Hellwigd0606462011-12-18 20:00:14 +0000784 ret = -xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
785 if (!ret)
786 goto write_retry;
Dave Chinner637bbc72011-01-11 10:17:30 +1100787 }
Christoph Hellwigd0606462011-12-18 20:00:14 +0000788
Dave Chinner637bbc72011-01-11 10:17:30 +1100789 current->backing_dev_info = NULL;
Christoph Hellwigd0606462011-12-18 20:00:14 +0000790out:
791 xfs_rw_iunlock(ip, iolock);
Dave Chinner637bbc72011-01-11 10:17:30 +1100792 return ret;
793}
794
795STATIC ssize_t
Christoph Hellwig00258e32010-02-15 09:44:47 +0000796xfs_file_aio_write(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000797 struct kiocb *iocb,
798 const struct iovec *iovp,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000799 unsigned long nr_segs,
800 loff_t pos)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000801{
802 struct file *file = iocb->ki_filp;
803 struct address_space *mapping = file->f_mapping;
804 struct inode *inode = mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000805 struct xfs_inode *ip = XFS_I(inode);
Dave Chinner637bbc72011-01-11 10:17:30 +1100806 ssize_t ret;
Dave Chinner637bbc72011-01-11 10:17:30 +1100807 size_t ocount = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000808
809 XFS_STATS_INC(xs_write_calls);
810
Christoph Hellwig00258e32010-02-15 09:44:47 +0000811 BUG_ON(iocb->ki_pos != pos);
812
Dave Chinnera363f0c2011-01-11 10:13:53 +1100813 ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
814 if (ret)
815 return ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000816
Dave Chinner637bbc72011-01-11 10:17:30 +1100817 if (ocount == 0)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000818 return 0;
819
Dave Chinner637bbc72011-01-11 10:17:30 +1100820 xfs_wait_for_freeze(ip->i_mount, SB_FREEZE_WRITE);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000821
Dave Chinner637bbc72011-01-11 10:17:30 +1100822 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000823 return -EIO;
824
Dave Chinner637bbc72011-01-11 10:17:30 +1100825 if (unlikely(file->f_flags & O_DIRECT))
Christoph Hellwigd0606462011-12-18 20:00:14 +0000826 ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos, ocount);
Dave Chinner637bbc72011-01-11 10:17:30 +1100827 else
828 ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
Christoph Hellwigd0606462011-12-18 20:00:14 +0000829 ocount);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000830
Christoph Hellwigd0606462011-12-18 20:00:14 +0000831 if (ret > 0) {
832 ssize_t err;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000833
Christoph Hellwigd0606462011-12-18 20:00:14 +0000834 XFS_STATS_ADD(xs_write_bytes, ret);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000835
Christoph Hellwigd0606462011-12-18 20:00:14 +0000836 /* Handle various SYNC-type writes */
837 err = generic_write_sync(file, pos, ret);
838 if (err < 0)
839 ret = err;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000840 }
841
Dave Chinnera363f0c2011-01-11 10:13:53 +1100842 return ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000843}
844
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100845STATIC long
846xfs_file_fallocate(
847 struct file *file,
848 int mode,
849 loff_t offset,
850 loff_t len)
851{
852 struct inode *inode = file->f_path.dentry->d_inode;
853 long error;
854 loff_t new_size = 0;
855 xfs_flock64_t bf;
856 xfs_inode_t *ip = XFS_I(inode);
857 int cmd = XFS_IOC_RESVSP;
Dave Chinner82878892011-03-26 09:13:08 +1100858 int attr_flags = XFS_ATTR_NOLOCK;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100859
860 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
861 return -EOPNOTSUPP;
862
863 bf.l_whence = 0;
864 bf.l_start = offset;
865 bf.l_len = len;
866
867 xfs_ilock(ip, XFS_IOLOCK_EXCL);
868
869 if (mode & FALLOC_FL_PUNCH_HOLE)
870 cmd = XFS_IOC_UNRESVSP;
871
872 /* check the new inode size is valid before allocating */
873 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
874 offset + len > i_size_read(inode)) {
875 new_size = offset + len;
876 error = inode_newsize_ok(inode, new_size);
877 if (error)
878 goto out_unlock;
879 }
880
Dave Chinner82878892011-03-26 09:13:08 +1100881 if (file->f_flags & O_DSYNC)
882 attr_flags |= XFS_ATTR_SYNC;
883
884 error = -xfs_change_file_space(ip, cmd, &bf, 0, attr_flags);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100885 if (error)
886 goto out_unlock;
887
888 /* Change file size if needed */
889 if (new_size) {
890 struct iattr iattr;
891
892 iattr.ia_valid = ATTR_SIZE;
893 iattr.ia_size = new_size;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200894 error = -xfs_setattr_size(ip, &iattr, XFS_ATTR_NOLOCK);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100895 }
896
897out_unlock:
898 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
899 return error;
900}
901
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100904xfs_file_open(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 struct inode *inode,
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100906 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100908 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return -EFBIG;
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100910 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
911 return -EIO;
912 return 0;
913}
914
915STATIC int
916xfs_dir_open(
917 struct inode *inode,
918 struct file *file)
919{
920 struct xfs_inode *ip = XFS_I(inode);
921 int mode;
922 int error;
923
924 error = xfs_file_open(inode, file);
925 if (error)
926 return error;
927
928 /*
929 * If there are any blocks, read-ahead block 0 as we're almost
930 * certain to have the next operation be a read there.
931 */
932 mode = xfs_ilock_map_shared(ip);
933 if (ip->i_d.di_nextents > 0)
934 xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
935 xfs_iunlock(ip, mode);
936 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100940xfs_file_release(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 struct inode *inode,
942 struct file *filp)
943{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000944 return -xfs_release(XFS_I(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100948xfs_file_readdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 struct file *filp,
950 void *dirent,
951 filldir_t filldir)
952{
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000953 struct inode *inode = filp->f_path.dentry->d_inode;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000954 xfs_inode_t *ip = XFS_I(inode);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000955 int error;
956 size_t bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000958 /*
959 * The Linux API doesn't pass down the total size of the buffer
960 * we read into down to the filesystem. With the filldir concept
961 * it's not needed for correct information, but the XFS dir2 leaf
962 * code wants an estimate of the buffer size to calculate it's
963 * readahead window and size the buffers used for mapping to
964 * physical blocks.
965 *
966 * Try to give it an estimate that's good enough, maybe at some
967 * point we can change the ->readdir prototype to include the
Eric Sandeena9cc7992010-02-03 17:50:13 +0000968 * buffer size. For now we use the current glibc buffer size.
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000969 */
Eric Sandeena9cc7992010-02-03 17:50:13 +0000970 bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000972 error = xfs_readdir(ip, dirent, bufsize,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000973 (xfs_off_t *)&filp->f_pos, filldir);
974 if (error)
975 return -error;
976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100980xfs_file_mmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 struct file *filp,
982 struct vm_area_struct *vma)
983{
Nathan Scott3562fd42006-03-14 14:00:35 +1100984 vma->vm_ops = &xfs_file_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -0700985 vma->vm_flags |= VM_CAN_NONLINEAR;
Dean Roehrich6fac0cb2005-06-21 14:07:45 +1000986
Nathan Scottfbc14622006-06-09 14:52:13 +1000987 file_accessed(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return 0;
989}
990
David Chinner4f57dbc2007-07-19 16:28:17 +1000991/*
992 * mmap()d file has taken write protection fault and is being made
993 * writable. We can set the page state up correctly for a writable
994 * page, which means we can do correct delalloc accounting (ENOSPC
995 * checking!) and unwritten extent mapping.
996 */
997STATIC int
998xfs_vm_page_mkwrite(
999 struct vm_area_struct *vma,
Nick Pigginc2ec1752009-03-31 15:23:21 -07001000 struct vm_fault *vmf)
David Chinner4f57dbc2007-07-19 16:28:17 +10001001{
Nick Pigginc2ec1752009-03-31 15:23:21 -07001002 return block_page_mkwrite(vma, vmf, xfs_get_blocks);
David Chinner4f57dbc2007-07-19 16:28:17 +10001003}
1004
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001005const struct file_operations xfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 .llseek = generic_file_llseek,
1007 .read = do_sync_read,
Dean Roehrichbb3f7242005-09-02 15:43:05 +10001008 .write = do_sync_write,
Nathan Scott3562fd42006-03-14 14:00:35 +11001009 .aio_read = xfs_file_aio_read,
1010 .aio_write = xfs_file_aio_write,
Nathan Scott1b895842006-03-31 13:08:59 +10001011 .splice_read = xfs_file_splice_read,
1012 .splice_write = xfs_file_splice_write,
Nathan Scott3562fd42006-03-14 14:00:35 +11001013 .unlocked_ioctl = xfs_file_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +11001015 .compat_ioctl = xfs_file_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016#endif
Nathan Scott3562fd42006-03-14 14:00:35 +11001017 .mmap = xfs_file_mmap,
1018 .open = xfs_file_open,
1019 .release = xfs_file_release,
1020 .fsync = xfs_file_fsync,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001021 .fallocate = xfs_file_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022};
1023
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001024const struct file_operations xfs_dir_file_operations = {
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001025 .open = xfs_dir_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 .read = generic_read_dir,
Nathan Scott3562fd42006-03-14 14:00:35 +11001027 .readdir = xfs_file_readdir,
Al Viro59af1582008-08-24 07:24:41 -04001028 .llseek = generic_file_llseek,
Nathan Scott3562fd42006-03-14 14:00:35 +11001029 .unlocked_ioctl = xfs_file_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -07001030#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +11001031 .compat_ioctl = xfs_file_compat_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -07001032#endif
Christoph Hellwig1da2f2d2011-10-02 14:25:16 +00001033 .fsync = xfs_dir_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034};
1035
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001036static const struct vm_operations_struct xfs_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07001037 .fault = filemap_fault,
David Chinner4f57dbc2007-07-19 16:28:17 +10001038 .page_mkwrite = xfs_vm_page_mkwrite,
Dean Roehrich6fac0cb2005-06-21 14:07:45 +10001039};