blob: 51fc510828a4baed168a48855dfd22e328c91bd1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000019#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_dir2.h"
26#include "xfs_trans.h"
27#include "xfs_dmapi.h"
28#include "xfs_mount.h"
29#include "xfs_bmap_btree.h"
30#include "xfs_alloc_btree.h"
31#include "xfs_ialloc_btree.h"
32#include "xfs_alloc.h"
33#include "xfs_btree.h"
34#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dir2_sf.h"
36#include "xfs_dinode.h"
37#include "xfs_inode.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000038#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_error.h"
40#include "xfs_rw.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100041#include "xfs_vnodeops.h"
Christoph Hellwigf999a5b2008-11-28 14:23:32 +110042#include "xfs_da_btree.h"
Christoph Hellwigddcd8562008-12-03 07:55:34 -050043#include "xfs_ioctl.h"
Christoph Hellwigdda35b82010-02-15 09:44:46 +000044#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <linux/dcache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040048static const struct vm_operations_struct xfs_file_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Christoph Hellwigdda35b82010-02-15 09:44:46 +000050/*
51 * xfs_iozero
52 *
53 * xfs_iozero clears the specified range of buffer supplied,
54 * and marks all the affected blocks as valid and modified. If
55 * an affected block is not allocated, it will be allocated. If
56 * an affected block is not completely overwritten, and is not
57 * valid before the operation, it will be read from disk before
58 * being partially zeroed.
59 */
60STATIC int
61xfs_iozero(
62 struct xfs_inode *ip, /* inode */
63 loff_t pos, /* offset in file */
64 size_t count) /* size of data to zero */
65{
66 struct page *page;
67 struct address_space *mapping;
68 int status;
69
70 mapping = VFS_I(ip)->i_mapping;
71 do {
72 unsigned offset, bytes;
73 void *fsdata;
74
75 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
76 bytes = PAGE_CACHE_SIZE - offset;
77 if (bytes > count)
78 bytes = count;
79
80 status = pagecache_write_begin(NULL, mapping, pos, bytes,
81 AOP_FLAG_UNINTERRUPTIBLE,
82 &page, &fsdata);
83 if (status)
84 break;
85
86 zero_user(page, offset, bytes);
87
88 status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
89 page, fsdata);
90 WARN_ON(status <= 0); /* can't return less than zero! */
91 pos += bytes;
92 count -= bytes;
93 status = 0;
94 } while (count);
95
96 return (-status);
97}
98
99ssize_t /* bytes read, or (-) error */
100xfs_read(
101 xfs_inode_t *ip,
102 struct kiocb *iocb,
103 const struct iovec *iovp,
104 unsigned int segs,
105 loff_t *offset,
106 int ioflags)
107{
108 struct file *file = iocb->ki_filp;
109 struct inode *inode = file->f_mapping->host;
110 xfs_mount_t *mp = ip->i_mount;
111 size_t size = 0;
112 ssize_t ret = 0;
113 xfs_fsize_t n;
114 unsigned long seg;
115
116
117 XFS_STATS_INC(xs_read_calls);
118
119 /* START copy & waste from filemap.c */
120 for (seg = 0; seg < segs; seg++) {
121 const struct iovec *iv = &iovp[seg];
122
123 /*
124 * If any segment has a negative length, or the cumulative
125 * length ever wraps negative then return -EINVAL.
126 */
127 size += iv->iov_len;
128 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
129 return XFS_ERROR(-EINVAL);
130 }
131 /* END copy & waste from filemap.c */
132
133 if (unlikely(ioflags & IO_ISDIRECT)) {
134 xfs_buftarg_t *target =
135 XFS_IS_REALTIME_INODE(ip) ?
136 mp->m_rtdev_targp : mp->m_ddev_targp;
137 if ((*offset & target->bt_smask) ||
138 (size & target->bt_smask)) {
139 if (*offset == ip->i_size) {
140 return (0);
141 }
142 return -XFS_ERROR(EINVAL);
143 }
144 }
145
146 n = XFS_MAXIOFFSET(mp) - *offset;
147 if ((n <= 0) || (size == 0))
148 return 0;
149
150 if (n < size)
151 size = n;
152
153 if (XFS_FORCED_SHUTDOWN(mp))
154 return -EIO;
155
156 if (unlikely(ioflags & IO_ISDIRECT))
157 mutex_lock(&inode->i_mutex);
158 xfs_ilock(ip, XFS_IOLOCK_SHARED);
159
160 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
161 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
162 int iolock = XFS_IOLOCK_SHARED;
163
164 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ, ip, *offset, size,
165 dmflags, &iolock);
166 if (ret) {
167 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
168 if (unlikely(ioflags & IO_ISDIRECT))
169 mutex_unlock(&inode->i_mutex);
170 return ret;
171 }
172 }
173
174 if (unlikely(ioflags & IO_ISDIRECT)) {
175 if (inode->i_mapping->nrpages)
176 ret = -xfs_flushinval_pages(ip, (*offset & PAGE_CACHE_MASK),
177 -1, FI_REMAPF_LOCKED);
178 mutex_unlock(&inode->i_mutex);
179 if (ret) {
180 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
181 return ret;
182 }
183 }
184
185 trace_xfs_file_read(ip, size, *offset, ioflags);
186
187 iocb->ki_pos = *offset;
188 ret = generic_file_aio_read(iocb, iovp, segs, *offset);
189 if (ret > 0)
190 XFS_STATS_ADD(xs_read_bytes, ret);
191
192 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
193 return ret;
194}
195
196ssize_t
197xfs_splice_read(
198 xfs_inode_t *ip,
199 struct file *infilp,
200 loff_t *ppos,
201 struct pipe_inode_info *pipe,
202 size_t count,
203 int flags,
204 int ioflags)
205{
206 xfs_mount_t *mp = ip->i_mount;
207 ssize_t ret;
208
209 XFS_STATS_INC(xs_read_calls);
210 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
211 return -EIO;
212
213 xfs_ilock(ip, XFS_IOLOCK_SHARED);
214
215 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
216 int iolock = XFS_IOLOCK_SHARED;
217 int error;
218
219 error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, *ppos, count,
220 FILP_DELAY_FLAG(infilp), &iolock);
221 if (error) {
222 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
223 return -error;
224 }
225 }
226
227 trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
228
229 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
230 if (ret > 0)
231 XFS_STATS_ADD(xs_read_bytes, ret);
232
233 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
234 return ret;
235}
236
237ssize_t
238xfs_splice_write(
239 xfs_inode_t *ip,
240 struct pipe_inode_info *pipe,
241 struct file *outfilp,
242 loff_t *ppos,
243 size_t count,
244 int flags,
245 int ioflags)
246{
247 xfs_mount_t *mp = ip->i_mount;
248 ssize_t ret;
249 struct inode *inode = outfilp->f_mapping->host;
250 xfs_fsize_t isize, new_size;
251
252 XFS_STATS_INC(xs_write_calls);
253 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
254 return -EIO;
255
256 xfs_ilock(ip, XFS_IOLOCK_EXCL);
257
258 if (DM_EVENT_ENABLED(ip, DM_EVENT_WRITE) && !(ioflags & IO_INVIS)) {
259 int iolock = XFS_IOLOCK_EXCL;
260 int error;
261
262 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, *ppos, count,
263 FILP_DELAY_FLAG(outfilp), &iolock);
264 if (error) {
265 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
266 return -error;
267 }
268 }
269
270 new_size = *ppos + count;
271
272 xfs_ilock(ip, XFS_ILOCK_EXCL);
273 if (new_size > ip->i_size)
274 ip->i_new_size = new_size;
275 xfs_iunlock(ip, XFS_ILOCK_EXCL);
276
277 trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
278
279 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
280 if (ret > 0)
281 XFS_STATS_ADD(xs_write_bytes, ret);
282
283 isize = i_size_read(inode);
284 if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
285 *ppos = isize;
286
287 if (*ppos > ip->i_size) {
288 xfs_ilock(ip, XFS_ILOCK_EXCL);
289 if (*ppos > ip->i_size)
290 ip->i_size = *ppos;
291 xfs_iunlock(ip, XFS_ILOCK_EXCL);
292 }
293
294 if (ip->i_new_size) {
295 xfs_ilock(ip, XFS_ILOCK_EXCL);
296 ip->i_new_size = 0;
297 if (ip->i_d.di_size > ip->i_size)
298 ip->i_d.di_size = ip->i_size;
299 xfs_iunlock(ip, XFS_ILOCK_EXCL);
300 }
301 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
302 return ret;
303}
304
305/*
306 * This routine is called to handle zeroing any space in the last
307 * block of the file that is beyond the EOF. We do this since the
308 * size is being increased without writing anything to that block
309 * and we don't want anyone to read the garbage on the disk.
310 */
311STATIC int /* error (positive) */
312xfs_zero_last_block(
313 xfs_inode_t *ip,
314 xfs_fsize_t offset,
315 xfs_fsize_t isize)
316{
317 xfs_fileoff_t last_fsb;
318 xfs_mount_t *mp = ip->i_mount;
319 int nimaps;
320 int zero_offset;
321 int zero_len;
322 int error = 0;
323 xfs_bmbt_irec_t imap;
324
325 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
326
327 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
328 if (zero_offset == 0) {
329 /*
330 * There are no extra bytes in the last block on disk to
331 * zero, so return.
332 */
333 return 0;
334 }
335
336 last_fsb = XFS_B_TO_FSBT(mp, isize);
337 nimaps = 1;
338 error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
339 &nimaps, NULL, NULL);
340 if (error) {
341 return error;
342 }
343 ASSERT(nimaps > 0);
344 /*
345 * If the block underlying isize is just a hole, then there
346 * is nothing to zero.
347 */
348 if (imap.br_startblock == HOLESTARTBLOCK) {
349 return 0;
350 }
351 /*
352 * Zero the part of the last block beyond the EOF, and write it
353 * out sync. We need to drop the ilock while we do this so we
354 * don't deadlock when the buffer cache calls back to us.
355 */
356 xfs_iunlock(ip, XFS_ILOCK_EXCL);
357
358 zero_len = mp->m_sb.sb_blocksize - zero_offset;
359 if (isize + zero_len > offset)
360 zero_len = offset - isize;
361 error = xfs_iozero(ip, isize, zero_len);
362
363 xfs_ilock(ip, XFS_ILOCK_EXCL);
364 ASSERT(error >= 0);
365 return error;
366}
367
368/*
369 * Zero any on disk space between the current EOF and the new,
370 * larger EOF. This handles the normal case of zeroing the remainder
371 * of the last block in the file and the unusual case of zeroing blocks
372 * out beyond the size of the file. This second case only happens
373 * with fixed size extents and when the system crashes before the inode
374 * size was updated but after blocks were allocated. If fill is set,
375 * then any holes in the range are filled and zeroed. If not, the holes
376 * are left alone as holes.
377 */
378
379int /* error (positive) */
380xfs_zero_eof(
381 xfs_inode_t *ip,
382 xfs_off_t offset, /* starting I/O offset */
383 xfs_fsize_t isize) /* current inode size */
384{
385 xfs_mount_t *mp = ip->i_mount;
386 xfs_fileoff_t start_zero_fsb;
387 xfs_fileoff_t end_zero_fsb;
388 xfs_fileoff_t zero_count_fsb;
389 xfs_fileoff_t last_fsb;
390 xfs_fileoff_t zero_off;
391 xfs_fsize_t zero_len;
392 int nimaps;
393 int error = 0;
394 xfs_bmbt_irec_t imap;
395
396 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
397 ASSERT(offset > isize);
398
399 /*
400 * First handle zeroing the block on which isize resides.
401 * We only zero a part of that block so it is handled specially.
402 */
403 error = xfs_zero_last_block(ip, offset, isize);
404 if (error) {
405 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
406 return error;
407 }
408
409 /*
410 * Calculate the range between the new size and the old
411 * where blocks needing to be zeroed may exist. To get the
412 * block where the last byte in the file currently resides,
413 * we need to subtract one from the size and truncate back
414 * to a block boundary. We subtract 1 in case the size is
415 * exactly on a block boundary.
416 */
417 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
418 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
419 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
420 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
421 if (last_fsb == end_zero_fsb) {
422 /*
423 * The size was only incremented on its last block.
424 * We took care of that above, so just return.
425 */
426 return 0;
427 }
428
429 ASSERT(start_zero_fsb <= end_zero_fsb);
430 while (start_zero_fsb <= end_zero_fsb) {
431 nimaps = 1;
432 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
433 error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
434 0, NULL, 0, &imap, &nimaps, NULL, NULL);
435 if (error) {
436 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
437 return error;
438 }
439 ASSERT(nimaps > 0);
440
441 if (imap.br_state == XFS_EXT_UNWRITTEN ||
442 imap.br_startblock == HOLESTARTBLOCK) {
443 /*
444 * This loop handles initializing pages that were
445 * partially initialized by the code below this
446 * loop. It basically zeroes the part of the page
447 * that sits on a hole and sets the page as P_HOLE
448 * and calls remapf if it is a mapped file.
449 */
450 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
451 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
452 continue;
453 }
454
455 /*
456 * There are blocks we need to zero.
457 * Drop the inode lock while we're doing the I/O.
458 * We'll still have the iolock to protect us.
459 */
460 xfs_iunlock(ip, XFS_ILOCK_EXCL);
461
462 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
463 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
464
465 if ((zero_off + zero_len) > offset)
466 zero_len = offset - zero_off;
467
468 error = xfs_iozero(ip, zero_off, zero_len);
469 if (error) {
470 goto out_lock;
471 }
472
473 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
474 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
475
476 xfs_ilock(ip, XFS_ILOCK_EXCL);
477 }
478
479 return 0;
480
481out_lock:
482 xfs_ilock(ip, XFS_ILOCK_EXCL);
483 ASSERT(error >= 0);
484 return error;
485}
486
487ssize_t /* bytes written, or (-) error */
488xfs_write(
489 struct xfs_inode *xip,
490 struct kiocb *iocb,
491 const struct iovec *iovp,
492 unsigned int nsegs,
493 loff_t *offset,
494 int ioflags)
495{
496 struct file *file = iocb->ki_filp;
497 struct address_space *mapping = file->f_mapping;
498 struct inode *inode = mapping->host;
499 unsigned long segs = nsegs;
500 xfs_mount_t *mp;
501 ssize_t ret = 0, error = 0;
502 xfs_fsize_t isize, new_size;
503 int iolock;
504 int eventsent = 0;
505 size_t ocount = 0, count;
506 loff_t pos;
507 int need_i_mutex;
508
509 XFS_STATS_INC(xs_write_calls);
510
511 error = generic_segment_checks(iovp, &segs, &ocount, VERIFY_READ);
512 if (error)
513 return error;
514
515 count = ocount;
516 pos = *offset;
517
518 if (count == 0)
519 return 0;
520
521 mp = xip->i_mount;
522
523 xfs_wait_for_freeze(mp, SB_FREEZE_WRITE);
524
525 if (XFS_FORCED_SHUTDOWN(mp))
526 return -EIO;
527
528relock:
529 if (ioflags & IO_ISDIRECT) {
530 iolock = XFS_IOLOCK_SHARED;
531 need_i_mutex = 0;
532 } else {
533 iolock = XFS_IOLOCK_EXCL;
534 need_i_mutex = 1;
535 mutex_lock(&inode->i_mutex);
536 }
537
538 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
539
540start:
541 error = -generic_write_checks(file, &pos, &count,
542 S_ISBLK(inode->i_mode));
543 if (error) {
544 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
545 goto out_unlock_mutex;
546 }
547
548 if ((DM_EVENT_ENABLED(xip, DM_EVENT_WRITE) &&
549 !(ioflags & IO_INVIS) && !eventsent)) {
550 int dmflags = FILP_DELAY_FLAG(file);
551
552 if (need_i_mutex)
553 dmflags |= DM_FLAGS_IMUX;
554
555 xfs_iunlock(xip, XFS_ILOCK_EXCL);
556 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, xip,
557 pos, count, dmflags, &iolock);
558 if (error) {
559 goto out_unlock_internal;
560 }
561 xfs_ilock(xip, XFS_ILOCK_EXCL);
562 eventsent = 1;
563
564 /*
565 * The iolock was dropped and reacquired in XFS_SEND_DATA
566 * so we have to recheck the size when appending.
567 * We will only "goto start;" once, since having sent the
568 * event prevents another call to XFS_SEND_DATA, which is
569 * what allows the size to change in the first place.
570 */
571 if ((file->f_flags & O_APPEND) && pos != xip->i_size)
572 goto start;
573 }
574
575 if (ioflags & IO_ISDIRECT) {
576 xfs_buftarg_t *target =
577 XFS_IS_REALTIME_INODE(xip) ?
578 mp->m_rtdev_targp : mp->m_ddev_targp;
579
580 if ((pos & target->bt_smask) || (count & target->bt_smask)) {
581 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
582 return XFS_ERROR(-EINVAL);
583 }
584
585 if (!need_i_mutex && (mapping->nrpages || pos > xip->i_size)) {
586 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
587 iolock = XFS_IOLOCK_EXCL;
588 need_i_mutex = 1;
589 mutex_lock(&inode->i_mutex);
590 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
591 goto start;
592 }
593 }
594
595 new_size = pos + count;
596 if (new_size > xip->i_size)
597 xip->i_new_size = new_size;
598
599 if (likely(!(ioflags & IO_INVIS)))
600 file_update_time(file);
601
602 /*
603 * If the offset is beyond the size of the file, we have a couple
604 * of things to do. First, if there is already space allocated
605 * we need to either create holes or zero the disk or ...
606 *
607 * If there is a page where the previous size lands, we need
608 * to zero it out up to the new size.
609 */
610
611 if (pos > xip->i_size) {
612 error = xfs_zero_eof(xip, pos, xip->i_size);
613 if (error) {
614 xfs_iunlock(xip, XFS_ILOCK_EXCL);
615 goto out_unlock_internal;
616 }
617 }
618 xfs_iunlock(xip, XFS_ILOCK_EXCL);
619
620 /*
621 * If we're writing the file then make sure to clear the
622 * setuid and setgid bits if the process is not being run
623 * by root. This keeps people from modifying setuid and
624 * setgid binaries.
625 */
626 error = -file_remove_suid(file);
627 if (unlikely(error))
628 goto out_unlock_internal;
629
630 /* We can write back this queue in page reclaim */
631 current->backing_dev_info = mapping->backing_dev_info;
632
633 if ((ioflags & IO_ISDIRECT)) {
634 if (mapping->nrpages) {
635 WARN_ON(need_i_mutex == 0);
636 error = xfs_flushinval_pages(xip,
637 (pos & PAGE_CACHE_MASK),
638 -1, FI_REMAPF_LOCKED);
639 if (error)
640 goto out_unlock_internal;
641 }
642
643 if (need_i_mutex) {
644 /* demote the lock now the cached pages are gone */
645 xfs_ilock_demote(xip, XFS_IOLOCK_EXCL);
646 mutex_unlock(&inode->i_mutex);
647
648 iolock = XFS_IOLOCK_SHARED;
649 need_i_mutex = 0;
650 }
651
652 trace_xfs_file_direct_write(xip, count, *offset, ioflags);
653 ret = generic_file_direct_write(iocb, iovp,
654 &segs, pos, offset, count, ocount);
655
656 /*
657 * direct-io write to a hole: fall through to buffered I/O
658 * for completing the rest of the request.
659 */
660 if (ret >= 0 && ret != count) {
661 XFS_STATS_ADD(xs_write_bytes, ret);
662
663 pos += ret;
664 count -= ret;
665
666 ioflags &= ~IO_ISDIRECT;
667 xfs_iunlock(xip, iolock);
668 goto relock;
669 }
670 } else {
671 int enospc = 0;
672 ssize_t ret2 = 0;
673
674write_retry:
675 trace_xfs_file_buffered_write(xip, count, *offset, ioflags);
676 ret2 = generic_file_buffered_write(iocb, iovp, segs,
677 pos, offset, count, ret);
678 /*
679 * if we just got an ENOSPC, flush the inode now we
680 * aren't holding any page locks and retry *once*
681 */
682 if (ret2 == -ENOSPC && !enospc) {
683 error = xfs_flush_pages(xip, 0, -1, 0, FI_NONE);
684 if (error)
685 goto out_unlock_internal;
686 enospc = 1;
687 goto write_retry;
688 }
689 ret = ret2;
690 }
691
692 current->backing_dev_info = NULL;
693
694 isize = i_size_read(inode);
695 if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
696 *offset = isize;
697
698 if (*offset > xip->i_size) {
699 xfs_ilock(xip, XFS_ILOCK_EXCL);
700 if (*offset > xip->i_size)
701 xip->i_size = *offset;
702 xfs_iunlock(xip, XFS_ILOCK_EXCL);
703 }
704
705 if (ret == -ENOSPC &&
706 DM_EVENT_ENABLED(xip, DM_EVENT_NOSPACE) && !(ioflags & IO_INVIS)) {
707 xfs_iunlock(xip, iolock);
708 if (need_i_mutex)
709 mutex_unlock(&inode->i_mutex);
710 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, xip,
711 DM_RIGHT_NULL, xip, DM_RIGHT_NULL, NULL, NULL,
712 0, 0, 0); /* Delay flag intentionally unused */
713 if (need_i_mutex)
714 mutex_lock(&inode->i_mutex);
715 xfs_ilock(xip, iolock);
716 if (error)
717 goto out_unlock_internal;
718 goto start;
719 }
720
721 error = -ret;
722 if (ret <= 0)
723 goto out_unlock_internal;
724
725 XFS_STATS_ADD(xs_write_bytes, ret);
726
727 /* Handle various SYNC-type writes */
728 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
729 loff_t end = pos + ret - 1;
730 int error2;
731
732 xfs_iunlock(xip, iolock);
733 if (need_i_mutex)
734 mutex_unlock(&inode->i_mutex);
735
736 error2 = filemap_write_and_wait_range(mapping, pos, end);
737 if (!error)
738 error = error2;
739 if (need_i_mutex)
740 mutex_lock(&inode->i_mutex);
741 xfs_ilock(xip, iolock);
742
743 error2 = xfs_fsync(xip);
744 if (!error)
745 error = error2;
746 }
747
748 out_unlock_internal:
749 if (xip->i_new_size) {
750 xfs_ilock(xip, XFS_ILOCK_EXCL);
751 xip->i_new_size = 0;
752 /*
753 * If this was a direct or synchronous I/O that failed (such
754 * as ENOSPC) then part of the I/O may have been written to
755 * disk before the error occured. In this case the on-disk
756 * file size may have been adjusted beyond the in-memory file
757 * size and now needs to be truncated back.
758 */
759 if (xip->i_d.di_size > xip->i_size)
760 xip->i_d.di_size = xip->i_size;
761 xfs_iunlock(xip, XFS_ILOCK_EXCL);
762 }
763 xfs_iunlock(xip, iolock);
764 out_unlock_mutex:
765 if (need_i_mutex)
766 mutex_unlock(&inode->i_mutex);
767 return -error;
768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770STATIC ssize_t
Nathan Scott3562fd42006-03-14 14:00:35 +1100771xfs_file_aio_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700773 const struct iovec *iov,
774 unsigned long nr_segs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 loff_t pos)
776{
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500777 struct file *file = iocb->ki_filp;
Christoph Hellwigc355c652009-11-14 16:17:18 +0000778 int ioflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 BUG_ON(iocb->ki_pos != pos);
781 if (unlikely(file->f_flags & O_DIRECT))
782 ioflags |= IO_ISDIRECT;
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500783 if (file->f_mode & FMODE_NOCMTIME)
784 ioflags |= IO_INVIS;
785 return xfs_read(XFS_I(file->f_path.dentry->d_inode), iocb, iov,
786 nr_segs, &iocb->ki_pos, ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789STATIC ssize_t
Nathan Scott3562fd42006-03-14 14:00:35 +1100790xfs_file_aio_write(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -0700792 const struct iovec *iov,
793 unsigned long nr_segs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 loff_t pos)
795{
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500796 struct file *file = iocb->ki_filp;
Christoph Hellwigc355c652009-11-14 16:17:18 +0000797 int ioflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500799 BUG_ON(iocb->ki_pos != pos);
800 if (unlikely(file->f_flags & O_DIRECT))
801 ioflags |= IO_ISDIRECT;
802 if (file->f_mode & FMODE_NOCMTIME)
803 ioflags |= IO_INVIS;
804 return xfs_write(XFS_I(file->f_mapping->host), iocb, iov, nr_segs,
805 &iocb->ki_pos, ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808STATIC ssize_t
Nathan Scott1b895842006-03-31 13:08:59 +1000809xfs_file_splice_read(
810 struct file *infilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200811 loff_t *ppos,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200812 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000813 size_t len,
814 unsigned int flags)
815{
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500816 int ioflags = 0;
Nathan Scott1b895842006-03-31 13:08:59 +1000817
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500818 if (infilp->f_mode & FMODE_NOCMTIME)
819 ioflags |= IO_INVIS;
820
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000821 return xfs_splice_read(XFS_I(infilp->f_path.dentry->d_inode),
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500822 infilp, ppos, pipe, len, flags, ioflags);
Nathan Scott1b895842006-03-31 13:08:59 +1000823}
824
825STATIC ssize_t
826xfs_file_splice_write(
Ingo Molnar3a326a22006-04-10 15:18:35 +0200827 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000828 struct file *outfilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200829 loff_t *ppos,
Nathan Scott1b895842006-03-31 13:08:59 +1000830 size_t len,
831 unsigned int flags)
832{
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500833 int ioflags = 0;
Nathan Scott1b895842006-03-31 13:08:59 +1000834
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500835 if (outfilp->f_mode & FMODE_NOCMTIME)
836 ioflags |= IO_INVIS;
837
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000838 return xfs_splice_write(XFS_I(outfilp->f_path.dentry->d_inode),
Christoph Hellwig4d4be482008-12-09 04:47:33 -0500839 pipe, outfilp, ppos, len, flags, ioflags);
Nathan Scott1b895842006-03-31 13:08:59 +1000840}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100843xfs_file_open(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 struct inode *inode,
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100845 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100847 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return -EFBIG;
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100849 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
850 return -EIO;
851 return 0;
852}
853
854STATIC int
855xfs_dir_open(
856 struct inode *inode,
857 struct file *file)
858{
859 struct xfs_inode *ip = XFS_I(inode);
860 int mode;
861 int error;
862
863 error = xfs_file_open(inode, file);
864 if (error)
865 return error;
866
867 /*
868 * If there are any blocks, read-ahead block 0 as we're almost
869 * certain to have the next operation be a read there.
870 */
871 mode = xfs_ilock_map_shared(ip);
872 if (ip->i_d.di_nextents > 0)
873 xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
874 xfs_iunlock(ip, mode);
875 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100879xfs_file_release(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct inode *inode,
881 struct file *filp)
882{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000883 return -xfs_release(XFS_I(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
David Chinner978b7232008-05-19 16:29:46 +1000886/*
887 * We ignore the datasync flag here because a datasync is effectively
888 * identical to an fsync. That is, datasync implies that we need to write
889 * only the metadata needed to be able to access the data that is written
890 * if we crash after the call completes. Hence if we are writing beyond
891 * EOF we have to log the inode size change as well, which makes it a
892 * full fsync. If we don't write beyond EOF, the inode core will be
893 * clean in memory and so we don't need to log the inode, just like
894 * fsync.
895 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100897xfs_file_fsync(
Christoph Hellwig13e6d5c2009-08-31 21:00:31 -0300898 struct file *file,
899 struct dentry *dentry,
900 int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Christoph Hellwigd0800702009-09-26 19:55:04 +0000902 struct xfs_inode *ip = XFS_I(dentry->d_inode);
Christoph Hellwig13e6d5c2009-08-31 21:00:31 -0300903
904 xfs_iflags_clear(ip, XFS_ITRUNCATED);
905 return -xfs_fsync(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100909xfs_file_readdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct file *filp,
911 void *dirent,
912 filldir_t filldir)
913{
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000914 struct inode *inode = filp->f_path.dentry->d_inode;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000915 xfs_inode_t *ip = XFS_I(inode);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000916 int error;
917 size_t bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000919 /*
920 * The Linux API doesn't pass down the total size of the buffer
921 * we read into down to the filesystem. With the filldir concept
922 * it's not needed for correct information, but the XFS dir2 leaf
923 * code wants an estimate of the buffer size to calculate it's
924 * readahead window and size the buffers used for mapping to
925 * physical blocks.
926 *
927 * Try to give it an estimate that's good enough, maybe at some
928 * point we can change the ->readdir prototype to include the
Eric Sandeena9cc7992010-02-03 17:50:13 +0000929 * buffer size. For now we use the current glibc buffer size.
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000930 */
Eric Sandeena9cc7992010-02-03 17:50:13 +0000931 bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000933 error = xfs_readdir(ip, dirent, bufsize,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000934 (xfs_off_t *)&filp->f_pos, filldir);
935 if (error)
936 return -error;
937 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940STATIC int
Nathan Scott3562fd42006-03-14 14:00:35 +1100941xfs_file_mmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 struct file *filp,
943 struct vm_area_struct *vma)
944{
Nathan Scott3562fd42006-03-14 14:00:35 +1100945 vma->vm_ops = &xfs_file_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -0700946 vma->vm_flags |= VM_CAN_NONLINEAR;
Dean Roehrich6fac0cb2005-06-21 14:07:45 +1000947
Nathan Scottfbc14622006-06-09 14:52:13 +1000948 file_accessed(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return 0;
950}
951
David Chinner4f57dbc2007-07-19 16:28:17 +1000952/*
953 * mmap()d file has taken write protection fault and is being made
954 * writable. We can set the page state up correctly for a writable
955 * page, which means we can do correct delalloc accounting (ENOSPC
956 * checking!) and unwritten extent mapping.
957 */
958STATIC int
959xfs_vm_page_mkwrite(
960 struct vm_area_struct *vma,
Nick Pigginc2ec1752009-03-31 15:23:21 -0700961 struct vm_fault *vmf)
David Chinner4f57dbc2007-07-19 16:28:17 +1000962{
Nick Pigginc2ec1752009-03-31 15:23:21 -0700963 return block_page_mkwrite(vma, vmf, xfs_get_blocks);
David Chinner4f57dbc2007-07-19 16:28:17 +1000964}
965
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800966const struct file_operations xfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 .llseek = generic_file_llseek,
968 .read = do_sync_read,
Dean Roehrichbb3f7242005-09-02 15:43:05 +1000969 .write = do_sync_write,
Nathan Scott3562fd42006-03-14 14:00:35 +1100970 .aio_read = xfs_file_aio_read,
971 .aio_write = xfs_file_aio_write,
Nathan Scott1b895842006-03-31 13:08:59 +1000972 .splice_read = xfs_file_splice_read,
973 .splice_write = xfs_file_splice_write,
Nathan Scott3562fd42006-03-14 14:00:35 +1100974 .unlocked_ioctl = xfs_file_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +1100976 .compat_ioctl = xfs_file_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977#endif
Nathan Scott3562fd42006-03-14 14:00:35 +1100978 .mmap = xfs_file_mmap,
979 .open = xfs_file_open,
980 .release = xfs_file_release,
981 .fsync = xfs_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982#ifdef HAVE_FOP_OPEN_EXEC
Nathan Scott3562fd42006-03-14 14:00:35 +1100983 .open_exec = xfs_file_open_exec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984#endif
985};
986
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800987const struct file_operations xfs_dir_file_operations = {
Christoph Hellwigf999a5b2008-11-28 14:23:32 +1100988 .open = xfs_dir_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 .read = generic_read_dir,
Nathan Scott3562fd42006-03-14 14:00:35 +1100990 .readdir = xfs_file_readdir,
Al Viro59af1582008-08-24 07:24:41 -0400991 .llseek = generic_file_llseek,
Nathan Scott3562fd42006-03-14 14:00:35 +1100992 .unlocked_ioctl = xfs_file_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -0700993#ifdef CONFIG_COMPAT
Nathan Scott3562fd42006-03-14 14:00:35 +1100994 .compat_ioctl = xfs_file_compat_ioctl,
Nathan Scottd3870392005-05-06 06:44:46 -0700995#endif
Nathan Scott3562fd42006-03-14 14:00:35 +1100996 .fsync = xfs_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997};
998
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400999static const struct vm_operations_struct xfs_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07001000 .fault = filemap_fault,
David Chinner4f57dbc2007-07-19 16:28:17 +10001001 .page_mkwrite = xfs_vm_page_mkwrite,
Dean Roehrich6fac0cb2005-06-21 14:07:45 +10001002};