xfs: don't serialise direct IO reads on page cache checks
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / xfs / xfs_file.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * 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.
1da177e4 13 *
7b718769
NS
14 * 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
1da177e4 17 */
1da177e4 18#include "xfs.h"
dda35b8f 19#include "xfs_fs.h"
a844f451 20#include "xfs_bit.h"
1da177e4 21#include "xfs_log.h"
a844f451 22#include "xfs_inum.h"
1da177e4 23#include "xfs_sb.h"
a844f451 24#include "xfs_ag.h"
1da177e4 25#include "xfs_trans.h"
1da177e4
LT
26#include "xfs_mount.h"
27#include "xfs_bmap_btree.h"
1da177e4 28#include "xfs_alloc.h"
1da177e4
LT
29#include "xfs_dinode.h"
30#include "xfs_inode.h"
fd3200be 31#include "xfs_inode_item.h"
dda35b8f 32#include "xfs_bmap.h"
1da177e4 33#include "xfs_error.h"
739bfb2a 34#include "xfs_vnodeops.h"
f999a5bf 35#include "xfs_da_btree.h"
ddcd856d 36#include "xfs_ioctl.h"
dda35b8f 37#include "xfs_trace.h"
1da177e4
LT
38
39#include <linux/dcache.h>
2fe17c10 40#include <linux/falloc.h>
1da177e4 41
f0f37e2f 42static const struct vm_operations_struct xfs_file_vm_ops;
1da177e4 43
487f84f3
DC
44/*
45 * 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
dda35b8f
CH
78/*
79 * 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
fd3200be
CH
127STATIC int
128xfs_file_fsync(
129 struct file *file,
02c24a82
JB
130 loff_t start,
131 loff_t end,
fd3200be
CH
132 int datasync)
133{
7ea80859
CH
134 struct inode *inode = file->f_mapping->host;
135 struct xfs_inode *ip = XFS_I(inode);
a27a263b 136 struct xfs_mount *mp = ip->i_mount;
fd3200be
CH
137 struct xfs_trans *tp;
138 int error = 0;
139 int log_flushed = 0;
140
cca28fb8 141 trace_xfs_file_fsync(ip);
fd3200be 142
02c24a82
JB
143 error = filemap_write_and_wait_range(inode->i_mapping, start, end);
144 if (error)
145 return error;
146
a27a263b 147 if (XFS_FORCED_SHUTDOWN(mp))
fd3200be
CH
148 return -XFS_ERROR(EIO);
149
150 xfs_iflags_clear(ip, XFS_ITRUNCATED);
151
d1166ec7 152 xfs_ilock(ip, XFS_IOLOCK_SHARED);
37bc5743 153 xfs_ioend_wait(ip);
d1166ec7 154 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
37bc5743 155
a27a263b
CH
156 if (mp->m_flags & XFS_MOUNT_BARRIER) {
157 /*
158 * If we have an RT and/or log subvolume we need to make sure
159 * to flush the write cache the device used for file data
160 * first. This is to ensure newly written file data make
161 * it to disk before logging the new inode size in case of
162 * an extending write.
163 */
164 if (XFS_IS_REALTIME_INODE(ip))
165 xfs_blkdev_issue_flush(mp->m_rtdev_targp);
166 else if (mp->m_logdev_targp != mp->m_ddev_targp)
167 xfs_blkdev_issue_flush(mp->m_ddev_targp);
168 }
169
fd3200be
CH
170 /*
171 * We always need to make sure that the required inode state is safe on
172 * disk. The inode might be clean but we still might need to force the
173 * log because of committed transactions that haven't hit the disk yet.
174 * Likewise, there could be unflushed non-transactional changes to the
175 * inode core that have to go to disk and this requires us to issue
176 * a synchronous transaction to capture these changes correctly.
177 *
178 * This code relies on the assumption that if the i_update_core field
179 * of the inode is clear and the inode is unpinned then it is clean
180 * and no action is required.
181 */
182 xfs_ilock(ip, XFS_ILOCK_SHARED);
183
66d834ea
CH
184 /*
185 * First check if the VFS inode is marked dirty. All the dirtying
186 * of non-transactional updates no goes through mark_inode_dirty*,
187 * which allows us to distinguish beteeen pure timestamp updates
188 * and i_size updates which need to be caught for fdatasync.
189 * After that also theck for the dirty state in the XFS inode, which
190 * might gets cleared when the inode gets written out via the AIL
191 * or xfs_iflush_cluster.
192 */
7ea80859
CH
193 if (((inode->i_state & I_DIRTY_DATASYNC) ||
194 ((inode->i_state & I_DIRTY_SYNC) && !datasync)) &&
66d834ea 195 ip->i_update_core) {
fd3200be
CH
196 /*
197 * Kick off a transaction to log the inode core to get the
198 * updates. The sync transaction will also force the log.
199 */
200 xfs_iunlock(ip, XFS_ILOCK_SHARED);
a27a263b 201 tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
fd3200be 202 error = xfs_trans_reserve(tp, 0,
a27a263b 203 XFS_FSYNC_TS_LOG_RES(mp), 0, 0, 0);
fd3200be
CH
204 if (error) {
205 xfs_trans_cancel(tp, 0);
206 return -error;
207 }
208 xfs_ilock(ip, XFS_ILOCK_EXCL);
209
210 /*
211 * Note - it's possible that we might have pushed ourselves out
212 * of the way during trans_reserve which would flush the inode.
213 * But there's no guarantee that the inode buffer has actually
214 * gone out yet (it's delwri). Plus the buffer could be pinned
215 * anyway if it's part of an inode in another recent
216 * transaction. So we play it safe and fire off the
217 * transaction anyway.
218 */
898621d5 219 xfs_trans_ijoin(tp, ip);
fd3200be
CH
220 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
221 xfs_trans_set_sync(tp);
222 error = _xfs_trans_commit(tp, 0, &log_flushed);
223
224 xfs_iunlock(ip, XFS_ILOCK_EXCL);
225 } else {
226 /*
227 * Timestamps/size haven't changed since last inode flush or
228 * inode transaction commit. That means either nothing got
229 * written or a transaction committed which caught the updates.
230 * If the latter happened and the transaction hasn't hit the
231 * disk yet, the inode will be still be pinned. If it is,
232 * force the log.
233 */
fd3200be 234 if (xfs_ipincount(ip)) {
a27a263b 235 error = _xfs_log_force_lsn(mp,
024910cb
CH
236 ip->i_itemp->ili_last_lsn,
237 XFS_LOG_SYNC, &log_flushed);
fd3200be 238 }
024910cb 239 xfs_iunlock(ip, XFS_ILOCK_SHARED);
fd3200be
CH
240 }
241
a27a263b
CH
242 /*
243 * If we only have a single device, and the log force about was
244 * a no-op we might have to flush the data device cache here.
245 * This can only happen for fdatasync/O_DSYNC if we were overwriting
246 * an already allocated file and thus do not have any metadata to
247 * commit.
248 */
249 if ((mp->m_flags & XFS_MOUNT_BARRIER) &&
250 mp->m_logdev_targp == mp->m_ddev_targp &&
251 !XFS_IS_REALTIME_INODE(ip) &&
252 !log_flushed)
253 xfs_blkdev_issue_flush(mp->m_ddev_targp);
fd3200be
CH
254
255 return -error;
256}
257
00258e36
CH
258STATIC ssize_t
259xfs_file_aio_read(
dda35b8f
CH
260 struct kiocb *iocb,
261 const struct iovec *iovp,
00258e36
CH
262 unsigned long nr_segs,
263 loff_t pos)
dda35b8f
CH
264{
265 struct file *file = iocb->ki_filp;
266 struct inode *inode = file->f_mapping->host;
00258e36
CH
267 struct xfs_inode *ip = XFS_I(inode);
268 struct xfs_mount *mp = ip->i_mount;
dda35b8f
CH
269 size_t size = 0;
270 ssize_t ret = 0;
00258e36 271 int ioflags = 0;
dda35b8f
CH
272 xfs_fsize_t n;
273 unsigned long seg;
274
dda35b8f
CH
275 XFS_STATS_INC(xs_read_calls);
276
00258e36
CH
277 BUG_ON(iocb->ki_pos != pos);
278
279 if (unlikely(file->f_flags & O_DIRECT))
280 ioflags |= IO_ISDIRECT;
281 if (file->f_mode & FMODE_NOCMTIME)
282 ioflags |= IO_INVIS;
283
dda35b8f 284 /* START copy & waste from filemap.c */
00258e36 285 for (seg = 0; seg < nr_segs; seg++) {
dda35b8f
CH
286 const struct iovec *iv = &iovp[seg];
287
288 /*
289 * If any segment has a negative length, or the cumulative
290 * length ever wraps negative then return -EINVAL.
291 */
292 size += iv->iov_len;
293 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
294 return XFS_ERROR(-EINVAL);
295 }
296 /* END copy & waste from filemap.c */
297
298 if (unlikely(ioflags & IO_ISDIRECT)) {
299 xfs_buftarg_t *target =
300 XFS_IS_REALTIME_INODE(ip) ?
301 mp->m_rtdev_targp : mp->m_ddev_targp;
00258e36 302 if ((iocb->ki_pos & target->bt_smask) ||
dda35b8f 303 (size & target->bt_smask)) {
00258e36
CH
304 if (iocb->ki_pos == ip->i_size)
305 return 0;
dda35b8f
CH
306 return -XFS_ERROR(EINVAL);
307 }
308 }
309
00258e36
CH
310 n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
311 if (n <= 0 || size == 0)
dda35b8f
CH
312 return 0;
313
314 if (n < size)
315 size = n;
316
317 if (XFS_FORCED_SHUTDOWN(mp))
318 return -EIO;
319
0c38a251
DC
320 /*
321 * Locking is a bit tricky here. If we take an exclusive lock
322 * for direct IO, we effectively serialise all new concurrent
323 * read IO to this file and block it behind IO that is currently in
324 * progress because IO in progress holds the IO lock shared. We only
325 * need to hold the lock exclusive to blow away the page cache, so
326 * only take lock exclusively if the page cache needs invalidation.
327 * This allows the normal direct IO case of no page cache pages to
328 * proceeed concurrently without serialisation.
329 */
330 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
331 if ((ioflags & IO_ISDIRECT) && inode->i_mapping->nrpages) {
332 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
487f84f3
DC
333 xfs_rw_ilock(ip, XFS_IOLOCK_EXCL);
334
00258e36
CH
335 if (inode->i_mapping->nrpages) {
336 ret = -xfs_flushinval_pages(ip,
337 (iocb->ki_pos & PAGE_CACHE_MASK),
338 -1, FI_REMAPF_LOCKED);
487f84f3
DC
339 if (ret) {
340 xfs_rw_iunlock(ip, XFS_IOLOCK_EXCL);
341 return ret;
342 }
00258e36 343 }
487f84f3 344 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
0c38a251 345 }
dda35b8f 346
00258e36 347 trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags);
dda35b8f 348
00258e36 349 ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos);
dda35b8f
CH
350 if (ret > 0)
351 XFS_STATS_ADD(xs_read_bytes, ret);
352
487f84f3 353 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
dda35b8f
CH
354 return ret;
355}
356
00258e36
CH
357STATIC ssize_t
358xfs_file_splice_read(
dda35b8f
CH
359 struct file *infilp,
360 loff_t *ppos,
361 struct pipe_inode_info *pipe,
362 size_t count,
00258e36 363 unsigned int flags)
dda35b8f 364{
00258e36 365 struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
00258e36 366 int ioflags = 0;
dda35b8f
CH
367 ssize_t ret;
368
369 XFS_STATS_INC(xs_read_calls);
00258e36
CH
370
371 if (infilp->f_mode & FMODE_NOCMTIME)
372 ioflags |= IO_INVIS;
373
dda35b8f
CH
374 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
375 return -EIO;
376
487f84f3 377 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
dda35b8f 378
dda35b8f
CH
379 trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
380
381 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
382 if (ret > 0)
383 XFS_STATS_ADD(xs_read_bytes, ret);
384
487f84f3 385 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
dda35b8f
CH
386 return ret;
387}
388
edafb6da
DC
389STATIC void
390xfs_aio_write_isize_update(
391 struct inode *inode,
392 loff_t *ppos,
393 ssize_t bytes_written)
394{
395 struct xfs_inode *ip = XFS_I(inode);
396 xfs_fsize_t isize = i_size_read(inode);
397
398 if (bytes_written > 0)
399 XFS_STATS_ADD(xs_write_bytes, bytes_written);
400
401 if (unlikely(bytes_written < 0 && bytes_written != -EFAULT &&
402 *ppos > isize))
403 *ppos = isize;
404
405 if (*ppos > ip->i_size) {
487f84f3 406 xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
edafb6da
DC
407 if (*ppos > ip->i_size)
408 ip->i_size = *ppos;
487f84f3 409 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
edafb6da
DC
410 }
411}
412
4c5cfd1b
DC
413/*
414 * If this was a direct or synchronous I/O that failed (such as ENOSPC) then
25985edc 415 * part of the I/O may have been written to disk before the error occurred. In
4c5cfd1b
DC
416 * this case the on-disk file size may have been adjusted beyond the in-memory
417 * file size and now needs to be truncated back.
418 */
419STATIC void
420xfs_aio_write_newsize_update(
421 struct xfs_inode *ip)
422{
423 if (ip->i_new_size) {
487f84f3 424 xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
4c5cfd1b
DC
425 ip->i_new_size = 0;
426 if (ip->i_d.di_size > ip->i_size)
427 ip->i_d.di_size = ip->i_size;
487f84f3 428 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
4c5cfd1b
DC
429 }
430}
431
487f84f3
DC
432/*
433 * xfs_file_splice_write() does not use xfs_rw_ilock() because
434 * generic_file_splice_write() takes the i_mutex itself. This, in theory,
435 * couuld cause lock inversions between the aio_write path and the splice path
436 * if someone is doing concurrent splice(2) based writes and write(2) based
437 * writes to the same inode. The only real way to fix this is to re-implement
438 * the generic code here with correct locking orders.
439 */
00258e36
CH
440STATIC ssize_t
441xfs_file_splice_write(
dda35b8f
CH
442 struct pipe_inode_info *pipe,
443 struct file *outfilp,
444 loff_t *ppos,
445 size_t count,
00258e36 446 unsigned int flags)
dda35b8f 447{
dda35b8f 448 struct inode *inode = outfilp->f_mapping->host;
00258e36 449 struct xfs_inode *ip = XFS_I(inode);
edafb6da 450 xfs_fsize_t new_size;
00258e36
CH
451 int ioflags = 0;
452 ssize_t ret;
dda35b8f
CH
453
454 XFS_STATS_INC(xs_write_calls);
00258e36
CH
455
456 if (outfilp->f_mode & FMODE_NOCMTIME)
457 ioflags |= IO_INVIS;
458
dda35b8f
CH
459 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
460 return -EIO;
461
462 xfs_ilock(ip, XFS_IOLOCK_EXCL);
463
dda35b8f
CH
464 new_size = *ppos + count;
465
466 xfs_ilock(ip, XFS_ILOCK_EXCL);
467 if (new_size > ip->i_size)
468 ip->i_new_size = new_size;
469 xfs_iunlock(ip, XFS_ILOCK_EXCL);
470
471 trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
472
473 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
dda35b8f 474
edafb6da 475 xfs_aio_write_isize_update(inode, ppos, ret);
4c5cfd1b 476 xfs_aio_write_newsize_update(ip);
dda35b8f
CH
477 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
478 return ret;
479}
480
481/*
482 * This routine is called to handle zeroing any space in the last
483 * block of the file that is beyond the EOF. We do this since the
484 * size is being increased without writing anything to that block
485 * and we don't want anyone to read the garbage on the disk.
486 */
487STATIC int /* error (positive) */
488xfs_zero_last_block(
489 xfs_inode_t *ip,
490 xfs_fsize_t offset,
491 xfs_fsize_t isize)
492{
493 xfs_fileoff_t last_fsb;
494 xfs_mount_t *mp = ip->i_mount;
495 int nimaps;
496 int zero_offset;
497 int zero_len;
498 int error = 0;
499 xfs_bmbt_irec_t imap;
500
501 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
502
503 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
504 if (zero_offset == 0) {
505 /*
506 * There are no extra bytes in the last block on disk to
507 * zero, so return.
508 */
509 return 0;
510 }
511
512 last_fsb = XFS_B_TO_FSBT(mp, isize);
513 nimaps = 1;
514 error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
b4e9181e 515 &nimaps, NULL);
dda35b8f
CH
516 if (error) {
517 return error;
518 }
519 ASSERT(nimaps > 0);
520 /*
521 * If the block underlying isize is just a hole, then there
522 * is nothing to zero.
523 */
524 if (imap.br_startblock == HOLESTARTBLOCK) {
525 return 0;
526 }
527 /*
528 * Zero the part of the last block beyond the EOF, and write it
529 * out sync. We need to drop the ilock while we do this so we
530 * don't deadlock when the buffer cache calls back to us.
531 */
532 xfs_iunlock(ip, XFS_ILOCK_EXCL);
533
534 zero_len = mp->m_sb.sb_blocksize - zero_offset;
535 if (isize + zero_len > offset)
536 zero_len = offset - isize;
537 error = xfs_iozero(ip, isize, zero_len);
538
539 xfs_ilock(ip, XFS_ILOCK_EXCL);
540 ASSERT(error >= 0);
541 return error;
542}
543
544/*
545 * Zero any on disk space between the current EOF and the new,
546 * larger EOF. This handles the normal case of zeroing the remainder
547 * of the last block in the file and the unusual case of zeroing blocks
548 * out beyond the size of the file. This second case only happens
549 * with fixed size extents and when the system crashes before the inode
550 * size was updated but after blocks were allocated. If fill is set,
551 * then any holes in the range are filled and zeroed. If not, the holes
552 * are left alone as holes.
553 */
554
555int /* error (positive) */
556xfs_zero_eof(
557 xfs_inode_t *ip,
558 xfs_off_t offset, /* starting I/O offset */
559 xfs_fsize_t isize) /* current inode size */
560{
561 xfs_mount_t *mp = ip->i_mount;
562 xfs_fileoff_t start_zero_fsb;
563 xfs_fileoff_t end_zero_fsb;
564 xfs_fileoff_t zero_count_fsb;
565 xfs_fileoff_t last_fsb;
566 xfs_fileoff_t zero_off;
567 xfs_fsize_t zero_len;
568 int nimaps;
569 int error = 0;
570 xfs_bmbt_irec_t imap;
571
572 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
573 ASSERT(offset > isize);
574
575 /*
576 * First handle zeroing the block on which isize resides.
577 * We only zero a part of that block so it is handled specially.
578 */
579 error = xfs_zero_last_block(ip, offset, isize);
580 if (error) {
581 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
582 return error;
583 }
584
585 /*
586 * Calculate the range between the new size and the old
587 * where blocks needing to be zeroed may exist. To get the
588 * block where the last byte in the file currently resides,
589 * we need to subtract one from the size and truncate back
590 * to a block boundary. We subtract 1 in case the size is
591 * exactly on a block boundary.
592 */
593 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
594 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
595 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
596 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
597 if (last_fsb == end_zero_fsb) {
598 /*
599 * The size was only incremented on its last block.
600 * We took care of that above, so just return.
601 */
602 return 0;
603 }
604
605 ASSERT(start_zero_fsb <= end_zero_fsb);
606 while (start_zero_fsb <= end_zero_fsb) {
607 nimaps = 1;
608 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
609 error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
b4e9181e 610 0, NULL, 0, &imap, &nimaps, NULL);
dda35b8f
CH
611 if (error) {
612 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
613 return error;
614 }
615 ASSERT(nimaps > 0);
616
617 if (imap.br_state == XFS_EXT_UNWRITTEN ||
618 imap.br_startblock == HOLESTARTBLOCK) {
619 /*
620 * This loop handles initializing pages that were
621 * partially initialized by the code below this
622 * loop. It basically zeroes the part of the page
623 * that sits on a hole and sets the page as P_HOLE
624 * and calls remapf if it is a mapped file.
625 */
626 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
627 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
628 continue;
629 }
630
631 /*
632 * There are blocks we need to zero.
633 * Drop the inode lock while we're doing the I/O.
634 * We'll still have the iolock to protect us.
635 */
636 xfs_iunlock(ip, XFS_ILOCK_EXCL);
637
638 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
639 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
640
641 if ((zero_off + zero_len) > offset)
642 zero_len = offset - zero_off;
643
644 error = xfs_iozero(ip, zero_off, zero_len);
645 if (error) {
646 goto out_lock;
647 }
648
649 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
650 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
651
652 xfs_ilock(ip, XFS_ILOCK_EXCL);
653 }
654
655 return 0;
656
657out_lock:
658 xfs_ilock(ip, XFS_ILOCK_EXCL);
659 ASSERT(error >= 0);
660 return error;
661}
662
4d8d1581
DC
663/*
664 * Common pre-write limit and setup checks.
665 *
666 * Returns with iolock held according to @iolock.
667 */
668STATIC ssize_t
669xfs_file_aio_write_checks(
670 struct file *file,
671 loff_t *pos,
672 size_t *count,
673 int *iolock)
674{
675 struct inode *inode = file->f_mapping->host;
676 struct xfs_inode *ip = XFS_I(inode);
677 xfs_fsize_t new_size;
678 int error = 0;
679
680 error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode));
681 if (error) {
682 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL | *iolock);
683 *iolock = 0;
684 return error;
685 }
686
687 new_size = *pos + *count;
688 if (new_size > ip->i_size)
689 ip->i_new_size = new_size;
690
691 if (likely(!(file->f_mode & FMODE_NOCMTIME)))
692 file_update_time(file);
693
694 /*
695 * If the offset is beyond the size of the file, we need to zero any
696 * blocks that fall between the existing EOF and the start of this
697 * write.
698 */
699 if (*pos > ip->i_size)
700 error = -xfs_zero_eof(ip, *pos, ip->i_size);
701
702 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
703 if (error)
704 return error;
705
706 /*
707 * If we're writing the file then make sure to clear the setuid and
708 * setgid bits if the process is not being run by root. This keeps
709 * people from modifying setuid and setgid binaries.
710 */
711 return file_remove_suid(file);
712
713}
714
f0d26e86
DC
715/*
716 * xfs_file_dio_aio_write - handle direct IO writes
717 *
718 * Lock the inode appropriately to prepare for and issue a direct IO write.
eda77982 719 * By separating it from the buffered write path we remove all the tricky to
f0d26e86
DC
720 * follow locking changes and looping.
721 *
eda77982
DC
722 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
723 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
724 * pages are flushed out.
725 *
726 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
727 * allowing them to be done in parallel with reads and other direct IO writes.
728 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
729 * needs to do sub-block zeroing and that requires serialisation against other
730 * direct IOs to the same block. In this case we need to serialise the
731 * submission of the unaligned IOs so that we don't get racing block zeroing in
732 * the dio layer. To avoid the problem with aio, we also need to wait for
733 * outstanding IOs to complete so that unwritten extent conversion is completed
734 * before we try to map the overlapping block. This is currently implemented by
735 * hitting it with a big hammer (i.e. xfs_ioend_wait()).
736 *
f0d26e86
DC
737 * Returns with locks held indicated by @iolock and errors indicated by
738 * negative return values.
739 */
740STATIC ssize_t
741xfs_file_dio_aio_write(
742 struct kiocb *iocb,
743 const struct iovec *iovp,
744 unsigned long nr_segs,
745 loff_t pos,
746 size_t ocount,
747 int *iolock)
748{
749 struct file *file = iocb->ki_filp;
750 struct address_space *mapping = file->f_mapping;
751 struct inode *inode = mapping->host;
752 struct xfs_inode *ip = XFS_I(inode);
753 struct xfs_mount *mp = ip->i_mount;
754 ssize_t ret = 0;
f0d26e86 755 size_t count = ocount;
eda77982 756 int unaligned_io = 0;
f0d26e86
DC
757 struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
758 mp->m_rtdev_targp : mp->m_ddev_targp;
759
760 *iolock = 0;
761 if ((pos & target->bt_smask) || (count & target->bt_smask))
762 return -XFS_ERROR(EINVAL);
763
eda77982
DC
764 if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
765 unaligned_io = 1;
766
767 if (unaligned_io || mapping->nrpages || pos > ip->i_size)
f0d26e86
DC
768 *iolock = XFS_IOLOCK_EXCL;
769 else
770 *iolock = XFS_IOLOCK_SHARED;
771 xfs_rw_ilock(ip, XFS_ILOCK_EXCL | *iolock);
772
4d8d1581
DC
773 ret = xfs_file_aio_write_checks(file, &pos, &count, iolock);
774 if (ret)
f0d26e86
DC
775 return ret;
776
777 if (mapping->nrpages) {
778 WARN_ON(*iolock != XFS_IOLOCK_EXCL);
779 ret = -xfs_flushinval_pages(ip, (pos & PAGE_CACHE_MASK), -1,
780 FI_REMAPF_LOCKED);
781 if (ret)
782 return ret;
783 }
784
eda77982
DC
785 /*
786 * If we are doing unaligned IO, wait for all other IO to drain,
787 * otherwise demote the lock if we had to flush cached pages
788 */
789 if (unaligned_io)
790 xfs_ioend_wait(ip);
791 else if (*iolock == XFS_IOLOCK_EXCL) {
f0d26e86
DC
792 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
793 *iolock = XFS_IOLOCK_SHARED;
794 }
795
796 trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
797 ret = generic_file_direct_write(iocb, iovp,
798 &nr_segs, pos, &iocb->ki_pos, count, ocount);
799
800 /* No fallback to buffered IO on errors for XFS. */
801 ASSERT(ret < 0 || ret == count);
802 return ret;
803}
804
00258e36 805STATIC ssize_t
637bbc75 806xfs_file_buffered_aio_write(
dda35b8f
CH
807 struct kiocb *iocb,
808 const struct iovec *iovp,
00258e36 809 unsigned long nr_segs,
637bbc75
DC
810 loff_t pos,
811 size_t ocount,
812 int *iolock)
dda35b8f
CH
813{
814 struct file *file = iocb->ki_filp;
815 struct address_space *mapping = file->f_mapping;
816 struct inode *inode = mapping->host;
00258e36 817 struct xfs_inode *ip = XFS_I(inode);
637bbc75
DC
818 ssize_t ret;
819 int enospc = 0;
637bbc75 820 size_t count = ocount;
dda35b8f 821
637bbc75
DC
822 *iolock = XFS_IOLOCK_EXCL;
823 xfs_rw_ilock(ip, XFS_ILOCK_EXCL | *iolock);
dda35b8f 824
4d8d1581
DC
825 ret = xfs_file_aio_write_checks(file, &pos, &count, iolock);
826 if (ret)
637bbc75 827 return ret;
dda35b8f
CH
828
829 /* We can write back this queue in page reclaim */
830 current->backing_dev_info = mapping->backing_dev_info;
831
dda35b8f 832write_retry:
637bbc75
DC
833 trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
834 ret = generic_file_buffered_write(iocb, iovp, nr_segs,
835 pos, &iocb->ki_pos, count, ret);
836 /*
837 * if we just got an ENOSPC, flush the inode now we aren't holding any
838 * page locks and retry *once*
839 */
840 if (ret == -ENOSPC && !enospc) {
841 ret = -xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
842 if (ret)
843 return ret;
844 enospc = 1;
845 goto write_retry;
dda35b8f 846 }
dda35b8f 847 current->backing_dev_info = NULL;
637bbc75
DC
848 return ret;
849}
850
851STATIC ssize_t
852xfs_file_aio_write(
853 struct kiocb *iocb,
854 const struct iovec *iovp,
855 unsigned long nr_segs,
856 loff_t pos)
857{
858 struct file *file = iocb->ki_filp;
859 struct address_space *mapping = file->f_mapping;
860 struct inode *inode = mapping->host;
861 struct xfs_inode *ip = XFS_I(inode);
862 ssize_t ret;
863 int iolock;
864 size_t ocount = 0;
865
866 XFS_STATS_INC(xs_write_calls);
867
868 BUG_ON(iocb->ki_pos != pos);
869
870 ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
871 if (ret)
872 return ret;
873
874 if (ocount == 0)
875 return 0;
876
877 xfs_wait_for_freeze(ip->i_mount, SB_FREEZE_WRITE);
878
879 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
880 return -EIO;
881
882 if (unlikely(file->f_flags & O_DIRECT))
883 ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos,
884 ocount, &iolock);
885 else
886 ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
887 ocount, &iolock);
dda35b8f 888
edafb6da 889 xfs_aio_write_isize_update(inode, &iocb->ki_pos, ret);
dda35b8f 890
dda35b8f 891 if (ret <= 0)
637bbc75 892 goto out_unlock;
dda35b8f 893
dda35b8f
CH
894 /* Handle various SYNC-type writes */
895 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
896 loff_t end = pos + ret - 1;
340a0a01 897 int error;
dda35b8f 898
487f84f3 899 xfs_rw_iunlock(ip, iolock);
340a0a01 900 error = xfs_file_fsync(file, pos, end,
02c24a82 901 (file->f_flags & __O_SYNC) ? 0 : 1);
487f84f3 902 xfs_rw_ilock(ip, iolock);
340a0a01
MT
903 if (error)
904 ret = error;
dda35b8f
CH
905 }
906
637bbc75 907out_unlock:
4c5cfd1b 908 xfs_aio_write_newsize_update(ip);
487f84f3 909 xfs_rw_iunlock(ip, iolock);
a363f0c2 910 return ret;
dda35b8f
CH
911}
912
2fe17c10
CH
913STATIC long
914xfs_file_fallocate(
915 struct file *file,
916 int mode,
917 loff_t offset,
918 loff_t len)
919{
920 struct inode *inode = file->f_path.dentry->d_inode;
921 long error;
922 loff_t new_size = 0;
923 xfs_flock64_t bf;
924 xfs_inode_t *ip = XFS_I(inode);
925 int cmd = XFS_IOC_RESVSP;
82878897 926 int attr_flags = XFS_ATTR_NOLOCK;
2fe17c10
CH
927
928 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
929 return -EOPNOTSUPP;
930
931 bf.l_whence = 0;
932 bf.l_start = offset;
933 bf.l_len = len;
934
935 xfs_ilock(ip, XFS_IOLOCK_EXCL);
936
937 if (mode & FALLOC_FL_PUNCH_HOLE)
938 cmd = XFS_IOC_UNRESVSP;
939
940 /* check the new inode size is valid before allocating */
941 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
942 offset + len > i_size_read(inode)) {
943 new_size = offset + len;
944 error = inode_newsize_ok(inode, new_size);
945 if (error)
946 goto out_unlock;
947 }
948
82878897
DC
949 if (file->f_flags & O_DSYNC)
950 attr_flags |= XFS_ATTR_SYNC;
951
952 error = -xfs_change_file_space(ip, cmd, &bf, 0, attr_flags);
2fe17c10
CH
953 if (error)
954 goto out_unlock;
955
956 /* Change file size if needed */
957 if (new_size) {
958 struct iattr iattr;
959
960 iattr.ia_valid = ATTR_SIZE;
961 iattr.ia_size = new_size;
c4ed4243 962 error = -xfs_setattr_size(ip, &iattr, XFS_ATTR_NOLOCK);
2fe17c10
CH
963 }
964
965out_unlock:
966 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
967 return error;
968}
969
970
1da177e4 971STATIC int
3562fd45 972xfs_file_open(
1da177e4 973 struct inode *inode,
f999a5bf 974 struct file *file)
1da177e4 975{
f999a5bf 976 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1da177e4 977 return -EFBIG;
f999a5bf
CH
978 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
979 return -EIO;
980 return 0;
981}
982
983STATIC int
984xfs_dir_open(
985 struct inode *inode,
986 struct file *file)
987{
988 struct xfs_inode *ip = XFS_I(inode);
989 int mode;
990 int error;
991
992 error = xfs_file_open(inode, file);
993 if (error)
994 return error;
995
996 /*
997 * If there are any blocks, read-ahead block 0 as we're almost
998 * certain to have the next operation be a read there.
999 */
1000 mode = xfs_ilock_map_shared(ip);
1001 if (ip->i_d.di_nextents > 0)
1002 xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
1003 xfs_iunlock(ip, mode);
1004 return 0;
1da177e4
LT
1005}
1006
1da177e4 1007STATIC int
3562fd45 1008xfs_file_release(
1da177e4
LT
1009 struct inode *inode,
1010 struct file *filp)
1011{
739bfb2a 1012 return -xfs_release(XFS_I(inode));
1da177e4
LT
1013}
1014
1da177e4 1015STATIC int
3562fd45 1016xfs_file_readdir(
1da177e4
LT
1017 struct file *filp,
1018 void *dirent,
1019 filldir_t filldir)
1020{
051e7cd4 1021 struct inode *inode = filp->f_path.dentry->d_inode;
739bfb2a 1022 xfs_inode_t *ip = XFS_I(inode);
051e7cd4
CH
1023 int error;
1024 size_t bufsize;
1025
1026 /*
1027 * The Linux API doesn't pass down the total size of the buffer
1028 * we read into down to the filesystem. With the filldir concept
1029 * it's not needed for correct information, but the XFS dir2 leaf
1030 * code wants an estimate of the buffer size to calculate it's
1031 * readahead window and size the buffers used for mapping to
1032 * physical blocks.
1033 *
1034 * Try to give it an estimate that's good enough, maybe at some
1035 * point we can change the ->readdir prototype to include the
a9cc799e 1036 * buffer size. For now we use the current glibc buffer size.
051e7cd4 1037 */
a9cc799e 1038 bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
051e7cd4 1039
739bfb2a 1040 error = xfs_readdir(ip, dirent, bufsize,
051e7cd4
CH
1041 (xfs_off_t *)&filp->f_pos, filldir);
1042 if (error)
1043 return -error;
1044 return 0;
1da177e4
LT
1045}
1046
1da177e4 1047STATIC int
3562fd45 1048xfs_file_mmap(
1da177e4
LT
1049 struct file *filp,
1050 struct vm_area_struct *vma)
1051{
3562fd45 1052 vma->vm_ops = &xfs_file_vm_ops;
d0217ac0 1053 vma->vm_flags |= VM_CAN_NONLINEAR;
6fac0cb4 1054
fbc1462b 1055 file_accessed(filp);
1da177e4
LT
1056 return 0;
1057}
1058
4f57dbc6
DC
1059/*
1060 * mmap()d file has taken write protection fault and is being made
1061 * writable. We can set the page state up correctly for a writable
1062 * page, which means we can do correct delalloc accounting (ENOSPC
1063 * checking!) and unwritten extent mapping.
1064 */
1065STATIC int
1066xfs_vm_page_mkwrite(
1067 struct vm_area_struct *vma,
c2ec175c 1068 struct vm_fault *vmf)
4f57dbc6 1069{
c2ec175c 1070 return block_page_mkwrite(vma, vmf, xfs_get_blocks);
4f57dbc6
DC
1071}
1072
4b6f5d20 1073const struct file_operations xfs_file_operations = {
1da177e4
LT
1074 .llseek = generic_file_llseek,
1075 .read = do_sync_read,
bb3f724e 1076 .write = do_sync_write,
3562fd45
NS
1077 .aio_read = xfs_file_aio_read,
1078 .aio_write = xfs_file_aio_write,
1b895840
NS
1079 .splice_read = xfs_file_splice_read,
1080 .splice_write = xfs_file_splice_write,
3562fd45 1081 .unlocked_ioctl = xfs_file_ioctl,
1da177e4 1082#ifdef CONFIG_COMPAT
3562fd45 1083 .compat_ioctl = xfs_file_compat_ioctl,
1da177e4 1084#endif
3562fd45
NS
1085 .mmap = xfs_file_mmap,
1086 .open = xfs_file_open,
1087 .release = xfs_file_release,
1088 .fsync = xfs_file_fsync,
2fe17c10 1089 .fallocate = xfs_file_fallocate,
1da177e4
LT
1090};
1091
4b6f5d20 1092const struct file_operations xfs_dir_file_operations = {
f999a5bf 1093 .open = xfs_dir_open,
1da177e4 1094 .read = generic_read_dir,
3562fd45 1095 .readdir = xfs_file_readdir,
59af1584 1096 .llseek = generic_file_llseek,
3562fd45 1097 .unlocked_ioctl = xfs_file_ioctl,
d3870398 1098#ifdef CONFIG_COMPAT
3562fd45 1099 .compat_ioctl = xfs_file_compat_ioctl,
d3870398 1100#endif
3562fd45 1101 .fsync = xfs_file_fsync,
1da177e4
LT
1102};
1103
f0f37e2f 1104static const struct vm_operations_struct xfs_file_vm_ops = {
54cb8821 1105 .fault = filemap_fault,
4f57dbc6 1106 .page_mkwrite = xfs_vm_page_mkwrite,
6fac0cb4 1107};