commented early_printk patch because of rejects.
[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 > i_size_read(ip))
131                                 i_size_write(ip, 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 kiocb            *iocb,
149         const struct iovec      *iovp,
150         unsigned int            segs,
151         loff_t                  *offset,
152         cred_t                  *credp)
153 {
154         struct file             *file = iocb->ki_filp;
155         size_t                  size = 0;
156         ssize_t                 ret;
157         xfs_fsize_t             n;
158         xfs_inode_t             *ip;
159         xfs_mount_t             *mp;
160         vnode_t                 *vp;
161         unsigned long           seg;
162         int                     direct = (file->f_flags & O_DIRECT);
163         int                     invisible = (file->f_mode & FINVIS);
164
165         ip = XFS_BHVTOI(bdp);
166         vp = BHV_TO_VNODE(bdp);
167         mp = ip->i_mount;
168         vn_trace_entry(vp, "xfs_read", (inst_t *)__return_address);
169
170         XFS_STATS_INC(xfsstats.xs_read_calls);
171
172         /* START copy & waste from filemap.c */
173         for (seg = 0; seg < segs; seg++) {
174                 const struct iovec *iv = &iovp[seg];
175
176                 /*
177                  * If any segment has a negative length, or the cumulative
178                  * length ever wraps negative then return -EINVAL.
179                  */
180                 size += iv->iov_len;
181                 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
182                         return XFS_ERROR(-EINVAL);
183         }
184         /* END copy & waste from filemap.c */
185
186         if (direct) {
187                 pb_target_t     *target =
188                         (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
189                                 mp->m_rtdev_targp : mp->m_ddev_targp;
190                 if ((*offset & target->pbr_smask) ||
191                     (size & target->pbr_smask)) {
192                         if (*offset == ip->i_d.di_size) {
193                                 return (0);
194                         }
195                         return -XFS_ERROR(EINVAL);
196                 }
197         }
198
199         n = XFS_MAXIOFFSET(mp) - *offset;
200         if ((n <= 0) || (size == 0))
201                 return 0;
202
203         if (n < size)
204                 size = n;
205
206         if (XFS_FORCED_SHUTDOWN(mp)) {
207                 return -EIO;
208         }
209
210         /* OK so we are holding the I/O lock for the duration
211          * of the submission, then what happens if the I/O
212          * does not really happen here, but is scheduled 
213          * later?
214          */
215         xfs_ilock(ip, XFS_IOLOCK_SHARED);
216
217         if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ) && !invisible) {
218                 int error;
219                 vrwlock_t locktype = VRWLOCK_READ;
220
221                 error = XFS_SEND_DATA(mp, DM_EVENT_READ, bdp, *offset, size,
222                                       FILP_DELAY_FLAG(file), &locktype);
223                 if (error) {
224                         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
225                         return -error;
226                 }
227         }
228
229         /* We need to deal with the iovec case seperately here */
230         ret = __generic_file_aio_read(iocb, iovp, segs, offset);
231         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
232
233         if (ret > 0)
234                 XFS_STATS_ADD(xfsstats.xs_read_bytes, ret);
235
236         if (!invisible)
237                 xfs_ichgtime(ip, XFS_ICHGTIME_ACC);
238
239         return ret;
240 }
241
242 ssize_t
243 xfs_sendfile(
244         bhv_desc_t              *bdp,
245         struct file             *filp,
246         loff_t                  *offset,
247         size_t                  count,
248         read_actor_t            actor,
249         void                    *target,
250         cred_t                  *credp)
251 {
252         ssize_t                 ret;
253         xfs_fsize_t             n;
254         xfs_inode_t             *ip;
255         xfs_mount_t             *mp;
256         vnode_t                 *vp;
257         int                     invisible = (filp->f_mode & FINVIS);
258
259         ip = XFS_BHVTOI(bdp);
260         vp = BHV_TO_VNODE(bdp);
261         mp = ip->i_mount;
262         vn_trace_entry(vp, "xfs_sendfile", (inst_t *)__return_address);
263
264         XFS_STATS_INC(xfsstats.xs_read_calls);
265
266         n = XFS_MAXIOFFSET(mp) - *offset;
267         if ((n <= 0) || (count == 0))
268                 return 0;
269
270         if (n < count)
271                 count = n;
272
273         if (XFS_FORCED_SHUTDOWN(ip->i_mount))
274                 return -EIO;
275
276         xfs_ilock(ip, XFS_IOLOCK_SHARED);
277         if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ) && !invisible) {
278                 vrwlock_t locktype = VRWLOCK_READ;
279                 int error;
280
281                 error = XFS_SEND_DATA(mp, DM_EVENT_READ, bdp, *offset, count,
282                                       FILP_DELAY_FLAG(filp), &locktype);
283                 if (error) {
284                         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
285                         return -error;
286                 }
287         }
288         ret = generic_file_sendfile(filp, offset, count, actor, target);
289         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
290
291         XFS_STATS_ADD(xfsstats.xs_read_bytes, ret);
292         if (!invisible)
293                 xfs_ichgtime(ip, XFS_ICHGTIME_ACC);
294         return ret;
295 }
296
297 /*
298  * This routine is called to handle zeroing any space in the last
299  * block of the file that is beyond the EOF.  We do this since the
300  * size is being increased without writing anything to that block
301  * and we don't want anyone to read the garbage on the disk.
302  */
303 STATIC int                              /* error (positive) */
304 xfs_zero_last_block(
305         struct inode    *ip,
306         xfs_iocore_t    *io,
307         xfs_off_t       offset,
308         xfs_fsize_t     isize,
309         xfs_fsize_t     end_size)
310 {
311         xfs_fileoff_t   last_fsb;
312         xfs_mount_t     *mp;
313         int             nimaps;
314         int             zero_offset;
315         int             zero_len;
316         int             isize_fsb_offset;
317         int             error = 0;
318         xfs_bmbt_irec_t imap;
319         loff_t          loff;
320         size_t          lsize;
321
322         ASSERT(ismrlocked(io->io_lock, MR_UPDATE) != 0);
323         ASSERT(offset > isize);
324
325         mp = io->io_mount;
326
327         isize_fsb_offset = XFS_B_FSB_OFFSET(mp, isize);
328         if (isize_fsb_offset == 0) {
329                 /*
330                  * There are no extra bytes in the last block on disk to
331                  * zero, so return.
332                  */
333                 return 0;
334         }
335
336         last_fsb = XFS_B_TO_FSBT(mp, isize);
337         nimaps = 1;
338         error = XFS_BMAPI(mp, NULL, io, last_fsb, 1, 0, NULL, 0, &imap,
339                           &nimaps, NULL);
340         if (error) {
341                 return error;
342         }
343         ASSERT(nimaps > 0);
344         /*
345          * If the block underlying isize is just a hole, then there
346          * is nothing to zero.
347          */
348         if (imap.br_startblock == HOLESTARTBLOCK) {
349                 return 0;
350         }
351         /*
352          * Zero the part of the last block beyond the EOF, and write it
353          * out sync.  We need to drop the ilock while we do this so we
354          * don't deadlock when the buffer cache calls back to us.
355          */
356         XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
357         loff = XFS_FSB_TO_B(mp, last_fsb);
358         lsize = XFS_FSB_TO_B(mp, 1);
359
360         zero_offset = isize_fsb_offset;
361         zero_len = mp->m_sb.sb_blocksize - isize_fsb_offset;
362
363         error = xfs_iozero(ip, loff + zero_offset, zero_len, end_size);
364
365         XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
366         ASSERT(error >= 0);
367         return error;
368 }
369
370 /*
371  * Zero any on disk space between the current EOF and the new,
372  * larger EOF.  This handles the normal case of zeroing the remainder
373  * of the last block in the file and the unusual case of zeroing blocks
374  * out beyond the size of the file.  This second case only happens
375  * with fixed size extents and when the system crashes before the inode
376  * size was updated but after blocks were allocated.  If fill is set,
377  * then any holes in the range are filled and zeroed.  If not, the holes
378  * are left alone as holes.
379  */
380
381 int                                     /* error (positive) */
382 xfs_zero_eof(
383         vnode_t         *vp,
384         xfs_iocore_t    *io,
385         xfs_off_t       offset,         /* starting I/O offset */
386         xfs_fsize_t     isize,          /* current inode size */
387         xfs_fsize_t     end_size)       /* terminal inode size */
388 {
389         struct inode    *ip = LINVFS_GET_IP(vp);
390         xfs_fileoff_t   start_zero_fsb;
391         xfs_fileoff_t   end_zero_fsb;
392         xfs_fileoff_t   prev_zero_fsb;
393         xfs_fileoff_t   zero_count_fsb;
394         xfs_fileoff_t   last_fsb;
395         xfs_extlen_t    buf_len_fsb;
396         xfs_extlen_t    prev_zero_count;
397         xfs_mount_t     *mp;
398         int             nimaps;
399         int             error = 0;
400         xfs_bmbt_irec_t imap;
401         loff_t          loff;
402         size_t          lsize;
403
404         ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
405         ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
406
407         mp = io->io_mount;
408
409         /*
410          * First handle zeroing the block on which isize resides.
411          * We only zero a part of that block so it is handled specially.
412          */
413         error = xfs_zero_last_block(ip, io, offset, isize, end_size);
414         if (error) {
415                 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
416                 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
417                 return error;
418         }
419
420         /*
421          * Calculate the range between the new size and the old
422          * where blocks needing to be zeroed may exist.  To get the
423          * block where the last byte in the file currently resides,
424          * we need to subtract one from the size and truncate back
425          * to a block boundary.  We subtract 1 in case the size is
426          * exactly on a block boundary.
427          */
428         last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
429         start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
430         end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
431         ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
432         if (last_fsb == end_zero_fsb) {
433                 /*
434                  * The size was only incremented on its last block.
435                  * We took care of that above, so just return.
436                  */
437                 return 0;
438         }
439
440         ASSERT(start_zero_fsb <= end_zero_fsb);
441         prev_zero_fsb = NULLFILEOFF;
442         prev_zero_count = 0;
443         while (start_zero_fsb <= end_zero_fsb) {
444                 nimaps = 1;
445                 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
446                 error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb,
447                                   0, NULL, 0, &imap, &nimaps, NULL);
448                 if (error) {
449                         ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
450                         ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
451                         return error;
452                 }
453                 ASSERT(nimaps > 0);
454
455                 if (imap.br_state == XFS_EXT_UNWRITTEN ||
456                     imap.br_startblock == HOLESTARTBLOCK) {
457                         /*
458                          * This loop handles initializing pages that were
459                          * partially initialized by the code below this
460                          * loop. It basically zeroes the part of the page
461                          * that sits on a hole and sets the page as P_HOLE
462                          * and calls remapf if it is a mapped file.
463                          */
464                         prev_zero_fsb = NULLFILEOFF;
465                         prev_zero_count = 0;
466                         start_zero_fsb = imap.br_startoff +
467                                          imap.br_blockcount;
468                         ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
469                         continue;
470                 }
471
472                 /*
473                  * There are blocks in the range requested.
474                  * Zero them a single write at a time.  We actually
475                  * don't zero the entire range returned if it is
476                  * too big and simply loop around to get the rest.
477                  * That is not the most efficient thing to do, but it
478                  * is simple and this path should not be exercised often.
479                  */
480                 buf_len_fsb = XFS_FILBLKS_MIN(imap.br_blockcount,
481                                               mp->m_writeio_blocks << 8);
482                 /*
483                  * Drop the inode lock while we're doing the I/O.
484                  * We'll still have the iolock to protect us.
485                  */
486                 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
487
488                 loff = XFS_FSB_TO_B(mp, start_zero_fsb);
489                 lsize = XFS_FSB_TO_B(mp, buf_len_fsb);
490
491                 error = xfs_iozero(ip, loff, lsize, end_size);
492
493                 if (error) {
494                         goto out_lock;
495                 }
496
497                 prev_zero_fsb = start_zero_fsb;
498                 prev_zero_count = buf_len_fsb;
499                 start_zero_fsb = imap.br_startoff + buf_len_fsb;
500                 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
501
502                 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
503         }
504
505         return 0;
506
507 out_lock:
508
509         XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
510         ASSERT(error >= 0);
511         return error;
512 }
513
514 ssize_t                         /* bytes written, or (-) error */
515 xfs_write(
516         bhv_desc_t              *bdp,
517         struct kiocb            *iocb,
518         const struct iovec      *iovp,
519         unsigned int            segs,
520         loff_t                  *offset,
521         cred_t                  *credp)
522 {
523         struct file             *file = iocb->ki_filp;
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;
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         }
558         /* END copy & waste from filemap.c */
559
560         if (size == 0)
561                 return 0;
562
563         io = &(xip->i_iocore);
564         mp = io->io_mount;
565
566         xfs_check_frozen(mp, bdp, XFS_FREEZE_WRITE);
567
568         if (XFS_FORCED_SHUTDOWN(mp)) {
569                 return -EIO;
570         }
571
572         if (direct) {
573                 pb_target_t     *target =
574                         (xip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
575                                 mp->m_rtdev_targp : mp->m_ddev_targp;
576
577                 if ((*offset & target->pbr_smask) ||
578                     (size & target->pbr_smask)) {
579                         return XFS_ERROR(-EINVAL);
580                 }
581                 iolock = XFS_IOLOCK_SHARED;
582                 locktype = VRWLOCK_WRITE_DIRECT;
583         } else {
584                 iolock = XFS_IOLOCK_EXCL;
585                 locktype = VRWLOCK_WRITE;
586         }
587
588         xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
589         isize = xip->i_d.di_size;
590         limit = XFS_MAXIOFFSET(mp);
591
592         if (file->f_flags & O_APPEND)
593                 *offset = isize;
594
595 start:
596         n = limit - *offset;
597         if (n <= 0) {
598                 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
599                 return -EFBIG;
600         }
601
602         if (n < size)
603                 size = n;
604
605         new_size = *offset + size;
606         if (new_size > isize) {
607                 io->io_new_size = new_size;
608         }
609
610         if ((DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_WRITE) &&
611             !invisible && !eventsent)) {
612                 loff_t          savedsize = *offset;
613
614                 xfs_iunlock(xip, XFS_ILOCK_EXCL);
615                 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, bdp,
616                                       *offset, size,
617                                       FILP_DELAY_FLAG(file), &locktype);
618                 if (error) {
619                         xfs_iunlock(xip, iolock);
620                         return -error;
621                 }
622                 xfs_ilock(xip, XFS_ILOCK_EXCL);
623                 eventsent = 1;
624
625                 /*
626                  * The iolock was dropped and reaquired in XFS_SEND_DATA
627                  * so we have to recheck the size when appending.
628                  * We will only "goto start;" once, since having sent the
629                  * event prevents another call to XFS_SEND_DATA, which is
630                  * what allows the size to change in the first place.
631                  */
632                 if ((file->f_flags & O_APPEND) &&
633                     savedsize != xip->i_d.di_size) {
634                         *offset = isize = xip->i_d.di_size;
635                         goto start;
636                 }
637         }
638
639         /*
640          * On Linux, generic_file_write updates the times even if
641          * no data is copied in so long as the write had a size.
642          *
643          * We must update xfs' times since revalidate will overcopy xfs.
644          */
645         if (size && !invisible)
646                 xfs_ichgtime(xip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
647
648         /*
649          * If the offset is beyond the size of the file, we have a couple
650          * of things to do. First, if there is already space allocated
651          * we need to either create holes or zero the disk or ...
652          *
653          * If there is a page where the previous size lands, we need
654          * to zero it out up to the new size.
655          */
656
657         if (!direct && (*offset > isize && isize)) {
658                 error = xfs_zero_eof(BHV_TO_VNODE(bdp), io, *offset,
659                         isize, *offset + size);
660                 if (error) {
661                         xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
662                         return(-error);
663                 }
664         }
665         xfs_iunlock(xip, XFS_ILOCK_EXCL);
666
667         /*
668          * If we're writing the file then make sure to clear the
669          * setuid and setgid bits if the process is not being run
670          * by root.  This keeps people from modifying setuid and
671          * setgid binaries.
672          */
673
674         if (((xip->i_d.di_mode & ISUID) ||
675             ((xip->i_d.di_mode & (ISGID | (IEXEC >> 3))) ==
676                 (ISGID | (IEXEC >> 3)))) &&
677              !capable(CAP_FSETID)) {
678                 error = xfs_write_clear_setuid(xip);
679                 if (error) {
680                         xfs_iunlock(xip, iolock);
681                         return -error;
682                 }
683         }
684
685 retry:
686         if (direct) {
687                 xfs_inval_cached_pages(vp, &xip->i_iocore, *offset, 1, 1);
688         }
689
690         ret = generic_file_aio_write_nolock(iocb, iovp, segs, offset);
691
692         if ((ret == -ENOSPC) &&
693             DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_NOSPACE) && !invisible) {
694
695                 xfs_rwunlock(bdp, locktype);
696                 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, bdp,
697                                 DM_RIGHT_NULL, bdp, DM_RIGHT_NULL, NULL, NULL,
698                                 0, 0, 0); /* Delay flag intentionally  unused */
699                 if (error)
700                         return -error;
701                 xfs_rwlock(bdp, locktype);
702                 *offset = xip->i_d.di_size;
703                 goto retry;
704
705         }
706
707         if (*offset > xip->i_d.di_size) {
708                 xfs_ilock(xip, XFS_ILOCK_EXCL);
709                 if (*offset > xip->i_d.di_size) {
710                         struct inode    *inode = LINVFS_GET_IP(vp);
711
712                         xip->i_d.di_size = *offset;
713                         i_size_write(inode, *offset);
714                         xip->i_update_core = 1;
715                         xip->i_update_size = 1;
716                 }
717                 xfs_iunlock(xip, XFS_ILOCK_EXCL);
718         }
719
720         if (ret <= 0) {
721                 xfs_rwunlock(bdp, locktype);
722                 return ret;
723         }
724
725         XFS_STATS_ADD(xfsstats.xs_write_bytes, ret);
726
727         /* Handle various SYNC-type writes */
728         if ((file->f_flags & O_SYNC) || IS_SYNC(file->f_dentry->d_inode)) {
729
730                 /*
731                  * If we're treating this as O_DSYNC and we have not updated the
732                  * size, force the log.
733                  */
734
735                 if (!(mp->m_flags & XFS_MOUNT_OSYNCISOSYNC)
736                         && !(xip->i_update_size)) {
737                         /*
738                          * If an allocation transaction occurred
739                          * without extending the size, then we have to force
740                          * the log up the proper point to ensure that the
741                          * allocation is permanent.  We can't count on
742                          * the fact that buffered writes lock out direct I/O
743                          * writes - the direct I/O write could have extended
744                          * the size nontransactionally, then finished before
745                          * we started.  xfs_write_file will think that the file
746                          * didn't grow but the update isn't safe unless the
747                          * size change is logged.
748                          *
749                          * Force the log if we've committed a transaction
750                          * against the inode or if someone else has and
751                          * the commit record hasn't gone to disk (e.g.
752                          * the inode is pinned).  This guarantees that
753                          * all changes affecting the inode are permanent
754                          * when we return.
755                          */
756
757                         xfs_inode_log_item_t *iip;
758                         xfs_lsn_t lsn;
759
760                         iip = xip->i_itemp;
761                         if (iip && iip->ili_last_lsn) {
762                                 lsn = iip->ili_last_lsn;
763                                 xfs_log_force(mp, lsn,
764                                                 XFS_LOG_FORCE | XFS_LOG_SYNC);
765                         } else if (xfs_ipincount(xip) > 0) {
766                                 xfs_log_force(mp, (xfs_lsn_t)0,
767                                                 XFS_LOG_FORCE | XFS_LOG_SYNC);
768                         }
769
770                 } else {
771                         xfs_trans_t     *tp;
772
773                         /*
774                          * O_SYNC or O_DSYNC _with_ a size update are handled
775                          * the same way.
776                          *
777                          * If the write was synchronous then we need to make
778                          * sure that the inode modification time is permanent.
779                          * We'll have updated the timestamp above, so here
780                          * we use a synchronous transaction to log the inode.
781                          * It's not fast, but it's necessary.
782                          *
783                          * If this a dsync write and the size got changed
784                          * non-transactionally, then we need to ensure that
785                          * the size change gets logged in a synchronous
786                          * transaction.
787                          */
788
789                         tp = xfs_trans_alloc(mp, XFS_TRANS_WRITE_SYNC);
790                         if ((error = xfs_trans_reserve(tp, 0,
791                                                       XFS_SWRITE_LOG_RES(mp),
792                                                       0, 0, 0))) {
793                                 /* Transaction reserve failed */
794                                 xfs_trans_cancel(tp, 0);
795                         } else {
796                                 /* Transaction reserve successful */
797                                 xfs_ilock(xip, XFS_ILOCK_EXCL);
798                                 xfs_trans_ijoin(tp, xip, XFS_ILOCK_EXCL);
799                                 xfs_trans_ihold(tp, xip);
800                                 xfs_trans_log_inode(tp, xip, XFS_ILOG_CORE);
801                                 xfs_trans_set_sync(tp);
802                                 error = xfs_trans_commit(tp, 0, (xfs_lsn_t)0);
803                                 xfs_iunlock(xip, XFS_ILOCK_EXCL);
804                         }
805                 }
806         } /* (ioflags & O_SYNC) */
807
808         xfs_rwunlock(bdp, locktype);
809         return(ret);
810 }
811
812 /*
813  * All xfs metadata buffers except log state machine buffers
814  * get this attached as their b_bdstrat callback function.
815  * This is so that we can catch a buffer
816  * after prematurely unpinning it to forcibly shutdown the filesystem.
817  */
818 int
819 xfs_bdstrat_cb(struct xfs_buf *bp)
820 {
821         xfs_mount_t     *mp;
822
823         mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
824         if (!XFS_FORCED_SHUTDOWN(mp)) {
825                 pagebuf_iorequest(bp);
826                 return 0;
827         } else {
828                 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
829                 /*
830                  * Metadata write that didn't get logged but
831                  * written delayed anyway. These aren't associated
832                  * with a transaction, and can be ignored.
833                  */
834                 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
835                     (XFS_BUF_ISREAD(bp)) == 0)
836                         return (xfs_bioerror_relse(bp));
837                 else
838                         return (xfs_bioerror(bp));
839         }
840 }
841
842
843 int
844 xfs_bmap(bhv_desc_t     *bdp,
845         xfs_off_t       offset,
846         ssize_t         count,
847         int             flags,
848         page_buf_bmap_t *pbmapp,
849         int             *npbmaps)
850 {
851         xfs_inode_t     *ip = XFS_BHVTOI(bdp);
852         xfs_iocore_t    *io = &ip->i_iocore;
853
854         ASSERT((ip->i_d.di_mode & IFMT) == IFREG);
855         ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
856                ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
857
858         return xfs_iomap(io, offset, count, flags, pbmapp, npbmaps);
859 }
860
861 /*
862  * Wrapper around bdstrat so that we can stop data
863  * from going to disk in case we are shutting down the filesystem.
864  * Typically user data goes thru this path; one of the exceptions
865  * is the superblock.
866  */
867 int
868 xfsbdstrat(
869         struct xfs_mount        *mp,
870         struct xfs_buf          *bp)
871 {
872         ASSERT(mp);
873         if (!XFS_FORCED_SHUTDOWN(mp)) {
874                 /* Grio redirection would go here
875                  * if (XFS_BUF_IS_GRIO(bp)) {
876                  */
877
878                 pagebuf_iorequest(bp);
879                 return 0;
880         }
881
882         xfs_buftrace("XFSBDSTRAT IOERROR", bp);
883         return (xfs_bioerror_relse(bp));
884 }
885
886 /*
887  * If the underlying (data/log/rt) device is readonly, there are some
888  * operations that cannot proceed.
889  */
890 int
891 xfs_dev_is_read_only(
892         xfs_mount_t             *mp,
893         char                    *message)
894 {
895         if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
896             xfs_readonly_buftarg(mp->m_logdev_targp) ||
897             (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
898                 cmn_err(CE_NOTE,
899                         "XFS: %s required on read-only device.", message);
900                 cmn_err(CE_NOTE,
901                         "XFS: write access unavailable, cannot proceed.");
902                 return EROFS;
903         }
904         return 0;
905 }