Merge branch 'for-linus' of git://oss.sgi.com:8090/xfs/xfs-2.6
[linux-3.10.git] / fs / xfs / linux-2.6 / xfs_lrw.c
1 /*
2  * Copyright (c) 2000-2003,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_trans.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #include "xfs_dir2.h"
27 #include "xfs_alloc.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_quota.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
38 #include "xfs_bmap.h"
39 #include "xfs_btree.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_error.h"
43 #include "xfs_itable.h"
44 #include "xfs_rw.h"
45 #include "xfs_acl.h"
46 #include "xfs_attr.h"
47 #include "xfs_inode_item.h"
48 #include "xfs_buf_item.h"
49 #include "xfs_utils.h"
50 #include "xfs_iomap.h"
51 #include "xfs_vnodeops.h"
52
53 #include <linux/capability.h>
54 #include <linux/writeback.h>
55
56
57 #if defined(XFS_RW_TRACE)
58 void
59 xfs_rw_enter_trace(
60         int                     tag,
61         xfs_iocore_t            *io,
62         void                    *data,
63         size_t                  segs,
64         loff_t                  offset,
65         int                     ioflags)
66 {
67         xfs_inode_t     *ip = XFS_IO_INODE(io);
68
69         if (ip->i_rwtrace == NULL)
70                 return;
71         ktrace_enter(ip->i_rwtrace,
72                 (void *)(unsigned long)tag,
73                 (void *)ip,
74                 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
75                 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
76                 (void *)data,
77                 (void *)((unsigned long)segs),
78                 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
79                 (void *)((unsigned long)(offset & 0xffffffff)),
80                 (void *)((unsigned long)ioflags),
81                 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
82                 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
83                 (void *)((unsigned long)current_pid()),
84                 (void *)NULL,
85                 (void *)NULL,
86                 (void *)NULL,
87                 (void *)NULL);
88 }
89
90 void
91 xfs_inval_cached_trace(
92         xfs_iocore_t    *io,
93         xfs_off_t       offset,
94         xfs_off_t       len,
95         xfs_off_t       first,
96         xfs_off_t       last)
97 {
98         xfs_inode_t     *ip = XFS_IO_INODE(io);
99
100         if (ip->i_rwtrace == NULL)
101                 return;
102         ktrace_enter(ip->i_rwtrace,
103                 (void *)(__psint_t)XFS_INVAL_CACHED,
104                 (void *)ip,
105                 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
106                 (void *)((unsigned long)(offset & 0xffffffff)),
107                 (void *)((unsigned long)((len >> 32) & 0xffffffff)),
108                 (void *)((unsigned long)(len & 0xffffffff)),
109                 (void *)((unsigned long)((first >> 32) & 0xffffffff)),
110                 (void *)((unsigned long)(first & 0xffffffff)),
111                 (void *)((unsigned long)((last >> 32) & 0xffffffff)),
112                 (void *)((unsigned long)(last & 0xffffffff)),
113                 (void *)((unsigned long)current_pid()),
114                 (void *)NULL,
115                 (void *)NULL,
116                 (void *)NULL,
117                 (void *)NULL,
118                 (void *)NULL);
119 }
120 #endif
121
122 /*
123  *      xfs_iozero
124  *
125  *      xfs_iozero clears the specified range of buffer supplied,
126  *      and marks all the affected blocks as valid and modified.  If
127  *      an affected block is not allocated, it will be allocated.  If
128  *      an affected block is not completely overwritten, and is not
129  *      valid before the operation, it will be read from disk before
130  *      being partially zeroed.
131  */
132 STATIC int
133 xfs_iozero(
134         struct inode            *ip,    /* inode                        */
135         loff_t                  pos,    /* offset in file               */
136         size_t                  count)  /* size of data to zero         */
137 {
138         struct page             *page;
139         struct address_space    *mapping;
140         int                     status;
141
142         mapping = ip->i_mapping;
143         do {
144                 unsigned offset, bytes;
145                 void *fsdata;
146
147                 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
148                 bytes = PAGE_CACHE_SIZE - offset;
149                 if (bytes > count)
150                         bytes = count;
151
152                 status = pagecache_write_begin(NULL, mapping, pos, bytes,
153                                         AOP_FLAG_UNINTERRUPTIBLE,
154                                         &page, &fsdata);
155                 if (status)
156                         break;
157
158                 zero_user_page(page, offset, bytes, KM_USER0);
159
160                 status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
161                                         page, fsdata);
162                 WARN_ON(status <= 0); /* can't return less than zero! */
163                 pos += bytes;
164                 count -= bytes;
165                 status = 0;
166         } while (count);
167
168         return (-status);
169 }
170
171 ssize_t                 /* bytes read, or (-)  error */
172 xfs_read(
173         xfs_inode_t             *ip,
174         struct kiocb            *iocb,
175         const struct iovec      *iovp,
176         unsigned int            segs,
177         loff_t                  *offset,
178         int                     ioflags)
179 {
180         struct file             *file = iocb->ki_filp;
181         struct inode            *inode = file->f_mapping->host;
182         bhv_vnode_t             *vp = XFS_ITOV(ip);
183         xfs_mount_t             *mp = ip->i_mount;
184         size_t                  size = 0;
185         ssize_t                 ret = 0;
186         xfs_fsize_t             n;
187         unsigned long           seg;
188
189
190         XFS_STATS_INC(xs_read_calls);
191
192         /* START copy & waste from filemap.c */
193         for (seg = 0; seg < segs; seg++) {
194                 const struct iovec *iv = &iovp[seg];
195
196                 /*
197                  * If any segment has a negative length, or the cumulative
198                  * length ever wraps negative then return -EINVAL.
199                  */
200                 size += iv->iov_len;
201                 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
202                         return XFS_ERROR(-EINVAL);
203         }
204         /* END copy & waste from filemap.c */
205
206         if (unlikely(ioflags & IO_ISDIRECT)) {
207                 xfs_buftarg_t   *target =
208                         (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
209                                 mp->m_rtdev_targp : mp->m_ddev_targp;
210                 if ((*offset & target->bt_smask) ||
211                     (size & target->bt_smask)) {
212                         if (*offset == ip->i_size) {
213                                 return (0);
214                         }
215                         return -XFS_ERROR(EINVAL);
216                 }
217         }
218
219         n = XFS_MAXIOFFSET(mp) - *offset;
220         if ((n <= 0) || (size == 0))
221                 return 0;
222
223         if (n < size)
224                 size = n;
225
226         if (XFS_FORCED_SHUTDOWN(mp))
227                 return -EIO;
228
229         if (unlikely(ioflags & IO_ISDIRECT))
230                 mutex_lock(&inode->i_mutex);
231         xfs_ilock(ip, XFS_IOLOCK_SHARED);
232
233         if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
234                 bhv_vrwlock_t locktype = VRWLOCK_READ;
235                 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
236
237                 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ, vp, *offset, size,
238                                         dmflags, &locktype);
239                 if (ret) {
240                         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
241                         if (unlikely(ioflags & IO_ISDIRECT))
242                                 mutex_unlock(&inode->i_mutex);
243                         return ret;
244                 }
245         }
246
247         if (unlikely(ioflags & IO_ISDIRECT)) {
248                 if (VN_CACHED(vp))
249                         ret = xfs_flushinval_pages(ip,
250                                         ctooff(offtoct(*offset)),
251                                         -1, FI_REMAPF_LOCKED);
252                 mutex_unlock(&inode->i_mutex);
253                 if (ret) {
254                         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
255                         return ret;
256                 }
257         }
258
259         xfs_rw_enter_trace(XFS_READ_ENTER, &ip->i_iocore,
260                                 (void *)iovp, segs, *offset, ioflags);
261
262         iocb->ki_pos = *offset;
263         ret = generic_file_aio_read(iocb, iovp, segs, *offset);
264         if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
265                 ret = wait_on_sync_kiocb(iocb);
266         if (ret > 0)
267                 XFS_STATS_ADD(xs_read_bytes, ret);
268
269         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
270         return ret;
271 }
272
273 ssize_t
274 xfs_splice_read(
275         xfs_inode_t             *ip,
276         struct file             *infilp,
277         loff_t                  *ppos,
278         struct pipe_inode_info  *pipe,
279         size_t                  count,
280         int                     flags,
281         int                     ioflags)
282 {
283         bhv_vnode_t             *vp = XFS_ITOV(ip);
284         xfs_mount_t             *mp = ip->i_mount;
285         ssize_t                 ret;
286
287         XFS_STATS_INC(xs_read_calls);
288         if (XFS_FORCED_SHUTDOWN(ip->i_mount))
289                 return -EIO;
290
291         xfs_ilock(ip, XFS_IOLOCK_SHARED);
292
293         if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
294                 bhv_vrwlock_t locktype = VRWLOCK_READ;
295                 int error;
296
297                 error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, *ppos, count,
298                                         FILP_DELAY_FLAG(infilp), &locktype);
299                 if (error) {
300                         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
301                         return -error;
302                 }
303         }
304         xfs_rw_enter_trace(XFS_SPLICE_READ_ENTER, &ip->i_iocore,
305                            pipe, count, *ppos, ioflags);
306         ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
307         if (ret > 0)
308                 XFS_STATS_ADD(xs_read_bytes, ret);
309
310         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
311         return ret;
312 }
313
314 ssize_t
315 xfs_splice_write(
316         xfs_inode_t             *ip,
317         struct pipe_inode_info  *pipe,
318         struct file             *outfilp,
319         loff_t                  *ppos,
320         size_t                  count,
321         int                     flags,
322         int                     ioflags)
323 {
324         bhv_vnode_t             *vp = XFS_ITOV(ip);
325         xfs_mount_t             *mp = ip->i_mount;
326         xfs_iocore_t            *io = &ip->i_iocore;
327         ssize_t                 ret;
328         struct inode            *inode = outfilp->f_mapping->host;
329         xfs_fsize_t             isize, new_size;
330
331         XFS_STATS_INC(xs_write_calls);
332         if (XFS_FORCED_SHUTDOWN(ip->i_mount))
333                 return -EIO;
334
335         xfs_ilock(ip, XFS_IOLOCK_EXCL);
336
337         if (DM_EVENT_ENABLED(ip, DM_EVENT_WRITE) && !(ioflags & IO_INVIS)) {
338                 bhv_vrwlock_t locktype = VRWLOCK_WRITE;
339                 int error;
340
341                 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp, *ppos, count,
342                                         FILP_DELAY_FLAG(outfilp), &locktype);
343                 if (error) {
344                         xfs_iunlock(ip, XFS_IOLOCK_EXCL);
345                         return -error;
346                 }
347         }
348
349         new_size = *ppos + count;
350
351         xfs_ilock(ip, XFS_ILOCK_EXCL);
352         if (new_size > ip->i_size)
353                 io->io_new_size = new_size;
354         xfs_iunlock(ip, XFS_ILOCK_EXCL);
355
356         xfs_rw_enter_trace(XFS_SPLICE_WRITE_ENTER, &ip->i_iocore,
357                            pipe, count, *ppos, ioflags);
358         ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
359         if (ret > 0)
360                 XFS_STATS_ADD(xs_write_bytes, ret);
361
362         isize = i_size_read(inode);
363         if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
364                 *ppos = isize;
365
366         if (*ppos > ip->i_size) {
367                 xfs_ilock(ip, XFS_ILOCK_EXCL);
368                 if (*ppos > ip->i_size)
369                         ip->i_size = *ppos;
370                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
371         }
372
373         if (io->io_new_size) {
374                 xfs_ilock(ip, XFS_ILOCK_EXCL);
375                 io->io_new_size = 0;
376                 if (ip->i_d.di_size > ip->i_size)
377                         ip->i_d.di_size = ip->i_size;
378                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
379         }
380         xfs_iunlock(ip, XFS_IOLOCK_EXCL);
381         return ret;
382 }
383
384 /*
385  * This routine is called to handle zeroing any space in the last
386  * block of the file that is beyond the EOF.  We do this since the
387  * size is being increased without writing anything to that block
388  * and we don't want anyone to read the garbage on the disk.
389  */
390 STATIC int                              /* error (positive) */
391 xfs_zero_last_block(
392         struct inode    *ip,
393         xfs_iocore_t    *io,
394         xfs_fsize_t     offset,
395         xfs_fsize_t     isize)
396 {
397         xfs_fileoff_t   last_fsb;
398         xfs_mount_t     *mp = io->io_mount;
399         int             nimaps;
400         int             zero_offset;
401         int             zero_len;
402         int             error = 0;
403         xfs_bmbt_irec_t imap;
404
405         ASSERT(ismrlocked(io->io_lock, MR_UPDATE) != 0);
406
407         zero_offset = XFS_B_FSB_OFFSET(mp, isize);
408         if (zero_offset == 0) {
409                 /*
410                  * There are no extra bytes in the last block on disk to
411                  * zero, so return.
412                  */
413                 return 0;
414         }
415
416         last_fsb = XFS_B_TO_FSBT(mp, isize);
417         nimaps = 1;
418         error = XFS_BMAPI(mp, NULL, io, last_fsb, 1, 0, NULL, 0, &imap,
419                           &nimaps, NULL, NULL);
420         if (error) {
421                 return error;
422         }
423         ASSERT(nimaps > 0);
424         /*
425          * If the block underlying isize is just a hole, then there
426          * is nothing to zero.
427          */
428         if (imap.br_startblock == HOLESTARTBLOCK) {
429                 return 0;
430         }
431         /*
432          * Zero the part of the last block beyond the EOF, and write it
433          * out sync.  We need to drop the ilock while we do this so we
434          * don't deadlock when the buffer cache calls back to us.
435          */
436         XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
437
438         zero_len = mp->m_sb.sb_blocksize - zero_offset;
439         if (isize + zero_len > offset)
440                 zero_len = offset - isize;
441         error = xfs_iozero(ip, isize, zero_len);
442
443         XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
444         ASSERT(error >= 0);
445         return error;
446 }
447
448 /*
449  * Zero any on disk space between the current EOF and the new,
450  * larger EOF.  This handles the normal case of zeroing the remainder
451  * of the last block in the file and the unusual case of zeroing blocks
452  * out beyond the size of the file.  This second case only happens
453  * with fixed size extents and when the system crashes before the inode
454  * size was updated but after blocks were allocated.  If fill is set,
455  * then any holes in the range are filled and zeroed.  If not, the holes
456  * are left alone as holes.
457  */
458
459 int                                     /* error (positive) */
460 xfs_zero_eof(
461         bhv_vnode_t     *vp,
462         xfs_iocore_t    *io,
463         xfs_off_t       offset,         /* starting I/O offset */
464         xfs_fsize_t     isize)          /* current inode size */
465 {
466         struct inode    *ip = vn_to_inode(vp);
467         xfs_fileoff_t   start_zero_fsb;
468         xfs_fileoff_t   end_zero_fsb;
469         xfs_fileoff_t   zero_count_fsb;
470         xfs_fileoff_t   last_fsb;
471         xfs_fileoff_t   zero_off;
472         xfs_fsize_t     zero_len;
473         xfs_mount_t     *mp = io->io_mount;
474         int             nimaps;
475         int             error = 0;
476         xfs_bmbt_irec_t imap;
477
478         ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
479         ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
480         ASSERT(offset > isize);
481
482         /*
483          * First handle zeroing the block on which isize resides.
484          * We only zero a part of that block so it is handled specially.
485          */
486         error = xfs_zero_last_block(ip, io, offset, isize);
487         if (error) {
488                 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
489                 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
490                 return error;
491         }
492
493         /*
494          * Calculate the range between the new size and the old
495          * where blocks needing to be zeroed may exist.  To get the
496          * block where the last byte in the file currently resides,
497          * we need to subtract one from the size and truncate back
498          * to a block boundary.  We subtract 1 in case the size is
499          * exactly on a block boundary.
500          */
501         last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
502         start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
503         end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
504         ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
505         if (last_fsb == end_zero_fsb) {
506                 /*
507                  * The size was only incremented on its last block.
508                  * We took care of that above, so just return.
509                  */
510                 return 0;
511         }
512
513         ASSERT(start_zero_fsb <= end_zero_fsb);
514         while (start_zero_fsb <= end_zero_fsb) {
515                 nimaps = 1;
516                 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
517                 error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb,
518                                   0, NULL, 0, &imap, &nimaps, NULL, NULL);
519                 if (error) {
520                         ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
521                         ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
522                         return error;
523                 }
524                 ASSERT(nimaps > 0);
525
526                 if (imap.br_state == XFS_EXT_UNWRITTEN ||
527                     imap.br_startblock == HOLESTARTBLOCK) {
528                         /*
529                          * This loop handles initializing pages that were
530                          * partially initialized by the code below this
531                          * loop. It basically zeroes the part of the page
532                          * that sits on a hole and sets the page as P_HOLE
533                          * and calls remapf if it is a mapped file.
534                          */
535                         start_zero_fsb = imap.br_startoff + imap.br_blockcount;
536                         ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
537                         continue;
538                 }
539
540                 /*
541                  * There are blocks we need to zero.
542                  * Drop the inode lock while we're doing the I/O.
543                  * We'll still have the iolock to protect us.
544                  */
545                 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
546
547                 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
548                 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
549
550                 if ((zero_off + zero_len) > offset)
551                         zero_len = offset - zero_off;
552
553                 error = xfs_iozero(ip, zero_off, zero_len);
554                 if (error) {
555                         goto out_lock;
556                 }
557
558                 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
559                 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
560
561                 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
562         }
563
564         return 0;
565
566 out_lock:
567
568         XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
569         ASSERT(error >= 0);
570         return error;
571 }
572
573 ssize_t                         /* bytes written, or (-) error */
574 xfs_write(
575         struct xfs_inode        *xip,
576         struct kiocb            *iocb,
577         const struct iovec      *iovp,
578         unsigned int            nsegs,
579         loff_t                  *offset,
580         int                     ioflags)
581 {
582         struct file             *file = iocb->ki_filp;
583         struct address_space    *mapping = file->f_mapping;
584         struct inode            *inode = mapping->host;
585         bhv_vnode_t             *vp = XFS_ITOV(xip);
586         unsigned long           segs = nsegs;
587         xfs_mount_t             *mp;
588         ssize_t                 ret = 0, error = 0;
589         xfs_fsize_t             isize, new_size;
590         xfs_iocore_t            *io;
591         int                     iolock;
592         int                     eventsent = 0;
593         bhv_vrwlock_t           locktype;
594         size_t                  ocount = 0, count;
595         loff_t                  pos;
596         int                     need_i_mutex;
597
598         XFS_STATS_INC(xs_write_calls);
599
600         error = generic_segment_checks(iovp, &segs, &ocount, VERIFY_READ);
601         if (error)
602                 return error;
603
604         count = ocount;
605         pos = *offset;
606
607         if (count == 0)
608                 return 0;
609
610         io = &xip->i_iocore;
611         mp = io->io_mount;
612
613         xfs_wait_for_freeze(mp, SB_FREEZE_WRITE);
614
615         if (XFS_FORCED_SHUTDOWN(mp))
616                 return -EIO;
617
618 relock:
619         if (ioflags & IO_ISDIRECT) {
620                 iolock = XFS_IOLOCK_SHARED;
621                 locktype = VRWLOCK_WRITE_DIRECT;
622                 need_i_mutex = 0;
623         } else {
624                 iolock = XFS_IOLOCK_EXCL;
625                 locktype = VRWLOCK_WRITE;
626                 need_i_mutex = 1;
627                 mutex_lock(&inode->i_mutex);
628         }
629
630         xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
631
632 start:
633         error = -generic_write_checks(file, &pos, &count,
634                                         S_ISBLK(inode->i_mode));
635         if (error) {
636                 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
637                 goto out_unlock_mutex;
638         }
639
640         if ((DM_EVENT_ENABLED(xip, DM_EVENT_WRITE) &&
641             !(ioflags & IO_INVIS) && !eventsent)) {
642                 int             dmflags = FILP_DELAY_FLAG(file);
643
644                 if (need_i_mutex)
645                         dmflags |= DM_FLAGS_IMUX;
646
647                 xfs_iunlock(xip, XFS_ILOCK_EXCL);
648                 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,
649                                       pos, count,
650                                       dmflags, &locktype);
651                 if (error) {
652                         goto out_unlock_internal;
653                 }
654                 xfs_ilock(xip, XFS_ILOCK_EXCL);
655                 eventsent = 1;
656
657                 /*
658                  * The iolock was dropped and reacquired in XFS_SEND_DATA
659                  * so we have to recheck the size when appending.
660                  * We will only "goto start;" once, since having sent the
661                  * event prevents another call to XFS_SEND_DATA, which is
662                  * what allows the size to change in the first place.
663                  */
664                 if ((file->f_flags & O_APPEND) && pos != xip->i_size)
665                         goto start;
666         }
667
668         if (ioflags & IO_ISDIRECT) {
669                 xfs_buftarg_t   *target =
670                         (xip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
671                                 mp->m_rtdev_targp : mp->m_ddev_targp;
672
673                 if ((pos & target->bt_smask) || (count & target->bt_smask)) {
674                         xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
675                         return XFS_ERROR(-EINVAL);
676                 }
677
678                 if (!need_i_mutex && (VN_CACHED(vp) || pos > xip->i_size)) {
679                         xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
680                         iolock = XFS_IOLOCK_EXCL;
681                         locktype = VRWLOCK_WRITE;
682                         need_i_mutex = 1;
683                         mutex_lock(&inode->i_mutex);
684                         xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
685                         goto start;
686                 }
687         }
688
689         new_size = pos + count;
690         if (new_size > xip->i_size)
691                 io->io_new_size = new_size;
692
693         if (likely(!(ioflags & IO_INVIS))) {
694                 file_update_time(file);
695                 xfs_ichgtime_fast(xip, inode,
696                                   XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
697         }
698
699         /*
700          * If the offset is beyond the size of the file, we have a couple
701          * of things to do. First, if there is already space allocated
702          * we need to either create holes or zero the disk or ...
703          *
704          * If there is a page where the previous size lands, we need
705          * to zero it out up to the new size.
706          */
707
708         if (pos > xip->i_size) {
709                 error = xfs_zero_eof(vp, io, pos, xip->i_size);
710                 if (error) {
711                         xfs_iunlock(xip, XFS_ILOCK_EXCL);
712                         goto out_unlock_internal;
713                 }
714         }
715         xfs_iunlock(xip, XFS_ILOCK_EXCL);
716
717         /*
718          * If we're writing the file then make sure to clear the
719          * setuid and setgid bits if the process is not being run
720          * by root.  This keeps people from modifying setuid and
721          * setgid binaries.
722          */
723
724         if (((xip->i_d.di_mode & S_ISUID) ||
725             ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
726                 (S_ISGID | S_IXGRP))) &&
727              !capable(CAP_FSETID)) {
728                 error = xfs_write_clear_setuid(xip);
729                 if (likely(!error))
730                         error = -remove_suid(file->f_path.dentry);
731                 if (unlikely(error)) {
732                         goto out_unlock_internal;
733                 }
734         }
735
736 retry:
737         /* We can write back this queue in page reclaim */
738         current->backing_dev_info = mapping->backing_dev_info;
739
740         if ((ioflags & IO_ISDIRECT)) {
741                 if (VN_CACHED(vp)) {
742                         WARN_ON(need_i_mutex == 0);
743                         xfs_inval_cached_trace(io, pos, -1,
744                                         ctooff(offtoct(pos)), -1);
745                         error = xfs_flushinval_pages(xip,
746                                         ctooff(offtoct(pos)),
747                                         -1, FI_REMAPF_LOCKED);
748                         if (error)
749                                 goto out_unlock_internal;
750                 }
751
752                 if (need_i_mutex) {
753                         /* demote the lock now the cached pages are gone */
754                         XFS_ILOCK_DEMOTE(mp, io, XFS_IOLOCK_EXCL);
755                         mutex_unlock(&inode->i_mutex);
756
757                         iolock = XFS_IOLOCK_SHARED;
758                         locktype = VRWLOCK_WRITE_DIRECT;
759                         need_i_mutex = 0;
760                 }
761
762                 xfs_rw_enter_trace(XFS_DIOWR_ENTER, io, (void *)iovp, segs,
763                                 *offset, ioflags);
764                 ret = generic_file_direct_write(iocb, iovp,
765                                 &segs, pos, offset, count, ocount);
766
767                 /*
768                  * direct-io write to a hole: fall through to buffered I/O
769                  * for completing the rest of the request.
770                  */
771                 if (ret >= 0 && ret != count) {
772                         XFS_STATS_ADD(xs_write_bytes, ret);
773
774                         pos += ret;
775                         count -= ret;
776
777                         ioflags &= ~IO_ISDIRECT;
778                         xfs_iunlock(xip, iolock);
779                         goto relock;
780                 }
781         } else {
782                 xfs_rw_enter_trace(XFS_WRITE_ENTER, io, (void *)iovp, segs,
783                                 *offset, ioflags);
784                 ret = generic_file_buffered_write(iocb, iovp, segs,
785                                 pos, offset, count, ret);
786         }
787
788         current->backing_dev_info = NULL;
789
790         if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
791                 ret = wait_on_sync_kiocb(iocb);
792
793         if (ret == -ENOSPC &&
794             DM_EVENT_ENABLED(xip, DM_EVENT_NOSPACE) && !(ioflags & IO_INVIS)) {
795                 xfs_rwunlock(xip, locktype);
796                 if (need_i_mutex)
797                         mutex_unlock(&inode->i_mutex);
798                 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp,
799                                 DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL,
800                                 0, 0, 0); /* Delay flag intentionally  unused */
801                 if (need_i_mutex)
802                         mutex_lock(&inode->i_mutex);
803                 xfs_rwlock(xip, locktype);
804                 if (error)
805                         goto out_unlock_internal;
806                 pos = xip->i_size;
807                 ret = 0;
808                 goto retry;
809         }
810
811         isize = i_size_read(inode);
812         if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
813                 *offset = isize;
814
815         if (*offset > xip->i_size) {
816                 xfs_ilock(xip, XFS_ILOCK_EXCL);
817                 if (*offset > xip->i_size)
818                         xip->i_size = *offset;
819                 xfs_iunlock(xip, XFS_ILOCK_EXCL);
820         }
821
822         error = -ret;
823         if (ret <= 0)
824                 goto out_unlock_internal;
825
826         XFS_STATS_ADD(xs_write_bytes, ret);
827
828         /* Handle various SYNC-type writes */
829         if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
830                 int error2;
831                 xfs_rwunlock(xip, locktype);
832                 if (need_i_mutex)
833                         mutex_unlock(&inode->i_mutex);
834                 error2 = sync_page_range(inode, mapping, pos, ret);
835                 if (!error)
836                         error = error2;
837                 if (need_i_mutex)
838                         mutex_lock(&inode->i_mutex);
839                 xfs_rwlock(xip, locktype);
840                 error2 = xfs_write_sync_logforce(mp, xip);
841                 if (!error)
842                         error = error2;
843         }
844
845  out_unlock_internal:
846         if (io->io_new_size) {
847                 xfs_ilock(xip, XFS_ILOCK_EXCL);
848                 io->io_new_size = 0;
849                 /*
850                  * If this was a direct or synchronous I/O that failed (such
851                  * as ENOSPC) then part of the I/O may have been written to
852                  * disk before the error occured.  In this case the on-disk
853                  * file size may have been adjusted beyond the in-memory file
854                  * size and now needs to be truncated back.
855                  */
856                 if (xip->i_d.di_size > xip->i_size)
857                         xip->i_d.di_size = xip->i_size;
858                 xfs_iunlock(xip, XFS_ILOCK_EXCL);
859         }
860         xfs_rwunlock(xip, locktype);
861  out_unlock_mutex:
862         if (need_i_mutex)
863                 mutex_unlock(&inode->i_mutex);
864         return -error;
865 }
866
867 /*
868  * All xfs metadata buffers except log state machine buffers
869  * get this attached as their b_bdstrat callback function.
870  * This is so that we can catch a buffer
871  * after prematurely unpinning it to forcibly shutdown the filesystem.
872  */
873 int
874 xfs_bdstrat_cb(struct xfs_buf *bp)
875 {
876         xfs_mount_t     *mp;
877
878         mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
879         if (!XFS_FORCED_SHUTDOWN(mp)) {
880                 xfs_buf_iorequest(bp);
881                 return 0;
882         } else {
883                 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
884                 /*
885                  * Metadata write that didn't get logged but
886                  * written delayed anyway. These aren't associated
887                  * with a transaction, and can be ignored.
888                  */
889                 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
890                     (XFS_BUF_ISREAD(bp)) == 0)
891                         return (xfs_bioerror_relse(bp));
892                 else
893                         return (xfs_bioerror(bp));
894         }
895 }
896
897
898 int
899 xfs_bmap(
900         xfs_inode_t     *ip,
901         xfs_off_t       offset,
902         ssize_t         count,
903         int             flags,
904         xfs_iomap_t     *iomapp,
905         int             *niomaps)
906 {
907         xfs_iocore_t    *io = &ip->i_iocore;
908
909         ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
910         ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
911                ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
912
913         return xfs_iomap(io, offset, count, flags, iomapp, niomaps);
914 }
915
916 /*
917  * Wrapper around bdstrat so that we can stop data
918  * from going to disk in case we are shutting down the filesystem.
919  * Typically user data goes thru this path; one of the exceptions
920  * is the superblock.
921  */
922 int
923 xfsbdstrat(
924         struct xfs_mount        *mp,
925         struct xfs_buf          *bp)
926 {
927         ASSERT(mp);
928         if (!XFS_FORCED_SHUTDOWN(mp)) {
929                 /* Grio redirection would go here
930                  * if (XFS_BUF_IS_GRIO(bp)) {
931                  */
932
933                 xfs_buf_iorequest(bp);
934                 return 0;
935         }
936
937         xfs_buftrace("XFSBDSTRAT IOERROR", bp);
938         return (xfs_bioerror_relse(bp));
939 }
940
941 /*
942  * If the underlying (data/log/rt) device is readonly, there are some
943  * operations that cannot proceed.
944  */
945 int
946 xfs_dev_is_read_only(
947         xfs_mount_t             *mp,
948         char                    *message)
949 {
950         if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
951             xfs_readonly_buftarg(mp->m_logdev_targp) ||
952             (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
953                 cmn_err(CE_NOTE,
954                         "XFS: %s required on read-only device.", message);
955                 cmn_err(CE_NOTE,
956                         "XFS: write access unavailable, cannot proceed.");
957                 return EROFS;
958         }
959         return 0;
960 }