a3ba857d5f95cbdd95070d2a81025c2c3b13d3b7
[linux-flexiantxendom0-3.2.10.git] / fs / xfs / linux / xfs_lrw.c
1 /*
2  * Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  *
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  * For further information regarding this notice, see:
29  *
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32 /*
33  *  fs/xfs/linux/xfs_lrw.c (Linux Read Write stuff)
34  *
35  */
36
37 #include "xfs.h"
38
39 #include "xfs_fs.h"
40 #include "xfs_inum.h"
41 #include "xfs_log.h"
42 #include "xfs_trans.h"
43 #include "xfs_sb.h"
44 #include "xfs_ag.h"
45 #include "xfs_dir.h"
46 #include "xfs_dir2.h"
47 #include "xfs_alloc.h"
48 #include "xfs_dmapi.h"
49 #include "xfs_quota.h"
50 #include "xfs_mount.h"
51 #include "xfs_alloc_btree.h"
52 #include "xfs_bmap_btree.h"
53 #include "xfs_ialloc_btree.h"
54 #include "xfs_btree.h"
55 #include "xfs_ialloc.h"
56 #include "xfs_attr_sf.h"
57 #include "xfs_dir_sf.h"
58 #include "xfs_dir2_sf.h"
59 #include "xfs_dinode.h"
60 #include "xfs_inode.h"
61 #include "xfs_bmap.h"
62 #include "xfs_bit.h"
63 #include "xfs_rtalloc.h"
64 #include "xfs_error.h"
65 #include "xfs_itable.h"
66 #include "xfs_rw.h"
67 #include "xfs_acl.h"
68 #include "xfs_cap.h"
69 #include "xfs_mac.h"
70 #include "xfs_attr.h"
71 #include "xfs_inode_item.h"
72 #include "xfs_buf_item.h"
73 #include "xfs_utils.h"
74
75 #include <linux/capability.h>
76
77
78 /*
79  *      xfs_iozero
80  *
81  *      xfs_iozero clears the specified range of buffer supplied,
82  *      and marks all the affected blocks as valid and modified.  If
83  *      an affected block is not allocated, it will be allocated.  If
84  *      an affected block is not completely overwritten, and is not
85  *      valid before the operation, it will be read from disk before
86  *      being partially zeroed.
87  */
88 STATIC int
89 xfs_iozero(
90         struct inode            *ip,    /* inode                        */
91         loff_t                  pos,    /* offset in file               */
92         size_t                  count,  /* size of data to zero         */
93         loff_t                  end_size)       /* max file size to set */
94 {
95         unsigned                bytes;
96         struct page             *page;
97         struct address_space    *mapping;
98         char                    *kaddr;
99         int                     status;
100
101         mapping = ip->i_mapping;
102         do {
103                 unsigned long index, offset;
104
105                 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
106                 index = pos >> PAGE_CACHE_SHIFT;
107                 bytes = PAGE_CACHE_SIZE - offset;
108                 if (bytes > count)
109                         bytes = count;
110
111                 status = -ENOMEM;
112                 page = grab_cache_page(mapping, index);
113                 if (!page)
114                         break;
115
116                 kaddr = kmap(page);
117                 status = mapping->a_ops->prepare_write(NULL, page, offset,
118                                                         offset + bytes);
119                 if (status) {
120                         goto unlock;
121                 }
122
123                 memset((void *) (kaddr + offset), 0, bytes);
124                 flush_dcache_page(page);
125                 status = mapping->a_ops->commit_write(NULL, page, offset,
126                                                         offset + bytes);
127                 if (!status) {
128                         pos += bytes;
129                         count -= bytes;
130                         if (pos > ip->i_size)
131                                 ip->i_size = pos < end_size ? pos : end_size;
132                 }
133
134 unlock:
135                 kunmap(page);
136                 unlock_page(page);
137                 page_cache_release(page);
138                 if (status)
139                         break;
140         } while (count);
141
142         return (-status);
143 }
144
145 ssize_t                 /* bytes read, or (-)  error */
146 xfs_read(
147         bhv_desc_t              *bdp,
148         struct file             *filp,
149         const struct iovec      *iovp,
150         unsigned long           segs,
151         loff_t                  *offp,
152         cred_t                  *credp)
153 {
154         size_t                  size = 0;
155         ssize_t                 ret;
156         xfs_fsize_t             n;
157         xfs_inode_t             *ip;
158         xfs_mount_t             *mp;
159         vnode_t                 *vp;
160         unsigned long           seg;
161         int                     direct = (filp->f_flags & O_DIRECT);
162         int                     invisible = (filp->f_mode & FINVIS);
163
164         ip = XFS_BHVTOI(bdp);
165         vp = BHV_TO_VNODE(bdp);
166         mp = ip->i_mount;
167         vn_trace_entry(vp, "xfs_read", (inst_t *)__return_address);
168
169         XFS_STATS_INC(xfsstats.xs_read_calls);
170
171         /* START copy & waste from filemap.c */
172         for (seg = 0; seg < segs; seg++) {
173                 const struct iovec *iv = &iovp[seg];
174
175                 /*
176                  * If any segment has a negative length, or the cumulative
177                  * length ever wraps negative then return -EINVAL.
178                  */
179                 size += iv->iov_len;
180                 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
181                         return XFS_ERROR(-EINVAL);
182                 if (direct) {   /* XFS specific check */
183                         if ((__psint_t)iv->iov_base & BBMASK) {
184                                 if (*offp == ip->i_d.di_size)
185                                         return 0;
186                                 return XFS_ERROR(-EINVAL);
187                         }
188                 }
189                 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
190                         continue;
191                 if (seg == 0)
192                         return XFS_ERROR(-EFAULT);
193                 segs = seg;
194                 break;
195         }
196         /* END copy & waste from filemap.c */
197
198         if (direct) {
199                 if ((*offp & mp->m_blockmask) ||
200                     (size & mp->m_blockmask)) {
201                         if (*offp == ip->i_d.di_size) {
202                                 return (0);
203                         }
204                         return -XFS_ERROR(EINVAL);
205                 }
206         }
207
208         n = XFS_MAX_FILE_OFFSET - *offp;
209         if ((n <= 0) || (size == 0))
210                 return 0;
211
212         if (n < size)
213                 size = n;
214
215         if (XFS_FORCED_SHUTDOWN(mp)) {
216                 return -EIO;
217         }
218
219         xfs_ilock(ip, XFS_IOLOCK_SHARED);
220
221         if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ) && !invisible) {
222                 int error;
223                 vrwlock_t locktype = VRWLOCK_READ;
224
225                 error = XFS_SEND_DATA(mp, DM_EVENT_READ, bdp, *offp, size,
226                                       FILP_DELAY_FLAG(filp), &locktype);
227                 if (error) {
228                         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
229                         return -error;
230                 }
231         }
232
233         ret = generic_file_readv(filp, iovp, segs, offp);
234         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
235
236         XFS_STATS_ADD(xfsstats.xs_read_bytes, ret);
237
238         if (!invisible)
239                 xfs_ichgtime(ip, XFS_ICHGTIME_ACC);
240
241         return ret;
242 }
243
244 ssize_t
245 xfs_sendfile(
246         bhv_desc_t              *bdp,
247         struct file             *filp,
248         loff_t                  *offp,
249         size_t                  count,
250         read_actor_t            actor,
251         void                    *target,
252         cred_t                  *credp)
253 {
254         ssize_t                 ret;
255         xfs_fsize_t             n;
256         xfs_inode_t             *ip;
257         xfs_mount_t             *mp;
258         vnode_t                 *vp;
259         int                     invisible = (filp->f_mode & FINVIS);
260
261         ip = XFS_BHVTOI(bdp);
262         vp = BHV_TO_VNODE(bdp);
263         mp = ip->i_mount;
264         vn_trace_entry(vp, "xfs_sendfile", (inst_t *)__return_address);
265
266         XFS_STATS_INC(xfsstats.xs_read_calls);
267
268         n = XFS_MAX_FILE_OFFSET - *offp;
269         if ((n <= 0) || (count == 0))
270                 return 0;
271
272         if (n < count)
273                 count = n;
274
275         if (XFS_FORCED_SHUTDOWN(ip->i_mount))
276                 return -EIO;
277
278         xfs_ilock(ip, XFS_IOLOCK_SHARED);
279         if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ) && !invisible) {
280                 vrwlock_t locktype = VRWLOCK_READ;
281                 int error;
282
283                 error = XFS_SEND_DATA(mp, DM_EVENT_READ, bdp, *offp, count,
284                                       FILP_DELAY_FLAG(filp), &locktype);
285                 if (error) {
286                         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
287                         return -error;
288                 }
289         }
290         ret = generic_file_sendfile(filp, offp, count, actor, target);
291         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
292
293         XFS_STATS_ADD(xfsstats.xs_read_bytes, ret);
294         if (!invisible)
295                 xfs_ichgtime(ip, XFS_ICHGTIME_ACC);
296         return ret;
297 }
298
299 /*
300  * This routine is called to handle zeroing any space in the last
301  * block of the file that is beyond the EOF.  We do this since the
302  * size is being increased without writing anything to that block
303  * and we don't want anyone to read the garbage on the disk.
304  */
305 STATIC int                              /* error (positive) */
306 xfs_zero_last_block(
307         struct inode    *ip,
308         xfs_iocore_t    *io,
309         xfs_off_t       offset,
310         xfs_fsize_t     isize,
311         xfs_fsize_t     end_size)
312 {
313         xfs_fileoff_t   last_fsb;
314         xfs_mount_t     *mp;
315         int             nimaps;
316         int             zero_offset;
317         int             zero_len;
318         int             isize_fsb_offset;
319         int             error = 0;
320         xfs_bmbt_irec_t imap;
321         loff_t          loff;
322         size_t          lsize;
323
324         ASSERT(ismrlocked(io->io_lock, MR_UPDATE) != 0);
325         ASSERT(offset > isize);
326
327         mp = io->io_mount;
328
329         isize_fsb_offset = XFS_B_FSB_OFFSET(mp, isize);
330         if (isize_fsb_offset == 0) {
331                 /*
332                  * There are no extra bytes in the last block on disk to
333                  * zero, so return.
334                  */
335                 return 0;
336         }
337
338         last_fsb = XFS_B_TO_FSBT(mp, isize);
339         nimaps = 1;
340         error = XFS_BMAPI(mp, NULL, io, last_fsb, 1, 0, NULL, 0, &imap,
341                           &nimaps, NULL);
342         if (error) {
343                 return error;
344         }
345         ASSERT(nimaps > 0);
346         /*
347          * If the block underlying isize is just a hole, then there
348          * is nothing to zero.
349          */
350         if (imap.br_startblock == HOLESTARTBLOCK) {
351                 return 0;
352         }
353         /*
354          * Zero the part of the last block beyond the EOF, and write it
355          * out sync.  We need to drop the ilock while we do this so we
356          * don't deadlock when the buffer cache calls back to us.
357          */
358         XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
359         loff = XFS_FSB_TO_B(mp, last_fsb);
360         lsize = XFS_FSB_TO_B(mp, 1);
361
362         zero_offset = isize_fsb_offset;
363         zero_len = mp->m_sb.sb_blocksize - isize_fsb_offset;
364
365         error = xfs_iozero(ip, loff + zero_offset, zero_len, end_size);
366
367         XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
368         ASSERT(error >= 0);
369         return error;
370 }
371
372 /*
373  * Zero any on disk space between the current EOF and the new,
374  * larger EOF.  This handles the normal case of zeroing the remainder
375  * of the last block in the file and the unusual case of zeroing blocks
376  * out beyond the size of the file.  This second case only happens
377  * with fixed size extents and when the system crashes before the inode
378  * size was updated but after blocks were allocated.  If fill is set,
379  * then any holes in the range are filled and zeroed.  If not, the holes
380  * are left alone as holes.
381  */
382
383 int                                     /* error (positive) */
384 xfs_zero_eof(
385         vnode_t         *vp,
386         xfs_iocore_t    *io,
387         xfs_off_t       offset,         /* starting I/O offset */
388         xfs_fsize_t     isize,          /* current inode size */
389         xfs_fsize_t     end_size)       /* terminal inode size */
390 {
391         struct inode    *ip = LINVFS_GET_IP(vp);
392         xfs_fileoff_t   start_zero_fsb;
393         xfs_fileoff_t   end_zero_fsb;
394         xfs_fileoff_t   prev_zero_fsb;
395         xfs_fileoff_t   zero_count_fsb;
396         xfs_fileoff_t   last_fsb;
397         xfs_extlen_t    buf_len_fsb;
398         xfs_extlen_t    prev_zero_count;
399         xfs_mount_t     *mp;
400         int             nimaps;
401         int             error = 0;
402         xfs_bmbt_irec_t imap;
403         loff_t          loff;
404         size_t          lsize;
405
406         ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
407         ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
408
409         mp = io->io_mount;
410
411         /*
412          * First handle zeroing the block on which isize resides.
413          * We only zero a part of that block so it is handled specially.
414          */
415         error = xfs_zero_last_block(ip, io, offset, isize, end_size);
416         if (error) {
417                 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
418                 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
419                 return error;
420         }
421
422         /*
423          * Calculate the range between the new size and the old
424          * where blocks needing to be zeroed may exist.  To get the
425          * block where the last byte in the file currently resides,
426          * we need to subtract one from the size and truncate back
427          * to a block boundary.  We subtract 1 in case the size is
428          * exactly on a block boundary.
429          */
430         last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
431         start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
432         end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
433         ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
434         if (last_fsb == end_zero_fsb) {
435                 /*
436                  * The size was only incremented on its last block.
437                  * We took care of that above, so just return.
438                  */
439                 return 0;
440         }
441
442         ASSERT(start_zero_fsb <= end_zero_fsb);
443         prev_zero_fsb = NULLFILEOFF;
444         prev_zero_count = 0;
445         while (start_zero_fsb <= end_zero_fsb) {
446                 nimaps = 1;
447                 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
448                 error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb,
449                                   0, NULL, 0, &imap, &nimaps, NULL);
450                 if (error) {
451                         ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
452                         ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
453                         return error;
454                 }
455                 ASSERT(nimaps > 0);
456
457                 if (imap.br_startblock == HOLESTARTBLOCK) {
458                         /*
459                          * This loop handles initializing pages that were
460                          * partially initialized by the code below this
461                          * loop. It basically zeroes the part of the page
462                          * that sits on a hole and sets the page as P_HOLE
463                          * and calls remapf if it is a mapped file.
464                          */
465                         prev_zero_fsb = NULLFILEOFF;
466                         prev_zero_count = 0;
467                         start_zero_fsb = imap.br_startoff +
468                                          imap.br_blockcount;
469                         ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
470                         continue;
471                 }
472
473                 /*
474                  * There are blocks in the range requested.
475                  * Zero them a single write at a time.  We actually
476                  * don't zero the entire range returned if it is
477                  * too big and simply loop around to get the rest.
478                  * That is not the most efficient thing to do, but it
479                  * is simple and this path should not be exercised often.
480                  */
481                 buf_len_fsb = XFS_FILBLKS_MIN(imap.br_blockcount,
482                                               mp->m_writeio_blocks << 8);
483                 /*
484                  * Drop the inode lock while we're doing the I/O.
485                  * We'll still have the iolock to protect us.
486                  */
487                 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
488
489                 loff = XFS_FSB_TO_B(mp, start_zero_fsb);
490                 lsize = XFS_FSB_TO_B(mp, buf_len_fsb);
491
492                 error = xfs_iozero(ip, loff, lsize, end_size);
493
494                 if (error) {
495                         goto out_lock;
496                 }
497
498                 prev_zero_fsb = start_zero_fsb;
499                 prev_zero_count = buf_len_fsb;
500                 start_zero_fsb = imap.br_startoff + buf_len_fsb;
501                 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
502
503                 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
504         }
505
506         return 0;
507
508 out_lock:
509
510         XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
511         ASSERT(error >= 0);
512         return error;
513 }
514
515 ssize_t                         /* bytes written, or (-) error */
516 xfs_write(
517         bhv_desc_t              *bdp,
518         struct file             *file,
519         const struct iovec      *iovp,
520         unsigned long           segs,
521         loff_t                  *offset,
522         cred_t                  *credp)
523 {
524         size_t                  size = 0;
525         xfs_inode_t             *xip;
526         xfs_mount_t             *mp;
527         ssize_t                 ret;
528         int                     error = 0;
529         xfs_fsize_t             isize, new_size;
530         xfs_fsize_t             n, limit = XFS_MAX_FILE_OFFSET;
531         xfs_iocore_t            *io;
532         vnode_t                 *vp;
533         unsigned long           seg;
534         int                     iolock;
535         int                     direct = (file->f_flags & O_DIRECT);
536         int                     invisible = (file->f_mode & FINVIS);
537         int                     eventsent = 0;
538         vrwlock_t               locktype;
539
540         XFS_STATS_INC(xfsstats.xs_write_calls);
541
542         vp = BHV_TO_VNODE(bdp);
543         vn_trace_entry(vp, "xfs_write", (inst_t *)__return_address);
544         xip = XFS_BHVTOI(bdp);
545
546         /* START copy & waste from filemap.c */
547         for (seg = 0; seg < segs; seg++) {
548                 const struct iovec *iv = &iovp[seg];
549
550                 /*
551                  * If any segment has a negative length, or the cumulative
552                  * length ever wraps negative then return -EINVAL.
553                  */
554                 size += iv->iov_len;
555                 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
556                         return XFS_ERROR(-EINVAL);
557                 if (direct) {   /* XFS specific check */
558                         if ((__psint_t)iv->iov_base & BBMASK)
559                                 return XFS_ERROR(-EINVAL);
560                 }
561                 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
562                         continue;
563                 if (seg == 0)
564                         return XFS_ERROR(-EFAULT);
565                 segs = seg;
566                 break;
567         }
568         /* END copy & waste from filemap.c */
569
570         if (size == 0)
571                 return 0;
572
573         io = &(xip->i_iocore);
574         mp = io->io_mount;
575
576         xfs_check_frozen(mp, bdp, XFS_FREEZE_WRITE);
577
578         if (XFS_FORCED_SHUTDOWN(xip->i_mount)) {
579                 return -EIO;
580         }
581
582         if (direct) {
583                 if ((*offset & mp->m_blockmask) ||
584                     (size & mp->m_blockmask)) {
585                         return XFS_ERROR(-EINVAL);
586                 }
587                 iolock = XFS_IOLOCK_SHARED;
588                 locktype = VRWLOCK_WRITE_DIRECT;
589         } else {
590                 iolock = XFS_IOLOCK_EXCL;
591                 locktype = VRWLOCK_WRITE;
592         }
593
594         xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
595         isize = xip->i_d.di_size;
596
597         if (file->f_flags & O_APPEND)
598                 *offset = isize;
599
600 start:
601         n = limit - *offset;
602         if (n <= 0) {
603                 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
604                 return -EFBIG;
605         }
606
607         if (n < size)
608                 size = n;
609
610         new_size = *offset + size;
611         if (new_size > isize) {
612                 io->io_new_size = new_size;
613         }
614
615         if ((DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_WRITE) &&
616             !invisible && !eventsent)) {
617                 loff_t          savedsize = *offset;
618
619                 xfs_iunlock(xip, XFS_ILOCK_EXCL);
620                 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, bdp,
621                                       *offset, size,
622                                       FILP_DELAY_FLAG(file), &locktype);
623                 if (error) {
624                         xfs_iunlock(xip, iolock);
625                         return -error;
626                 }
627                 xfs_ilock(xip, XFS_ILOCK_EXCL);
628                 eventsent = 1;
629
630                 /*
631                  * The iolock was dropped and reaquired in XFS_SEND_DATA
632                  * so we have to recheck the size when appending.
633                  * We will only "goto start;" once, since having sent the
634                  * event prevents another call to XFS_SEND_DATA, which is
635                  * what allows the size to change in the first place.
636                  */
637                 if ((file->f_flags & O_APPEND) &&
638                     savedsize != xip->i_d.di_size) {
639                         *offset = isize = xip->i_d.di_size;
640                         goto start;
641                 }
642         }
643
644         /*
645          * On Linux, generic_file_write updates the times even if
646          * no data is copied in so long as the write had a size.
647          *
648          * We must update xfs' times since revalidate will overcopy xfs.
649          */
650         if (size && !invisible)
651                 xfs_ichgtime(xip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
652
653         /*
654          * If the offset is beyond the size of the file, we have a couple
655          * of things to do. First, if there is already space allocated
656          * we need to either create holes or zero the disk or ...
657          *
658          * If there is a page where the previous size lands, we need
659          * to zero it out up to the new size.
660          */
661
662         if (!direct && (*offset > isize && isize)) {
663                 error = xfs_zero_eof(BHV_TO_VNODE(bdp), io, *offset,
664                         isize, *offset + size);
665                 if (error) {
666                         xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
667                         return(-error);
668                 }
669         }
670         xfs_iunlock(xip, XFS_ILOCK_EXCL);
671
672         /*
673          * If we're writing the file then make sure to clear the
674          * setuid and setgid bits if the process is not being run
675          * by root.  This keeps people from modifying setuid and
676          * setgid binaries.
677          */
678
679         if (((xip->i_d.di_mode & ISUID) ||
680             ((xip->i_d.di_mode & (ISGID | (IEXEC >> 3))) ==
681                 (ISGID | (IEXEC >> 3)))) &&
682              !capable(CAP_FSETID)) {
683                 error = xfs_write_clear_setuid(xip);
684                 if (error) {
685                         xfs_iunlock(xip, iolock);
686                         return -error;
687                 }
688         }
689
690 retry:
691         if (direct) {
692                 xfs_inval_cached_pages(vp, &xip->i_iocore, *offset, 1, 1);
693         }
694
695         ret = generic_file_write_nolock(file, iovp, segs, offset);
696
697         if ((ret == -ENOSPC) &&
698             DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_NOSPACE) && !invisible) {
699
700                 xfs_rwunlock(bdp, locktype);
701                 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, bdp,
702                                 DM_RIGHT_NULL, bdp, DM_RIGHT_NULL, NULL, NULL,
703                                 0, 0, 0); /* Delay flag intentionally  unused */
704                 if (error)
705                         return -error;
706                 xfs_rwlock(bdp, locktype);
707                 *offset = xip->i_d.di_size;
708                 goto retry;
709
710         }
711
712         if (ret <= 0) {
713                 xfs_rwunlock(bdp, locktype);
714                 return ret;
715         }
716
717         XFS_STATS_ADD(xfsstats.xs_write_bytes, ret);
718
719         if (*offset > xip->i_d.di_size) {
720                 xfs_ilock(xip, XFS_ILOCK_EXCL);
721                 if (*offset > xip->i_d.di_size) {
722                         struct inode    *inode = LINVFS_GET_IP(vp);
723
724                         inode->i_size = xip->i_d.di_size = *offset;
725                         xip->i_update_core = 1;
726                         xip->i_update_size = 1;
727                 }
728                 xfs_iunlock(xip, XFS_ILOCK_EXCL);
729         }
730
731         /* Handle various SYNC-type writes */
732         if ((file->f_flags & O_SYNC) || IS_SYNC(file->f_dentry->d_inode)) {
733
734                 /*
735                  * If we're treating this as O_DSYNC and we have not updated the
736                  * size, force the log.
737                  */
738
739                 if (!(mp->m_flags & XFS_MOUNT_OSYNCISOSYNC)
740                         && !(xip->i_update_size)) {
741                         /*
742                          * If an allocation transaction occurred
743                          * without extending the size, then we have to force
744                          * the log up the proper point to ensure that the
745                          * allocation is permanent.  We can't count on
746                          * the fact that buffered writes lock out direct I/O
747                          * writes - the direct I/O write could have extended
748                          * the size nontransactionally, then finished before
749                          * we started.  xfs_write_file will think that the file
750                          * didn't grow but the update isn't safe unless the
751                          * size change is logged.
752                          *
753                          * Force the log if we've committed a transaction
754                          * against the inode or if someone else has and
755                          * the commit record hasn't gone to disk (e.g.
756                          * the inode is pinned).  This guarantees that
757                          * all changes affecting the inode are permanent
758                          * when we return.
759                          */
760
761                         xfs_inode_log_item_t *iip;
762                         xfs_lsn_t lsn;
763
764                         iip = xip->i_itemp;
765                         if (iip && iip->ili_last_lsn) {
766                                 lsn = iip->ili_last_lsn;
767                                 xfs_log_force(mp, lsn,
768                                                 XFS_LOG_FORCE | XFS_LOG_SYNC);
769                         } else if (xfs_ipincount(xip) > 0) {
770                                 xfs_log_force(mp, (xfs_lsn_t)0,
771                                                 XFS_LOG_FORCE | XFS_LOG_SYNC);
772                         }
773
774                 } else {
775                         xfs_trans_t     *tp;
776
777                         /*
778                          * O_SYNC or O_DSYNC _with_ a size update are handled
779                          * the same way.
780                          *
781                          * If the write was synchronous then we need to make
782                          * sure that the inode modification time is permanent.
783                          * We'll have updated the timestamp above, so here
784                          * we use a synchronous transaction to log the inode.
785                          * It's not fast, but it's necessary.
786                          *
787                          * If this a dsync write and the size got changed
788                          * non-transactionally, then we need to ensure that
789                          * the size change gets logged in a synchronous
790                          * transaction.
791                          */
792
793                         tp = xfs_trans_alloc(mp, XFS_TRANS_WRITE_SYNC);
794                         if ((error = xfs_trans_reserve(tp, 0,
795                                                       XFS_SWRITE_LOG_RES(mp),
796                                                       0, 0, 0))) {
797                                 /* Transaction reserve failed */
798                                 xfs_trans_cancel(tp, 0);
799                         } else {
800                                 /* Transaction reserve successful */
801                                 xfs_ilock(xip, XFS_ILOCK_EXCL);
802                                 xfs_trans_ijoin(tp, xip, XFS_ILOCK_EXCL);
803                                 xfs_trans_ihold(tp, xip);
804                                 xfs_trans_log_inode(tp, xip, XFS_ILOG_CORE);
805                                 xfs_trans_set_sync(tp);
806                                 error = xfs_trans_commit(tp, 0, (xfs_lsn_t)0);
807                                 xfs_iunlock(xip, XFS_ILOCK_EXCL);
808                         }
809                 }
810         } /* (ioflags & O_SYNC) */
811
812         xfs_rwunlock(bdp, locktype);
813         return(ret);
814 }
815
816 /*
817  * All xfs metadata buffers except log state machine buffers
818  * get this attached as their b_bdstrat callback function.
819  * This is so that we can catch a buffer
820  * after prematurely unpinning it to forcibly shutdown the filesystem.
821  */
822 int
823 xfs_bdstrat_cb(struct xfs_buf *bp)
824 {
825         xfs_mount_t     *mp;
826
827         mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
828         if (!XFS_FORCED_SHUTDOWN(mp)) {
829                 pagebuf_iorequest(bp);
830                 return 0;
831         } else {
832                 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
833                 /*
834                  * Metadata write that didn't get logged but
835                  * written delayed anyway. These aren't associated
836                  * with a transaction, and can be ignored.
837                  */
838                 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
839                     (XFS_BUF_ISREAD(bp)) == 0)
840                         return (xfs_bioerror_relse(bp));
841                 else
842                         return (xfs_bioerror(bp));
843         }
844 }
845
846
847 int
848 xfs_bmap(bhv_desc_t     *bdp,
849         xfs_off_t       offset,
850         ssize_t         count,
851         int             flags,
852         page_buf_bmap_t *pbmapp,
853         int             *npbmaps)
854 {
855         xfs_inode_t     *ip = XFS_BHVTOI(bdp);
856         xfs_iocore_t    *io = &ip->i_iocore;
857
858         ASSERT((ip->i_d.di_mode & IFMT) == IFREG);
859         ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
860                ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
861
862         return xfs_iomap(io, offset, count, flags, pbmapp, npbmaps);
863 }
864
865 /*
866  * Wrapper around bdstrat so that we can stop data
867  * from going to disk in case we are shutting down the filesystem.
868  * Typically user data goes thru this path; one of the exceptions
869  * is the superblock.
870  */
871 int
872 xfsbdstrat(
873         struct xfs_mount        *mp,
874         struct xfs_buf          *bp)
875 {
876         ASSERT(mp);
877         if (!XFS_FORCED_SHUTDOWN(mp)) {
878                 /* Grio redirection would go here
879                  * if (XFS_BUF_IS_GRIO(bp)) {
880                  */
881
882                 pagebuf_iorequest(bp);
883                 return 0;
884         }
885
886         xfs_buftrace("XFSBDSTRAT IOERROR", bp);
887         return (xfs_bioerror_relse(bp));
888 }
889
890 /*
891  * If the underlying (data/log/rt) device is readonly, there are some
892  * operations that cannot proceed.
893  */
894 int
895 xfs_dev_is_read_only(
896         xfs_mount_t             *mp,
897         char                    *message)
898 {
899         if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
900             xfs_readonly_buftarg(mp->m_logdev_targp) ||
901             (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
902                 cmn_err(CE_NOTE,
903                         "XFS: %s required on read-only device.", message);
904                 cmn_err(CE_NOTE,
905                         "XFS: write access unavailable, cannot proceed.");
906                 return EROFS;
907         }
908         return 0;
909 }