Merge branch 'master'
[linux-2.6] / 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_dir.h"
27 #include "xfs_dir2.h"
28 #include "xfs_alloc.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_quota.h"
31 #include "xfs_mount.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir_sf.h"
36 #include "xfs_dir2_sf.h"
37 #include "xfs_attr_sf.h"
38 #include "xfs_dinode.h"
39 #include "xfs_inode.h"
40 #include "xfs_bmap.h"
41 #include "xfs_btree.h"
42 #include "xfs_ialloc.h"
43 #include "xfs_rtalloc.h"
44 #include "xfs_error.h"
45 #include "xfs_itable.h"
46 #include "xfs_rw.h"
47 #include "xfs_acl.h"
48 #include "xfs_cap.h"
49 #include "xfs_mac.h"
50 #include "xfs_attr.h"
51 #include "xfs_inode_item.h"
52 #include "xfs_buf_item.h"
53 #include "xfs_utils.h"
54 #include "xfs_iomap.h"
55
56 #include <linux/capability.h>
57 #include <linux/writeback.h>
58
59
60 #if defined(XFS_RW_TRACE)
61 void
62 xfs_rw_enter_trace(
63         int                     tag,
64         xfs_iocore_t            *io,
65         void                    *data,
66         size_t                  segs,
67         loff_t                  offset,
68         int                     ioflags)
69 {
70         xfs_inode_t     *ip = XFS_IO_INODE(io);
71
72         if (ip->i_rwtrace == NULL)
73                 return;
74         ktrace_enter(ip->i_rwtrace,
75                 (void *)(unsigned long)tag,
76                 (void *)ip,
77                 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
78                 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
79                 (void *)data,
80                 (void *)((unsigned long)segs),
81                 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
82                 (void *)((unsigned long)(offset & 0xffffffff)),
83                 (void *)((unsigned long)ioflags),
84                 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
85                 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
86                 (void *)((unsigned long)current_pid()),
87                 (void *)NULL,
88                 (void *)NULL,
89                 (void *)NULL,
90                 (void *)NULL);
91 }
92
93 void
94 xfs_inval_cached_trace(
95         xfs_iocore_t    *io,
96         xfs_off_t       offset,
97         xfs_off_t       len,
98         xfs_off_t       first,
99         xfs_off_t       last)
100 {
101         xfs_inode_t     *ip = XFS_IO_INODE(io);
102
103         if (ip->i_rwtrace == NULL)
104                 return;
105         ktrace_enter(ip->i_rwtrace,
106                 (void *)(__psint_t)XFS_INVAL_CACHED,
107                 (void *)ip,
108                 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
109                 (void *)((unsigned long)(offset & 0xffffffff)),
110                 (void *)((unsigned long)((len >> 32) & 0xffffffff)),
111                 (void *)((unsigned long)(len & 0xffffffff)),
112                 (void *)((unsigned long)((first >> 32) & 0xffffffff)),
113                 (void *)((unsigned long)(first & 0xffffffff)),
114                 (void *)((unsigned long)((last >> 32) & 0xffffffff)),
115                 (void *)((unsigned long)(last & 0xffffffff)),
116                 (void *)((unsigned long)current_pid()),
117                 (void *)NULL,
118                 (void *)NULL,
119                 (void *)NULL,
120                 (void *)NULL,
121                 (void *)NULL);
122 }
123 #endif
124
125 /*
126  *      xfs_iozero
127  *
128  *      xfs_iozero clears the specified range of buffer supplied,
129  *      and marks all the affected blocks as valid and modified.  If
130  *      an affected block is not allocated, it will be allocated.  If
131  *      an affected block is not completely overwritten, and is not
132  *      valid before the operation, it will be read from disk before
133  *      being partially zeroed.
134  */
135 STATIC int
136 xfs_iozero(
137         struct inode            *ip,    /* inode                        */
138         loff_t                  pos,    /* offset in file               */
139         size_t                  count,  /* size of data to zero         */
140         loff_t                  end_size)       /* max file size to set */
141 {
142         unsigned                bytes;
143         struct page             *page;
144         struct address_space    *mapping;
145         char                    *kaddr;
146         int                     status;
147
148         mapping = ip->i_mapping;
149         do {
150                 unsigned long index, offset;
151
152                 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
153                 index = pos >> PAGE_CACHE_SHIFT;
154                 bytes = PAGE_CACHE_SIZE - offset;
155                 if (bytes > count)
156                         bytes = count;
157
158                 status = -ENOMEM;
159                 page = grab_cache_page(mapping, index);
160                 if (!page)
161                         break;
162
163                 kaddr = kmap(page);
164                 status = mapping->a_ops->prepare_write(NULL, page, offset,
165                                                         offset + bytes);
166                 if (status) {
167                         goto unlock;
168                 }
169
170                 memset((void *) (kaddr + offset), 0, bytes);
171                 flush_dcache_page(page);
172                 status = mapping->a_ops->commit_write(NULL, page, offset,
173                                                         offset + bytes);
174                 if (!status) {
175                         pos += bytes;
176                         count -= bytes;
177                         if (pos > i_size_read(ip))
178                                 i_size_write(ip, pos < end_size ? pos : end_size);
179                 }
180
181 unlock:
182                 kunmap(page);
183                 unlock_page(page);
184                 page_cache_release(page);
185                 if (status)
186                         break;
187         } while (count);
188
189         return (-status);
190 }
191
192 ssize_t                 /* bytes read, or (-)  error */
193 xfs_read(
194         bhv_desc_t              *bdp,
195         struct kiocb            *iocb,
196         const struct iovec      *iovp,
197         unsigned int            segs,
198         loff_t                  *offset,
199         int                     ioflags,
200         cred_t                  *credp)
201 {
202         struct file             *file = iocb->ki_filp;
203         struct inode            *inode = file->f_mapping->host;
204         size_t                  size = 0;
205         ssize_t                 ret;
206         xfs_fsize_t             n;
207         xfs_inode_t             *ip;
208         xfs_mount_t             *mp;
209         vnode_t                 *vp;
210         unsigned long           seg;
211
212         ip = XFS_BHVTOI(bdp);
213         vp = BHV_TO_VNODE(bdp);
214         mp = ip->i_mount;
215
216         XFS_STATS_INC(xs_read_calls);
217
218         /* START copy & waste from filemap.c */
219         for (seg = 0; seg < segs; seg++) {
220                 const struct iovec *iv = &iovp[seg];
221
222                 /*
223                  * If any segment has a negative length, or the cumulative
224                  * length ever wraps negative then return -EINVAL.
225                  */
226                 size += iv->iov_len;
227                 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
228                         return XFS_ERROR(-EINVAL);
229         }
230         /* END copy & waste from filemap.c */
231
232         if (unlikely(ioflags & IO_ISDIRECT)) {
233                 xfs_buftarg_t   *target =
234                         (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
235                                 mp->m_rtdev_targp : mp->m_ddev_targp;
236                 if ((*offset & target->bt_smask) ||
237                     (size & target->bt_smask)) {
238                         if (*offset == ip->i_d.di_size) {
239                                 return (0);
240                         }
241                         return -XFS_ERROR(EINVAL);
242                 }
243         }
244
245         n = XFS_MAXIOFFSET(mp) - *offset;
246         if ((n <= 0) || (size == 0))
247                 return 0;
248
249         if (n < size)
250                 size = n;
251
252         if (XFS_FORCED_SHUTDOWN(mp))
253                 return -EIO;
254
255         if (unlikely(ioflags & IO_ISDIRECT))
256                 mutex_lock(&inode->i_mutex);
257         xfs_ilock(ip, XFS_IOLOCK_SHARED);
258
259         if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ) &&
260             !(ioflags & IO_INVIS)) {
261                 vrwlock_t locktype = VRWLOCK_READ;
262                 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
263
264                 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ,
265                                         BHV_TO_VNODE(bdp), *offset, size,
266                                         dmflags, &locktype);
267                 if (ret) {
268                         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
269                         goto unlock_mutex;
270                 }
271         }
272
273         if (unlikely((ioflags & IO_ISDIRECT) && VN_CACHED(vp)))
274                 VOP_FLUSHINVAL_PAGES(vp, ctooff(offtoct(*offset)),
275                                                 -1, FI_REMAPF_LOCKED);
276
277         xfs_rw_enter_trace(XFS_READ_ENTER, &ip->i_iocore,
278                                 (void *)iovp, segs, *offset, ioflags);
279         ret = __generic_file_aio_read(iocb, iovp, segs, offset);
280         if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
281                 ret = wait_on_sync_kiocb(iocb);
282         if (ret > 0)
283                 XFS_STATS_ADD(xs_read_bytes, ret);
284
285         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
286
287 unlock_mutex:
288         if (unlikely(ioflags & IO_ISDIRECT))
289                 mutex_unlock(&inode->i_mutex);
290         return ret;
291 }
292
293 ssize_t
294 xfs_sendfile(
295         bhv_desc_t              *bdp,
296         struct file             *filp,
297         loff_t                  *offset,
298         int                     ioflags,
299         size_t                  count,
300         read_actor_t            actor,
301         void                    *target,
302         cred_t                  *credp)
303 {
304         xfs_inode_t             *ip = XFS_BHVTOI(bdp);
305         xfs_mount_t             *mp = ip->i_mount;
306         ssize_t                 ret;
307
308         XFS_STATS_INC(xs_read_calls);
309         if (XFS_FORCED_SHUTDOWN(mp))
310                 return -EIO;
311
312         xfs_ilock(ip, XFS_IOLOCK_SHARED);
313
314         if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_READ) &&
315             (!(ioflags & IO_INVIS))) {
316                 vrwlock_t locktype = VRWLOCK_READ;
317                 int error;
318
319                 error = XFS_SEND_DATA(mp, DM_EVENT_READ, BHV_TO_VNODE(bdp),
320                                       *offset, count,
321                                       FILP_DELAY_FLAG(filp), &locktype);
322                 if (error) {
323                         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
324                         return -error;
325                 }
326         }
327         xfs_rw_enter_trace(XFS_SENDFILE_ENTER, &ip->i_iocore,
328                    (void *)(unsigned long)target, count, *offset, ioflags);
329         ret = generic_file_sendfile(filp, offset, count, actor, target);
330         if (ret > 0)
331                 XFS_STATS_ADD(xs_read_bytes, ret);
332
333         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
334         return ret;
335 }
336
337 ssize_t
338 xfs_splice_read(
339         bhv_desc_t              *bdp,
340         struct file             *infilp,
341         struct pipe_inode_info  *pipe,
342         size_t                  count,
343         int                     flags,
344         int                     ioflags,
345         cred_t                  *credp)
346 {
347         xfs_inode_t             *ip = XFS_BHVTOI(bdp);
348         xfs_mount_t             *mp = ip->i_mount;
349         ssize_t                 ret;
350
351         XFS_STATS_INC(xs_read_calls);
352         if (XFS_FORCED_SHUTDOWN(ip->i_mount))
353                 return -EIO;
354
355         xfs_ilock(ip, XFS_IOLOCK_SHARED);
356
357         if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_READ) &&
358             (!(ioflags & IO_INVIS))) {
359                 vrwlock_t locktype = VRWLOCK_READ;
360                 int error;
361
362                 error = XFS_SEND_DATA(mp, DM_EVENT_READ, BHV_TO_VNODE(bdp),
363                                         infilp->f_pos, count,
364                                         FILP_DELAY_FLAG(infilp), &locktype);
365                 if (error) {
366                         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
367                         return -error;
368                 }
369         }
370         xfs_rw_enter_trace(XFS_SPLICE_READ_ENTER, &ip->i_iocore,
371                            pipe, count, infilp->f_pos, ioflags);
372         ret = generic_file_splice_read(infilp, pipe, count, flags);
373         if (ret > 0)
374                 XFS_STATS_ADD(xs_read_bytes, ret);
375
376         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
377         return ret;
378 }
379
380 ssize_t
381 xfs_splice_write(
382         bhv_desc_t              *bdp,
383         struct pipe_inode_info  *pipe,
384         struct file             *outfilp,
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                 vrwlock_t locktype = VRWLOCK_WRITE;
403                 int error;
404
405                 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, BHV_TO_VNODE(bdp),
406                                         outfilp->f_pos, 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, outfilp->f_pos, ioflags);
415         ret = generic_file_splice_write(pipe, outfilp, 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);
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         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_extlen_t    buf_len_fsb;
512         xfs_mount_t     *mp = io->io_mount;
513         int             nimaps;
514         int             error = 0;
515         xfs_bmbt_irec_t imap;
516
517         ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
518         ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
519         ASSERT(offset > isize);
520
521         /*
522          * First handle zeroing the block on which isize resides.
523          * We only zero a part of that block so it is handled specially.
524          */
525         error = xfs_zero_last_block(ip, io, isize, end_size);
526         if (error) {
527                 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
528                 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
529                 return error;
530         }
531
532         /*
533          * Calculate the range between the new size and the old
534          * where blocks needing to be zeroed may exist.  To get the
535          * block where the last byte in the file currently resides,
536          * we need to subtract one from the size and truncate back
537          * to a block boundary.  We subtract 1 in case the size is
538          * exactly on a block boundary.
539          */
540         last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
541         start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
542         end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
543         ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
544         if (last_fsb == end_zero_fsb) {
545                 /*
546                  * The size was only incremented on its last block.
547                  * We took care of that above, so just return.
548                  */
549                 return 0;
550         }
551
552         ASSERT(start_zero_fsb <= end_zero_fsb);
553         while (start_zero_fsb <= end_zero_fsb) {
554                 nimaps = 1;
555                 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
556                 error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb,
557                                   0, NULL, 0, &imap, &nimaps, NULL);
558                 if (error) {
559                         ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
560                         ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
561                         return error;
562                 }
563                 ASSERT(nimaps > 0);
564
565                 if (imap.br_state == XFS_EXT_UNWRITTEN ||
566                     imap.br_startblock == HOLESTARTBLOCK) {
567                         /*
568                          * This loop handles initializing pages that were
569                          * partially initialized by the code below this
570                          * loop. It basically zeroes the part of the page
571                          * that sits on a hole and sets the page as P_HOLE
572                          * and calls remapf if it is a mapped file.
573                          */
574                         start_zero_fsb = imap.br_startoff + imap.br_blockcount;
575                         ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
576                         continue;
577                 }
578
579                 /*
580                  * There are blocks in the range requested.
581                  * Zero them a single write at a time.  We actually
582                  * don't zero the entire range returned if it is
583                  * too big and simply loop around to get the rest.
584                  * That is not the most efficient thing to do, but it
585                  * is simple and this path should not be exercised often.
586                  */
587                 buf_len_fsb = XFS_FILBLKS_MIN(imap.br_blockcount,
588                                               mp->m_writeio_blocks << 8);
589                 /*
590                  * Drop the inode lock while we're doing the I/O.
591                  * We'll still have the iolock to protect us.
592                  */
593                 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
594
595                 error = xfs_iozero(ip,
596                                    XFS_FSB_TO_B(mp, start_zero_fsb),
597                                    XFS_FSB_TO_B(mp, buf_len_fsb),
598                                    end_size);
599
600                 if (error) {
601                         goto out_lock;
602                 }
603
604                 start_zero_fsb = imap.br_startoff + buf_len_fsb;
605                 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
606
607                 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
608         }
609
610         return 0;
611
612 out_lock:
613
614         XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
615         ASSERT(error >= 0);
616         return error;
617 }
618
619 ssize_t                         /* bytes written, or (-) error */
620 xfs_write(
621         bhv_desc_t              *bdp,
622         struct kiocb            *iocb,
623         const struct iovec      *iovp,
624         unsigned int            nsegs,
625         loff_t                  *offset,
626         int                     ioflags,
627         cred_t                  *credp)
628 {
629         struct file             *file = iocb->ki_filp;
630         struct address_space    *mapping = file->f_mapping;
631         struct inode            *inode = mapping->host;
632         unsigned long           segs = nsegs;
633         xfs_inode_t             *xip;
634         xfs_mount_t             *mp;
635         ssize_t                 ret = 0, error = 0;
636         xfs_fsize_t             isize, new_size;
637         xfs_iocore_t            *io;
638         vnode_t                 *vp;
639         unsigned long           seg;
640         int                     iolock;
641         int                     eventsent = 0;
642         vrwlock_t               locktype;
643         size_t                  ocount = 0, count;
644         loff_t                  pos;
645         int                     need_i_mutex = 1, need_flush = 0;
646
647         XFS_STATS_INC(xs_write_calls);
648
649         vp = BHV_TO_VNODE(bdp);
650         xip = XFS_BHVTOI(bdp);
651
652         for (seg = 0; seg < segs; seg++) {
653                 const struct iovec *iv = &iovp[seg];
654
655                 /*
656                  * If any segment has a negative length, or the cumulative
657                  * length ever wraps negative then return -EINVAL.
658                  */
659                 ocount += iv->iov_len;
660                 if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
661                         return -EINVAL;
662                 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
663                         continue;
664                 if (seg == 0)
665                         return -EFAULT;
666                 segs = seg;
667                 ocount -= iv->iov_len;  /* This segment is no good */
668                 break;
669         }
670
671         count = ocount;
672         pos = *offset;
673
674         if (count == 0)
675                 return 0;
676
677         io = &xip->i_iocore;
678         mp = io->io_mount;
679
680         if (XFS_FORCED_SHUTDOWN(mp))
681                 return -EIO;
682
683         fs_check_frozen(vp->v_vfsp, SB_FREEZE_WRITE);
684
685         if (ioflags & IO_ISDIRECT) {
686                 xfs_buftarg_t   *target =
687                         (xip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
688                                 mp->m_rtdev_targp : mp->m_ddev_targp;
689
690                 if ((pos & target->bt_smask) || (count & target->bt_smask))
691                         return XFS_ERROR(-EINVAL);
692
693                 if (!VN_CACHED(vp) && pos < i_size_read(inode))
694                         need_i_mutex = 0;
695
696                 if (VN_CACHED(vp))
697                         need_flush = 1;
698         }
699
700 relock:
701         if (need_i_mutex) {
702                 iolock = XFS_IOLOCK_EXCL;
703                 locktype = VRWLOCK_WRITE;
704
705                 mutex_lock(&inode->i_mutex);
706         } else {
707                 iolock = XFS_IOLOCK_SHARED;
708                 locktype = VRWLOCK_WRITE_DIRECT;
709         }
710
711         xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
712
713         isize = i_size_read(inode);
714
715         if (file->f_flags & O_APPEND)
716                 *offset = isize;
717
718 start:
719         error = -generic_write_checks(file, &pos, &count,
720                                         S_ISBLK(inode->i_mode));
721         if (error) {
722                 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
723                 goto out_unlock_mutex;
724         }
725
726         new_size = pos + count;
727         if (new_size > isize)
728                 io->io_new_size = new_size;
729
730         if ((DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_WRITE) &&
731             !(ioflags & IO_INVIS) && !eventsent)) {
732                 loff_t          savedsize = pos;
733                 int             dmflags = FILP_DELAY_FLAG(file);
734
735                 if (need_i_mutex)
736                         dmflags |= DM_FLAGS_IMUX;
737
738                 xfs_iunlock(xip, XFS_ILOCK_EXCL);
739                 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,
740                                       pos, count,
741                                       dmflags, &locktype);
742                 if (error) {
743                         xfs_iunlock(xip, iolock);
744                         goto out_unlock_mutex;
745                 }
746                 xfs_ilock(xip, XFS_ILOCK_EXCL);
747                 eventsent = 1;
748
749                 /*
750                  * The iolock was dropped and reacquired in XFS_SEND_DATA
751                  * so we have to recheck the size when appending.
752                  * We will only "goto start;" once, since having sent the
753                  * event prevents another call to XFS_SEND_DATA, which is
754                  * what allows the size to change in the first place.
755                  */
756                 if ((file->f_flags & O_APPEND) && savedsize != isize) {
757                         pos = isize = xip->i_d.di_size;
758                         goto start;
759                 }
760         }
761
762         if (likely(!(ioflags & IO_INVIS))) {
763                 file_update_time(file);
764                 xfs_ichgtime_fast(xip, inode,
765                                   XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
766         }
767
768         /*
769          * If the offset is beyond the size of the file, we have a couple
770          * of things to do. First, if there is already space allocated
771          * we need to either create holes or zero the disk or ...
772          *
773          * If there is a page where the previous size lands, we need
774          * to zero it out up to the new size.
775          */
776
777         if (pos > isize) {
778                 error = xfs_zero_eof(BHV_TO_VNODE(bdp), io, pos,
779                                         isize, pos + count);
780                 if (error) {
781                         xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
782                         goto out_unlock_mutex;
783                 }
784         }
785         xfs_iunlock(xip, XFS_ILOCK_EXCL);
786
787         /*
788          * If we're writing the file then make sure to clear the
789          * setuid and setgid bits if the process is not being run
790          * by root.  This keeps people from modifying setuid and
791          * setgid binaries.
792          */
793
794         if (((xip->i_d.di_mode & S_ISUID) ||
795             ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
796                 (S_ISGID | S_IXGRP))) &&
797              !capable(CAP_FSETID)) {
798                 error = xfs_write_clear_setuid(xip);
799                 if (likely(!error))
800                         error = -remove_suid(file->f_dentry);
801                 if (unlikely(error)) {
802                         xfs_iunlock(xip, iolock);
803                         goto out_unlock_mutex;
804                 }
805         }
806
807 retry:
808         /* We can write back this queue in page reclaim */
809         current->backing_dev_info = mapping->backing_dev_info;
810
811         if ((ioflags & IO_ISDIRECT)) {
812                 if (need_flush) {
813                         xfs_inval_cached_trace(io, pos, -1,
814                                         ctooff(offtoct(pos)), -1);
815                         VOP_FLUSHINVAL_PAGES(vp, ctooff(offtoct(pos)),
816                                         -1, FI_REMAPF_LOCKED);
817                 }
818
819                 if (need_i_mutex) {
820                         /* demote the lock now the cached pages are gone */
821                         XFS_ILOCK_DEMOTE(mp, io, XFS_IOLOCK_EXCL);
822                         mutex_unlock(&inode->i_mutex);
823
824                         iolock = XFS_IOLOCK_SHARED;
825                         locktype = VRWLOCK_WRITE_DIRECT;
826                         need_i_mutex = 0;
827                 }
828
829                 xfs_rw_enter_trace(XFS_DIOWR_ENTER, io, (void *)iovp, segs,
830                                 *offset, ioflags);
831                 ret = generic_file_direct_write(iocb, iovp,
832                                 &segs, pos, offset, count, ocount);
833
834                 /*
835                  * direct-io write to a hole: fall through to buffered I/O
836                  * for completing the rest of the request.
837                  */
838                 if (ret >= 0 && ret != count) {
839                         XFS_STATS_ADD(xs_write_bytes, ret);
840
841                         pos += ret;
842                         count -= ret;
843
844                         need_i_mutex = 1;
845                         ioflags &= ~IO_ISDIRECT;
846                         xfs_iunlock(xip, iolock);
847                         goto relock;
848                 }
849         } else {
850                 xfs_rw_enter_trace(XFS_WRITE_ENTER, io, (void *)iovp, segs,
851                                 *offset, ioflags);
852                 ret = generic_file_buffered_write(iocb, iovp, segs,
853                                 pos, offset, count, ret);
854         }
855
856         current->backing_dev_info = NULL;
857
858         if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
859                 ret = wait_on_sync_kiocb(iocb);
860
861         if ((ret == -ENOSPC) &&
862             DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_NOSPACE) &&
863             !(ioflags & IO_INVIS)) {
864
865                 xfs_rwunlock(bdp, locktype);
866                 if (need_i_mutex)
867                         mutex_unlock(&inode->i_mutex);
868                 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp,
869                                 DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL,
870                                 0, 0, 0); /* Delay flag intentionally  unused */
871                 if (error)
872                         goto out_nounlocks;
873                 if (need_i_mutex)
874                         mutex_lock(&inode->i_mutex);
875                 xfs_rwlock(bdp, locktype);
876                 pos = xip->i_d.di_size;
877                 ret = 0;
878                 goto retry;
879         }
880
881         isize = i_size_read(inode);
882         if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
883                 *offset = isize;
884
885         if (*offset > xip->i_d.di_size) {
886                 xfs_ilock(xip, XFS_ILOCK_EXCL);
887                 if (*offset > xip->i_d.di_size) {
888                         xip->i_d.di_size = *offset;
889                         i_size_write(inode, *offset);
890                         xip->i_update_core = 1;
891                         xip->i_update_size = 1;
892                 }
893                 xfs_iunlock(xip, XFS_ILOCK_EXCL);
894         }
895
896         error = -ret;
897         if (ret <= 0)
898                 goto out_unlock_internal;
899
900         XFS_STATS_ADD(xs_write_bytes, ret);
901
902         /* Handle various SYNC-type writes */
903         if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
904                 /*
905                  * If we're treating this as O_DSYNC and we have not updated the
906                  * size, force the log.
907                  */
908                 if (!(mp->m_flags & XFS_MOUNT_OSYNCISOSYNC) &&
909                     !(xip->i_update_size)) {
910                         xfs_inode_log_item_t    *iip = xip->i_itemp;
911
912                         /*
913                          * If an allocation transaction occurred
914                          * without extending the size, then we have to force
915                          * the log up the proper point to ensure that the
916                          * allocation is permanent.  We can't count on
917                          * the fact that buffered writes lock out direct I/O
918                          * writes - the direct I/O write could have extended
919                          * the size nontransactionally, then finished before
920                          * we started.  xfs_write_file will think that the file
921                          * didn't grow but the update isn't safe unless the
922                          * size change is logged.
923                          *
924                          * Force the log if we've committed a transaction
925                          * against the inode or if someone else has and
926                          * the commit record hasn't gone to disk (e.g.
927                          * the inode is pinned).  This guarantees that
928                          * all changes affecting the inode are permanent
929                          * when we return.
930                          */
931                         if (iip && iip->ili_last_lsn) {
932                                 xfs_log_force(mp, iip->ili_last_lsn,
933                                                 XFS_LOG_FORCE | XFS_LOG_SYNC);
934                         } else if (xfs_ipincount(xip) > 0) {
935                                 xfs_log_force(mp, (xfs_lsn_t)0,
936                                                 XFS_LOG_FORCE | XFS_LOG_SYNC);
937                         }
938
939                 } else {
940                         xfs_trans_t     *tp;
941
942                         /*
943                          * O_SYNC or O_DSYNC _with_ a size update are handled
944                          * the same way.
945                          *
946                          * If the write was synchronous then we need to make
947                          * sure that the inode modification time is permanent.
948                          * We'll have updated the timestamp above, so here
949                          * we use a synchronous transaction to log the inode.
950                          * It's not fast, but it's necessary.
951                          *
952                          * If this a dsync write and the size got changed
953                          * non-transactionally, then we need to ensure that
954                          * the size change gets logged in a synchronous
955                          * transaction.
956                          */
957
958                         tp = xfs_trans_alloc(mp, XFS_TRANS_WRITE_SYNC);
959                         if ((error = xfs_trans_reserve(tp, 0,
960                                                       XFS_SWRITE_LOG_RES(mp),
961                                                       0, 0, 0))) {
962                                 /* Transaction reserve failed */
963                                 xfs_trans_cancel(tp, 0);
964                         } else {
965                                 /* Transaction reserve successful */
966                                 xfs_ilock(xip, XFS_ILOCK_EXCL);
967                                 xfs_trans_ijoin(tp, xip, XFS_ILOCK_EXCL);
968                                 xfs_trans_ihold(tp, xip);
969                                 xfs_trans_log_inode(tp, xip, XFS_ILOG_CORE);
970                                 xfs_trans_set_sync(tp);
971                                 error = xfs_trans_commit(tp, 0, NULL);
972                                 xfs_iunlock(xip, XFS_ILOCK_EXCL);
973                         }
974                         if (error)
975                                 goto out_unlock_internal;
976                 }
977
978                 xfs_rwunlock(bdp, locktype);
979                 if (need_i_mutex)
980                         mutex_unlock(&inode->i_mutex);
981
982                 error = sync_page_range(inode, mapping, pos, ret);
983                 if (!error)
984                         error = ret;
985                 return error;
986         }
987
988  out_unlock_internal:
989         xfs_rwunlock(bdp, locktype);
990  out_unlock_mutex:
991         if (need_i_mutex)
992                 mutex_unlock(&inode->i_mutex);
993  out_nounlocks:
994         return -error;
995 }
996
997 /*
998  * All xfs metadata buffers except log state machine buffers
999  * get this attached as their b_bdstrat callback function.
1000  * This is so that we can catch a buffer
1001  * after prematurely unpinning it to forcibly shutdown the filesystem.
1002  */
1003 int
1004 xfs_bdstrat_cb(struct xfs_buf *bp)
1005 {
1006         xfs_mount_t     *mp;
1007
1008         mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
1009         if (!XFS_FORCED_SHUTDOWN(mp)) {
1010                 xfs_buf_iorequest(bp);
1011                 return 0;
1012         } else {
1013                 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
1014                 /*
1015                  * Metadata write that didn't get logged but
1016                  * written delayed anyway. These aren't associated
1017                  * with a transaction, and can be ignored.
1018                  */
1019                 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
1020                     (XFS_BUF_ISREAD(bp)) == 0)
1021                         return (xfs_bioerror_relse(bp));
1022                 else
1023                         return (xfs_bioerror(bp));
1024         }
1025 }
1026
1027
1028 int
1029 xfs_bmap(bhv_desc_t     *bdp,
1030         xfs_off_t       offset,
1031         ssize_t         count,
1032         int             flags,
1033         xfs_iomap_t     *iomapp,
1034         int             *niomaps)
1035 {
1036         xfs_inode_t     *ip = XFS_BHVTOI(bdp);
1037         xfs_iocore_t    *io = &ip->i_iocore;
1038
1039         ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
1040         ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
1041                ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
1042
1043         return xfs_iomap(io, offset, count, flags, iomapp, niomaps);
1044 }
1045
1046 /*
1047  * Wrapper around bdstrat so that we can stop data
1048  * from going to disk in case we are shutting down the filesystem.
1049  * Typically user data goes thru this path; one of the exceptions
1050  * is the superblock.
1051  */
1052 int
1053 xfsbdstrat(
1054         struct xfs_mount        *mp,
1055         struct xfs_buf          *bp)
1056 {
1057         ASSERT(mp);
1058         if (!XFS_FORCED_SHUTDOWN(mp)) {
1059                 /* Grio redirection would go here
1060                  * if (XFS_BUF_IS_GRIO(bp)) {
1061                  */
1062
1063                 xfs_buf_iorequest(bp);
1064                 return 0;
1065         }
1066
1067         xfs_buftrace("XFSBDSTRAT IOERROR", bp);
1068         return (xfs_bioerror_relse(bp));
1069 }
1070
1071 /*
1072  * If the underlying (data/log/rt) device is readonly, there are some
1073  * operations that cannot proceed.
1074  */
1075 int
1076 xfs_dev_is_read_only(
1077         xfs_mount_t             *mp,
1078         char                    *message)
1079 {
1080         if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
1081             xfs_readonly_buftarg(mp->m_logdev_targp) ||
1082             (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
1083                 cmn_err(CE_NOTE,
1084                         "XFS: %s required on read-only device.", message);
1085                 cmn_err(CE_NOTE,
1086                         "XFS: write access unavailable, cannot proceed.");
1087                 return EROFS;
1088         }
1089         return 0;
1090 }