ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / fs / xfs / linux-2.6 / xfs_file.c
blobb679198dcc01c7e279fe426b0f2c1f153e03c14d
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
7 * published by the Free Software Foundation.
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.
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
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_bit.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_sb.h"
24 #include "xfs_ag.h"
25 #include "xfs_trans.h"
26 #include "xfs_mount.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_alloc.h"
29 #include "xfs_dinode.h"
30 #include "xfs_inode.h"
31 #include "xfs_inode_item.h"
32 #include "xfs_bmap.h"
33 #include "xfs_error.h"
34 #include "xfs_vnodeops.h"
35 #include "xfs_da_btree.h"
36 #include "xfs_ioctl.h"
37 #include "xfs_trace.h"
39 #include <linux/dcache.h>
40 #include <linux/falloc.h>
42 static const struct vm_operations_struct xfs_file_vm_ops;
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.
48 static inline void
49 xfs_rw_ilock(
50 struct xfs_inode *ip,
51 int type)
53 if (type & XFS_IOLOCK_EXCL)
54 mutex_lock(&VFS_I(ip)->i_mutex);
55 xfs_ilock(ip, type);
58 static inline void
59 xfs_rw_iunlock(
60 struct xfs_inode *ip,
61 int type)
63 xfs_iunlock(ip, type);
64 if (type & XFS_IOLOCK_EXCL)
65 mutex_unlock(&VFS_I(ip)->i_mutex);
68 static inline void
69 xfs_rw_ilock_demote(
70 struct xfs_inode *ip,
71 int type)
73 xfs_ilock_demote(ip, type);
74 if (type & XFS_IOLOCK_EXCL)
75 mutex_unlock(&VFS_I(ip)->i_mutex);
79 * xfs_iozero
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.
88 STATIC int
89 xfs_iozero(
90 struct xfs_inode *ip, /* inode */
91 loff_t pos, /* offset in file */
92 size_t count) /* size of data to zero */
94 struct page *page;
95 struct address_space *mapping;
96 int status;
98 mapping = VFS_I(ip)->i_mapping;
99 do {
100 unsigned offset, bytes;
101 void *fsdata;
103 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
104 bytes = PAGE_CACHE_SIZE - offset;
105 if (bytes > count)
106 bytes = count;
108 status = pagecache_write_begin(NULL, mapping, pos, bytes,
109 AOP_FLAG_UNINTERRUPTIBLE,
110 &page, &fsdata);
111 if (status)
112 break;
114 zero_user(page, offset, bytes);
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);
124 return (-status);
127 STATIC int
128 xfs_file_fsync(
129 struct file *file,
130 int datasync)
132 struct inode *inode = file->f_mapping->host;
133 struct xfs_inode *ip = XFS_I(inode);
134 struct xfs_mount *mp = ip->i_mount;
135 struct xfs_trans *tp;
136 int error = 0;
137 int log_flushed = 0;
139 trace_xfs_file_fsync(ip);
141 if (XFS_FORCED_SHUTDOWN(mp))
142 return -XFS_ERROR(EIO);
144 xfs_iflags_clear(ip, XFS_ITRUNCATED);
146 xfs_ioend_wait(ip);
148 if (mp->m_flags & XFS_MOUNT_BARRIER) {
150 * If we have an RT and/or log subvolume we need to make sure
151 * to flush the write cache the device used for file data
152 * first. This is to ensure newly written file data make
153 * it to disk before logging the new inode size in case of
154 * an extending write.
156 if (XFS_IS_REALTIME_INODE(ip))
157 xfs_blkdev_issue_flush(mp->m_rtdev_targp);
158 else if (mp->m_logdev_targp != mp->m_ddev_targp)
159 xfs_blkdev_issue_flush(mp->m_ddev_targp);
163 * We always need to make sure that the required inode state is safe on
164 * disk. The inode might be clean but we still might need to force the
165 * log because of committed transactions that haven't hit the disk yet.
166 * Likewise, there could be unflushed non-transactional changes to the
167 * inode core that have to go to disk and this requires us to issue
168 * a synchronous transaction to capture these changes correctly.
170 * This code relies on the assumption that if the i_update_core field
171 * of the inode is clear and the inode is unpinned then it is clean
172 * and no action is required.
174 xfs_ilock(ip, XFS_ILOCK_SHARED);
177 * First check if the VFS inode is marked dirty. All the dirtying
178 * of non-transactional updates no goes through mark_inode_dirty*,
179 * which allows us to distinguish beteeen pure timestamp updates
180 * and i_size updates which need to be caught for fdatasync.
181 * After that also theck for the dirty state in the XFS inode, which
182 * might gets cleared when the inode gets written out via the AIL
183 * or xfs_iflush_cluster.
185 if (((inode->i_state & I_DIRTY_DATASYNC) ||
186 ((inode->i_state & I_DIRTY_SYNC) && !datasync)) &&
187 ip->i_update_core) {
189 * Kick off a transaction to log the inode core to get the
190 * updates. The sync transaction will also force the log.
192 xfs_iunlock(ip, XFS_ILOCK_SHARED);
193 tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
194 error = xfs_trans_reserve(tp, 0,
195 XFS_FSYNC_TS_LOG_RES(mp), 0, 0, 0);
196 if (error) {
197 xfs_trans_cancel(tp, 0);
198 return -error;
200 xfs_ilock(ip, XFS_ILOCK_EXCL);
203 * Note - it's possible that we might have pushed ourselves out
204 * of the way during trans_reserve which would flush the inode.
205 * But there's no guarantee that the inode buffer has actually
206 * gone out yet (it's delwri). Plus the buffer could be pinned
207 * anyway if it's part of an inode in another recent
208 * transaction. So we play it safe and fire off the
209 * transaction anyway.
211 xfs_trans_ijoin(tp, ip);
212 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
213 xfs_trans_set_sync(tp);
214 error = _xfs_trans_commit(tp, 0, &log_flushed);
216 xfs_iunlock(ip, XFS_ILOCK_EXCL);
217 } else {
219 * Timestamps/size haven't changed since last inode flush or
220 * inode transaction commit. That means either nothing got
221 * written or a transaction committed which caught the updates.
222 * If the latter happened and the transaction hasn't hit the
223 * disk yet, the inode will be still be pinned. If it is,
224 * force the log.
226 if (xfs_ipincount(ip)) {
227 error = _xfs_log_force_lsn(mp,
228 ip->i_itemp->ili_last_lsn,
229 XFS_LOG_SYNC, &log_flushed);
231 xfs_iunlock(ip, XFS_ILOCK_SHARED);
235 * If we only have a single device, and the log force about was
236 * a no-op we might have to flush the data device cache here.
237 * This can only happen for fdatasync/O_DSYNC if we were overwriting
238 * an already allocated file and thus do not have any metadata to
239 * commit.
241 if ((mp->m_flags & XFS_MOUNT_BARRIER) &&
242 mp->m_logdev_targp == mp->m_ddev_targp &&
243 !XFS_IS_REALTIME_INODE(ip) &&
244 !log_flushed)
245 xfs_blkdev_issue_flush(mp->m_ddev_targp);
247 return -error;
250 STATIC ssize_t
251 xfs_file_aio_read(
252 struct kiocb *iocb,
253 const struct iovec *iovp,
254 unsigned long nr_segs,
255 loff_t pos)
257 struct file *file = iocb->ki_filp;
258 struct inode *inode = file->f_mapping->host;
259 struct xfs_inode *ip = XFS_I(inode);
260 struct xfs_mount *mp = ip->i_mount;
261 size_t size = 0;
262 ssize_t ret = 0;
263 int ioflags = 0;
264 xfs_fsize_t n;
265 unsigned long seg;
267 XFS_STATS_INC(xs_read_calls);
269 BUG_ON(iocb->ki_pos != pos);
271 if (unlikely(file->f_flags & O_DIRECT))
272 ioflags |= IO_ISDIRECT;
273 if (file->f_mode & FMODE_NOCMTIME)
274 ioflags |= IO_INVIS;
276 /* START copy & waste from filemap.c */
277 for (seg = 0; seg < nr_segs; seg++) {
278 const struct iovec *iv = &iovp[seg];
281 * If any segment has a negative length, or the cumulative
282 * length ever wraps negative then return -EINVAL.
284 size += iv->iov_len;
285 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
286 return XFS_ERROR(-EINVAL);
288 /* END copy & waste from filemap.c */
290 if (unlikely(ioflags & IO_ISDIRECT)) {
291 xfs_buftarg_t *target =
292 XFS_IS_REALTIME_INODE(ip) ?
293 mp->m_rtdev_targp : mp->m_ddev_targp;
294 if ((iocb->ki_pos & target->bt_smask) ||
295 (size & target->bt_smask)) {
296 if (iocb->ki_pos == ip->i_size)
297 return 0;
298 return -XFS_ERROR(EINVAL);
302 n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
303 if (n <= 0 || size == 0)
304 return 0;
306 if (n < size)
307 size = n;
309 if (XFS_FORCED_SHUTDOWN(mp))
310 return -EIO;
313 * Locking is a bit tricky here. If we take an exclusive lock
314 * for direct IO, we effectively serialise all new concurrent
315 * read IO to this file and block it behind IO that is currently in
316 * progress because IO in progress holds the IO lock shared. We only
317 * need to hold the lock exclusive to blow away the page cache, so
318 * only take lock exclusively if the page cache needs invalidation.
319 * This allows the normal direct IO case of no page cache pages to
320 * proceeed concurrently without serialisation.
322 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
323 if ((ioflags & IO_ISDIRECT) && inode->i_mapping->nrpages) {
324 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
325 xfs_rw_ilock(ip, XFS_IOLOCK_EXCL);
327 if (inode->i_mapping->nrpages) {
328 ret = -xfs_flushinval_pages(ip,
329 (iocb->ki_pos & PAGE_CACHE_MASK),
330 -1, FI_REMAPF_LOCKED);
331 if (ret) {
332 xfs_rw_iunlock(ip, XFS_IOLOCK_EXCL);
333 return ret;
336 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
339 trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags);
341 ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos);
342 if (ret > 0)
343 XFS_STATS_ADD(xs_read_bytes, ret);
345 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
346 return ret;
349 STATIC ssize_t
350 xfs_file_splice_read(
351 struct file *infilp,
352 loff_t *ppos,
353 struct pipe_inode_info *pipe,
354 size_t count,
355 unsigned int flags)
357 struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
358 int ioflags = 0;
359 ssize_t ret;
361 XFS_STATS_INC(xs_read_calls);
363 if (infilp->f_mode & FMODE_NOCMTIME)
364 ioflags |= IO_INVIS;
366 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
367 return -EIO;
369 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
371 trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
373 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
374 if (ret > 0)
375 XFS_STATS_ADD(xs_read_bytes, ret);
377 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
378 return ret;
381 STATIC void
382 xfs_aio_write_isize_update(
383 struct inode *inode,
384 loff_t *ppos,
385 ssize_t bytes_written)
387 struct xfs_inode *ip = XFS_I(inode);
388 xfs_fsize_t isize = i_size_read(inode);
390 if (bytes_written > 0)
391 XFS_STATS_ADD(xs_write_bytes, bytes_written);
393 if (unlikely(bytes_written < 0 && bytes_written != -EFAULT &&
394 *ppos > isize))
395 *ppos = isize;
397 if (*ppos > ip->i_size) {
398 xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
399 if (*ppos > ip->i_size)
400 ip->i_size = *ppos;
401 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
406 * If this was a direct or synchronous I/O that failed (such as ENOSPC) then
407 * part of the I/O may have been written to disk before the error occurred. In
408 * this case the on-disk file size may have been adjusted beyond the in-memory
409 * file size and now needs to be truncated back.
411 STATIC void
412 xfs_aio_write_newsize_update(
413 struct xfs_inode *ip)
415 if (ip->i_new_size) {
416 xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
417 ip->i_new_size = 0;
418 if (ip->i_d.di_size > ip->i_size)
419 ip->i_d.di_size = ip->i_size;
420 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
425 * xfs_file_splice_write() does not use xfs_rw_ilock() because
426 * generic_file_splice_write() takes the i_mutex itself. This, in theory,
427 * couuld cause lock inversions between the aio_write path and the splice path
428 * if someone is doing concurrent splice(2) based writes and write(2) based
429 * writes to the same inode. The only real way to fix this is to re-implement
430 * the generic code here with correct locking orders.
432 STATIC ssize_t
433 xfs_file_splice_write(
434 struct pipe_inode_info *pipe,
435 struct file *outfilp,
436 loff_t *ppos,
437 size_t count,
438 unsigned int flags)
440 struct inode *inode = outfilp->f_mapping->host;
441 struct xfs_inode *ip = XFS_I(inode);
442 xfs_fsize_t new_size;
443 int ioflags = 0;
444 ssize_t ret;
446 XFS_STATS_INC(xs_write_calls);
448 if (outfilp->f_mode & FMODE_NOCMTIME)
449 ioflags |= IO_INVIS;
451 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
452 return -EIO;
454 xfs_ilock(ip, XFS_IOLOCK_EXCL);
456 new_size = *ppos + count;
458 xfs_ilock(ip, XFS_ILOCK_EXCL);
459 if (new_size > ip->i_size)
460 ip->i_new_size = new_size;
461 xfs_iunlock(ip, XFS_ILOCK_EXCL);
463 trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
465 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
467 xfs_aio_write_isize_update(inode, ppos, ret);
468 xfs_aio_write_newsize_update(ip);
469 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
470 return ret;
474 * This routine is called to handle zeroing any space in the last
475 * block of the file that is beyond the EOF. We do this since the
476 * size is being increased without writing anything to that block
477 * and we don't want anyone to read the garbage on the disk.
479 STATIC int /* error (positive) */
480 xfs_zero_last_block(
481 xfs_inode_t *ip,
482 xfs_fsize_t offset,
483 xfs_fsize_t isize)
485 xfs_fileoff_t last_fsb;
486 xfs_mount_t *mp = ip->i_mount;
487 int nimaps;
488 int zero_offset;
489 int zero_len;
490 int error = 0;
491 xfs_bmbt_irec_t imap;
493 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
495 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
496 if (zero_offset == 0) {
498 * There are no extra bytes in the last block on disk to
499 * zero, so return.
501 return 0;
504 last_fsb = XFS_B_TO_FSBT(mp, isize);
505 nimaps = 1;
506 error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
507 &nimaps, NULL);
508 if (error) {
509 return error;
511 ASSERT(nimaps > 0);
513 * If the block underlying isize is just a hole, then there
514 * is nothing to zero.
516 if (imap.br_startblock == HOLESTARTBLOCK) {
517 return 0;
520 * Zero the part of the last block beyond the EOF, and write it
521 * out sync. We need to drop the ilock while we do this so we
522 * don't deadlock when the buffer cache calls back to us.
524 xfs_iunlock(ip, XFS_ILOCK_EXCL);
526 zero_len = mp->m_sb.sb_blocksize - zero_offset;
527 if (isize + zero_len > offset)
528 zero_len = offset - isize;
529 error = xfs_iozero(ip, isize, zero_len);
531 xfs_ilock(ip, XFS_ILOCK_EXCL);
532 ASSERT(error >= 0);
533 return error;
537 * Zero any on disk space between the current EOF and the new,
538 * larger EOF. This handles the normal case of zeroing the remainder
539 * of the last block in the file and the unusual case of zeroing blocks
540 * out beyond the size of the file. This second case only happens
541 * with fixed size extents and when the system crashes before the inode
542 * size was updated but after blocks were allocated. If fill is set,
543 * then any holes in the range are filled and zeroed. If not, the holes
544 * are left alone as holes.
547 int /* error (positive) */
548 xfs_zero_eof(
549 xfs_inode_t *ip,
550 xfs_off_t offset, /* starting I/O offset */
551 xfs_fsize_t isize) /* current inode size */
553 xfs_mount_t *mp = ip->i_mount;
554 xfs_fileoff_t start_zero_fsb;
555 xfs_fileoff_t end_zero_fsb;
556 xfs_fileoff_t zero_count_fsb;
557 xfs_fileoff_t last_fsb;
558 xfs_fileoff_t zero_off;
559 xfs_fsize_t zero_len;
560 int nimaps;
561 int error = 0;
562 xfs_bmbt_irec_t imap;
564 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
565 ASSERT(offset > isize);
568 * First handle zeroing the block on which isize resides.
569 * We only zero a part of that block so it is handled specially.
571 error = xfs_zero_last_block(ip, offset, isize);
572 if (error) {
573 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
574 return error;
578 * Calculate the range between the new size and the old
579 * where blocks needing to be zeroed may exist. To get the
580 * block where the last byte in the file currently resides,
581 * we need to subtract one from the size and truncate back
582 * to a block boundary. We subtract 1 in case the size is
583 * exactly on a block boundary.
585 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
586 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
587 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
588 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
589 if (last_fsb == end_zero_fsb) {
591 * The size was only incremented on its last block.
592 * We took care of that above, so just return.
594 return 0;
597 ASSERT(start_zero_fsb <= end_zero_fsb);
598 while (start_zero_fsb <= end_zero_fsb) {
599 nimaps = 1;
600 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
601 error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
602 0, NULL, 0, &imap, &nimaps, NULL);
603 if (error) {
604 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
605 return error;
607 ASSERT(nimaps > 0);
609 if (imap.br_state == XFS_EXT_UNWRITTEN ||
610 imap.br_startblock == HOLESTARTBLOCK) {
612 * This loop handles initializing pages that were
613 * partially initialized by the code below this
614 * loop. It basically zeroes the part of the page
615 * that sits on a hole and sets the page as P_HOLE
616 * and calls remapf if it is a mapped file.
618 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
619 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
620 continue;
624 * There are blocks we need to zero.
625 * Drop the inode lock while we're doing the I/O.
626 * We'll still have the iolock to protect us.
628 xfs_iunlock(ip, XFS_ILOCK_EXCL);
630 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
631 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
633 if ((zero_off + zero_len) > offset)
634 zero_len = offset - zero_off;
636 error = xfs_iozero(ip, zero_off, zero_len);
637 if (error) {
638 goto out_lock;
641 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
642 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
644 xfs_ilock(ip, XFS_ILOCK_EXCL);
647 return 0;
649 out_lock:
650 xfs_ilock(ip, XFS_ILOCK_EXCL);
651 ASSERT(error >= 0);
652 return error;
656 * Common pre-write limit and setup checks.
658 * Returns with iolock held according to @iolock.
660 STATIC ssize_t
661 xfs_file_aio_write_checks(
662 struct file *file,
663 loff_t *pos,
664 size_t *count,
665 int *iolock)
667 struct inode *inode = file->f_mapping->host;
668 struct xfs_inode *ip = XFS_I(inode);
669 xfs_fsize_t new_size;
670 int error = 0;
672 xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
673 error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode));
674 if (error) {
675 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL | *iolock);
676 *iolock = 0;
677 return error;
680 new_size = *pos + *count;
681 if (new_size > ip->i_size)
682 ip->i_new_size = new_size;
684 if (likely(!(file->f_mode & FMODE_NOCMTIME)))
685 file_update_time(file);
688 * If the offset is beyond the size of the file, we need to zero any
689 * blocks that fall between the existing EOF and the start of this
690 * write.
692 if (*pos > ip->i_size)
693 error = -xfs_zero_eof(ip, *pos, ip->i_size);
695 xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
696 if (error)
697 return error;
700 * If we're writing the file then make sure to clear the setuid and
701 * setgid bits if the process is not being run by root. This keeps
702 * people from modifying setuid and setgid binaries.
704 return file_remove_suid(file);
709 * xfs_file_dio_aio_write - handle direct IO writes
711 * Lock the inode appropriately to prepare for and issue a direct IO write.
712 * By separating it from the buffered write path we remove all the tricky to
713 * follow locking changes and looping.
715 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
716 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
717 * pages are flushed out.
719 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
720 * allowing them to be done in parallel with reads and other direct IO writes.
721 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
722 * needs to do sub-block zeroing and that requires serialisation against other
723 * direct IOs to the same block. In this case we need to serialise the
724 * submission of the unaligned IOs so that we don't get racing block zeroing in
725 * the dio layer. To avoid the problem with aio, we also need to wait for
726 * outstanding IOs to complete so that unwritten extent conversion is completed
727 * before we try to map the overlapping block. This is currently implemented by
728 * hitting it with a big hammer (i.e. xfs_ioend_wait()).
730 * Returns with locks held indicated by @iolock and errors indicated by
731 * negative return values.
733 STATIC ssize_t
734 xfs_file_dio_aio_write(
735 struct kiocb *iocb,
736 const struct iovec *iovp,
737 unsigned long nr_segs,
738 loff_t pos,
739 size_t ocount,
740 int *iolock)
742 struct file *file = iocb->ki_filp;
743 struct address_space *mapping = file->f_mapping;
744 struct inode *inode = mapping->host;
745 struct xfs_inode *ip = XFS_I(inode);
746 struct xfs_mount *mp = ip->i_mount;
747 ssize_t ret = 0;
748 size_t count = ocount;
749 int unaligned_io = 0;
750 struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
751 mp->m_rtdev_targp : mp->m_ddev_targp;
753 *iolock = 0;
754 if ((pos & target->bt_smask) || (count & target->bt_smask))
755 return -XFS_ERROR(EINVAL);
757 if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
758 unaligned_io = 1;
760 if (unaligned_io || mapping->nrpages || pos > ip->i_size)
761 *iolock = XFS_IOLOCK_EXCL;
762 else
763 *iolock = XFS_IOLOCK_SHARED;
764 xfs_rw_ilock(ip, *iolock);
766 ret = xfs_file_aio_write_checks(file, &pos, &count, iolock);
767 if (ret)
768 return ret;
771 * Recheck if there are cached pages that need invalidate after we got
772 * the iolock to protect against other threads adding new pages while
773 * we were waiting for the iolock.
775 if (mapping->nrpages && *iolock == XFS_IOLOCK_SHARED) {
776 xfs_rw_iunlock(ip, *iolock);
777 *iolock = XFS_IOLOCK_EXCL;
778 xfs_rw_ilock(ip, *iolock);
781 if (mapping->nrpages) {
782 ret = -xfs_flushinval_pages(ip, (pos & PAGE_CACHE_MASK), -1,
783 FI_REMAPF_LOCKED);
784 if (ret)
785 return ret;
789 * If we are doing unaligned IO, wait for all other IO to drain,
790 * otherwise demote the lock if we had to flush cached pages
792 if (unaligned_io)
793 xfs_ioend_wait(ip);
794 else if (*iolock == XFS_IOLOCK_EXCL) {
795 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
796 *iolock = XFS_IOLOCK_SHARED;
799 trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
800 ret = generic_file_direct_write(iocb, iovp,
801 &nr_segs, pos, &iocb->ki_pos, count, ocount);
803 /* No fallback to buffered IO on errors for XFS. */
804 ASSERT(ret < 0 || ret == count);
805 return ret;
808 STATIC ssize_t
809 xfs_file_buffered_aio_write(
810 struct kiocb *iocb,
811 const struct iovec *iovp,
812 unsigned long nr_segs,
813 loff_t pos,
814 size_t ocount,
815 int *iolock)
817 struct file *file = iocb->ki_filp;
818 struct address_space *mapping = file->f_mapping;
819 struct inode *inode = mapping->host;
820 struct xfs_inode *ip = XFS_I(inode);
821 ssize_t ret;
822 int enospc = 0;
823 size_t count = ocount;
825 *iolock = XFS_IOLOCK_EXCL;
826 xfs_rw_ilock(ip, *iolock);
828 ret = xfs_file_aio_write_checks(file, &pos, &count, iolock);
829 if (ret)
830 return ret;
832 /* We can write back this queue in page reclaim */
833 current->backing_dev_info = mapping->backing_dev_info;
835 write_retry:
836 trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
837 ret = generic_file_buffered_write(iocb, iovp, nr_segs,
838 pos, &iocb->ki_pos, count, ret);
840 * if we just got an ENOSPC, flush the inode now we aren't holding any
841 * page locks and retry *once*
843 if (ret == -ENOSPC && !enospc) {
844 ret = -xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
845 if (ret)
846 return ret;
847 enospc = 1;
848 goto write_retry;
850 current->backing_dev_info = NULL;
851 return ret;
854 STATIC ssize_t
855 xfs_file_aio_write(
856 struct kiocb *iocb,
857 const struct iovec *iovp,
858 unsigned long nr_segs,
859 loff_t pos)
861 struct file *file = iocb->ki_filp;
862 struct address_space *mapping = file->f_mapping;
863 struct inode *inode = mapping->host;
864 struct xfs_inode *ip = XFS_I(inode);
865 ssize_t ret;
866 int iolock;
867 size_t ocount = 0;
869 XFS_STATS_INC(xs_write_calls);
871 BUG_ON(iocb->ki_pos != pos);
873 ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
874 if (ret)
875 return ret;
877 if (ocount == 0)
878 return 0;
880 xfs_wait_for_freeze(ip->i_mount, SB_FREEZE_WRITE);
882 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
883 return -EIO;
885 if (unlikely(file->f_flags & O_DIRECT))
886 ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos,
887 ocount, &iolock);
888 else
889 ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
890 ocount, &iolock);
892 xfs_aio_write_isize_update(inode, &iocb->ki_pos, ret);
894 if (ret <= 0)
895 goto out_unlock;
897 /* Handle various SYNC-type writes */
898 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
899 loff_t end = pos + ret - 1;
900 int error, error2;
902 xfs_rw_iunlock(ip, iolock);
903 error = filemap_write_and_wait_range(mapping, pos, end);
904 xfs_rw_ilock(ip, iolock);
906 error2 = -xfs_file_fsync(file,
907 (file->f_flags & __O_SYNC) ? 0 : 1);
908 if (error)
909 ret = error;
910 else if (error2)
911 ret = error2;
914 out_unlock:
915 xfs_aio_write_newsize_update(ip);
916 xfs_rw_iunlock(ip, iolock);
917 return ret;
920 STATIC long
921 xfs_file_fallocate(
922 struct file *file,
923 int mode,
924 loff_t offset,
925 loff_t len)
927 struct inode *inode = file->f_path.dentry->d_inode;
928 long error;
929 loff_t new_size = 0;
930 xfs_flock64_t bf;
931 xfs_inode_t *ip = XFS_I(inode);
932 int cmd = XFS_IOC_RESVSP;
933 int attr_flags = XFS_ATTR_NOLOCK;
935 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
936 return -EOPNOTSUPP;
938 bf.l_whence = 0;
939 bf.l_start = offset;
940 bf.l_len = len;
942 xfs_ilock(ip, XFS_IOLOCK_EXCL);
944 if (mode & FALLOC_FL_PUNCH_HOLE)
945 cmd = XFS_IOC_UNRESVSP;
947 /* check the new inode size is valid before allocating */
948 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
949 offset + len > i_size_read(inode)) {
950 new_size = offset + len;
951 error = inode_newsize_ok(inode, new_size);
952 if (error)
953 goto out_unlock;
956 if (file->f_flags & O_DSYNC)
957 attr_flags |= XFS_ATTR_SYNC;
959 error = -xfs_change_file_space(ip, cmd, &bf, 0, attr_flags);
960 if (error)
961 goto out_unlock;
963 /* Change file size if needed */
964 if (new_size) {
965 struct iattr iattr;
967 iattr.ia_valid = ATTR_SIZE;
968 iattr.ia_size = new_size;
969 error = -xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK);
972 out_unlock:
973 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
974 return error;
978 STATIC int
979 xfs_file_open(
980 struct inode *inode,
981 struct file *file)
983 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
984 return -EFBIG;
985 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
986 return -EIO;
987 return 0;
990 STATIC int
991 xfs_dir_open(
992 struct inode *inode,
993 struct file *file)
995 struct xfs_inode *ip = XFS_I(inode);
996 int mode;
997 int error;
999 error = xfs_file_open(inode, file);
1000 if (error)
1001 return error;
1004 * If there are any blocks, read-ahead block 0 as we're almost
1005 * certain to have the next operation be a read there.
1007 mode = xfs_ilock_map_shared(ip);
1008 if (ip->i_d.di_nextents > 0)
1009 xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
1010 xfs_iunlock(ip, mode);
1011 return 0;
1014 STATIC int
1015 xfs_file_release(
1016 struct inode *inode,
1017 struct file *filp)
1019 return -xfs_release(XFS_I(inode));
1022 STATIC int
1023 xfs_file_readdir(
1024 struct file *filp,
1025 void *dirent,
1026 filldir_t filldir)
1028 struct inode *inode = filp->f_path.dentry->d_inode;
1029 xfs_inode_t *ip = XFS_I(inode);
1030 int error;
1031 size_t bufsize;
1034 * The Linux API doesn't pass down the total size of the buffer
1035 * we read into down to the filesystem. With the filldir concept
1036 * it's not needed for correct information, but the XFS dir2 leaf
1037 * code wants an estimate of the buffer size to calculate it's
1038 * readahead window and size the buffers used for mapping to
1039 * physical blocks.
1041 * Try to give it an estimate that's good enough, maybe at some
1042 * point we can change the ->readdir prototype to include the
1043 * buffer size. For now we use the current glibc buffer size.
1045 bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
1047 error = xfs_readdir(ip, dirent, bufsize,
1048 (xfs_off_t *)&filp->f_pos, filldir);
1049 if (error)
1050 return -error;
1051 return 0;
1054 STATIC int
1055 xfs_file_mmap(
1056 struct file *filp,
1057 struct vm_area_struct *vma)
1059 vma->vm_ops = &xfs_file_vm_ops;
1060 vma->vm_flags |= VM_CAN_NONLINEAR;
1062 file_accessed(filp);
1063 return 0;
1067 * mmap()d file has taken write protection fault and is being made
1068 * writable. We can set the page state up correctly for a writable
1069 * page, which means we can do correct delalloc accounting (ENOSPC
1070 * checking!) and unwritten extent mapping.
1072 STATIC int
1073 xfs_vm_page_mkwrite(
1074 struct vm_area_struct *vma,
1075 struct vm_fault *vmf)
1077 return block_page_mkwrite(vma, vmf, xfs_get_blocks);
1080 const struct file_operations xfs_file_operations = {
1081 .llseek = generic_file_llseek,
1082 .read = do_sync_read,
1083 .write = do_sync_write,
1084 .aio_read = xfs_file_aio_read,
1085 .aio_write = xfs_file_aio_write,
1086 .splice_read = xfs_file_splice_read,
1087 .splice_write = xfs_file_splice_write,
1088 .unlocked_ioctl = xfs_file_ioctl,
1089 #ifdef CONFIG_COMPAT
1090 .compat_ioctl = xfs_file_compat_ioctl,
1091 #endif
1092 .mmap = xfs_file_mmap,
1093 .open = xfs_file_open,
1094 .release = xfs_file_release,
1095 .fsync = xfs_file_fsync,
1096 .fallocate = xfs_file_fallocate,
1099 const struct file_operations xfs_dir_file_operations = {
1100 .open = xfs_dir_open,
1101 .read = generic_read_dir,
1102 .readdir = xfs_file_readdir,
1103 .llseek = generic_file_llseek,
1104 .unlocked_ioctl = xfs_file_ioctl,
1105 #ifdef CONFIG_COMPAT
1106 .compat_ioctl = xfs_file_compat_ioctl,
1107 #endif
1108 .fsync = xfs_file_fsync,
1111 static const struct vm_operations_struct xfs_file_vm_ops = {
1112 .fault = filemap_fault,
1113 .page_mkwrite = xfs_vm_page_mkwrite,