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