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