]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/gnu/fs/xfs/FreeBSD/xfs_frw.c
This commit was generated by cvs2svn to compensate for changes in r155131,
[FreeBSD/FreeBSD.git] / sys / gnu / fs / xfs / FreeBSD / xfs_frw.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_refcache.h"
68 #include "xfs_acl.h"
69 #include "xfs_cap.h"
70 #include "xfs_mac.h"
71 #include "xfs_attr.h"
72 #include "xfs_inode_item.h"
73 #include "xfs_buf_item.h"
74 #include "xfs_utils.h"
75 #include "xfs_iomap.h"
76
77 #if defined(XFS_RW_TRACE)
78 void
79 xfs_rw_enter_trace(
80         int             tag,
81         xfs_iocore_t    *io,
82         const char      *buf,
83         size_t          size,
84         loff_t          offset,
85         int             ioflags)
86 {
87         xfs_inode_t     *ip = XFS_IO_INODE(io);
88
89         if (ip->i_rwtrace == NULL)
90                 return;
91         ktrace_enter(ip->i_rwtrace,
92                 (void *)(unsigned long)tag,
93                 (void *)ip,
94                 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
95                 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
96                 (void *)(__psint_t)buf,
97                 (void *)((unsigned long)size),
98                 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
99                 (void *)((unsigned long)(offset & 0xffffffff)),
100                 (void *)((unsigned long)ioflags),
101                 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
102                 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
103                 (void *)NULL,
104                 (void *)NULL,
105                 (void *)NULL,
106                 (void *)NULL,
107                 (void *)NULL);
108 }
109
110 void
111 xfs_inval_cached_trace(
112         xfs_iocore_t    *io,
113         xfs_off_t       offset,
114         xfs_off_t       len,
115         xfs_off_t       first,
116         xfs_off_t       last)
117 {
118         xfs_inode_t     *ip = XFS_IO_INODE(io);
119
120         if (ip->i_rwtrace == NULL)
121                 return;
122         ktrace_enter(ip->i_rwtrace,
123                 (void *)(__psint_t)XFS_INVAL_CACHED,
124                 (void *)ip,
125                 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
126                 (void *)((unsigned long)(offset & 0xffffffff)),
127                 (void *)((unsigned long)((len >> 32) & 0xffffffff)),
128                 (void *)((unsigned long)(len & 0xffffffff)),
129                 (void *)((unsigned long)((first >> 32) & 0xffffffff)),
130                 (void *)((unsigned long)(first & 0xffffffff)),
131                 (void *)((unsigned long)((last >> 32) & 0xffffffff)),
132                 (void *)((unsigned long)(last & 0xffffffff)),
133                 (void *)NULL,
134                 (void *)NULL,
135                 (void *)NULL,
136                 (void *)NULL,
137                 (void *)NULL,
138                 (void *)NULL);
139 }
140 #endif
141
142 /*
143  *      xfs_iozero
144  *
145  *      xfs_iozero clears the specified range of buffer supplied,
146  *      and marks all the affected blocks as valid and modified.  If
147  *      an affected block is not allocated, it will be allocated.  If
148  *      an affected block is not completely overwritten, and is not
149  *      valid before the operation, it will be read from disk before
150  *      being partially zeroed.
151  */
152 STATIC int
153 xfs_iozero(
154         xfs_vnode_t             *vp,    /* vnode                        */
155         loff_t                  pos,    /* offset in file               */
156         size_t                  count,  /* size of data to zero         */
157         loff_t                  end_size)       /* max file size to set */
158 {
159 #if XXXKAN
160         unsigned                bytes;
161         struct page             *page;
162         struct address_space    *mapping;
163         char                    *kaddr;
164         int                     status;
165
166         mapping = ip->i_mapping;
167         do {
168                 unsigned long index, offset;
169
170                 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
171                 index = pos >> PAGE_CACHE_SHIFT;
172                 bytes = PAGE_CACHE_SIZE - offset;
173                 if (bytes > count)
174                         bytes = count;
175
176                 status = -ENOMEM;
177                 page = grab_cache_page(mapping, index);
178                 if (!page)
179                         break;
180
181                 kaddr = kmap(page);
182                 status = mapping->a_ops->prepare_write(NULL, page, offset,
183                                                         offset + bytes);
184                 if (status) {
185                         goto unlock;
186                 }
187
188                 memset((void *) (kaddr + offset), 0, bytes);
189                 flush_dcache_page(page);
190                 status = mapping->a_ops->commit_write(NULL, page, offset,
191                                                         offset + bytes);
192                 if (!status) {
193                         pos += bytes;
194                         count -= bytes;
195                         if (pos > i_size_read(ip))
196                                 i_size_write(ip, pos < end_size ? pos : end_size);
197                 }
198
199 unlock:
200                 kunmap(page);
201                 unlock_page(page);
202                 page_cache_release(page);
203                 if (status)
204                         break;
205         } while (count);
206
207         return (-status);
208 #endif  /* XXXKAN */
209         return (0);
210 }
211
212 /*
213  * xfs_inval_cached_pages
214  *
215  * This routine is responsible for keeping direct I/O and buffered I/O
216  * somewhat coherent.  From here we make sure that we're at least
217  * temporarily holding the inode I/O lock exclusively and then call
218  * the page cache to flush and invalidate any cached pages.  If there
219  * are no cached pages this routine will be very quick.
220  */
221 void
222 xfs_inval_cached_pages(
223         xfs_vnode_t     *vp,
224         xfs_iocore_t    *io,
225         xfs_off_t       offset,
226         int             write,
227         int             relock)
228 {
229         xfs_mount_t     *mp;
230
231         if (!VN_CACHED(vp)) {
232                 return;
233         }
234
235         mp = io->io_mount;
236
237         /*
238          * We need to get the I/O lock exclusively in order
239          * to safely invalidate pages and mappings.
240          */
241         if (relock) {
242                 XFS_IUNLOCK(mp, io, XFS_IOLOCK_SHARED);
243                 XFS_ILOCK(mp, io, XFS_IOLOCK_EXCL);
244         }
245
246         /* Writing beyond EOF creates a hole that must be zeroed */
247         if (write && (offset > XFS_SIZE(mp, io))) {
248                 xfs_fsize_t     isize;
249
250                 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
251                 isize = XFS_SIZE(mp, io);
252                 if (offset > isize) {
253                         xfs_zero_eof(vp, io, offset, isize, offset);
254                 }
255                 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
256         }
257
258         xfs_inval_cached_trace(io, offset, -1, ctooff(offtoct(offset)), -1);
259         XVOP_FLUSHINVAL_PAGES(vp, ctooff(offtoct(offset)), -1, FI_REMAPF_LOCKED);
260         if (relock) {
261                 XFS_ILOCK_DEMOTE(mp, io, XFS_IOLOCK_EXCL);
262         }
263 }
264
265 int
266 xfs_read_file(xfs_mount_t *mp, xfs_inode_t *ip, struct uio *uio, int ioflag);
267
268 ssize_t                 /* bytes read, or (-)  error */
269 xfs_read(
270         bhv_desc_t      *bdp,
271         uio_t           *uio,
272         int             ioflags,
273         cred_t          *credp)
274 {
275         ssize_t         ret, size;
276         xfs_fsize_t     n;
277         xfs_inode_t     *ip;
278         xfs_mount_t     *mp;
279
280         ip = XFS_BHVTOI(bdp);
281         mp = ip->i_mount;
282
283         XFS_STATS_INC(xs_read_calls);
284
285         if (unlikely(ioflags & IO_ISDIRECT)) {
286                 if (((__psint_t)buf & BBMASK) ||
287                     (uio->uio_offset & mp->m_blockmask) ||
288                     (uio->uio_resid & mp->m_blockmask)) {
289                         if (uio->uio_offset >= ip->i_d.di_size) {
290                                 return (0);
291                         }
292                         return EINVAL;
293                 }
294         }
295
296         if (uio->uio_resid == 0)
297                 return 0;
298         n = XFS_MAXIOFFSET(mp) - uio->uio_offset;
299         if (n <= 0)
300                 return EFBIG;
301
302         size = (n < uio->uio_resid)? n : uio->uio_resid;
303
304         if (XFS_FORCED_SHUTDOWN(mp)) {
305                 return EIO;
306         }
307
308         if (!(ioflags & IO_ISLOCKED))
309                 xfs_ilock(ip, XFS_IOLOCK_SHARED);
310 #if XXXKAN
311         if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_READ) &&
312             !(ioflags & IO_INVIS)) {
313                 int error;
314                 vrwlock_t locktype = VRWLOCK_READ;
315                 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
316
317                 error = XFS_SEND_DATA(mp, DM_EVENT_READ, BHV_TO_VNODE(bdp),
318                         uio->uio_offset, size, dmflags, &locktype);
319                 if (error) {
320                         if (!(ioflags & IO_ISLOCKED))
321                                 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
322                         return (error);
323                 }
324         }
325
326         if (unlikely(ioflags & IO_ISDIRECT)) {
327                 xfs_rw_enter_trace(XFS_DIORD_ENTER, &ip->i_iocore,
328                                         buf, size, *offset, ioflags);
329 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,22)
330                 xfs_off_t pos = uio->uio_offset;
331
332                 ret = generic_file_direct_IO(READ, file, buf, size, pos);
333                 if (ret > 0)
334                         uio->uio_offset = pos + ret;
335
336                 UPDATE_ATIME(file->f_dentry->d_inode);
337 #else
338                 ret = generic_file_read(file, buf, size, offset);
339 #endif
340         } else {
341                 xfs_rw_enter_trace(XFS_READ_ENTER, &ip->i_iocore,
342                                         buf, size, *offset, ioflags);
343                 ret = generic_file_read(file, buf, size, offset);
344         }
345 #else  /* XXXKAN */
346         ret = xfs_read_file(mp, ip, uio, ioflags);
347 #endif /* XXXKAN */
348
349         if (!(ioflags & IO_ISLOCKED))
350                 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
351
352         XFS_STATS_ADD(xs_read_bytes, ret);
353
354         if (likely((ioflags & IO_INVIS) == 0)) {
355                 xfs_ichgtime(ip, XFS_ICHGTIME_ACC);
356         }
357
358         return ret;
359 }
360
361 /*
362  * This routine is called to handle zeroing any space in the last
363  * block of the file that is beyond the EOF.  We do this since the
364  * size is being increased without writing anything to that block
365  * and we don't want anyone to read the garbage on the disk.
366  */
367 STATIC int                              /* error (positive) */
368 xfs_zero_last_block(
369         xfs_vnode_t     *vp,
370         xfs_iocore_t    *io,
371         xfs_off_t       offset,
372         xfs_fsize_t     isize,
373         xfs_fsize_t     end_size)
374 {
375         xfs_fileoff_t   last_fsb;
376         xfs_mount_t     *mp;
377         int             nimaps;
378         int             zero_offset;
379         int             zero_len;
380         int             isize_fsb_offset;
381         int             error = 0;
382         xfs_bmbt_irec_t imap;
383         loff_t          loff;
384         size_t          lsize;
385
386         ASSERT(ismrlocked(io->io_lock, MR_UPDATE) != 0);
387         ASSERT(offset > isize);
388
389         mp = io->io_mount;
390
391         isize_fsb_offset = XFS_B_FSB_OFFSET(mp, isize);
392         if (isize_fsb_offset == 0) {
393                 /*
394                  * There are no extra bytes in the last block on disk to
395                  * zero, so return.
396                  */
397                 return 0;
398         }
399
400         last_fsb = XFS_B_TO_FSBT(mp, isize);
401         nimaps = 1;
402         error = XFS_BMAPI(mp, NULL, io, last_fsb, 1, 0, NULL, 0, &imap,
403                           &nimaps, NULL);
404         if (error) {
405                 return error;
406         }
407         ASSERT(nimaps > 0);
408         /*
409          * If the block underlying isize is just a hole, then there
410          * is nothing to zero.
411          */
412         if (imap.br_startblock == HOLESTARTBLOCK) {
413                 return 0;
414         }
415         /*
416          * Zero the part of the last block beyond the EOF, and write it
417          * out sync.  We need to drop the ilock while we do this so we
418          * don't deadlock when the buffer cache calls back to us.
419          */
420         XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
421         loff = XFS_FSB_TO_B(mp, last_fsb);
422         lsize = XFS_FSB_TO_B(mp, 1);
423
424         zero_offset = isize_fsb_offset;
425         zero_len = mp->m_sb.sb_blocksize - isize_fsb_offset;
426
427         error = xfs_iozero(vp, loff + zero_offset, zero_len, end_size);
428
429         XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
430         ASSERT(error >= 0);
431         return error;
432 }
433
434 /*
435  * Zero any on disk space between the current EOF and the new,
436  * larger EOF.  This handles the normal case of zeroing the remainder
437  * of the last block in the file and the unusual case of zeroing blocks
438  * out beyond the size of the file.  This second case only happens
439  * with fixed size extents and when the system crashes before the inode
440  * size was updated but after blocks were allocated.  If fill is set,
441  * then any holes in the range are filled and zeroed.  If not, the holes
442  * are left alone as holes.
443  */
444
445 int                                     /* error (positive) */
446 xfs_zero_eof(
447         xfs_vnode_t     *vp,
448         xfs_iocore_t    *io,
449         xfs_off_t       offset,         /* starting I/O offset */
450         xfs_fsize_t     isize,          /* current inode size */
451         xfs_fsize_t     end_size)       /* terminal inode size */
452 {
453         xfs_fileoff_t   start_zero_fsb;
454         xfs_fileoff_t   end_zero_fsb;
455         xfs_fileoff_t   prev_zero_fsb;
456         xfs_fileoff_t   zero_count_fsb;
457         xfs_fileoff_t   last_fsb;
458         xfs_extlen_t    buf_len_fsb;
459         xfs_extlen_t    prev_zero_count;
460         xfs_mount_t     *mp;
461         int             nimaps;
462         int             error = 0;
463         xfs_bmbt_irec_t imap;
464         loff_t          loff;
465         size_t          lsize;
466
467         ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
468         ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
469
470         mp = io->io_mount;
471
472         /*
473          * First handle zeroing the block on which isize resides.
474          * We only zero a part of that block so it is handled specially.
475          */
476         error = xfs_zero_last_block(vp, io, offset, isize, end_size);
477         if (error) {
478                 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
479                 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
480                 return error;
481         }
482
483         /*
484          * Calculate the range between the new size and the old
485          * where blocks needing to be zeroed may exist.  To get the
486          * block where the last byte in the file currently resides,
487          * we need to subtract one from the size and truncate back
488          * to a block boundary.  We subtract 1 in case the size is
489          * exactly on a block boundary.
490          */
491         last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
492         start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
493         end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
494         ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
495         if (last_fsb == end_zero_fsb) {
496                 /*
497                  * The size was only incremented on its last block.
498                  * We took care of that above, so just return.
499                  */
500                 return 0;
501         }
502
503         ASSERT(start_zero_fsb <= end_zero_fsb);
504         prev_zero_fsb = NULLFILEOFF;
505         prev_zero_count = 0;
506         while (start_zero_fsb <= end_zero_fsb) {
507                 nimaps = 1;
508                 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
509                 error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb,
510                                   0, NULL, 0, &imap, &nimaps, NULL);
511                 if (error) {
512                         ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
513                         ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
514                         return error;
515                 }
516                 ASSERT(nimaps > 0);
517
518                 if (imap.br_state == XFS_EXT_UNWRITTEN ||
519                     imap.br_startblock == HOLESTARTBLOCK) {
520                         /*
521                          * This loop handles initializing pages that were
522                          * partially initialized by the code below this
523                          * loop. It basically zeroes the part of the page
524                          * that sits on a hole and sets the page as P_HOLE
525                          * and calls remapf if it is a mapped file.
526                          */
527                         prev_zero_fsb = NULLFILEOFF;
528                         prev_zero_count = 0;
529                         start_zero_fsb = imap.br_startoff +
530                                          imap.br_blockcount;
531                         ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
532                         continue;
533                 }
534
535                 /*
536                  * There are blocks in the range requested.
537                  * Zero them a single write at a time.  We actually
538                  * don't zero the entire range returned if it is
539                  * too big and simply loop around to get the rest.
540                  * That is not the most efficient thing to do, but it
541                  * is simple and this path should not be exercised often.
542                  */
543                 buf_len_fsb = XFS_FILBLKS_MIN(imap.br_blockcount,
544                                               mp->m_writeio_blocks << 8);
545                 /*
546                  * Drop the inode lock while we're doing the I/O.
547                  * We'll still have the iolock to protect us.
548                  */
549                 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
550
551                 loff = XFS_FSB_TO_B(mp, start_zero_fsb);
552                 lsize = XFS_FSB_TO_B(mp, buf_len_fsb);
553
554                 error = xfs_iozero(vp, loff, lsize, end_size);
555
556                 if (error) {
557                         goto out_lock;
558                 }
559
560                 prev_zero_fsb = start_zero_fsb;
561                 prev_zero_count = buf_len_fsb;
562                 start_zero_fsb = imap.br_startoff + buf_len_fsb;
563                 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
564
565                 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
566         }
567
568         return 0;
569
570 out_lock:
571
572         XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
573         ASSERT(error >= 0);
574         return error;
575 }
576
577 ssize_t                         /* bytes written, or (-) error */
578 xfs_write(
579         bhv_desc_t      *bdp,
580         uio_t           *uiop,
581         int             ioflags,
582         cred_t          *credp)
583 {
584 #if XXXKAN
585         xfs_inode_t     *xip;
586         xfs_mount_t     *mp;
587         ssize_t         ret;
588         int             error = 0;
589         xfs_fsize_t     isize, new_size;
590         xfs_fsize_t     n, limit;
591         xfs_iocore_t    *io;
592         xfs_vnode_t     *vp;
593         int             iolock;
594         int             eventsent = 0;
595         vrwlock_t       locktype;
596
597         XFS_STATS_INC(xs_write_calls);
598
599         vp = BHV_TO_VNODE(bdp);
600         xip = XFS_BHVTOI(bdp);
601
602         if (size == 0)
603                 return 0;
604
605         io = &xip->i_iocore;
606         mp = io->io_mount;
607
608         xfs_check_frozen(mp, bdp, XFS_FREEZE_WRITE);
609
610         if (XFS_FORCED_SHUTDOWN(xip->i_mount)) {
611                 return EIO;
612         }
613
614         if (unlikely(ioflags & IO_ISDIRECT)) {
615                 if (((__psint_t)buf & BBMASK) ||
616                     (*offset & mp->m_blockmask) ||
617                     (size  & mp->m_blockmask)) {
618                         return EINVAL;
619                 }
620                 iolock = XFS_IOLOCK_SHARED;
621                 locktype = VRWLOCK_WRITE_DIRECT;
622         } else {
623                 if (io->io_flags & XFS_IOCORE_RT)
624                         return EINVAL;
625                 iolock = XFS_IOLOCK_EXCL;
626                 locktype = VRWLOCK_WRITE;
627         }
628
629         if (ioflags & IO_ISLOCKED)
630                 iolock = 0;
631
632         xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
633
634         isize = xip->i_d.di_size;
635         limit = XFS_MAXIOFFSET(mp);
636
637         if (file->f_flags & O_APPEND)
638                 *offset = isize;
639
640 start:
641         n = limit - *offset;
642         if (n <= 0) {
643                 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
644                 return EFBIG;
645         }
646         if (n < size)
647                 size = n;
648
649         new_size = *offset + size;
650         if (new_size > isize) {
651                 io->io_new_size = new_size;
652         }
653
654         if ((DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_WRITE) &&
655             !(ioflags & IO_INVIS) && !eventsent)) {
656                 loff_t          savedsize = *offset;
657                 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
658
659                 xfs_iunlock(xip, XFS_ILOCK_EXCL);
660                 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,
661                                       *offset, size,
662                                       dmflags, &locktype);
663                 if (error) {
664                         if (iolock) xfs_iunlock(xip, iolock);
665                         return -error;
666                 }
667                 xfs_ilock(xip, XFS_ILOCK_EXCL);
668                 eventsent = 1;
669
670                 /*
671                  * The iolock was dropped and reaquired in XFS_SEND_DATA
672                  * so we have to recheck the size when appending.
673                  * We will only "goto start;" once, since having sent the
674                  * event prevents another call to XFS_SEND_DATA, which is
675                  * what allows the size to change in the first place.
676                  */
677                 if ((file->f_flags & O_APPEND) &&
678                     savedsize != xip->i_d.di_size) {
679                         *offset = isize = xip->i_d.di_size;
680                         goto start;
681                 }
682         }
683
684         /*
685          * If the offset is beyond the size of the file, we have a couple
686          * of things to do. First, if there is already space allocated
687          * we need to either create holes or zero the disk or ...
688          *
689          * If there is a page where the previous size lands, we need
690          * to zero it out up to the new size.
691          */
692
693         if (!(ioflags & IO_ISDIRECT) && (*offset > isize && isize)) {
694                 error = xfs_zero_eof(BHV_TO_VNODE(bdp), io, *offset,
695                         isize, *offset + size);
696                 if (error) {
697                         xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
698                         return(-error);
699                 }
700         }
701         xfs_iunlock(xip, XFS_ILOCK_EXCL);
702
703         /*
704          * If we're writing the file then make sure to clear the
705          * setuid and setgid bits if the process is not being run
706          * by root.  This keeps people from modifying setuid and
707          * setgid binaries.
708          */
709
710         if (((xip->i_d.di_mode & S_ISUID) ||
711             ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
712                 (S_ISGID | S_IXGRP))) &&
713              !capable(CAP_FSETID)) {
714                 error = xfs_write_clear_setuid(xip);
715                 if (error) {
716                         xfs_iunlock(xip, iolock);
717                         return -error;
718                 }
719         }
720
721
722 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,22)
723         if ((ssize_t) size < 0) {
724                 ret = EINVAL;
725                 goto error;
726         }
727
728         if (!access_ok(VERIFY_READ, buf, size)) {
729                 ret = EINVAL;
730                 goto error;
731         }
732 #else
733 #define do_generic_direct_write(file, buf, size, offset)        \
734         generic_file_write_nolock(file, buf, size, offset)
735 #define do_generic_file_write(file, buf, size, offset)          \
736         generic_file_write_nolock(file, buf, size, offset)
737 #endif
738
739 retry:
740         if (unlikely(ioflags & IO_ISDIRECT)) {
741                 loff_t  pos = *offset;
742                 struct address_space *mapping = file->f_dentry->d_inode->i_mapping;
743                 struct inode    *inode = mapping->host;
744
745                 ret = precheck_file_write(file, inode, &size,  &pos);
746                 if (ret || size == 0)
747                         goto error;
748
749                 xfs_inval_cached_pages(vp, io, pos, 1, 1);
750                 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
751                 /* mark_inode_dirty_sync(inode); - we do this later */
752
753                 xfs_rw_enter_trace(XFS_DIOWR_ENTER, io, buf, size, pos, ioflags);
754                 ret = generic_file_direct_IO(WRITE, file, (char *)buf, size, pos);
755                 if (ret > 0)
756                         *offset += ret;
757         } else {
758                 xfs_rw_enter_trace(XFS_WRITE_ENTER, io, buf, size, *offset, ioflags);
759                 ret = do_generic_file_write(file, buf, size, offset);
760         }
761
762         if (unlikely(ioflags & IO_INVIS)) {
763                 /* generic_file_write updates the mtime/ctime but we need
764                  * to undo that because this I/O was supposed to be
765                  * invisible.
766                  */
767                 struct inode    *inode = LINVFS_GET_IP(vp);
768                 inode->i_mtime = xip->i_d.di_mtime.t_sec;
769                 inode->i_ctime = xip->i_d.di_ctime.t_sec;
770         } else {
771                 xfs_ichgtime(xip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
772         }
773
774         if ((ret == -ENOSPC) &&
775             DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_NOSPACE) &&
776             !(ioflags & IO_INVIS)) {
777
778                 xfs_rwunlock(bdp, locktype);
779                 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp,
780                                 DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL,
781                                 0, 0, 0); /* Delay flag intentionally  unused */
782                 if (error)
783                         return -error;
784                 xfs_rwlock(bdp, locktype);
785                 *offset = xip->i_d.di_size;
786                 goto retry;
787         }
788
789 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,22)
790 error:
791 #endif
792         if (ret <= 0) {
793                 if (iolock)
794                         xfs_rwunlock(bdp, locktype);
795                 return ret;
796         }
797
798         XFS_STATS_ADD(xs_write_bytes, ret);
799
800         if (*offset > xip->i_d.di_size) {
801                 xfs_ilock(xip, XFS_ILOCK_EXCL);
802                 if (*offset > xip->i_d.di_size) {
803                         struct inode    *inode = LINVFS_GET_IP(vp);
804
805                         xip->i_d.di_size = *offset;
806                         i_size_write(inode, *offset);
807                         xip->i_update_core = 1;
808                         xip->i_update_size = 1;
809                         mark_inode_dirty_sync(inode);
810                 }
811                 xfs_iunlock(xip, XFS_ILOCK_EXCL);
812         }
813
814         /* Handle various SYNC-type writes */
815         if ((file->f_flags & O_SYNC) || IS_SYNC(file->f_dentry->d_inode)) {
816
817                 /*
818                  * If we're treating this as O_DSYNC and we have not updated the
819                  * size, force the log.
820                  */
821
822                 if (!(mp->m_flags & XFS_MOUNT_OSYNCISOSYNC)
823                         && !(xip->i_update_size)) {
824                         /*
825                          * If an allocation transaction occurred
826                          * without extending the size, then we have to force
827                          * the log up the proper point to ensure that the
828                          * allocation is permanent.  We can't count on
829                          * the fact that buffered writes lock out direct I/O
830                          * writes - the direct I/O write could have extended
831                          * the size nontransactionally, then finished before
832                          * we started.  xfs_write_file will think that the file
833                          * didn't grow but the update isn't safe unless the
834                          * size change is logged.
835                          *
836                          * Force the log if we've committed a transaction
837                          * against the inode or if someone else has and
838                          * the commit record hasn't gone to disk (e.g.
839                          * the inode is pinned).  This guarantees that
840                          * all changes affecting the inode are permanent
841                          * when we return.
842                          */
843
844                         xfs_inode_log_item_t *iip;
845                         xfs_lsn_t lsn;
846
847                         iip = xip->i_itemp;
848                         if (iip && iip->ili_last_lsn) {
849                                 lsn = iip->ili_last_lsn;
850                                 xfs_log_force(mp, lsn,
851                                                 XFS_LOG_FORCE | XFS_LOG_SYNC);
852                         } else if (xfs_ipincount(xip) > 0) {
853                                 xfs_log_force(mp, (xfs_lsn_t)0,
854                                                 XFS_LOG_FORCE | XFS_LOG_SYNC);
855                         }
856
857                 } else {
858                         xfs_trans_t     *tp;
859
860                         /*
861                          * O_SYNC or O_DSYNC _with_ a size update are handled
862                          * the same way.
863                          *
864                          * If the write was synchronous then we need to make
865                          * sure that the inode modification time is permanent.
866                          * We'll have updated the timestamp above, so here
867                          * we use a synchronous transaction to log the inode.
868                          * It's not fast, but it's necessary.
869                          *
870                          * If this a dsync write and the size got changed
871                          * non-transactionally, then we need to ensure that
872                          * the size change gets logged in a synchronous
873                          * transaction.
874                          */
875
876                         tp = xfs_trans_alloc(mp, XFS_TRANS_WRITE_SYNC);
877                         if ((error = xfs_trans_reserve(tp, 0,
878                                                       XFS_SWRITE_LOG_RES(mp),
879                                                       0, 0, 0))) {
880                                 /* Transaction reserve failed */
881                                 xfs_trans_cancel(tp, 0);
882                         } else {
883                                 /* Transaction reserve successful */
884                                 xfs_ilock(xip, XFS_ILOCK_EXCL);
885                                 xfs_trans_ijoin(tp, xip, XFS_ILOCK_EXCL);
886                                 xfs_trans_ihold(tp, xip);
887                                 xfs_trans_log_inode(tp, xip, XFS_ILOG_CORE);
888                                 xfs_trans_set_sync(tp);
889                                 error = xfs_trans_commit(tp, 0, (xfs_lsn_t)0);
890                                 xfs_iunlock(xip, XFS_ILOCK_EXCL);
891                         }
892                 }
893         } /* (ioflags & O_SYNC) */
894
895         /*
896          * If we are coming from an nfsd thread then insert into the
897          * reference cache.
898          */
899
900         if (!strcmp(current->comm, "nfsd"))
901                 xfs_refcache_insert(xip);
902
903         /* Drop lock this way - the old refcache release is in here */
904         if (iolock)
905                 xfs_rwunlock(bdp, locktype);
906
907         return(ret);
908 #endif /* XXXKAN */
909         return (0);
910 }
911
912 /*
913  * Initiate IO on given buffer.
914  */
915 int
916 xfs_buf_iorequest(struct xfs_buf *bp)
917 {
918         bp->b_flags &= ~(B_INVAL|B_DONE);
919         bp->b_ioflags &= ~BIO_ERROR;
920
921         if (bp->b_flags & B_ASYNC)
922                 BUF_KERNPROC(bp);
923
924         if (bp->b_vp == NULL) {
925                 if (bp->b_iocmd == BIO_WRITE) {
926                         bp->b_flags &= ~(B_DELWRI | B_DEFERRED);
927                         bufobj_wref(bp->b_bufobj);
928                 }
929
930                 bp->b_iooffset = (bp->b_blkno << BBSHIFT);
931                 bstrategy(bp);
932         } else {
933                 if (bp->b_iocmd == BIO_WRITE) {
934                         /* Mark the buffer clean */
935                         bundirty(bp);
936                         bufobj_wref(bp->b_bufobj);
937                         vfs_busy_pages(bp, 1);
938                 } else if (bp->b_iocmd == BIO_READ) {
939                         vfs_busy_pages(bp, 0);
940                 }
941                 bp->b_iooffset = dbtob(bp->b_blkno);
942                 bstrategy(bp);
943         }
944         return 0;
945 }
946
947 /*
948  * All xfs metadata buffers except log state machine buffers
949  * get this attached as their b_bdstrat callback function.
950  * This is so that we can catch a buffer
951  * after prematurely unpinning it to forcibly shutdown the filesystem.
952  */
953 int
954 xfs_bdstrat_cb(struct xfs_buf *bp)
955 {
956         xfs_mount_t     *mp;
957
958         mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
959         if (!XFS_FORCED_SHUTDOWN(mp)) {
960                 xfs_buf_iorequest(bp);
961                 return 0;
962         } else {
963                 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
964                 /*
965                  * Metadata write that didn't get logged but
966                  * written delayed anyway. These aren't associated
967                  * with a transaction, and can be ignored.
968                  */
969                 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
970                     (XFS_BUF_ISREAD(bp)) == 0)
971                         return (xfs_bioerror_relse(bp));
972                 else
973                         return (xfs_bioerror(bp));
974         }
975 }
976
977
978 int
979 xfs_bmap(bhv_desc_t     *bdp,
980         xfs_off_t       offset,
981         ssize_t         count,
982         int             flags,
983         xfs_iomap_t     *iomapp,
984         int             *niomaps)
985 {
986         xfs_inode_t     *ip = XFS_BHVTOI(bdp);
987         xfs_iocore_t    *io = &ip->i_iocore;
988
989         ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
990         ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
991                ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
992
993         return xfs_iomap(io, offset, count, flags, iomapp, niomaps);
994 }
995
996 /*
997  * Wrapper around bdstrat so that we can stop data
998  * from going to disk in case we are shutting down the filesystem.
999  * Typically user data goes thru this path; one of the exceptions
1000  * is the superblock.
1001  */
1002 int
1003 xfsbdstrat(
1004         struct xfs_mount        *mp,
1005         struct xfs_buf          *bp)
1006 {
1007         ASSERT(mp);
1008         if (!XFS_FORCED_SHUTDOWN(mp)) {
1009                 /* Grio redirection would go here
1010                  * if (XFS_BUF_IS_GRIO(bp)) {
1011                  */
1012
1013                 return xfs_buf_iorequest(bp);
1014                 return 0;
1015         }
1016
1017         xfs_buftrace("XFSBDSTRAT IOERROR", bp);
1018         return (xfs_bioerror_relse(bp));
1019 }
1020
1021 /*
1022  * If the underlying (data/log/rt) device is readonly, there are some
1023  * operations that cannot proceed.
1024  */
1025 int
1026 xfs_dev_is_read_only(
1027         xfs_mount_t             *mp,
1028         char                    *message)
1029 {
1030         if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
1031             xfs_readonly_buftarg(mp->m_logdev_targp) ||
1032             (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
1033                 cmn_err(CE_NOTE,
1034                         "XFS: %s required on read-only device.", message);
1035                 cmn_err(CE_NOTE,
1036                         "XFS: write access unavailable, cannot proceed.");
1037                 return EROFS;
1038         }
1039         return 0;
1040 }