xfs: move xfs_get_extsz_hint() and kill xfs_rw.h
[deliverable/linux.git] / fs / xfs / xfs_file.c
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
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.
8 *
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.
13 *
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
17 */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_bit.h"
21 #include "xfs_log.h"
22 #include "xfs_sb.h"
23 #include "xfs_ag.h"
24 #include "xfs_trans.h"
25 #include "xfs_mount.h"
26 #include "xfs_bmap_btree.h"
27 #include "xfs_alloc.h"
28 #include "xfs_dinode.h"
29 #include "xfs_inode.h"
30 #include "xfs_inode_item.h"
31 #include "xfs_bmap.h"
32 #include "xfs_error.h"
33 #include "xfs_vnodeops.h"
34 #include "xfs_da_btree.h"
35 #include "xfs_ioctl.h"
36 #include "xfs_trace.h"
37
38 #include <linux/dcache.h>
39 #include <linux/falloc.h>
40
41 static const struct vm_operations_struct xfs_file_vm_ops;
42
43 /*
44 * Locking primitives for read and write IO paths to ensure we consistently use
45 * and order the inode->i_mutex, ip->i_lock and ip->i_iolock.
46 */
47 static inline void
48 xfs_rw_ilock(
49 struct xfs_inode *ip,
50 int type)
51 {
52 if (type & XFS_IOLOCK_EXCL)
53 mutex_lock(&VFS_I(ip)->i_mutex);
54 xfs_ilock(ip, type);
55 }
56
57 static inline void
58 xfs_rw_iunlock(
59 struct xfs_inode *ip,
60 int type)
61 {
62 xfs_iunlock(ip, type);
63 if (type & XFS_IOLOCK_EXCL)
64 mutex_unlock(&VFS_I(ip)->i_mutex);
65 }
66
67 static inline void
68 xfs_rw_ilock_demote(
69 struct xfs_inode *ip,
70 int type)
71 {
72 xfs_ilock_demote(ip, type);
73 if (type & XFS_IOLOCK_EXCL)
74 mutex_unlock(&VFS_I(ip)->i_mutex);
75 }
76
77 /*
78 * xfs_iozero
79 *
80 * xfs_iozero clears the specified range of buffer supplied,
81 * and marks all the affected blocks as valid and modified. If
82 * an affected block is not allocated, it will be allocated. If
83 * an affected block is not completely overwritten, and is not
84 * valid before the operation, it will be read from disk before
85 * being partially zeroed.
86 */
87 STATIC int
88 xfs_iozero(
89 struct xfs_inode *ip, /* inode */
90 loff_t pos, /* offset in file */
91 size_t count) /* size of data to zero */
92 {
93 struct page *page;
94 struct address_space *mapping;
95 int status;
96
97 mapping = VFS_I(ip)->i_mapping;
98 do {
99 unsigned offset, bytes;
100 void *fsdata;
101
102 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
103 bytes = PAGE_CACHE_SIZE - offset;
104 if (bytes > count)
105 bytes = count;
106
107 status = pagecache_write_begin(NULL, mapping, pos, bytes,
108 AOP_FLAG_UNINTERRUPTIBLE,
109 &page, &fsdata);
110 if (status)
111 break;
112
113 zero_user(page, offset, bytes);
114
115 status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
116 page, fsdata);
117 WARN_ON(status <= 0); /* can't return less than zero! */
118 pos += bytes;
119 count -= bytes;
120 status = 0;
121 } while (count);
122
123 return (-status);
124 }
125
126 /*
127 * Fsync operations on directories are much simpler than on regular files,
128 * as there is no file data to flush, and thus also no need for explicit
129 * cache flush operations, and there are no non-transaction metadata updates
130 * on directories either.
131 */
132 STATIC int
133 xfs_dir_fsync(
134 struct file *file,
135 loff_t start,
136 loff_t end,
137 int datasync)
138 {
139 struct xfs_inode *ip = XFS_I(file->f_mapping->host);
140 struct xfs_mount *mp = ip->i_mount;
141 xfs_lsn_t lsn = 0;
142
143 trace_xfs_dir_fsync(ip);
144
145 xfs_ilock(ip, XFS_ILOCK_SHARED);
146 if (xfs_ipincount(ip))
147 lsn = ip->i_itemp->ili_last_lsn;
148 xfs_iunlock(ip, XFS_ILOCK_SHARED);
149
150 if (!lsn)
151 return 0;
152 return _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
153 }
154
155 STATIC int
156 xfs_file_fsync(
157 struct file *file,
158 loff_t start,
159 loff_t end,
160 int datasync)
161 {
162 struct inode *inode = file->f_mapping->host;
163 struct xfs_inode *ip = XFS_I(inode);
164 struct xfs_mount *mp = ip->i_mount;
165 int error = 0;
166 int log_flushed = 0;
167 xfs_lsn_t lsn = 0;
168
169 trace_xfs_file_fsync(ip);
170
171 error = filemap_write_and_wait_range(inode->i_mapping, start, end);
172 if (error)
173 return error;
174
175 if (XFS_FORCED_SHUTDOWN(mp))
176 return -XFS_ERROR(EIO);
177
178 xfs_iflags_clear(ip, XFS_ITRUNCATED);
179
180 if (mp->m_flags & XFS_MOUNT_BARRIER) {
181 /*
182 * If we have an RT and/or log subvolume we need to make sure
183 * to flush the write cache the device used for file data
184 * first. This is to ensure newly written file data make
185 * it to disk before logging the new inode size in case of
186 * an extending write.
187 */
188 if (XFS_IS_REALTIME_INODE(ip))
189 xfs_blkdev_issue_flush(mp->m_rtdev_targp);
190 else if (mp->m_logdev_targp != mp->m_ddev_targp)
191 xfs_blkdev_issue_flush(mp->m_ddev_targp);
192 }
193
194 /*
195 * All metadata updates are logged, which means that we just have
196 * to flush the log up to the latest LSN that touched the inode.
197 */
198 xfs_ilock(ip, XFS_ILOCK_SHARED);
199 if (xfs_ipincount(ip)) {
200 if (!datasync ||
201 (ip->i_itemp->ili_fields & ~XFS_ILOG_TIMESTAMP))
202 lsn = ip->i_itemp->ili_last_lsn;
203 }
204 xfs_iunlock(ip, XFS_ILOCK_SHARED);
205
206 if (lsn)
207 error = _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, &log_flushed);
208
209 /*
210 * If we only have a single device, and the log force about was
211 * a no-op we might have to flush the data device cache here.
212 * This can only happen for fdatasync/O_DSYNC if we were overwriting
213 * an already allocated file and thus do not have any metadata to
214 * commit.
215 */
216 if ((mp->m_flags & XFS_MOUNT_BARRIER) &&
217 mp->m_logdev_targp == mp->m_ddev_targp &&
218 !XFS_IS_REALTIME_INODE(ip) &&
219 !log_flushed)
220 xfs_blkdev_issue_flush(mp->m_ddev_targp);
221
222 return -error;
223 }
224
225 STATIC ssize_t
226 xfs_file_aio_read(
227 struct kiocb *iocb,
228 const struct iovec *iovp,
229 unsigned long nr_segs,
230 loff_t pos)
231 {
232 struct file *file = iocb->ki_filp;
233 struct inode *inode = file->f_mapping->host;
234 struct xfs_inode *ip = XFS_I(inode);
235 struct xfs_mount *mp = ip->i_mount;
236 size_t size = 0;
237 ssize_t ret = 0;
238 int ioflags = 0;
239 xfs_fsize_t n;
240 unsigned long seg;
241
242 XFS_STATS_INC(xs_read_calls);
243
244 BUG_ON(iocb->ki_pos != pos);
245
246 if (unlikely(file->f_flags & O_DIRECT))
247 ioflags |= IO_ISDIRECT;
248 if (file->f_mode & FMODE_NOCMTIME)
249 ioflags |= IO_INVIS;
250
251 /* START copy & waste from filemap.c */
252 for (seg = 0; seg < nr_segs; seg++) {
253 const struct iovec *iv = &iovp[seg];
254
255 /*
256 * If any segment has a negative length, or the cumulative
257 * length ever wraps negative then return -EINVAL.
258 */
259 size += iv->iov_len;
260 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
261 return XFS_ERROR(-EINVAL);
262 }
263 /* END copy & waste from filemap.c */
264
265 if (unlikely(ioflags & IO_ISDIRECT)) {
266 xfs_buftarg_t *target =
267 XFS_IS_REALTIME_INODE(ip) ?
268 mp->m_rtdev_targp : mp->m_ddev_targp;
269 if ((iocb->ki_pos & target->bt_smask) ||
270 (size & target->bt_smask)) {
271 if (iocb->ki_pos == i_size_read(inode))
272 return 0;
273 return -XFS_ERROR(EINVAL);
274 }
275 }
276
277 n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
278 if (n <= 0 || size == 0)
279 return 0;
280
281 if (n < size)
282 size = n;
283
284 if (XFS_FORCED_SHUTDOWN(mp))
285 return -EIO;
286
287 /*
288 * Locking is a bit tricky here. If we take an exclusive lock
289 * for direct IO, we effectively serialise all new concurrent
290 * read IO to this file and block it behind IO that is currently in
291 * progress because IO in progress holds the IO lock shared. We only
292 * need to hold the lock exclusive to blow away the page cache, so
293 * only take lock exclusively if the page cache needs invalidation.
294 * This allows the normal direct IO case of no page cache pages to
295 * proceeed concurrently without serialisation.
296 */
297 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
298 if ((ioflags & IO_ISDIRECT) && inode->i_mapping->nrpages) {
299 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
300 xfs_rw_ilock(ip, XFS_IOLOCK_EXCL);
301
302 if (inode->i_mapping->nrpages) {
303 ret = -xfs_flushinval_pages(ip,
304 (iocb->ki_pos & PAGE_CACHE_MASK),
305 -1, FI_REMAPF_LOCKED);
306 if (ret) {
307 xfs_rw_iunlock(ip, XFS_IOLOCK_EXCL);
308 return ret;
309 }
310 }
311 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
312 }
313
314 trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags);
315
316 ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos);
317 if (ret > 0)
318 XFS_STATS_ADD(xs_read_bytes, ret);
319
320 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
321 return ret;
322 }
323
324 STATIC ssize_t
325 xfs_file_splice_read(
326 struct file *infilp,
327 loff_t *ppos,
328 struct pipe_inode_info *pipe,
329 size_t count,
330 unsigned int flags)
331 {
332 struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
333 int ioflags = 0;
334 ssize_t ret;
335
336 XFS_STATS_INC(xs_read_calls);
337
338 if (infilp->f_mode & FMODE_NOCMTIME)
339 ioflags |= IO_INVIS;
340
341 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
342 return -EIO;
343
344 xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
345
346 trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
347
348 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
349 if (ret > 0)
350 XFS_STATS_ADD(xs_read_bytes, ret);
351
352 xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
353 return ret;
354 }
355
356 /*
357 * xfs_file_splice_write() does not use xfs_rw_ilock() because
358 * generic_file_splice_write() takes the i_mutex itself. This, in theory,
359 * couuld cause lock inversions between the aio_write path and the splice path
360 * if someone is doing concurrent splice(2) based writes and write(2) based
361 * writes to the same inode. The only real way to fix this is to re-implement
362 * the generic code here with correct locking orders.
363 */
364 STATIC ssize_t
365 xfs_file_splice_write(
366 struct pipe_inode_info *pipe,
367 struct file *outfilp,
368 loff_t *ppos,
369 size_t count,
370 unsigned int flags)
371 {
372 struct inode *inode = outfilp->f_mapping->host;
373 struct xfs_inode *ip = XFS_I(inode);
374 int ioflags = 0;
375 ssize_t ret;
376
377 XFS_STATS_INC(xs_write_calls);
378
379 if (outfilp->f_mode & FMODE_NOCMTIME)
380 ioflags |= IO_INVIS;
381
382 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
383 return -EIO;
384
385 xfs_ilock(ip, XFS_IOLOCK_EXCL);
386
387 trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
388
389 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
390 if (ret > 0)
391 XFS_STATS_ADD(xs_write_bytes, ret);
392
393 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
394 return ret;
395 }
396
397 /*
398 * This routine is called to handle zeroing any space in the last block of the
399 * file that is beyond the EOF. We do this since the size is being increased
400 * without writing anything to that block and we don't want to read the
401 * garbage on the disk.
402 */
403 STATIC int /* error (positive) */
404 xfs_zero_last_block(
405 struct xfs_inode *ip,
406 xfs_fsize_t offset,
407 xfs_fsize_t isize)
408 {
409 struct xfs_mount *mp = ip->i_mount;
410 xfs_fileoff_t last_fsb = XFS_B_TO_FSBT(mp, isize);
411 int zero_offset = XFS_B_FSB_OFFSET(mp, isize);
412 int zero_len;
413 int nimaps = 1;
414 int error = 0;
415 struct xfs_bmbt_irec imap;
416
417 xfs_ilock(ip, XFS_ILOCK_EXCL);
418 error = xfs_bmapi_read(ip, last_fsb, 1, &imap, &nimaps, 0);
419 xfs_iunlock(ip, XFS_ILOCK_EXCL);
420 if (error)
421 return error;
422
423 ASSERT(nimaps > 0);
424
425 /*
426 * If the block underlying isize is just a hole, then there
427 * is nothing to zero.
428 */
429 if (imap.br_startblock == HOLESTARTBLOCK)
430 return 0;
431
432 zero_len = mp->m_sb.sb_blocksize - zero_offset;
433 if (isize + zero_len > offset)
434 zero_len = offset - isize;
435 return xfs_iozero(ip, isize, zero_len);
436 }
437
438 /*
439 * Zero any on disk space between the current EOF and the new, larger EOF.
440 *
441 * This handles the normal case of zeroing the remainder of the last block in
442 * the file and the unusual case of zeroing blocks out beyond the size of the
443 * file. This second case only happens with fixed size extents and when the
444 * system crashes before the inode size was updated but after blocks were
445 * allocated.
446 *
447 * Expects the iolock to be held exclusive, and will take the ilock internally.
448 */
449 int /* error (positive) */
450 xfs_zero_eof(
451 struct xfs_inode *ip,
452 xfs_off_t offset, /* starting I/O offset */
453 xfs_fsize_t isize) /* current inode size */
454 {
455 struct xfs_mount *mp = ip->i_mount;
456 xfs_fileoff_t start_zero_fsb;
457 xfs_fileoff_t end_zero_fsb;
458 xfs_fileoff_t zero_count_fsb;
459 xfs_fileoff_t last_fsb;
460 xfs_fileoff_t zero_off;
461 xfs_fsize_t zero_len;
462 int nimaps;
463 int error = 0;
464 struct xfs_bmbt_irec imap;
465
466 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
467 ASSERT(offset > isize);
468
469 /*
470 * First handle zeroing the block on which isize resides.
471 *
472 * We only zero a part of that block so it is handled specially.
473 */
474 if (XFS_B_FSB_OFFSET(mp, isize) != 0) {
475 error = xfs_zero_last_block(ip, offset, isize);
476 if (error)
477 return error;
478 }
479
480 /*
481 * Calculate the range between the new size and the old where blocks
482 * needing to be zeroed may exist.
483 *
484 * To get the block where the last byte in the file currently resides,
485 * we need to subtract one from the size and truncate back to a block
486 * boundary. We subtract 1 in case the size is exactly on a block
487 * boundary.
488 */
489 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
490 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
491 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
492 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
493 if (last_fsb == end_zero_fsb) {
494 /*
495 * The size was only incremented on its last block.
496 * We took care of that above, so just return.
497 */
498 return 0;
499 }
500
501 ASSERT(start_zero_fsb <= end_zero_fsb);
502 while (start_zero_fsb <= end_zero_fsb) {
503 nimaps = 1;
504 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
505
506 xfs_ilock(ip, XFS_ILOCK_EXCL);
507 error = xfs_bmapi_read(ip, start_zero_fsb, zero_count_fsb,
508 &imap, &nimaps, 0);
509 xfs_iunlock(ip, XFS_ILOCK_EXCL);
510 if (error)
511 return error;
512
513 ASSERT(nimaps > 0);
514
515 if (imap.br_state == XFS_EXT_UNWRITTEN ||
516 imap.br_startblock == HOLESTARTBLOCK) {
517 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
518 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
519 continue;
520 }
521
522 /*
523 * There are blocks we need to zero.
524 */
525 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
526 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
527
528 if ((zero_off + zero_len) > offset)
529 zero_len = offset - zero_off;
530
531 error = xfs_iozero(ip, zero_off, zero_len);
532 if (error)
533 return error;
534
535 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
536 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
537 }
538
539 return 0;
540 }
541
542 /*
543 * Common pre-write limit and setup checks.
544 *
545 * Called with the iolocked held either shared and exclusive according to
546 * @iolock, and returns with it held. Might upgrade the iolock to exclusive
547 * if called for a direct write beyond i_size.
548 */
549 STATIC ssize_t
550 xfs_file_aio_write_checks(
551 struct file *file,
552 loff_t *pos,
553 size_t *count,
554 int *iolock)
555 {
556 struct inode *inode = file->f_mapping->host;
557 struct xfs_inode *ip = XFS_I(inode);
558 int error = 0;
559
560 restart:
561 error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode));
562 if (error)
563 return error;
564
565 /*
566 * If the offset is beyond the size of the file, we need to zero any
567 * blocks that fall between the existing EOF and the start of this
568 * write. If zeroing is needed and we are currently holding the
569 * iolock shared, we need to update it to exclusive which implies
570 * having to redo all checks before.
571 */
572 if (*pos > i_size_read(inode)) {
573 if (*iolock == XFS_IOLOCK_SHARED) {
574 xfs_rw_iunlock(ip, *iolock);
575 *iolock = XFS_IOLOCK_EXCL;
576 xfs_rw_ilock(ip, *iolock);
577 goto restart;
578 }
579 error = -xfs_zero_eof(ip, *pos, i_size_read(inode));
580 if (error)
581 return error;
582 }
583
584 /*
585 * Updating the timestamps will grab the ilock again from
586 * xfs_fs_dirty_inode, so we have to call it after dropping the
587 * lock above. Eventually we should look into a way to avoid
588 * the pointless lock roundtrip.
589 */
590 if (likely(!(file->f_mode & FMODE_NOCMTIME)))
591 file_update_time(file);
592
593 /*
594 * If we're writing the file then make sure to clear the setuid and
595 * setgid bits if the process is not being run by root. This keeps
596 * people from modifying setuid and setgid binaries.
597 */
598 return file_remove_suid(file);
599 }
600
601 /*
602 * xfs_file_dio_aio_write - handle direct IO writes
603 *
604 * Lock the inode appropriately to prepare for and issue a direct IO write.
605 * By separating it from the buffered write path we remove all the tricky to
606 * follow locking changes and looping.
607 *
608 * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
609 * until we're sure the bytes at the new EOF have been zeroed and/or the cached
610 * pages are flushed out.
611 *
612 * In most cases the direct IO writes will be done holding IOLOCK_SHARED
613 * allowing them to be done in parallel with reads and other direct IO writes.
614 * However, if the IO is not aligned to filesystem blocks, the direct IO layer
615 * needs to do sub-block zeroing and that requires serialisation against other
616 * direct IOs to the same block. In this case we need to serialise the
617 * submission of the unaligned IOs so that we don't get racing block zeroing in
618 * the dio layer. To avoid the problem with aio, we also need to wait for
619 * outstanding IOs to complete so that unwritten extent conversion is completed
620 * before we try to map the overlapping block. This is currently implemented by
621 * hitting it with a big hammer (i.e. inode_dio_wait()).
622 *
623 * Returns with locks held indicated by @iolock and errors indicated by
624 * negative return values.
625 */
626 STATIC ssize_t
627 xfs_file_dio_aio_write(
628 struct kiocb *iocb,
629 const struct iovec *iovp,
630 unsigned long nr_segs,
631 loff_t pos,
632 size_t ocount)
633 {
634 struct file *file = iocb->ki_filp;
635 struct address_space *mapping = file->f_mapping;
636 struct inode *inode = mapping->host;
637 struct xfs_inode *ip = XFS_I(inode);
638 struct xfs_mount *mp = ip->i_mount;
639 ssize_t ret = 0;
640 size_t count = ocount;
641 int unaligned_io = 0;
642 int iolock;
643 struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
644 mp->m_rtdev_targp : mp->m_ddev_targp;
645
646 if ((pos & target->bt_smask) || (count & target->bt_smask))
647 return -XFS_ERROR(EINVAL);
648
649 if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
650 unaligned_io = 1;
651
652 /*
653 * We don't need to take an exclusive lock unless there page cache needs
654 * to be invalidated or unaligned IO is being executed. We don't need to
655 * consider the EOF extension case here because
656 * xfs_file_aio_write_checks() will relock the inode as necessary for
657 * EOF zeroing cases and fill out the new inode size as appropriate.
658 */
659 if (unaligned_io || mapping->nrpages)
660 iolock = XFS_IOLOCK_EXCL;
661 else
662 iolock = XFS_IOLOCK_SHARED;
663 xfs_rw_ilock(ip, iolock);
664
665 /*
666 * Recheck if there are cached pages that need invalidate after we got
667 * the iolock to protect against other threads adding new pages while
668 * we were waiting for the iolock.
669 */
670 if (mapping->nrpages && iolock == XFS_IOLOCK_SHARED) {
671 xfs_rw_iunlock(ip, iolock);
672 iolock = XFS_IOLOCK_EXCL;
673 xfs_rw_ilock(ip, iolock);
674 }
675
676 ret = xfs_file_aio_write_checks(file, &pos, &count, &iolock);
677 if (ret)
678 goto out;
679
680 if (mapping->nrpages) {
681 ret = -xfs_flushinval_pages(ip, (pos & PAGE_CACHE_MASK), -1,
682 FI_REMAPF_LOCKED);
683 if (ret)
684 goto out;
685 }
686
687 /*
688 * If we are doing unaligned IO, wait for all other IO to drain,
689 * otherwise demote the lock if we had to flush cached pages
690 */
691 if (unaligned_io)
692 inode_dio_wait(inode);
693 else if (iolock == XFS_IOLOCK_EXCL) {
694 xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
695 iolock = XFS_IOLOCK_SHARED;
696 }
697
698 trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
699 ret = generic_file_direct_write(iocb, iovp,
700 &nr_segs, pos, &iocb->ki_pos, count, ocount);
701
702 out:
703 xfs_rw_iunlock(ip, iolock);
704
705 /* No fallback to buffered IO on errors for XFS. */
706 ASSERT(ret < 0 || ret == count);
707 return ret;
708 }
709
710 STATIC ssize_t
711 xfs_file_buffered_aio_write(
712 struct kiocb *iocb,
713 const struct iovec *iovp,
714 unsigned long nr_segs,
715 loff_t pos,
716 size_t ocount)
717 {
718 struct file *file = iocb->ki_filp;
719 struct address_space *mapping = file->f_mapping;
720 struct inode *inode = mapping->host;
721 struct xfs_inode *ip = XFS_I(inode);
722 ssize_t ret;
723 int enospc = 0;
724 int iolock = XFS_IOLOCK_EXCL;
725 size_t count = ocount;
726
727 xfs_rw_ilock(ip, iolock);
728
729 ret = xfs_file_aio_write_checks(file, &pos, &count, &iolock);
730 if (ret)
731 goto out;
732
733 /* We can write back this queue in page reclaim */
734 current->backing_dev_info = mapping->backing_dev_info;
735
736 write_retry:
737 trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
738 ret = generic_file_buffered_write(iocb, iovp, nr_segs,
739 pos, &iocb->ki_pos, count, ret);
740 /*
741 * if we just got an ENOSPC, flush the inode now we aren't holding any
742 * page locks and retry *once*
743 */
744 if (ret == -ENOSPC && !enospc) {
745 enospc = 1;
746 ret = -xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
747 if (!ret)
748 goto write_retry;
749 }
750
751 current->backing_dev_info = NULL;
752 out:
753 xfs_rw_iunlock(ip, iolock);
754 return ret;
755 }
756
757 STATIC ssize_t
758 xfs_file_aio_write(
759 struct kiocb *iocb,
760 const struct iovec *iovp,
761 unsigned long nr_segs,
762 loff_t pos)
763 {
764 struct file *file = iocb->ki_filp;
765 struct address_space *mapping = file->f_mapping;
766 struct inode *inode = mapping->host;
767 struct xfs_inode *ip = XFS_I(inode);
768 ssize_t ret;
769 size_t ocount = 0;
770
771 XFS_STATS_INC(xs_write_calls);
772
773 BUG_ON(iocb->ki_pos != pos);
774
775 ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
776 if (ret)
777 return ret;
778
779 if (ocount == 0)
780 return 0;
781
782 xfs_wait_for_freeze(ip->i_mount, SB_FREEZE_WRITE);
783
784 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
785 return -EIO;
786
787 if (unlikely(file->f_flags & O_DIRECT))
788 ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos, ocount);
789 else
790 ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
791 ocount);
792
793 if (ret > 0) {
794 ssize_t err;
795
796 XFS_STATS_ADD(xs_write_bytes, ret);
797
798 /* Handle various SYNC-type writes */
799 err = generic_write_sync(file, pos, ret);
800 if (err < 0)
801 ret = err;
802 }
803
804 return ret;
805 }
806
807 STATIC long
808 xfs_file_fallocate(
809 struct file *file,
810 int mode,
811 loff_t offset,
812 loff_t len)
813 {
814 struct inode *inode = file->f_path.dentry->d_inode;
815 long error;
816 loff_t new_size = 0;
817 xfs_flock64_t bf;
818 xfs_inode_t *ip = XFS_I(inode);
819 int cmd = XFS_IOC_RESVSP;
820 int attr_flags = XFS_ATTR_NOLOCK;
821
822 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
823 return -EOPNOTSUPP;
824
825 bf.l_whence = 0;
826 bf.l_start = offset;
827 bf.l_len = len;
828
829 xfs_ilock(ip, XFS_IOLOCK_EXCL);
830
831 if (mode & FALLOC_FL_PUNCH_HOLE)
832 cmd = XFS_IOC_UNRESVSP;
833
834 /* check the new inode size is valid before allocating */
835 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
836 offset + len > i_size_read(inode)) {
837 new_size = offset + len;
838 error = inode_newsize_ok(inode, new_size);
839 if (error)
840 goto out_unlock;
841 }
842
843 if (file->f_flags & O_DSYNC)
844 attr_flags |= XFS_ATTR_SYNC;
845
846 error = -xfs_change_file_space(ip, cmd, &bf, 0, attr_flags);
847 if (error)
848 goto out_unlock;
849
850 /* Change file size if needed */
851 if (new_size) {
852 struct iattr iattr;
853
854 iattr.ia_valid = ATTR_SIZE;
855 iattr.ia_size = new_size;
856 error = -xfs_setattr_size(ip, &iattr, XFS_ATTR_NOLOCK);
857 }
858
859 out_unlock:
860 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
861 return error;
862 }
863
864
865 STATIC int
866 xfs_file_open(
867 struct inode *inode,
868 struct file *file)
869 {
870 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
871 return -EFBIG;
872 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
873 return -EIO;
874 return 0;
875 }
876
877 STATIC int
878 xfs_dir_open(
879 struct inode *inode,
880 struct file *file)
881 {
882 struct xfs_inode *ip = XFS_I(inode);
883 int mode;
884 int error;
885
886 error = xfs_file_open(inode, file);
887 if (error)
888 return error;
889
890 /*
891 * If there are any blocks, read-ahead block 0 as we're almost
892 * certain to have the next operation be a read there.
893 */
894 mode = xfs_ilock_map_shared(ip);
895 if (ip->i_d.di_nextents > 0)
896 xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
897 xfs_iunlock(ip, mode);
898 return 0;
899 }
900
901 STATIC int
902 xfs_file_release(
903 struct inode *inode,
904 struct file *filp)
905 {
906 return -xfs_release(XFS_I(inode));
907 }
908
909 STATIC int
910 xfs_file_readdir(
911 struct file *filp,
912 void *dirent,
913 filldir_t filldir)
914 {
915 struct inode *inode = filp->f_path.dentry->d_inode;
916 xfs_inode_t *ip = XFS_I(inode);
917 int error;
918 size_t bufsize;
919
920 /*
921 * The Linux API doesn't pass down the total size of the buffer
922 * we read into down to the filesystem. With the filldir concept
923 * it's not needed for correct information, but the XFS dir2 leaf
924 * code wants an estimate of the buffer size to calculate it's
925 * readahead window and size the buffers used for mapping to
926 * physical blocks.
927 *
928 * Try to give it an estimate that's good enough, maybe at some
929 * point we can change the ->readdir prototype to include the
930 * buffer size. For now we use the current glibc buffer size.
931 */
932 bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
933
934 error = xfs_readdir(ip, dirent, bufsize,
935 (xfs_off_t *)&filp->f_pos, filldir);
936 if (error)
937 return -error;
938 return 0;
939 }
940
941 STATIC int
942 xfs_file_mmap(
943 struct file *filp,
944 struct vm_area_struct *vma)
945 {
946 vma->vm_ops = &xfs_file_vm_ops;
947 vma->vm_flags |= VM_CAN_NONLINEAR;
948
949 file_accessed(filp);
950 return 0;
951 }
952
953 /*
954 * mmap()d file has taken write protection fault and is being made
955 * writable. We can set the page state up correctly for a writable
956 * page, which means we can do correct delalloc accounting (ENOSPC
957 * checking!) and unwritten extent mapping.
958 */
959 STATIC int
960 xfs_vm_page_mkwrite(
961 struct vm_area_struct *vma,
962 struct vm_fault *vmf)
963 {
964 return block_page_mkwrite(vma, vmf, xfs_get_blocks);
965 }
966
967 const struct file_operations xfs_file_operations = {
968 .llseek = generic_file_llseek,
969 .read = do_sync_read,
970 .write = do_sync_write,
971 .aio_read = xfs_file_aio_read,
972 .aio_write = xfs_file_aio_write,
973 .splice_read = xfs_file_splice_read,
974 .splice_write = xfs_file_splice_write,
975 .unlocked_ioctl = xfs_file_ioctl,
976 #ifdef CONFIG_COMPAT
977 .compat_ioctl = xfs_file_compat_ioctl,
978 #endif
979 .mmap = xfs_file_mmap,
980 .open = xfs_file_open,
981 .release = xfs_file_release,
982 .fsync = xfs_file_fsync,
983 .fallocate = xfs_file_fallocate,
984 };
985
986 const struct file_operations xfs_dir_file_operations = {
987 .open = xfs_dir_open,
988 .read = generic_read_dir,
989 .readdir = xfs_file_readdir,
990 .llseek = generic_file_llseek,
991 .unlocked_ioctl = xfs_file_ioctl,
992 #ifdef CONFIG_COMPAT
993 .compat_ioctl = xfs_file_compat_ioctl,
994 #endif
995 .fsync = xfs_dir_fsync,
996 };
997
998 static const struct vm_operations_struct xfs_file_vm_ops = {
999 .fault = filemap_fault,
1000 .page_mkwrite = xfs_vm_page_mkwrite,
1001 };
This page took 0.08863 seconds and 5 git commands to generate.