blob: cc22aa1b7b3b724007363904d5739720518ba86e [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"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
22#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110026#include "xfs_da_format.h"
27#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_dinode.h"
31#include "xfs_inode.h"
Christoph Hellwigfd3200b2010-02-15 09:44:48 +000032#include "xfs_inode_item.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000033#include "xfs_bmap.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100034#include "xfs_bmap_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_error.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100036#include "xfs_dir2.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100037#include "xfs_dir2_priv.h"
Christoph Hellwigddcd8562008-12-03 07:55:34 -050038#include "xfs_ioctl.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000039#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Kent Overstreeta27bb332013-05-07 16:19:08 -070041#include <linux/aio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/dcache.h>
Christoph Hellwig2fe17c12011-01-14 13:07:43 +010043#include <linux/falloc.h>
Jeff Liud126d432012-08-21 17:11:57 +080044#include <linux/pagevec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +040046static const struct vm_operations_struct xfs_file_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Christoph Hellwigdda35b82010-02-15 09:44:46 +000048/*
Dave Chinner487f84f2011-01-12 11:37:10 +110049 * Locking primitives for read and write IO paths to ensure we consistently use
50 * and order the inode->i_mutex, ip->i_lock and ip->i_iolock.
51 */
52static inline void
53xfs_rw_ilock(
54 struct xfs_inode *ip,
55 int type)
56{
57 if (type & XFS_IOLOCK_EXCL)
58 mutex_lock(&VFS_I(ip)->i_mutex);
59 xfs_ilock(ip, type);
60}
61
62static inline void
63xfs_rw_iunlock(
64 struct xfs_inode *ip,
65 int type)
66{
67 xfs_iunlock(ip, type);
68 if (type & XFS_IOLOCK_EXCL)
69 mutex_unlock(&VFS_I(ip)->i_mutex);
70}
71
72static inline void
73xfs_rw_ilock_demote(
74 struct xfs_inode *ip,
75 int type)
76{
77 xfs_ilock_demote(ip, type);
78 if (type & XFS_IOLOCK_EXCL)
79 mutex_unlock(&VFS_I(ip)->i_mutex);
80}
81
82/*
Christoph Hellwigdda35b82010-02-15 09:44:46 +000083 * xfs_iozero
84 *
85 * xfs_iozero clears the specified range of buffer supplied,
86 * and marks all the affected blocks as valid and modified. If
87 * an affected block is not allocated, it will be allocated. If
88 * an affected block is not completely overwritten, and is not
89 * valid before the operation, it will be read from disk before
90 * being partially zeroed.
91 */
Dave Chinneref9d8732012-11-29 15:26:33 +110092int
Christoph Hellwigdda35b82010-02-15 09:44:46 +000093xfs_iozero(
94 struct xfs_inode *ip, /* inode */
95 loff_t pos, /* offset in file */
96 size_t count) /* size of data to zero */
97{
98 struct page *page;
99 struct address_space *mapping;
100 int status;
101
102 mapping = VFS_I(ip)->i_mapping;
103 do {
104 unsigned offset, bytes;
105 void *fsdata;
106
107 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
108 bytes = PAGE_CACHE_SIZE - offset;
109 if (bytes > count)
110 bytes = count;
111
112 status = pagecache_write_begin(NULL, mapping, pos, bytes,
113 AOP_FLAG_UNINTERRUPTIBLE,
114 &page, &fsdata);
115 if (status)
116 break;
117
118 zero_user(page, offset, bytes);
119
120 status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
121 page, fsdata);
122 WARN_ON(status <= 0); /* can't return less than zero! */
123 pos += bytes;
124 count -= bytes;
125 status = 0;
126 } while (count);
127
128 return (-status);
129}
130
Christoph Hellwig1da2f2d2011-10-02 14:25:16 +0000131/*
132 * Fsync operations on directories are much simpler than on regular files,
133 * as there is no file data to flush, and thus also no need for explicit
134 * cache flush operations, and there are no non-transaction metadata updates
135 * on directories either.
136 */
137STATIC int
138xfs_dir_fsync(
139 struct file *file,
140 loff_t start,
141 loff_t end,
142 int datasync)
143{
144 struct xfs_inode *ip = XFS_I(file->f_mapping->host);
145 struct xfs_mount *mp = ip->i_mount;
146 xfs_lsn_t lsn = 0;
147
148 trace_xfs_dir_fsync(ip);
149
150 xfs_ilock(ip, XFS_ILOCK_SHARED);
151 if (xfs_ipincount(ip))
152 lsn = ip->i_itemp->ili_last_lsn;
153 xfs_iunlock(ip, XFS_ILOCK_SHARED);
154
155 if (!lsn)
156 return 0;
157 return _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
158}
159
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000160STATIC int
161xfs_file_fsync(
162 struct file *file,
Josef Bacik02c24a82011-07-16 20:44:56 -0400163 loff_t start,
164 loff_t end,
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000165 int datasync)
166{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200167 struct inode *inode = file->f_mapping->host;
168 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000169 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000170 int error = 0;
171 int log_flushed = 0;
Christoph Hellwigb1037052011-09-19 14:55:51 +0000172 xfs_lsn_t lsn = 0;
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000173
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000174 trace_xfs_file_fsync(ip);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000175
Josef Bacik02c24a82011-07-16 20:44:56 -0400176 error = filemap_write_and_wait_range(inode->i_mapping, start, end);
177 if (error)
178 return error;
179
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000180 if (XFS_FORCED_SHUTDOWN(mp))
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000181 return -XFS_ERROR(EIO);
182
183 xfs_iflags_clear(ip, XFS_ITRUNCATED);
184
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000185 if (mp->m_flags & XFS_MOUNT_BARRIER) {
186 /*
187 * If we have an RT and/or log subvolume we need to make sure
188 * to flush the write cache the device used for file data
189 * first. This is to ensure newly written file data make
190 * it to disk before logging the new inode size in case of
191 * an extending write.
192 */
193 if (XFS_IS_REALTIME_INODE(ip))
194 xfs_blkdev_issue_flush(mp->m_rtdev_targp);
195 else if (mp->m_logdev_targp != mp->m_ddev_targp)
196 xfs_blkdev_issue_flush(mp->m_ddev_targp);
197 }
198
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000199 /*
Christoph Hellwig8a9c9982012-02-29 09:53:52 +0000200 * All metadata updates are logged, which means that we just have
201 * to flush the log up to the latest LSN that touched the inode.
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000202 */
203 xfs_ilock(ip, XFS_ILOCK_SHARED);
Christoph Hellwig8f639dd2012-02-29 09:53:55 +0000204 if (xfs_ipincount(ip)) {
205 if (!datasync ||
206 (ip->i_itemp->ili_fields & ~XFS_ILOG_TIMESTAMP))
207 lsn = ip->i_itemp->ili_last_lsn;
208 }
Christoph Hellwig8a9c9982012-02-29 09:53:52 +0000209 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000210
Christoph Hellwig8a9c9982012-02-29 09:53:52 +0000211 if (lsn)
Christoph Hellwigb1037052011-09-19 14:55:51 +0000212 error = _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, &log_flushed);
213
Christoph Hellwiga27a2632011-06-16 12:02:23 +0000214 /*
215 * If we only have a single device, and the log force about was
216 * a no-op we might have to flush the data device cache here.
217 * This can only happen for fdatasync/O_DSYNC if we were overwriting
218 * an already allocated file and thus do not have any metadata to
219 * commit.
220 */
221 if ((mp->m_flags & XFS_MOUNT_BARRIER) &&
222 mp->m_logdev_targp == mp->m_ddev_targp &&
223 !XFS_IS_REALTIME_INODE(ip) &&
224 !log_flushed)
225 xfs_blkdev_issue_flush(mp->m_ddev_targp);
Christoph Hellwigfd3200b2010-02-15 09:44:48 +0000226
227 return -error;
228}
229
Christoph Hellwig00258e32010-02-15 09:44:47 +0000230STATIC ssize_t
231xfs_file_aio_read(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000232 struct kiocb *iocb,
233 const struct iovec *iovp,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000234 unsigned long nr_segs,
235 loff_t pos)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000236{
237 struct file *file = iocb->ki_filp;
238 struct inode *inode = file->f_mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000239 struct xfs_inode *ip = XFS_I(inode);
240 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000241 size_t size = 0;
242 ssize_t ret = 0;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000243 int ioflags = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000244 xfs_fsize_t n;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000245
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000246 XFS_STATS_INC(xs_read_calls);
247
Christoph Hellwig00258e32010-02-15 09:44:47 +0000248 BUG_ON(iocb->ki_pos != pos);
249
250 if (unlikely(file->f_flags & O_DIRECT))
251 ioflags |= IO_ISDIRECT;
252 if (file->f_mode & FMODE_NOCMTIME)
253 ioflags |= IO_INVIS;
254
Dave Chinner52764322012-06-08 15:45:44 +1000255 ret = generic_segment_checks(iovp, &nr_segs, &size, VERIFY_WRITE);
256 if (ret < 0)
257 return ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000258
259 if (unlikely(ioflags & IO_ISDIRECT)) {
260 xfs_buftarg_t *target =
261 XFS_IS_REALTIME_INODE(ip) ?
262 mp->m_rtdev_targp : mp->m_ddev_targp;
Dave Chinnerfb595812012-11-12 22:53:57 +1100263 if ((pos & target->bt_smask) || (size & target->bt_smask)) {
264 if (pos == i_size_read(inode))
Christoph Hellwig00258e32010-02-15 09:44:47 +0000265 return 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000266 return -XFS_ERROR(EINVAL);
267 }
268 }
269
Dave Chinnerfb595812012-11-12 22:53:57 +1100270 n = mp->m_super->s_maxbytes - pos;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000271 if (n <= 0 || size == 0)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000272 return 0;
273
274 if (n < size)
275 size = n;
276
277 if (XFS_FORCED_SHUTDOWN(mp))
278 return -EIO;
279
Dave Chinner0c38a252011-08-25 07:17:01 +0000280 /*
281 * Locking is a bit tricky here. If we take an exclusive lock
282 * for direct IO, we effectively serialise all new concurrent
283 * read IO to this file and block it behind IO that is currently in
284 * progress because IO in progress holds the IO lock shared. We only
285 * need to hold the lock exclusive to blow away the page cache, so
286 * only take lock exclusively if the page cache needs invalidation.
287 * This allows the normal direct IO case of no page cache pages to
288 * proceeed concurrently without serialisation.
289 */
290 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
291 if ((ioflags & IO_ISDIRECT) && inode->i_mapping->nrpages) {
292 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
Dave Chinner487f84f2011-01-12 11:37:10 +1100293 xfs_rw_ilock(ip, XFS_IOLOCK_EXCL);
294
Christoph Hellwig00258e32010-02-15 09:44:47 +0000295 if (inode->i_mapping->nrpages) {
Dave Chinnerfb595812012-11-12 22:53:57 +1100296 ret = -filemap_write_and_wait_range(
297 VFS_I(ip)->i_mapping,
298 pos, -1);
Dave Chinner487f84f2011-01-12 11:37:10 +1100299 if (ret) {
300 xfs_rw_iunlock(ip, XFS_IOLOCK_EXCL);
301 return ret;
302 }
Dave Chinnerfb595812012-11-12 22:53:57 +1100303 truncate_pagecache_range(VFS_I(ip), pos, -1);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000304 }
Dave Chinner487f84f2011-01-12 11:37:10 +1100305 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
Dave Chinner0c38a252011-08-25 07:17:01 +0000306 }
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000307
Dave Chinnerfb595812012-11-12 22:53:57 +1100308 trace_xfs_file_read(ip, size, pos, ioflags);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000309
Dave Chinnerfb595812012-11-12 22:53:57 +1100310 ret = generic_file_aio_read(iocb, iovp, nr_segs, pos);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000311 if (ret > 0)
312 XFS_STATS_ADD(xs_read_bytes, ret);
313
Dave Chinner487f84f2011-01-12 11:37:10 +1100314 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000315 return ret;
316}
317
Christoph Hellwig00258e32010-02-15 09:44:47 +0000318STATIC ssize_t
319xfs_file_splice_read(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000320 struct file *infilp,
321 loff_t *ppos,
322 struct pipe_inode_info *pipe,
323 size_t count,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000324 unsigned int flags)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000325{
Christoph Hellwig00258e32010-02-15 09:44:47 +0000326 struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000327 int ioflags = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000328 ssize_t ret;
329
330 XFS_STATS_INC(xs_read_calls);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000331
332 if (infilp->f_mode & FMODE_NOCMTIME)
333 ioflags |= IO_INVIS;
334
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000335 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
336 return -EIO;
337
Dave Chinner487f84f2011-01-12 11:37:10 +1100338 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000339
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000340 trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
341
342 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
343 if (ret > 0)
344 XFS_STATS_ADD(xs_read_bytes, ret);
345
Dave Chinner487f84f2011-01-12 11:37:10 +1100346 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000347 return ret;
348}
349
Dave Chinner4c5cfd12011-01-11 10:14:16 +1100350/*
Dave Chinner487f84f2011-01-12 11:37:10 +1100351 * xfs_file_splice_write() does not use xfs_rw_ilock() because
352 * generic_file_splice_write() takes the i_mutex itself. This, in theory,
353 * couuld cause lock inversions between the aio_write path and the splice path
354 * if someone is doing concurrent splice(2) based writes and write(2) based
355 * writes to the same inode. The only real way to fix this is to re-implement
356 * the generic code here with correct locking orders.
357 */
Christoph Hellwig00258e32010-02-15 09:44:47 +0000358STATIC ssize_t
359xfs_file_splice_write(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000360 struct pipe_inode_info *pipe,
361 struct file *outfilp,
362 loff_t *ppos,
363 size_t count,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000364 unsigned int flags)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000365{
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000366 struct inode *inode = outfilp->f_mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000367 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000368 int ioflags = 0;
369 ssize_t ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000370
371 XFS_STATS_INC(xs_write_calls);
Christoph Hellwig00258e32010-02-15 09:44:47 +0000372
373 if (outfilp->f_mode & FMODE_NOCMTIME)
374 ioflags |= IO_INVIS;
375
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000376 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
377 return -EIO;
378
379 xfs_ilock(ip, XFS_IOLOCK_EXCL);
380
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000381 trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
382
383 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000384 if (ret > 0)
385 XFS_STATS_ADD(xs_write_bytes, ret);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000386
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000387 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
388 return ret;
389}
390
391/*
Christoph Hellwig193aec12012-03-27 10:34:49 -0400392 * This routine is called to handle zeroing any space in the last block of the
393 * file that is beyond the EOF. We do this since the size is being increased
394 * without writing anything to that block and we don't want to read the
395 * garbage on the disk.
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000396 */
397STATIC int /* error (positive) */
398xfs_zero_last_block(
Christoph Hellwig193aec12012-03-27 10:34:49 -0400399 struct xfs_inode *ip,
400 xfs_fsize_t offset,
401 xfs_fsize_t isize)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000402{
Christoph Hellwig193aec12012-03-27 10:34:49 -0400403 struct xfs_mount *mp = ip->i_mount;
404 xfs_fileoff_t last_fsb = XFS_B_TO_FSBT(mp, isize);
405 int zero_offset = XFS_B_FSB_OFFSET(mp, isize);
406 int zero_len;
407 int nimaps = 1;
408 int error = 0;
409 struct xfs_bmbt_irec imap;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000410
Christoph Hellwig193aec12012-03-27 10:34:49 -0400411 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner5c8ed202011-09-18 20:40:45 +0000412 error = xfs_bmapi_read(ip, last_fsb, 1, &imap, &nimaps, 0);
Christoph Hellwig193aec12012-03-27 10:34:49 -0400413 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner5c8ed202011-09-18 20:40:45 +0000414 if (error)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000415 return error;
Christoph Hellwig193aec12012-03-27 10:34:49 -0400416
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000417 ASSERT(nimaps > 0);
Christoph Hellwig193aec12012-03-27 10:34:49 -0400418
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000419 /*
420 * If the block underlying isize is just a hole, then there
421 * is nothing to zero.
422 */
Christoph Hellwig193aec12012-03-27 10:34:49 -0400423 if (imap.br_startblock == HOLESTARTBLOCK)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000424 return 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000425
426 zero_len = mp->m_sb.sb_blocksize - zero_offset;
427 if (isize + zero_len > offset)
428 zero_len = offset - isize;
Christoph Hellwig193aec12012-03-27 10:34:49 -0400429 return xfs_iozero(ip, isize, zero_len);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000430}
431
432/*
Christoph Hellwig193aec12012-03-27 10:34:49 -0400433 * Zero any on disk space between the current EOF and the new, larger EOF.
434 *
435 * This handles the normal case of zeroing the remainder of the last block in
436 * the file and the unusual case of zeroing blocks out beyond the size of the
437 * file. This second case only happens with fixed size extents and when the
438 * system crashes before the inode size was updated but after blocks were
439 * allocated.
440 *
441 * Expects the iolock to be held exclusive, and will take the ilock internally.
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000442 */
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000443int /* error (positive) */
444xfs_zero_eof(
Christoph Hellwig193aec12012-03-27 10:34:49 -0400445 struct xfs_inode *ip,
446 xfs_off_t offset, /* starting I/O offset */
447 xfs_fsize_t isize) /* current inode size */
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000448{
Christoph Hellwig193aec12012-03-27 10:34:49 -0400449 struct xfs_mount *mp = ip->i_mount;
450 xfs_fileoff_t start_zero_fsb;
451 xfs_fileoff_t end_zero_fsb;
452 xfs_fileoff_t zero_count_fsb;
453 xfs_fileoff_t last_fsb;
454 xfs_fileoff_t zero_off;
455 xfs_fsize_t zero_len;
456 int nimaps;
457 int error = 0;
458 struct xfs_bmbt_irec imap;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000459
Christoph Hellwig193aec12012-03-27 10:34:49 -0400460 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000461 ASSERT(offset > isize);
462
463 /*
464 * First handle zeroing the block on which isize resides.
Christoph Hellwig193aec12012-03-27 10:34:49 -0400465 *
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000466 * We only zero a part of that block so it is handled specially.
467 */
Christoph Hellwig193aec12012-03-27 10:34:49 -0400468 if (XFS_B_FSB_OFFSET(mp, isize) != 0) {
469 error = xfs_zero_last_block(ip, offset, isize);
470 if (error)
471 return error;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000472 }
473
474 /*
Christoph Hellwig193aec12012-03-27 10:34:49 -0400475 * Calculate the range between the new size and the old where blocks
476 * needing to be zeroed may exist.
477 *
478 * To get the block where the last byte in the file currently resides,
479 * we need to subtract one from the size and truncate back to a block
480 * boundary. We subtract 1 in case the size is exactly on a block
481 * boundary.
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000482 */
483 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
484 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
485 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
486 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
487 if (last_fsb == end_zero_fsb) {
488 /*
489 * The size was only incremented on its last block.
490 * We took care of that above, so just return.
491 */
492 return 0;
493 }
494
495 ASSERT(start_zero_fsb <= end_zero_fsb);
496 while (start_zero_fsb <= end_zero_fsb) {
497 nimaps = 1;
498 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
Christoph Hellwig193aec12012-03-27 10:34:49 -0400499
500 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner5c8ed202011-09-18 20:40:45 +0000501 error = xfs_bmapi_read(ip, start_zero_fsb, zero_count_fsb,
502 &imap, &nimaps, 0);
Christoph Hellwig193aec12012-03-27 10:34:49 -0400503 xfs_iunlock(ip, XFS_ILOCK_EXCL);
504 if (error)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000505 return error;
Christoph Hellwig193aec12012-03-27 10:34:49 -0400506
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000507 ASSERT(nimaps > 0);
508
509 if (imap.br_state == XFS_EXT_UNWRITTEN ||
510 imap.br_startblock == HOLESTARTBLOCK) {
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000511 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
512 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
513 continue;
514 }
515
516 /*
517 * There are blocks we need to zero.
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000518 */
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000519 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
520 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
521
522 if ((zero_off + zero_len) > offset)
523 zero_len = offset - zero_off;
524
525 error = xfs_iozero(ip, zero_off, zero_len);
Christoph Hellwig193aec12012-03-27 10:34:49 -0400526 if (error)
527 return error;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000528
529 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
530 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000531 }
532
533 return 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000534}
535
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100536/*
Dave Chinner4d8d1582011-01-11 10:23:42 +1100537 * Common pre-write limit and setup checks.
538 *
Christoph Hellwig5bf1f262011-12-18 20:00:13 +0000539 * Called with the iolocked held either shared and exclusive according to
540 * @iolock, and returns with it held. Might upgrade the iolock to exclusive
541 * if called for a direct write beyond i_size.
Dave Chinner4d8d1582011-01-11 10:23:42 +1100542 */
543STATIC ssize_t
544xfs_file_aio_write_checks(
545 struct file *file,
546 loff_t *pos,
547 size_t *count,
548 int *iolock)
549{
550 struct inode *inode = file->f_mapping->host;
551 struct xfs_inode *ip = XFS_I(inode);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100552 int error = 0;
553
Dave Chinner7271d242011-08-25 07:17:02 +0000554restart:
Dave Chinner4d8d1582011-01-11 10:23:42 +1100555 error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode));
Christoph Hellwig467f7892012-03-27 10:34:47 -0400556 if (error)
Dave Chinner4d8d1582011-01-11 10:23:42 +1100557 return error;
Dave Chinner4d8d1582011-01-11 10:23:42 +1100558
Dave Chinner4d8d1582011-01-11 10:23:42 +1100559 /*
560 * If the offset is beyond the size of the file, we need to zero any
561 * blocks that fall between the existing EOF and the start of this
Christoph Hellwig2813d682011-12-18 20:00:12 +0000562 * write. If zeroing is needed and we are currently holding the
Christoph Hellwig467f7892012-03-27 10:34:47 -0400563 * iolock shared, we need to update it to exclusive which implies
564 * having to redo all checks before.
Dave Chinner4d8d1582011-01-11 10:23:42 +1100565 */
Christoph Hellwig2813d682011-12-18 20:00:12 +0000566 if (*pos > i_size_read(inode)) {
Dave Chinner7271d242011-08-25 07:17:02 +0000567 if (*iolock == XFS_IOLOCK_SHARED) {
Christoph Hellwig467f7892012-03-27 10:34:47 -0400568 xfs_rw_iunlock(ip, *iolock);
Dave Chinner7271d242011-08-25 07:17:02 +0000569 *iolock = XFS_IOLOCK_EXCL;
Christoph Hellwig467f7892012-03-27 10:34:47 -0400570 xfs_rw_ilock(ip, *iolock);
Dave Chinner7271d242011-08-25 07:17:02 +0000571 goto restart;
572 }
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000573 error = -xfs_zero_eof(ip, *pos, i_size_read(inode));
Christoph Hellwig467f7892012-03-27 10:34:47 -0400574 if (error)
575 return error;
Dave Chinner7271d242011-08-25 07:17:02 +0000576 }
Dave Chinner4d8d1582011-01-11 10:23:42 +1100577
578 /*
Christoph Hellwig8a9c9982012-02-29 09:53:52 +0000579 * Updating the timestamps will grab the ilock again from
580 * xfs_fs_dirty_inode, so we have to call it after dropping the
581 * lock above. Eventually we should look into a way to avoid
582 * the pointless lock roundtrip.
583 */
Josef Bacikc3b2da32012-03-26 09:59:21 -0400584 if (likely(!(file->f_mode & FMODE_NOCMTIME))) {
585 error = file_update_time(file);
586 if (error)
587 return error;
588 }
Christoph Hellwig8a9c9982012-02-29 09:53:52 +0000589
590 /*
Dave Chinner4d8d1582011-01-11 10:23:42 +1100591 * If we're writing the file then make sure to clear the setuid and
592 * setgid bits if the process is not being run by root. This keeps
593 * people from modifying setuid and setgid binaries.
594 */
595 return file_remove_suid(file);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100596}
597
598/*
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100599 * xfs_file_dio_aio_write - handle direct IO writes
600 *
601 * Lock the inode appropriately to prepare for and issue a direct IO write.
Dave Chinnereda77982011-01-11 10:22:40 +1100602 * By separating it from the buffered write path we remove all the tricky to
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100603 * follow locking changes and looping.
604 *
Dave Chinnereda77982011-01-11 10:22:40 +1100605 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
606 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
607 * pages are flushed out.
608 *
609 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
610 * allowing them to be done in parallel with reads and other direct IO writes.
611 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
612 * needs to do sub-block zeroing and that requires serialisation against other
613 * direct IOs to the same block. In this case we need to serialise the
614 * submission of the unaligned IOs so that we don't get racing block zeroing in
615 * the dio layer. To avoid the problem with aio, we also need to wait for
616 * outstanding IOs to complete so that unwritten extent conversion is completed
617 * before we try to map the overlapping block. This is currently implemented by
Christoph Hellwig4a06fd22011-08-23 08:28:13 +0000618 * hitting it with a big hammer (i.e. inode_dio_wait()).
Dave Chinnereda77982011-01-11 10:22:40 +1100619 *
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100620 * Returns with locks held indicated by @iolock and errors indicated by
621 * negative return values.
622 */
623STATIC ssize_t
624xfs_file_dio_aio_write(
625 struct kiocb *iocb,
626 const struct iovec *iovp,
627 unsigned long nr_segs,
628 loff_t pos,
Christoph Hellwigd0606462011-12-18 20:00:14 +0000629 size_t ocount)
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100630{
631 struct file *file = iocb->ki_filp;
632 struct address_space *mapping = file->f_mapping;
633 struct inode *inode = mapping->host;
634 struct xfs_inode *ip = XFS_I(inode);
635 struct xfs_mount *mp = ip->i_mount;
636 ssize_t ret = 0;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100637 size_t count = ocount;
Dave Chinnereda77982011-01-11 10:22:40 +1100638 int unaligned_io = 0;
Christoph Hellwigd0606462011-12-18 20:00:14 +0000639 int iolock;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100640 struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
641 mp->m_rtdev_targp : mp->m_ddev_targp;
642
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100643 if ((pos & target->bt_smask) || (count & target->bt_smask))
644 return -XFS_ERROR(EINVAL);
645
Dave Chinnereda77982011-01-11 10:22:40 +1100646 if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
647 unaligned_io = 1;
648
Dave Chinner7271d242011-08-25 07:17:02 +0000649 /*
650 * We don't need to take an exclusive lock unless there page cache needs
651 * to be invalidated or unaligned IO is being executed. We don't need to
652 * consider the EOF extension case here because
653 * xfs_file_aio_write_checks() will relock the inode as necessary for
654 * EOF zeroing cases and fill out the new inode size as appropriate.
655 */
656 if (unaligned_io || mapping->nrpages)
Christoph Hellwigd0606462011-12-18 20:00:14 +0000657 iolock = XFS_IOLOCK_EXCL;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100658 else
Christoph Hellwigd0606462011-12-18 20:00:14 +0000659 iolock = XFS_IOLOCK_SHARED;
660 xfs_rw_ilock(ip, iolock);
Christoph Hellwigc58cb162011-08-27 14:42:53 +0000661
662 /*
663 * Recheck if there are cached pages that need invalidate after we got
664 * the iolock to protect against other threads adding new pages while
665 * we were waiting for the iolock.
666 */
Christoph Hellwigd0606462011-12-18 20:00:14 +0000667 if (mapping->nrpages && iolock == XFS_IOLOCK_SHARED) {
668 xfs_rw_iunlock(ip, iolock);
669 iolock = XFS_IOLOCK_EXCL;
670 xfs_rw_ilock(ip, iolock);
Christoph Hellwigc58cb162011-08-27 14:42:53 +0000671 }
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100672
Christoph Hellwigd0606462011-12-18 20:00:14 +0000673 ret = xfs_file_aio_write_checks(file, &pos, &count, &iolock);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100674 if (ret)
Christoph Hellwigd0606462011-12-18 20:00:14 +0000675 goto out;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100676
677 if (mapping->nrpages) {
Dave Chinnerfb595812012-11-12 22:53:57 +1100678 ret = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
679 pos, -1);
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100680 if (ret)
Christoph Hellwigd0606462011-12-18 20:00:14 +0000681 goto out;
Dave Chinnerfb595812012-11-12 22:53:57 +1100682 truncate_pagecache_range(VFS_I(ip), pos, -1);
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100683 }
684
Dave Chinnereda77982011-01-11 10:22:40 +1100685 /*
686 * If we are doing unaligned IO, wait for all other IO to drain,
687 * otherwise demote the lock if we had to flush cached pages
688 */
689 if (unaligned_io)
Christoph Hellwig4a06fd22011-08-23 08:28:13 +0000690 inode_dio_wait(inode);
Christoph Hellwigd0606462011-12-18 20:00:14 +0000691 else if (iolock == XFS_IOLOCK_EXCL) {
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100692 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigd0606462011-12-18 20:00:14 +0000693 iolock = XFS_IOLOCK_SHARED;
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100694 }
695
696 trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
697 ret = generic_file_direct_write(iocb, iovp,
698 &nr_segs, pos, &iocb->ki_pos, count, ocount);
699
Christoph Hellwigd0606462011-12-18 20:00:14 +0000700out:
701 xfs_rw_iunlock(ip, iolock);
702
Dave Chinnerf0d26e82011-01-11 10:15:36 +1100703 /* No fallback to buffered IO on errors for XFS. */
704 ASSERT(ret < 0 || ret == count);
705 return ret;
706}
707
Christoph Hellwig00258e32010-02-15 09:44:47 +0000708STATIC ssize_t
Dave Chinner637bbc72011-01-11 10:17:30 +1100709xfs_file_buffered_aio_write(
710 struct kiocb *iocb,
711 const struct iovec *iovp,
712 unsigned long nr_segs,
713 loff_t pos,
Christoph Hellwigd0606462011-12-18 20:00:14 +0000714 size_t ocount)
Dave Chinner637bbc72011-01-11 10:17:30 +1100715{
716 struct file *file = iocb->ki_filp;
717 struct address_space *mapping = file->f_mapping;
718 struct inode *inode = mapping->host;
719 struct xfs_inode *ip = XFS_I(inode);
720 ssize_t ret;
721 int enospc = 0;
Christoph Hellwigd0606462011-12-18 20:00:14 +0000722 int iolock = XFS_IOLOCK_EXCL;
Dave Chinner637bbc72011-01-11 10:17:30 +1100723 size_t count = ocount;
724
Christoph Hellwigd0606462011-12-18 20:00:14 +0000725 xfs_rw_ilock(ip, iolock);
Dave Chinner637bbc72011-01-11 10:17:30 +1100726
Christoph Hellwigd0606462011-12-18 20:00:14 +0000727 ret = xfs_file_aio_write_checks(file, &pos, &count, &iolock);
Dave Chinner4d8d1582011-01-11 10:23:42 +1100728 if (ret)
Christoph Hellwigd0606462011-12-18 20:00:14 +0000729 goto out;
Dave Chinner637bbc72011-01-11 10:17:30 +1100730
731 /* We can write back this queue in page reclaim */
732 current->backing_dev_info = mapping->backing_dev_info;
733
734write_retry:
735 trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
736 ret = generic_file_buffered_write(iocb, iovp, nr_segs,
Dave Chinner9aa05002012-10-08 21:56:04 +1100737 pos, &iocb->ki_pos, count, 0);
738
Dave Chinner637bbc72011-01-11 10:17:30 +1100739 /*
Dave Chinner9aa05002012-10-08 21:56:04 +1100740 * If we just got an ENOSPC, try to write back all dirty inodes to
741 * convert delalloc space to free up some of the excess reserved
742 * metadata space.
Dave Chinner637bbc72011-01-11 10:17:30 +1100743 */
744 if (ret == -ENOSPC && !enospc) {
Dave Chinner637bbc72011-01-11 10:17:30 +1100745 enospc = 1;
Dave Chinner9aa05002012-10-08 21:56:04 +1100746 xfs_flush_inodes(ip->i_mount);
747 goto write_retry;
Dave Chinner637bbc72011-01-11 10:17:30 +1100748 }
Christoph Hellwigd0606462011-12-18 20:00:14 +0000749
Dave Chinner637bbc72011-01-11 10:17:30 +1100750 current->backing_dev_info = NULL;
Christoph Hellwigd0606462011-12-18 20:00:14 +0000751out:
752 xfs_rw_iunlock(ip, iolock);
Dave Chinner637bbc72011-01-11 10:17:30 +1100753 return ret;
754}
755
756STATIC ssize_t
Christoph Hellwig00258e32010-02-15 09:44:47 +0000757xfs_file_aio_write(
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000758 struct kiocb *iocb,
759 const struct iovec *iovp,
Christoph Hellwig00258e32010-02-15 09:44:47 +0000760 unsigned long nr_segs,
761 loff_t pos)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000762{
763 struct file *file = iocb->ki_filp;
764 struct address_space *mapping = file->f_mapping;
765 struct inode *inode = mapping->host;
Christoph Hellwig00258e32010-02-15 09:44:47 +0000766 struct xfs_inode *ip = XFS_I(inode);
Dave Chinner637bbc72011-01-11 10:17:30 +1100767 ssize_t ret;
Dave Chinner637bbc72011-01-11 10:17:30 +1100768 size_t ocount = 0;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000769
770 XFS_STATS_INC(xs_write_calls);
771
Christoph Hellwig00258e32010-02-15 09:44:47 +0000772 BUG_ON(iocb->ki_pos != pos);
773
Dave Chinnera363f0c2011-01-11 10:13:53 +1100774 ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
775 if (ret)
776 return ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000777
Dave Chinner637bbc72011-01-11 10:17:30 +1100778 if (ocount == 0)
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000779 return 0;
780
Jan Karad9457dc2012-06-12 16:20:39 +0200781 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
782 ret = -EIO;
783 goto out;
784 }
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000785
Dave Chinner637bbc72011-01-11 10:17:30 +1100786 if (unlikely(file->f_flags & O_DIRECT))
Christoph Hellwigd0606462011-12-18 20:00:14 +0000787 ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos, ocount);
Dave Chinner637bbc72011-01-11 10:17:30 +1100788 else
789 ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
Christoph Hellwigd0606462011-12-18 20:00:14 +0000790 ocount);
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000791
Christoph Hellwigd0606462011-12-18 20:00:14 +0000792 if (ret > 0) {
793 ssize_t err;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000794
Christoph Hellwigd0606462011-12-18 20:00:14 +0000795 XFS_STATS_ADD(xs_write_bytes, ret);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000796
Christoph Hellwigd0606462011-12-18 20:00:14 +0000797 /* Handle various SYNC-type writes */
798 err = generic_write_sync(file, pos, ret);
799 if (err < 0)
800 ret = err;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000801 }
802
Jan Karad9457dc2012-06-12 16:20:39 +0200803out:
Dave Chinnera363f0c2011-01-11 10:13:53 +1100804 return ret;
Christoph Hellwigdda35b82010-02-15 09:44:46 +0000805}
806
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100807STATIC long
808xfs_file_fallocate(
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700809 struct file *file,
810 int mode,
811 loff_t offset,
812 loff_t len)
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100813{
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700814 struct inode *inode = file_inode(file);
815 struct xfs_inode *ip = XFS_I(inode);
816 struct xfs_trans *tp;
817 long error;
818 loff_t new_size = 0;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100819
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700820 if (!S_ISREG(inode->i_mode))
821 return -EINVAL;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100822 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
823 return -EOPNOTSUPP;
824
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100825 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700826 if (mode & FALLOC_FL_PUNCH_HOLE) {
827 error = xfs_free_file_space(ip, offset, len);
828 if (error)
829 goto out_unlock;
830 } else {
831 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
832 offset + len > i_size_read(inode)) {
833 new_size = offset + len;
834 error = -inode_newsize_ok(inode, new_size);
835 if (error)
836 goto out_unlock;
837 }
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100838
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700839 error = xfs_alloc_file_space(ip, offset, len,
840 XFS_BMAPI_PREALLOC);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100841 if (error)
842 goto out_unlock;
843 }
844
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700845 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_WRITEID);
846 error = xfs_trans_reserve(tp, &M_RES(ip->i_mount)->tr_writeid, 0, 0);
847 if (error) {
848 xfs_trans_cancel(tp, 0);
849 goto out_unlock;
850 }
Dave Chinner82878892011-03-26 09:13:08 +1100851
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700852 xfs_ilock(ip, XFS_ILOCK_EXCL);
853 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
854 ip->i_d.di_mode &= ~S_ISUID;
855 if (ip->i_d.di_mode & S_IXGRP)
856 ip->i_d.di_mode &= ~S_ISGID;
857
858 if (!(mode & FALLOC_FL_PUNCH_HOLE))
859 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
860
861 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
862 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
863
864 if (file->f_flags & O_DSYNC)
865 xfs_trans_set_sync(tp);
866 error = xfs_trans_commit(tp, 0);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100867 if (error)
868 goto out_unlock;
869
870 /* Change file size if needed */
871 if (new_size) {
872 struct iattr iattr;
873
874 iattr.ia_valid = ATTR_SIZE;
875 iattr.ia_size = new_size;
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700876 error = xfs_setattr_size(ip, &iattr);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100877 }
878
879out_unlock:
880 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700881 return -error;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100882}
883
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100886xfs_file_open(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 struct inode *inode,
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100888 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100890 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return -EFBIG;
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100892 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
893 return -EIO;
894 return 0;
895}
896
897STATIC int
898xfs_dir_open(
899 struct inode *inode,
900 struct file *file)
901{
902 struct xfs_inode *ip = XFS_I(inode);
903 int mode;
904 int error;
905
906 error = xfs_file_open(inode, file);
907 if (error)
908 return error;
909
910 /*
911 * If there are any blocks, read-ahead block 0 as we're almost
912 * certain to have the next operation be a read there.
913 */
914 mode = xfs_ilock_map_shared(ip);
915 if (ip->i_d.di_nextents > 0)
Dave Chinner33363fe2013-04-03 16:11:22 +1100916 xfs_dir3_data_readahead(NULL, ip, 0, -1);
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100917 xfs_iunlock(ip, mode);
918 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100922xfs_file_release(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 struct inode *inode,
924 struct file *filp)
925{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000926 return -xfs_release(XFS_I(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100930xfs_file_readdir(
Al Virob8227552013-05-22 17:07:56 -0400931 struct file *file,
932 struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Al Virob8227552013-05-22 17:07:56 -0400934 struct inode *inode = file_inode(file);
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000935 xfs_inode_t *ip = XFS_I(inode);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000936 int error;
937 size_t bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000939 /*
940 * The Linux API doesn't pass down the total size of the buffer
941 * we read into down to the filesystem. With the filldir concept
942 * it's not needed for correct information, but the XFS dir2 leaf
943 * code wants an estimate of the buffer size to calculate it's
944 * readahead window and size the buffers used for mapping to
945 * physical blocks.
946 *
947 * Try to give it an estimate that's good enough, maybe at some
948 * point we can change the ->readdir prototype to include the
Eric Sandeena9cc7992010-02-03 17:50:13 +0000949 * buffer size. For now we use the current glibc buffer size.
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000950 */
Eric Sandeena9cc7992010-02-03 17:50:13 +0000951 bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Al Virob8227552013-05-22 17:07:56 -0400953 error = xfs_readdir(ip, ctx, bufsize);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000954 if (error)
955 return -error;
956 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100960xfs_file_mmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 struct file *filp,
962 struct vm_area_struct *vma)
963{
Nathan Scott3562fd42006-03-14 14:00:35 +1100964 vma->vm_ops = &xfs_file_vm_ops;
Dean Roehrich6fac0cb2005-06-21 14:07:45 +1000965
Nathan Scottfbc14622006-06-09 14:52:13 +1000966 file_accessed(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return 0;
968}
969
David Chinner4f57dbc2007-07-19 16:28:17 +1000970/*
971 * mmap()d file has taken write protection fault and is being made
972 * writable. We can set the page state up correctly for a writable
973 * page, which means we can do correct delalloc accounting (ENOSPC
974 * checking!) and unwritten extent mapping.
975 */
976STATIC int
977xfs_vm_page_mkwrite(
978 struct vm_area_struct *vma,
Nick Pigginc2ec1752009-03-31 15:23:21 -0700979 struct vm_fault *vmf)
David Chinner4f57dbc2007-07-19 16:28:17 +1000980{
Nick Pigginc2ec1752009-03-31 15:23:21 -0700981 return block_page_mkwrite(vma, vmf, xfs_get_blocks);
David Chinner4f57dbc2007-07-19 16:28:17 +1000982}
983
Jeff Liud126d432012-08-21 17:11:57 +0800984/*
985 * This type is designed to indicate the type of offset we would like
986 * to search from page cache for either xfs_seek_data() or xfs_seek_hole().
987 */
988enum {
989 HOLE_OFF = 0,
990 DATA_OFF,
991};
992
993/*
994 * Lookup the desired type of offset from the given page.
995 *
996 * On success, return true and the offset argument will point to the
997 * start of the region that was found. Otherwise this function will
998 * return false and keep the offset argument unchanged.
999 */
1000STATIC bool
1001xfs_lookup_buffer_offset(
1002 struct page *page,
1003 loff_t *offset,
1004 unsigned int type)
1005{
1006 loff_t lastoff = page_offset(page);
1007 bool found = false;
1008 struct buffer_head *bh, *head;
1009
1010 bh = head = page_buffers(page);
1011 do {
1012 /*
1013 * Unwritten extents that have data in the page
1014 * cache covering them can be identified by the
1015 * BH_Unwritten state flag. Pages with multiple
1016 * buffers might have a mix of holes, data and
1017 * unwritten extents - any buffer with valid
1018 * data in it should have BH_Uptodate flag set
1019 * on it.
1020 */
1021 if (buffer_unwritten(bh) ||
1022 buffer_uptodate(bh)) {
1023 if (type == DATA_OFF)
1024 found = true;
1025 } else {
1026 if (type == HOLE_OFF)
1027 found = true;
1028 }
1029
1030 if (found) {
1031 *offset = lastoff;
1032 break;
1033 }
1034 lastoff += bh->b_size;
1035 } while ((bh = bh->b_this_page) != head);
1036
1037 return found;
1038}
1039
1040/*
1041 * This routine is called to find out and return a data or hole offset
1042 * from the page cache for unwritten extents according to the desired
1043 * type for xfs_seek_data() or xfs_seek_hole().
1044 *
1045 * The argument offset is used to tell where we start to search from the
1046 * page cache. Map is used to figure out the end points of the range to
1047 * lookup pages.
1048 *
1049 * Return true if the desired type of offset was found, and the argument
1050 * offset is filled with that address. Otherwise, return false and keep
1051 * offset unchanged.
1052 */
1053STATIC bool
1054xfs_find_get_desired_pgoff(
1055 struct inode *inode,
1056 struct xfs_bmbt_irec *map,
1057 unsigned int type,
1058 loff_t *offset)
1059{
1060 struct xfs_inode *ip = XFS_I(inode);
1061 struct xfs_mount *mp = ip->i_mount;
1062 struct pagevec pvec;
1063 pgoff_t index;
1064 pgoff_t end;
1065 loff_t endoff;
1066 loff_t startoff = *offset;
1067 loff_t lastoff = startoff;
1068 bool found = false;
1069
1070 pagevec_init(&pvec, 0);
1071
1072 index = startoff >> PAGE_CACHE_SHIFT;
1073 endoff = XFS_FSB_TO_B(mp, map->br_startoff + map->br_blockcount);
1074 end = endoff >> PAGE_CACHE_SHIFT;
1075 do {
1076 int want;
1077 unsigned nr_pages;
1078 unsigned int i;
1079
1080 want = min_t(pgoff_t, end - index, PAGEVEC_SIZE);
1081 nr_pages = pagevec_lookup(&pvec, inode->i_mapping, index,
1082 want);
1083 /*
1084 * No page mapped into given range. If we are searching holes
1085 * and if this is the first time we got into the loop, it means
1086 * that the given offset is landed in a hole, return it.
1087 *
1088 * If we have already stepped through some block buffers to find
1089 * holes but they all contains data. In this case, the last
1090 * offset is already updated and pointed to the end of the last
1091 * mapped page, if it does not reach the endpoint to search,
1092 * that means there should be a hole between them.
1093 */
1094 if (nr_pages == 0) {
1095 /* Data search found nothing */
1096 if (type == DATA_OFF)
1097 break;
1098
1099 ASSERT(type == HOLE_OFF);
1100 if (lastoff == startoff || lastoff < endoff) {
1101 found = true;
1102 *offset = lastoff;
1103 }
1104 break;
1105 }
1106
1107 /*
1108 * At lease we found one page. If this is the first time we
1109 * step into the loop, and if the first page index offset is
1110 * greater than the given search offset, a hole was found.
1111 */
1112 if (type == HOLE_OFF && lastoff == startoff &&
1113 lastoff < page_offset(pvec.pages[0])) {
1114 found = true;
1115 break;
1116 }
1117
1118 for (i = 0; i < nr_pages; i++) {
1119 struct page *page = pvec.pages[i];
1120 loff_t b_offset;
1121
1122 /*
1123 * At this point, the page may be truncated or
1124 * invalidated (changing page->mapping to NULL),
1125 * or even swizzled back from swapper_space to tmpfs
1126 * file mapping. However, page->index will not change
1127 * because we have a reference on the page.
1128 *
1129 * Searching done if the page index is out of range.
1130 * If the current offset is not reaches the end of
1131 * the specified search range, there should be a hole
1132 * between them.
1133 */
1134 if (page->index > end) {
1135 if (type == HOLE_OFF && lastoff < endoff) {
1136 *offset = lastoff;
1137 found = true;
1138 }
1139 goto out;
1140 }
1141
1142 lock_page(page);
1143 /*
1144 * Page truncated or invalidated(page->mapping == NULL).
1145 * We can freely skip it and proceed to check the next
1146 * page.
1147 */
1148 if (unlikely(page->mapping != inode->i_mapping)) {
1149 unlock_page(page);
1150 continue;
1151 }
1152
1153 if (!page_has_buffers(page)) {
1154 unlock_page(page);
1155 continue;
1156 }
1157
1158 found = xfs_lookup_buffer_offset(page, &b_offset, type);
1159 if (found) {
1160 /*
1161 * The found offset may be less than the start
1162 * point to search if this is the first time to
1163 * come here.
1164 */
1165 *offset = max_t(loff_t, startoff, b_offset);
1166 unlock_page(page);
1167 goto out;
1168 }
1169
1170 /*
1171 * We either searching data but nothing was found, or
1172 * searching hole but found a data buffer. In either
1173 * case, probably the next page contains the desired
1174 * things, update the last offset to it so.
1175 */
1176 lastoff = page_offset(page) + PAGE_SIZE;
1177 unlock_page(page);
1178 }
1179
1180 /*
1181 * The number of returned pages less than our desired, search
1182 * done. In this case, nothing was found for searching data,
1183 * but we found a hole behind the last offset.
1184 */
1185 if (nr_pages < want) {
1186 if (type == HOLE_OFF) {
1187 *offset = lastoff;
1188 found = true;
1189 }
1190 break;
1191 }
1192
1193 index = pvec.pages[i - 1]->index + 1;
1194 pagevec_release(&pvec);
1195 } while (index <= end);
1196
1197out:
1198 pagevec_release(&pvec);
1199 return found;
1200}
1201
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001202STATIC loff_t
1203xfs_seek_data(
1204 struct file *file,
Jeff Liu834ab122012-08-21 17:11:45 +08001205 loff_t start)
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001206{
1207 struct inode *inode = file->f_mapping->host;
1208 struct xfs_inode *ip = XFS_I(inode);
1209 struct xfs_mount *mp = ip->i_mount;
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001210 loff_t uninitialized_var(offset);
1211 xfs_fsize_t isize;
1212 xfs_fileoff_t fsbno;
1213 xfs_filblks_t end;
1214 uint lock;
1215 int error;
1216
1217 lock = xfs_ilock_map_shared(ip);
1218
1219 isize = i_size_read(inode);
1220 if (start >= isize) {
1221 error = ENXIO;
1222 goto out_unlock;
1223 }
1224
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001225 /*
1226 * Try to read extents from the first block indicated
1227 * by fsbno to the end block of the file.
1228 */
Jeff Liu52f1acc2012-08-21 17:12:07 +08001229 fsbno = XFS_B_TO_FSBT(mp, start);
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001230 end = XFS_B_TO_FSB(mp, isize);
Jeff Liu52f1acc2012-08-21 17:12:07 +08001231 for (;;) {
1232 struct xfs_bmbt_irec map[2];
1233 int nmap = 2;
1234 unsigned int i;
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001235
Jeff Liu52f1acc2012-08-21 17:12:07 +08001236 error = xfs_bmapi_read(ip, fsbno, end - fsbno, map, &nmap,
1237 XFS_BMAPI_ENTIRE);
1238 if (error)
1239 goto out_unlock;
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001240
Jeff Liu52f1acc2012-08-21 17:12:07 +08001241 /* No extents at given offset, must be beyond EOF */
1242 if (nmap == 0) {
1243 error = ENXIO;
1244 goto out_unlock;
1245 }
1246
1247 for (i = 0; i < nmap; i++) {
1248 offset = max_t(loff_t, start,
1249 XFS_FSB_TO_B(mp, map[i].br_startoff));
1250
1251 /* Landed in a data extent */
1252 if (map[i].br_startblock == DELAYSTARTBLOCK ||
1253 (map[i].br_state == XFS_EXT_NORM &&
1254 !isnullstartblock(map[i].br_startblock)))
1255 goto out;
1256
1257 /*
1258 * Landed in an unwritten extent, try to search data
1259 * from page cache.
1260 */
1261 if (map[i].br_state == XFS_EXT_UNWRITTEN) {
1262 if (xfs_find_get_desired_pgoff(inode, &map[i],
1263 DATA_OFF, &offset))
1264 goto out;
1265 }
1266 }
1267
1268 /*
1269 * map[0] is hole or its an unwritten extent but
1270 * without data in page cache. Probably means that
1271 * we are reading after EOF if nothing in map[1].
1272 */
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001273 if (nmap == 1) {
1274 error = ENXIO;
1275 goto out_unlock;
1276 }
1277
Jeff Liu52f1acc2012-08-21 17:12:07 +08001278 ASSERT(i > 1);
1279
1280 /*
1281 * Nothing was found, proceed to the next round of search
1282 * if reading offset not beyond or hit EOF.
1283 */
1284 fsbno = map[i - 1].br_startoff + map[i - 1].br_blockcount;
1285 start = XFS_FSB_TO_B(mp, fsbno);
1286 if (start >= isize) {
1287 error = ENXIO;
1288 goto out_unlock;
1289 }
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001290 }
1291
Jeff Liu52f1acc2012-08-21 17:12:07 +08001292out:
Jie Liu46a1c2c2013-06-25 12:02:13 +08001293 offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001294
1295out_unlock:
1296 xfs_iunlock_map_shared(ip, lock);
1297
1298 if (error)
1299 return -error;
1300 return offset;
1301}
1302
1303STATIC loff_t
1304xfs_seek_hole(
1305 struct file *file,
Jeff Liu834ab122012-08-21 17:11:45 +08001306 loff_t start)
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001307{
1308 struct inode *inode = file->f_mapping->host;
1309 struct xfs_inode *ip = XFS_I(inode);
1310 struct xfs_mount *mp = ip->i_mount;
1311 loff_t uninitialized_var(offset);
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001312 xfs_fsize_t isize;
1313 xfs_fileoff_t fsbno;
Jeff Liub686d1f2012-08-21 17:12:18 +08001314 xfs_filblks_t end;
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001315 uint lock;
1316 int error;
1317
1318 if (XFS_FORCED_SHUTDOWN(mp))
1319 return -XFS_ERROR(EIO);
1320
1321 lock = xfs_ilock_map_shared(ip);
1322
1323 isize = i_size_read(inode);
1324 if (start >= isize) {
1325 error = ENXIO;
1326 goto out_unlock;
1327 }
1328
1329 fsbno = XFS_B_TO_FSBT(mp, start);
Jeff Liub686d1f2012-08-21 17:12:18 +08001330 end = XFS_B_TO_FSB(mp, isize);
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001331
Jeff Liub686d1f2012-08-21 17:12:18 +08001332 for (;;) {
1333 struct xfs_bmbt_irec map[2];
1334 int nmap = 2;
1335 unsigned int i;
1336
1337 error = xfs_bmapi_read(ip, fsbno, end - fsbno, map, &nmap,
1338 XFS_BMAPI_ENTIRE);
1339 if (error)
1340 goto out_unlock;
1341
1342 /* No extents at given offset, must be beyond EOF */
1343 if (nmap == 0) {
1344 error = ENXIO;
1345 goto out_unlock;
1346 }
1347
1348 for (i = 0; i < nmap; i++) {
1349 offset = max_t(loff_t, start,
1350 XFS_FSB_TO_B(mp, map[i].br_startoff));
1351
1352 /* Landed in a hole */
1353 if (map[i].br_startblock == HOLESTARTBLOCK)
1354 goto out;
1355
1356 /*
1357 * Landed in an unwritten extent, try to search hole
1358 * from page cache.
1359 */
1360 if (map[i].br_state == XFS_EXT_UNWRITTEN) {
1361 if (xfs_find_get_desired_pgoff(inode, &map[i],
1362 HOLE_OFF, &offset))
1363 goto out;
1364 }
1365 }
1366
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001367 /*
Jeff Liub686d1f2012-08-21 17:12:18 +08001368 * map[0] contains data or its unwritten but contains
1369 * data in page cache, probably means that we are
1370 * reading after EOF. We should fix offset to point
1371 * to the end of the file(i.e., there is an implicit
1372 * hole at the end of any file).
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001373 */
Jeff Liub686d1f2012-08-21 17:12:18 +08001374 if (nmap == 1) {
1375 offset = isize;
1376 break;
1377 }
1378
1379 ASSERT(i > 1);
1380
1381 /*
1382 * Both mappings contains data, proceed to the next round of
1383 * search if the current reading offset not beyond or hit EOF.
1384 */
1385 fsbno = map[i - 1].br_startoff + map[i - 1].br_blockcount;
1386 start = XFS_FSB_TO_B(mp, fsbno);
1387 if (start >= isize) {
1388 offset = isize;
1389 break;
1390 }
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001391 }
1392
Jeff Liub686d1f2012-08-21 17:12:18 +08001393out:
1394 /*
1395 * At this point, we must have found a hole. However, the returned
1396 * offset may be bigger than the file size as it may be aligned to
1397 * page boundary for unwritten extents, we need to deal with this
1398 * situation in particular.
1399 */
1400 offset = min_t(loff_t, offset, isize);
Jie Liu46a1c2c2013-06-25 12:02:13 +08001401 offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001402
1403out_unlock:
1404 xfs_iunlock_map_shared(ip, lock);
1405
1406 if (error)
1407 return -error;
1408 return offset;
1409}
1410
1411STATIC loff_t
1412xfs_file_llseek(
1413 struct file *file,
1414 loff_t offset,
1415 int origin)
1416{
1417 switch (origin) {
1418 case SEEK_END:
1419 case SEEK_CUR:
1420 case SEEK_SET:
1421 return generic_file_llseek(file, offset, origin);
1422 case SEEK_DATA:
Jeff Liu834ab122012-08-21 17:11:45 +08001423 return xfs_seek_data(file, offset);
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001424 case SEEK_HOLE:
Jeff Liu834ab122012-08-21 17:11:45 +08001425 return xfs_seek_hole(file, offset);
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001426 default:
1427 return -EINVAL;
1428 }
1429}
1430
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001431const struct file_operations xfs_file_operations = {
Jeff Liu3fe3e6b2012-05-10 21:29:17 +08001432 .llseek = xfs_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 .read = do_sync_read,
Dean Roehrichbb3f7242005-09-02 15:43:05 +10001434 .write = do_sync_write,
Nathan Scott3562fd42006-03-14 14:00:35 +11001435 .aio_read = xfs_file_aio_read,
1436 .aio_write = xfs_file_aio_write,
Nathan Scott1b895842006-03-31 13:08:59 +10001437 .splice_read = xfs_file_splice_read,
1438 .splice_write = xfs_file_splice_write,
Nathan Scott3562fd42006-03-14 14:00:35 +11001439 .unlocked_ioctl = xfs_file_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +11001441 .compat_ioctl = xfs_file_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442#endif
Nathan Scott3562fd42006-03-14 14:00:35 +11001443 .mmap = xfs_file_mmap,
1444 .open = xfs_file_open,
1445 .release = xfs_file_release,
1446 .fsync = xfs_file_fsync,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001447 .fallocate = xfs_file_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448};
1449
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001450const struct file_operations xfs_dir_file_operations = {
Christoph Hellwigf999a5b2008-11-28 14:23:32 +11001451 .open = xfs_dir_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 .read = generic_read_dir,
Al Virob8227552013-05-22 17:07:56 -04001453 .iterate = xfs_file_readdir,
Al Viro59af1582008-08-24 07:24:41 -04001454 .llseek = generic_file_llseek,
Nathan Scott3562fd42006-03-14 14:00:35 +11001455 .unlocked_ioctl = xfs_file_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -07001456#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +11001457 .compat_ioctl = xfs_file_compat_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -07001458#endif
Christoph Hellwig1da2f2d2011-10-02 14:25:16 +00001459 .fsync = xfs_dir_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460};
1461
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04001462static const struct vm_operations_struct xfs_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07001463 .fault = filemap_fault,
David Chinner4f57dbc2007-07-19 16:28:17 +10001464 .page_mkwrite = xfs_vm_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07001465 .remap_pages = generic_file_remap_pages,
Dean Roehrich6fac0cb2005-06-21 14:07:45 +10001466};