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