dca06131551aafb33e35d55781ec789e38a44608
[pandora-kernel.git] / fs / xfs / linux-2.6 / 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_inum.h"
23 #include "xfs_sb.h"
24 #include "xfs_ag.h"
25 #include "xfs_dir2.h"
26 #include "xfs_trans.h"
27 #include "xfs_mount.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_alloc_btree.h"
30 #include "xfs_ialloc_btree.h"
31 #include "xfs_alloc.h"
32 #include "xfs_btree.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_inode_item.h"
38 #include "xfs_bmap.h"
39 #include "xfs_error.h"
40 #include "xfs_rw.h"
41 #include "xfs_vnodeops.h"
42 #include "xfs_da_btree.h"
43 #include "xfs_ioctl.h"
44 #include "xfs_trace.h"
45
46 #include <linux/dcache.h>
47
48 static const struct vm_operations_struct xfs_file_vm_ops;
49
50 /*
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  */
60 STATIC int
61 xfs_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
99 STATIC int
100 xfs_file_fsync(
101         struct file             *file,
102         int                     datasync)
103 {
104         struct inode            *inode = file->f_mapping->host;
105         struct xfs_inode        *ip = XFS_I(inode);
106         struct xfs_trans        *tp;
107         int                     error = 0;
108         int                     log_flushed = 0;
109
110         xfs_itrace_entry(ip);
111
112         if (XFS_FORCED_SHUTDOWN(ip->i_mount))
113                 return -XFS_ERROR(EIO);
114
115         xfs_iflags_clear(ip, XFS_ITRUNCATED);
116
117         xfs_ioend_wait(ip);
118
119         /*
120          * We always need to make sure that the required inode state is safe on
121          * disk.  The inode might be clean but we still might need to force the
122          * log because of committed transactions that haven't hit the disk yet.
123          * Likewise, there could be unflushed non-transactional changes to the
124          * inode core that have to go to disk and this requires us to issue
125          * a synchronous transaction to capture these changes correctly.
126          *
127          * This code relies on the assumption that if the i_update_core field
128          * of the inode is clear and the inode is unpinned then it is clean
129          * and no action is required.
130          */
131         xfs_ilock(ip, XFS_ILOCK_SHARED);
132
133         /*
134          * First check if the VFS inode is marked dirty.  All the dirtying
135          * of non-transactional updates no goes through mark_inode_dirty*,
136          * which allows us to distinguish beteeen pure timestamp updates
137          * and i_size updates which need to be caught for fdatasync.
138          * After that also theck for the dirty state in the XFS inode, which
139          * might gets cleared when the inode gets written out via the AIL
140          * or xfs_iflush_cluster.
141          */
142         if (((inode->i_state & I_DIRTY_DATASYNC) ||
143             ((inode->i_state & I_DIRTY_SYNC) && !datasync)) &&
144             ip->i_update_core) {
145                 /*
146                  * Kick off a transaction to log the inode core to get the
147                  * updates.  The sync transaction will also force the log.
148                  */
149                 xfs_iunlock(ip, XFS_ILOCK_SHARED);
150                 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
151                 error = xfs_trans_reserve(tp, 0,
152                                 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
153                 if (error) {
154                         xfs_trans_cancel(tp, 0);
155                         return -error;
156                 }
157                 xfs_ilock(ip, XFS_ILOCK_EXCL);
158
159                 /*
160                  * Note - it's possible that we might have pushed ourselves out
161                  * of the way during trans_reserve which would flush the inode.
162                  * But there's no guarantee that the inode buffer has actually
163                  * gone out yet (it's delwri).  Plus the buffer could be pinned
164                  * anyway if it's part of an inode in another recent
165                  * transaction.  So we play it safe and fire off the
166                  * transaction anyway.
167                  */
168                 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
169                 xfs_trans_ihold(tp, ip);
170                 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
171                 xfs_trans_set_sync(tp);
172                 error = _xfs_trans_commit(tp, 0, &log_flushed);
173
174                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
175         } else {
176                 /*
177                  * Timestamps/size haven't changed since last inode flush or
178                  * inode transaction commit.  That means either nothing got
179                  * written or a transaction committed which caught the updates.
180                  * If the latter happened and the transaction hasn't hit the
181                  * disk yet, the inode will be still be pinned.  If it is,
182                  * force the log.
183                  */
184                 if (xfs_ipincount(ip)) {
185                         error = _xfs_log_force_lsn(ip->i_mount,
186                                         ip->i_itemp->ili_last_lsn,
187                                         XFS_LOG_SYNC, &log_flushed);
188                 }
189                 xfs_iunlock(ip, XFS_ILOCK_SHARED);
190         }
191
192         if (ip->i_mount->m_flags & XFS_MOUNT_BARRIER) {
193                 /*
194                  * If the log write didn't issue an ordered tag we need
195                  * to flush the disk cache for the data device now.
196                  */
197                 if (!log_flushed)
198                         xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
199
200                 /*
201                  * If this inode is on the RT dev we need to flush that
202                  * cache as well.
203                  */
204                 if (XFS_IS_REALTIME_INODE(ip))
205                         xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
206         }
207
208         return -error;
209 }
210
211 STATIC ssize_t
212 xfs_file_aio_read(
213         struct kiocb            *iocb,
214         const struct iovec      *iovp,
215         unsigned long           nr_segs,
216         loff_t                  pos)
217 {
218         struct file             *file = iocb->ki_filp;
219         struct inode            *inode = file->f_mapping->host;
220         struct xfs_inode        *ip = XFS_I(inode);
221         struct xfs_mount        *mp = ip->i_mount;
222         size_t                  size = 0;
223         ssize_t                 ret = 0;
224         int                     ioflags = 0;
225         xfs_fsize_t             n;
226         unsigned long           seg;
227
228         XFS_STATS_INC(xs_read_calls);
229
230         BUG_ON(iocb->ki_pos != pos);
231
232         if (unlikely(file->f_flags & O_DIRECT))
233                 ioflags |= IO_ISDIRECT;
234         if (file->f_mode & FMODE_NOCMTIME)
235                 ioflags |= IO_INVIS;
236
237         /* START copy & waste from filemap.c */
238         for (seg = 0; seg < nr_segs; seg++) {
239                 const struct iovec *iv = &iovp[seg];
240
241                 /*
242                  * If any segment has a negative length, or the cumulative
243                  * length ever wraps negative then return -EINVAL.
244                  */
245                 size += iv->iov_len;
246                 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
247                         return XFS_ERROR(-EINVAL);
248         }
249         /* END copy & waste from filemap.c */
250
251         if (unlikely(ioflags & IO_ISDIRECT)) {
252                 xfs_buftarg_t   *target =
253                         XFS_IS_REALTIME_INODE(ip) ?
254                                 mp->m_rtdev_targp : mp->m_ddev_targp;
255                 if ((iocb->ki_pos & target->bt_smask) ||
256                     (size & target->bt_smask)) {
257                         if (iocb->ki_pos == ip->i_size)
258                                 return 0;
259                         return -XFS_ERROR(EINVAL);
260                 }
261         }
262
263         n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
264         if (n <= 0 || size == 0)
265                 return 0;
266
267         if (n < size)
268                 size = n;
269
270         if (XFS_FORCED_SHUTDOWN(mp))
271                 return -EIO;
272
273         if (unlikely(ioflags & IO_ISDIRECT))
274                 mutex_lock(&inode->i_mutex);
275         xfs_ilock(ip, XFS_IOLOCK_SHARED);
276
277         if (unlikely(ioflags & IO_ISDIRECT)) {
278                 if (inode->i_mapping->nrpages) {
279                         ret = -xfs_flushinval_pages(ip,
280                                         (iocb->ki_pos & PAGE_CACHE_MASK),
281                                         -1, FI_REMAPF_LOCKED);
282                 }
283                 mutex_unlock(&inode->i_mutex);
284                 if (ret) {
285                         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
286                         return ret;
287                 }
288         }
289
290         trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags);
291
292         ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos);
293         if (ret > 0)
294                 XFS_STATS_ADD(xs_read_bytes, ret);
295
296         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
297         return ret;
298 }
299
300 STATIC ssize_t
301 xfs_file_splice_read(
302         struct file             *infilp,
303         loff_t                  *ppos,
304         struct pipe_inode_info  *pipe,
305         size_t                  count,
306         unsigned int            flags)
307 {
308         struct xfs_inode        *ip = XFS_I(infilp->f_mapping->host);
309         int                     ioflags = 0;
310         ssize_t                 ret;
311
312         XFS_STATS_INC(xs_read_calls);
313
314         if (infilp->f_mode & FMODE_NOCMTIME)
315                 ioflags |= IO_INVIS;
316
317         if (XFS_FORCED_SHUTDOWN(ip->i_mount))
318                 return -EIO;
319
320         xfs_ilock(ip, XFS_IOLOCK_SHARED);
321
322         trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
323
324         ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
325         if (ret > 0)
326                 XFS_STATS_ADD(xs_read_bytes, ret);
327
328         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
329         return ret;
330 }
331
332 STATIC ssize_t
333 xfs_file_splice_write(
334         struct pipe_inode_info  *pipe,
335         struct file             *outfilp,
336         loff_t                  *ppos,
337         size_t                  count,
338         unsigned int            flags)
339 {
340         struct inode            *inode = outfilp->f_mapping->host;
341         struct xfs_inode        *ip = XFS_I(inode);
342         xfs_fsize_t             isize, new_size;
343         int                     ioflags = 0;
344         ssize_t                 ret;
345
346         XFS_STATS_INC(xs_write_calls);
347
348         if (outfilp->f_mode & FMODE_NOCMTIME)
349                 ioflags |= IO_INVIS;
350
351         if (XFS_FORCED_SHUTDOWN(ip->i_mount))
352                 return -EIO;
353
354         xfs_ilock(ip, XFS_IOLOCK_EXCL);
355
356         new_size = *ppos + count;
357
358         xfs_ilock(ip, XFS_ILOCK_EXCL);
359         if (new_size > ip->i_size)
360                 ip->i_new_size = new_size;
361         xfs_iunlock(ip, XFS_ILOCK_EXCL);
362
363         trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
364
365         ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
366         if (ret > 0)
367                 XFS_STATS_ADD(xs_write_bytes, ret);
368
369         isize = i_size_read(inode);
370         if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
371                 *ppos = isize;
372
373         if (*ppos > ip->i_size) {
374                 xfs_ilock(ip, XFS_ILOCK_EXCL);
375                 if (*ppos > ip->i_size)
376                         ip->i_size = *ppos;
377                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
378         }
379
380         if (ip->i_new_size) {
381                 xfs_ilock(ip, XFS_ILOCK_EXCL);
382                 ip->i_new_size = 0;
383                 if (ip->i_d.di_size > ip->i_size)
384                         ip->i_d.di_size = ip->i_size;
385                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
386         }
387         xfs_iunlock(ip, XFS_IOLOCK_EXCL);
388         return ret;
389 }
390
391 /*
392  * This routine is called to handle zeroing any space in the last
393  * block of the file that is beyond the EOF.  We do this since the
394  * size is being increased without writing anything to that block
395  * and we don't want anyone to read the garbage on the disk.
396  */
397 STATIC int                              /* error (positive) */
398 xfs_zero_last_block(
399         xfs_inode_t     *ip,
400         xfs_fsize_t     offset,
401         xfs_fsize_t     isize)
402 {
403         xfs_fileoff_t   last_fsb;
404         xfs_mount_t     *mp = ip->i_mount;
405         int             nimaps;
406         int             zero_offset;
407         int             zero_len;
408         int             error = 0;
409         xfs_bmbt_irec_t imap;
410
411         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
412
413         zero_offset = XFS_B_FSB_OFFSET(mp, isize);
414         if (zero_offset == 0) {
415                 /*
416                  * There are no extra bytes in the last block on disk to
417                  * zero, so return.
418                  */
419                 return 0;
420         }
421
422         last_fsb = XFS_B_TO_FSBT(mp, isize);
423         nimaps = 1;
424         error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
425                           &nimaps, NULL, NULL);
426         if (error) {
427                 return error;
428         }
429         ASSERT(nimaps > 0);
430         /*
431          * If the block underlying isize is just a hole, then there
432          * is nothing to zero.
433          */
434         if (imap.br_startblock == HOLESTARTBLOCK) {
435                 return 0;
436         }
437         /*
438          * Zero the part of the last block beyond the EOF, and write it
439          * out sync.  We need to drop the ilock while we do this so we
440          * don't deadlock when the buffer cache calls back to us.
441          */
442         xfs_iunlock(ip, XFS_ILOCK_EXCL);
443
444         zero_len = mp->m_sb.sb_blocksize - zero_offset;
445         if (isize + zero_len > offset)
446                 zero_len = offset - isize;
447         error = xfs_iozero(ip, isize, zero_len);
448
449         xfs_ilock(ip, XFS_ILOCK_EXCL);
450         ASSERT(error >= 0);
451         return error;
452 }
453
454 /*
455  * Zero any on disk space between the current EOF and the new,
456  * larger EOF.  This handles the normal case of zeroing the remainder
457  * of the last block in the file and the unusual case of zeroing blocks
458  * out beyond the size of the file.  This second case only happens
459  * with fixed size extents and when the system crashes before the inode
460  * size was updated but after blocks were allocated.  If fill is set,
461  * then any holes in the range are filled and zeroed.  If not, the holes
462  * are left alone as holes.
463  */
464
465 int                                     /* error (positive) */
466 xfs_zero_eof(
467         xfs_inode_t     *ip,
468         xfs_off_t       offset,         /* starting I/O offset */
469         xfs_fsize_t     isize)          /* current inode size */
470 {
471         xfs_mount_t     *mp = ip->i_mount;
472         xfs_fileoff_t   start_zero_fsb;
473         xfs_fileoff_t   end_zero_fsb;
474         xfs_fileoff_t   zero_count_fsb;
475         xfs_fileoff_t   last_fsb;
476         xfs_fileoff_t   zero_off;
477         xfs_fsize_t     zero_len;
478         int             nimaps;
479         int             error = 0;
480         xfs_bmbt_irec_t imap;
481
482         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
483         ASSERT(offset > isize);
484
485         /*
486          * First handle zeroing the block on which isize resides.
487          * We only zero a part of that block so it is handled specially.
488          */
489         error = xfs_zero_last_block(ip, offset, isize);
490         if (error) {
491                 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
492                 return error;
493         }
494
495         /*
496          * Calculate the range between the new size and the old
497          * where blocks needing to be zeroed may exist.  To get the
498          * block where the last byte in the file currently resides,
499          * we need to subtract one from the size and truncate back
500          * to a block boundary.  We subtract 1 in case the size is
501          * exactly on a block boundary.
502          */
503         last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
504         start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
505         end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
506         ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
507         if (last_fsb == end_zero_fsb) {
508                 /*
509                  * The size was only incremented on its last block.
510                  * We took care of that above, so just return.
511                  */
512                 return 0;
513         }
514
515         ASSERT(start_zero_fsb <= end_zero_fsb);
516         while (start_zero_fsb <= end_zero_fsb) {
517                 nimaps = 1;
518                 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
519                 error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
520                                   0, NULL, 0, &imap, &nimaps, NULL, NULL);
521                 if (error) {
522                         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
523                         return error;
524                 }
525                 ASSERT(nimaps > 0);
526
527                 if (imap.br_state == XFS_EXT_UNWRITTEN ||
528                     imap.br_startblock == HOLESTARTBLOCK) {
529                         /*
530                          * This loop handles initializing pages that were
531                          * partially initialized by the code below this
532                          * loop. It basically zeroes the part of the page
533                          * that sits on a hole and sets the page as P_HOLE
534                          * and calls remapf if it is a mapped file.
535                          */
536                         start_zero_fsb = imap.br_startoff + imap.br_blockcount;
537                         ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
538                         continue;
539                 }
540
541                 /*
542                  * There are blocks we need to zero.
543                  * Drop the inode lock while we're doing the I/O.
544                  * We'll still have the iolock to protect us.
545                  */
546                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
547
548                 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
549                 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
550
551                 if ((zero_off + zero_len) > offset)
552                         zero_len = offset - zero_off;
553
554                 error = xfs_iozero(ip, zero_off, zero_len);
555                 if (error) {
556                         goto out_lock;
557                 }
558
559                 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
560                 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
561
562                 xfs_ilock(ip, XFS_ILOCK_EXCL);
563         }
564
565         return 0;
566
567 out_lock:
568         xfs_ilock(ip, XFS_ILOCK_EXCL);
569         ASSERT(error >= 0);
570         return error;
571 }
572
573 STATIC ssize_t
574 xfs_file_aio_write(
575         struct kiocb            *iocb,
576         const struct iovec      *iovp,
577         unsigned long           nr_segs,
578         loff_t                  pos)
579 {
580         struct file             *file = iocb->ki_filp;
581         struct address_space    *mapping = file->f_mapping;
582         struct inode            *inode = mapping->host;
583         struct xfs_inode        *ip = XFS_I(inode);
584         struct xfs_mount        *mp = ip->i_mount;
585         ssize_t                 ret = 0, error = 0;
586         int                     ioflags = 0;
587         xfs_fsize_t             isize, new_size;
588         int                     iolock;
589         size_t                  ocount = 0, count;
590         int                     need_i_mutex;
591
592         XFS_STATS_INC(xs_write_calls);
593
594         BUG_ON(iocb->ki_pos != pos);
595
596         if (unlikely(file->f_flags & O_DIRECT))
597                 ioflags |= IO_ISDIRECT;
598         if (file->f_mode & FMODE_NOCMTIME)
599                 ioflags |= IO_INVIS;
600
601         error = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
602         if (error)
603                 return error;
604
605         count = ocount;
606         if (count == 0)
607                 return 0;
608
609         xfs_wait_for_freeze(mp, SB_FREEZE_WRITE);
610
611         if (XFS_FORCED_SHUTDOWN(mp))
612                 return -EIO;
613
614 relock:
615         if (ioflags & IO_ISDIRECT) {
616                 iolock = XFS_IOLOCK_SHARED;
617                 need_i_mutex = 0;
618         } else {
619                 iolock = XFS_IOLOCK_EXCL;
620                 need_i_mutex = 1;
621                 mutex_lock(&inode->i_mutex);
622         }
623
624         xfs_ilock(ip, XFS_ILOCK_EXCL|iolock);
625
626 start:
627         error = -generic_write_checks(file, &pos, &count,
628                                         S_ISBLK(inode->i_mode));
629         if (error) {
630                 xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock);
631                 goto out_unlock_mutex;
632         }
633
634         if (ioflags & IO_ISDIRECT) {
635                 xfs_buftarg_t   *target =
636                         XFS_IS_REALTIME_INODE(ip) ?
637                                 mp->m_rtdev_targp : mp->m_ddev_targp;
638
639                 if ((pos & target->bt_smask) || (count & target->bt_smask)) {
640                         xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock);
641                         return XFS_ERROR(-EINVAL);
642                 }
643
644                 if (!need_i_mutex && (mapping->nrpages || pos > ip->i_size)) {
645                         xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock);
646                         iolock = XFS_IOLOCK_EXCL;
647                         need_i_mutex = 1;
648                         mutex_lock(&inode->i_mutex);
649                         xfs_ilock(ip, XFS_ILOCK_EXCL|iolock);
650                         goto start;
651                 }
652         }
653
654         new_size = pos + count;
655         if (new_size > ip->i_size)
656                 ip->i_new_size = new_size;
657
658         if (likely(!(ioflags & IO_INVIS)))
659                 file_update_time(file);
660
661         /*
662          * If the offset is beyond the size of the file, we have a couple
663          * of things to do. First, if there is already space allocated
664          * we need to either create holes or zero the disk or ...
665          *
666          * If there is a page where the previous size lands, we need
667          * to zero it out up to the new size.
668          */
669
670         if (pos > ip->i_size) {
671                 error = xfs_zero_eof(ip, pos, ip->i_size);
672                 if (error) {
673                         xfs_iunlock(ip, XFS_ILOCK_EXCL);
674                         goto out_unlock_internal;
675                 }
676         }
677         xfs_iunlock(ip, XFS_ILOCK_EXCL);
678
679         /*
680          * If we're writing the file then make sure to clear the
681          * setuid and setgid bits if the process is not being run
682          * by root.  This keeps people from modifying setuid and
683          * setgid binaries.
684          */
685         error = -file_remove_suid(file);
686         if (unlikely(error))
687                 goto out_unlock_internal;
688
689         /* We can write back this queue in page reclaim */
690         current->backing_dev_info = mapping->backing_dev_info;
691
692         if ((ioflags & IO_ISDIRECT)) {
693                 if (mapping->nrpages) {
694                         WARN_ON(need_i_mutex == 0);
695                         error = xfs_flushinval_pages(ip,
696                                         (pos & PAGE_CACHE_MASK),
697                                         -1, FI_REMAPF_LOCKED);
698                         if (error)
699                                 goto out_unlock_internal;
700                 }
701
702                 if (need_i_mutex) {
703                         /* demote the lock now the cached pages are gone */
704                         xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
705                         mutex_unlock(&inode->i_mutex);
706
707                         iolock = XFS_IOLOCK_SHARED;
708                         need_i_mutex = 0;
709                 }
710
711                 trace_xfs_file_direct_write(ip, count, iocb->ki_pos, ioflags);
712                 ret = generic_file_direct_write(iocb, iovp,
713                                 &nr_segs, pos, &iocb->ki_pos, count, ocount);
714
715                 /*
716                  * direct-io write to a hole: fall through to buffered I/O
717                  * for completing the rest of the request.
718                  */
719                 if (ret >= 0 && ret != count) {
720                         XFS_STATS_ADD(xs_write_bytes, ret);
721
722                         pos += ret;
723                         count -= ret;
724
725                         ioflags &= ~IO_ISDIRECT;
726                         xfs_iunlock(ip, iolock);
727                         goto relock;
728                 }
729         } else {
730                 int enospc = 0;
731                 ssize_t ret2 = 0;
732
733 write_retry:
734                 trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, ioflags);
735                 ret2 = generic_file_buffered_write(iocb, iovp, nr_segs,
736                                 pos, &iocb->ki_pos, count, ret);
737                 /*
738                  * if we just got an ENOSPC, flush the inode now we
739                  * aren't holding any page locks and retry *once*
740                  */
741                 if (ret2 == -ENOSPC && !enospc) {
742                         error = xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
743                         if (error)
744                                 goto out_unlock_internal;
745                         enospc = 1;
746                         goto write_retry;
747                 }
748                 ret = ret2;
749         }
750
751         current->backing_dev_info = NULL;
752
753         isize = i_size_read(inode);
754         if (unlikely(ret < 0 && ret != -EFAULT && iocb->ki_pos > isize))
755                 iocb->ki_pos = isize;
756
757         if (iocb->ki_pos > ip->i_size) {
758                 xfs_ilock(ip, XFS_ILOCK_EXCL);
759                 if (iocb->ki_pos > ip->i_size)
760                         ip->i_size = iocb->ki_pos;
761                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
762         }
763
764         error = -ret;
765         if (ret <= 0)
766                 goto out_unlock_internal;
767
768         XFS_STATS_ADD(xs_write_bytes, ret);
769
770         /* Handle various SYNC-type writes */
771         if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
772                 loff_t end = pos + ret - 1;
773                 int error2;
774
775                 xfs_iunlock(ip, iolock);
776                 if (need_i_mutex)
777                         mutex_unlock(&inode->i_mutex);
778
779                 error2 = filemap_write_and_wait_range(mapping, pos, end);
780                 if (!error)
781                         error = error2;
782                 if (need_i_mutex)
783                         mutex_lock(&inode->i_mutex);
784                 xfs_ilock(ip, iolock);
785
786                 error2 = -xfs_file_fsync(file,
787                                          (file->f_flags & __O_SYNC) ? 0 : 1);
788                 if (!error)
789                         error = error2;
790         }
791
792  out_unlock_internal:
793         if (ip->i_new_size) {
794                 xfs_ilock(ip, XFS_ILOCK_EXCL);
795                 ip->i_new_size = 0;
796                 /*
797                  * If this was a direct or synchronous I/O that failed (such
798                  * as ENOSPC) then part of the I/O may have been written to
799                  * disk before the error occured.  In this case the on-disk
800                  * file size may have been adjusted beyond the in-memory file
801                  * size and now needs to be truncated back.
802                  */
803                 if (ip->i_d.di_size > ip->i_size)
804                         ip->i_d.di_size = ip->i_size;
805                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
806         }
807         xfs_iunlock(ip, iolock);
808  out_unlock_mutex:
809         if (need_i_mutex)
810                 mutex_unlock(&inode->i_mutex);
811         return -error;
812 }
813
814 STATIC int
815 xfs_file_open(
816         struct inode    *inode,
817         struct file     *file)
818 {
819         if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
820                 return -EFBIG;
821         if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
822                 return -EIO;
823         return 0;
824 }
825
826 STATIC int
827 xfs_dir_open(
828         struct inode    *inode,
829         struct file     *file)
830 {
831         struct xfs_inode *ip = XFS_I(inode);
832         int             mode;
833         int             error;
834
835         error = xfs_file_open(inode, file);
836         if (error)
837                 return error;
838
839         /*
840          * If there are any blocks, read-ahead block 0 as we're almost
841          * certain to have the next operation be a read there.
842          */
843         mode = xfs_ilock_map_shared(ip);
844         if (ip->i_d.di_nextents > 0)
845                 xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
846         xfs_iunlock(ip, mode);
847         return 0;
848 }
849
850 STATIC int
851 xfs_file_release(
852         struct inode    *inode,
853         struct file     *filp)
854 {
855         return -xfs_release(XFS_I(inode));
856 }
857
858 STATIC int
859 xfs_file_readdir(
860         struct file     *filp,
861         void            *dirent,
862         filldir_t       filldir)
863 {
864         struct inode    *inode = filp->f_path.dentry->d_inode;
865         xfs_inode_t     *ip = XFS_I(inode);
866         int             error;
867         size_t          bufsize;
868
869         /*
870          * The Linux API doesn't pass down the total size of the buffer
871          * we read into down to the filesystem.  With the filldir concept
872          * it's not needed for correct information, but the XFS dir2 leaf
873          * code wants an estimate of the buffer size to calculate it's
874          * readahead window and size the buffers used for mapping to
875          * physical blocks.
876          *
877          * Try to give it an estimate that's good enough, maybe at some
878          * point we can change the ->readdir prototype to include the
879          * buffer size.  For now we use the current glibc buffer size.
880          */
881         bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
882
883         error = xfs_readdir(ip, dirent, bufsize,
884                                 (xfs_off_t *)&filp->f_pos, filldir);
885         if (error)
886                 return -error;
887         return 0;
888 }
889
890 STATIC int
891 xfs_file_mmap(
892         struct file     *filp,
893         struct vm_area_struct *vma)
894 {
895         vma->vm_ops = &xfs_file_vm_ops;
896         vma->vm_flags |= VM_CAN_NONLINEAR;
897
898         file_accessed(filp);
899         return 0;
900 }
901
902 /*
903  * mmap()d file has taken write protection fault and is being made
904  * writable. We can set the page state up correctly for a writable
905  * page, which means we can do correct delalloc accounting (ENOSPC
906  * checking!) and unwritten extent mapping.
907  */
908 STATIC int
909 xfs_vm_page_mkwrite(
910         struct vm_area_struct   *vma,
911         struct vm_fault         *vmf)
912 {
913         return block_page_mkwrite(vma, vmf, xfs_get_blocks);
914 }
915
916 const struct file_operations xfs_file_operations = {
917         .llseek         = generic_file_llseek,
918         .read           = do_sync_read,
919         .write          = do_sync_write,
920         .aio_read       = xfs_file_aio_read,
921         .aio_write      = xfs_file_aio_write,
922         .splice_read    = xfs_file_splice_read,
923         .splice_write   = xfs_file_splice_write,
924         .unlocked_ioctl = xfs_file_ioctl,
925 #ifdef CONFIG_COMPAT
926         .compat_ioctl   = xfs_file_compat_ioctl,
927 #endif
928         .mmap           = xfs_file_mmap,
929         .open           = xfs_file_open,
930         .release        = xfs_file_release,
931         .fsync          = xfs_file_fsync,
932 #ifdef HAVE_FOP_OPEN_EXEC
933         .open_exec      = xfs_file_open_exec,
934 #endif
935 };
936
937 const struct file_operations xfs_dir_file_operations = {
938         .open           = xfs_dir_open,
939         .read           = generic_read_dir,
940         .readdir        = xfs_file_readdir,
941         .llseek         = generic_file_llseek,
942         .unlocked_ioctl = xfs_file_ioctl,
943 #ifdef CONFIG_COMPAT
944         .compat_ioctl   = xfs_file_compat_ioctl,
945 #endif
946         .fsync          = xfs_file_fsync,
947 };
948
949 static const struct vm_operations_struct xfs_file_vm_ops = {
950         .fault          = filemap_fault,
951         .page_mkwrite   = xfs_vm_page_mkwrite,
952 };