initial commit with v2.6.9
[linux-2.6.9-moxart.git] / fs / xfs / linux-2.6 / xfs_lrw.c
blob903e60e594f1d58f48196df0b882e3737c632652
1 /*
2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
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.
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.
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.
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.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 * fs/xfs/linux/xfs_lrw.c (Linux Read Write stuff)
37 #include "xfs.h"
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 #include "xfs_iomap.h"
76 #include <linux/capability.h>
77 #include <linux/writeback.h>
80 #if defined(XFS_RW_TRACE)
81 void
82 xfs_rw_enter_trace(
83 int tag,
84 xfs_iocore_t *io,
85 void *data,
86 size_t segs,
87 loff_t offset,
88 int ioflags)
90 xfs_inode_t *ip = XFS_IO_INODE(io);
92 if (ip->i_rwtrace == NULL)
93 return;
94 ktrace_enter(ip->i_rwtrace,
95 (void *)(unsigned long)tag,
96 (void *)ip,
97 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
98 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
99 (void *)data,
100 (void *)((unsigned long)segs),
101 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
102 (void *)((unsigned long)(offset & 0xffffffff)),
103 (void *)((unsigned long)ioflags),
104 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
105 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
106 (void *)NULL,
107 (void *)NULL,
108 (void *)NULL,
109 (void *)NULL,
110 (void *)NULL);
113 void
114 xfs_inval_cached_trace(
115 xfs_iocore_t *io,
116 xfs_off_t offset,
117 xfs_off_t len,
118 xfs_off_t first,
119 xfs_off_t last)
121 xfs_inode_t *ip = XFS_IO_INODE(io);
123 if (ip->i_rwtrace == NULL)
124 return;
125 ktrace_enter(ip->i_rwtrace,
126 (void *)(__psint_t)XFS_INVAL_CACHED,
127 (void *)ip,
128 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
129 (void *)((unsigned long)(offset & 0xffffffff)),
130 (void *)((unsigned long)((len >> 32) & 0xffffffff)),
131 (void *)((unsigned long)(len & 0xffffffff)),
132 (void *)((unsigned long)((first >> 32) & 0xffffffff)),
133 (void *)((unsigned long)(first & 0xffffffff)),
134 (void *)((unsigned long)((last >> 32) & 0xffffffff)),
135 (void *)((unsigned long)(last & 0xffffffff)),
136 (void *)NULL,
137 (void *)NULL,
138 (void *)NULL,
139 (void *)NULL,
140 (void *)NULL,
141 (void *)NULL);
143 #endif
146 * xfs_iozero
148 * xfs_iozero clears the specified range of buffer supplied,
149 * and marks all the affected blocks as valid and modified. If
150 * an affected block is not allocated, it will be allocated. If
151 * an affected block is not completely overwritten, and is not
152 * valid before the operation, it will be read from disk before
153 * being partially zeroed.
155 STATIC int
156 xfs_iozero(
157 struct inode *ip, /* inode */
158 loff_t pos, /* offset in file */
159 size_t count, /* size of data to zero */
160 loff_t end_size) /* max file size to set */
162 unsigned bytes;
163 struct page *page;
164 struct address_space *mapping;
165 char *kaddr;
166 int status;
168 mapping = ip->i_mapping;
169 do {
170 unsigned long index, offset;
172 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
173 index = pos >> PAGE_CACHE_SHIFT;
174 bytes = PAGE_CACHE_SIZE - offset;
175 if (bytes > count)
176 bytes = count;
178 status = -ENOMEM;
179 page = grab_cache_page(mapping, index);
180 if (!page)
181 break;
183 kaddr = kmap(page);
184 status = mapping->a_ops->prepare_write(NULL, page, offset,
185 offset + bytes);
186 if (status) {
187 goto unlock;
190 memset((void *) (kaddr + offset), 0, bytes);
191 flush_dcache_page(page);
192 status = mapping->a_ops->commit_write(NULL, page, offset,
193 offset + bytes);
194 if (!status) {
195 pos += bytes;
196 count -= bytes;
197 if (pos > i_size_read(ip))
198 i_size_write(ip, pos < end_size ? pos : end_size);
201 unlock:
202 kunmap(page);
203 unlock_page(page);
204 page_cache_release(page);
205 if (status)
206 break;
207 } while (count);
209 return (-status);
213 * xfs_inval_cached_pages
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.
221 void
222 xfs_inval_cached_pages(
223 vnode_t *vp,
224 xfs_iocore_t *io,
225 xfs_off_t offset,
226 int write,
227 int relock)
229 if (VN_CACHED(vp)) {
230 xfs_inval_cached_trace(io, offset, -1, ctooff(offtoct(offset)), -1);
231 VOP_FLUSHINVAL_PAGES(vp, ctooff(offtoct(offset)), -1, FI_REMAPF_LOCKED);
236 ssize_t /* bytes read, or (-) error */
237 xfs_read(
238 bhv_desc_t *bdp,
239 struct kiocb *iocb,
240 const struct iovec *iovp,
241 unsigned int segs,
242 loff_t *offset,
243 int ioflags,
244 cred_t *credp)
246 struct file *file = iocb->ki_filp;
247 size_t size = 0;
248 ssize_t ret;
249 xfs_fsize_t n;
250 xfs_inode_t *ip;
251 xfs_mount_t *mp;
252 vnode_t *vp;
253 unsigned long seg;
255 ip = XFS_BHVTOI(bdp);
256 vp = BHV_TO_VNODE(bdp);
257 mp = ip->i_mount;
259 XFS_STATS_INC(xs_read_calls);
261 /* START copy & waste from filemap.c */
262 for (seg = 0; seg < segs; seg++) {
263 const struct iovec *iv = &iovp[seg];
266 * If any segment has a negative length, or the cumulative
267 * length ever wraps negative then return -EINVAL.
269 size += iv->iov_len;
270 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
271 return XFS_ERROR(-EINVAL);
273 /* END copy & waste from filemap.c */
275 if (ioflags & IO_ISDIRECT) {
276 xfs_buftarg_t *target =
277 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
278 mp->m_rtdev_targp : mp->m_ddev_targp;
279 if ((*offset & target->pbr_smask) ||
280 (size & target->pbr_smask)) {
281 if (*offset == ip->i_d.di_size) {
282 return (0);
284 return -XFS_ERROR(EINVAL);
288 n = XFS_MAXIOFFSET(mp) - *offset;
289 if ((n <= 0) || (size == 0))
290 return 0;
292 if (n < size)
293 size = n;
295 if (XFS_FORCED_SHUTDOWN(mp)) {
296 return -EIO;
299 xfs_ilock(ip, XFS_IOLOCK_SHARED);
301 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ) &&
302 !(ioflags & IO_INVIS)) {
303 vrwlock_t locktype = VRWLOCK_READ;
305 ret = XFS_SEND_DATA(mp, DM_EVENT_READ,
306 BHV_TO_VNODE(bdp), *offset, size,
307 FILP_DELAY_FLAG(file), &locktype);
308 if (ret) {
309 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
310 return -ret;
314 xfs_rw_enter_trace(XFS_READ_ENTER, &ip->i_iocore,
315 (void *)iovp, segs, *offset, ioflags);
316 ret = __generic_file_aio_read(iocb, iovp, segs, offset);
317 if (ret == -EIOCBQUEUED)
318 ret = wait_on_sync_kiocb(iocb);
320 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
322 if (ret > 0)
323 XFS_STATS_ADD(xs_read_bytes, ret);
325 if (likely(!(ioflags & IO_INVIS)))
326 xfs_ichgtime(ip, XFS_ICHGTIME_ACC);
328 return ret;
331 ssize_t
332 xfs_sendfile(
333 bhv_desc_t *bdp,
334 struct file *filp,
335 loff_t *offset,
336 int ioflags,
337 size_t count,
338 read_actor_t actor,
339 void *target,
340 cred_t *credp)
342 ssize_t ret;
343 xfs_fsize_t n;
344 xfs_inode_t *ip;
345 xfs_mount_t *mp;
346 vnode_t *vp;
348 ip = XFS_BHVTOI(bdp);
349 vp = BHV_TO_VNODE(bdp);
350 mp = ip->i_mount;
352 XFS_STATS_INC(xs_read_calls);
354 n = XFS_MAXIOFFSET(mp) - *offset;
355 if ((n <= 0) || (count == 0))
356 return 0;
358 if (n < count)
359 count = n;
361 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
362 return -EIO;
364 xfs_ilock(ip, XFS_IOLOCK_SHARED);
366 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ) &&
367 (!(ioflags & IO_INVIS))) {
368 vrwlock_t locktype = VRWLOCK_READ;
369 int error;
371 error = XFS_SEND_DATA(mp, DM_EVENT_READ, BHV_TO_VNODE(bdp), *offset, count,
372 FILP_DELAY_FLAG(filp), &locktype);
373 if (error) {
374 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
375 return -error;
378 xfs_rw_enter_trace(XFS_SENDFILE_ENTER, &ip->i_iocore,
379 (void *)(unsigned long)target, count, *offset, ioflags);
380 ret = generic_file_sendfile(filp, offset, count, actor, target);
382 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
384 if (ret > 0)
385 XFS_STATS_ADD(xs_read_bytes, ret);
387 if (likely(!(ioflags & IO_INVIS)))
388 xfs_ichgtime(ip, XFS_ICHGTIME_ACC);
390 return ret;
394 * This routine is called to handle zeroing any space in the last
395 * block of the file that is beyond the EOF. We do this since the
396 * size is being increased without writing anything to that block
397 * and we don't want anyone to read the garbage on the disk.
399 STATIC int /* error (positive) */
400 xfs_zero_last_block(
401 struct inode *ip,
402 xfs_iocore_t *io,
403 xfs_off_t offset,
404 xfs_fsize_t isize,
405 xfs_fsize_t end_size)
407 xfs_fileoff_t last_fsb;
408 xfs_mount_t *mp;
409 int nimaps;
410 int zero_offset;
411 int zero_len;
412 int isize_fsb_offset;
413 int error = 0;
414 xfs_bmbt_irec_t imap;
415 loff_t loff;
416 size_t lsize;
418 ASSERT(ismrlocked(io->io_lock, MR_UPDATE) != 0);
419 ASSERT(offset > isize);
421 mp = io->io_mount;
423 isize_fsb_offset = XFS_B_FSB_OFFSET(mp, isize);
424 if (isize_fsb_offset == 0) {
426 * There are no extra bytes in the last block on disk to
427 * zero, so return.
429 return 0;
432 last_fsb = XFS_B_TO_FSBT(mp, isize);
433 nimaps = 1;
434 error = XFS_BMAPI(mp, NULL, io, last_fsb, 1, 0, NULL, 0, &imap,
435 &nimaps, NULL);
436 if (error) {
437 return error;
439 ASSERT(nimaps > 0);
441 * If the block underlying isize is just a hole, then there
442 * is nothing to zero.
444 if (imap.br_startblock == HOLESTARTBLOCK) {
445 return 0;
448 * Zero the part of the last block beyond the EOF, and write it
449 * out sync. We need to drop the ilock while we do this so we
450 * don't deadlock when the buffer cache calls back to us.
452 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
453 loff = XFS_FSB_TO_B(mp, last_fsb);
454 lsize = XFS_FSB_TO_B(mp, 1);
456 zero_offset = isize_fsb_offset;
457 zero_len = mp->m_sb.sb_blocksize - isize_fsb_offset;
459 error = xfs_iozero(ip, loff + zero_offset, zero_len, end_size);
461 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
462 ASSERT(error >= 0);
463 return error;
467 * Zero any on disk space between the current EOF and the new,
468 * larger EOF. This handles the normal case of zeroing the remainder
469 * of the last block in the file and the unusual case of zeroing blocks
470 * out beyond the size of the file. This second case only happens
471 * with fixed size extents and when the system crashes before the inode
472 * size was updated but after blocks were allocated. If fill is set,
473 * then any holes in the range are filled and zeroed. If not, the holes
474 * are left alone as holes.
477 int /* error (positive) */
478 xfs_zero_eof(
479 vnode_t *vp,
480 xfs_iocore_t *io,
481 xfs_off_t offset, /* starting I/O offset */
482 xfs_fsize_t isize, /* current inode size */
483 xfs_fsize_t end_size) /* terminal inode size */
485 struct inode *ip = LINVFS_GET_IP(vp);
486 xfs_fileoff_t start_zero_fsb;
487 xfs_fileoff_t end_zero_fsb;
488 xfs_fileoff_t prev_zero_fsb;
489 xfs_fileoff_t zero_count_fsb;
490 xfs_fileoff_t last_fsb;
491 xfs_extlen_t buf_len_fsb;
492 xfs_extlen_t prev_zero_count;
493 xfs_mount_t *mp;
494 int nimaps;
495 int error = 0;
496 xfs_bmbt_irec_t imap;
497 loff_t loff;
498 size_t lsize;
500 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
501 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
503 mp = io->io_mount;
506 * First handle zeroing the block on which isize resides.
507 * We only zero a part of that block so it is handled specially.
509 error = xfs_zero_last_block(ip, io, offset, isize, end_size);
510 if (error) {
511 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
512 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
513 return error;
517 * Calculate the range between the new size and the old
518 * where blocks needing to be zeroed may exist. To get the
519 * block where the last byte in the file currently resides,
520 * we need to subtract one from the size and truncate back
521 * to a block boundary. We subtract 1 in case the size is
522 * exactly on a block boundary.
524 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
525 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
526 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
527 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
528 if (last_fsb == end_zero_fsb) {
530 * The size was only incremented on its last block.
531 * We took care of that above, so just return.
533 return 0;
536 ASSERT(start_zero_fsb <= end_zero_fsb);
537 prev_zero_fsb = NULLFILEOFF;
538 prev_zero_count = 0;
539 while (start_zero_fsb <= end_zero_fsb) {
540 nimaps = 1;
541 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
542 error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb,
543 0, NULL, 0, &imap, &nimaps, NULL);
544 if (error) {
545 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
546 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
547 return error;
549 ASSERT(nimaps > 0);
551 if (imap.br_state == XFS_EXT_UNWRITTEN ||
552 imap.br_startblock == HOLESTARTBLOCK) {
554 * This loop handles initializing pages that were
555 * partially initialized by the code below this
556 * loop. It basically zeroes the part of the page
557 * that sits on a hole and sets the page as P_HOLE
558 * and calls remapf if it is a mapped file.
560 prev_zero_fsb = NULLFILEOFF;
561 prev_zero_count = 0;
562 start_zero_fsb = imap.br_startoff +
563 imap.br_blockcount;
564 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
565 continue;
569 * There are blocks in the range requested.
570 * Zero them a single write at a time. We actually
571 * don't zero the entire range returned if it is
572 * too big and simply loop around to get the rest.
573 * That is not the most efficient thing to do, but it
574 * is simple and this path should not be exercised often.
576 buf_len_fsb = XFS_FILBLKS_MIN(imap.br_blockcount,
577 mp->m_writeio_blocks << 8);
579 * Drop the inode lock while we're doing the I/O.
580 * We'll still have the iolock to protect us.
582 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
584 loff = XFS_FSB_TO_B(mp, start_zero_fsb);
585 lsize = XFS_FSB_TO_B(mp, buf_len_fsb);
587 error = xfs_iozero(ip, loff, lsize, end_size);
589 if (error) {
590 goto out_lock;
593 prev_zero_fsb = start_zero_fsb;
594 prev_zero_count = buf_len_fsb;
595 start_zero_fsb = imap.br_startoff + buf_len_fsb;
596 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
598 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
601 return 0;
603 out_lock:
605 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
606 ASSERT(error >= 0);
607 return error;
610 ssize_t /* bytes written, or (-) error */
611 xfs_write(
612 bhv_desc_t *bdp,
613 struct kiocb *iocb,
614 const struct iovec *iovp,
615 unsigned int nsegs,
616 loff_t *offset,
617 int ioflags,
618 cred_t *credp)
620 struct file *file = iocb->ki_filp;
621 struct address_space *mapping = file->f_mapping;
622 struct inode *inode = mapping->host;
623 unsigned long segs = nsegs;
624 xfs_inode_t *xip;
625 xfs_mount_t *mp;
626 ssize_t ret = 0, error = 0;
627 xfs_fsize_t isize, new_size;
628 xfs_iocore_t *io;
629 vnode_t *vp;
630 unsigned long seg;
631 int iolock;
632 int eventsent = 0;
633 vrwlock_t locktype;
634 size_t ocount = 0, count;
635 loff_t pos;
636 int need_isem = 1, need_flush = 0;
638 XFS_STATS_INC(xs_write_calls);
640 vp = BHV_TO_VNODE(bdp);
641 xip = XFS_BHVTOI(bdp);
643 for (seg = 0; seg < segs; seg++) {
644 const struct iovec *iv = &iovp[seg];
647 * If any segment has a negative length, or the cumulative
648 * length ever wraps negative then return -EINVAL.
650 ocount += iv->iov_len;
651 if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
652 return -EINVAL;
653 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
654 continue;
655 if (seg == 0)
656 return -EFAULT;
657 segs = seg;
658 ocount -= iv->iov_len; /* This segment is no good */
659 break;
662 count = ocount;
663 pos = *offset;
665 if (count == 0)
666 return 0;
668 io = &xip->i_iocore;
669 mp = io->io_mount;
671 if (XFS_FORCED_SHUTDOWN(mp))
672 return -EIO;
674 if (ioflags & IO_ISDIRECT) {
675 xfs_buftarg_t *target =
676 (xip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
677 mp->m_rtdev_targp : mp->m_ddev_targp;
679 if ((pos & target->pbr_smask) || (count & target->pbr_smask))
680 return XFS_ERROR(-EINVAL);
682 if (!VN_CACHED(vp) && pos < i_size_read(inode))
683 need_isem = 0;
685 if (VN_CACHED(vp))
686 need_flush = 1;
689 relock:
690 if (need_isem) {
691 iolock = XFS_IOLOCK_EXCL;
692 locktype = VRWLOCK_WRITE;
694 down(&inode->i_sem);
695 } else {
696 iolock = XFS_IOLOCK_SHARED;
697 locktype = VRWLOCK_WRITE_DIRECT;
700 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
702 isize = i_size_read(inode);
704 if (file->f_flags & O_APPEND)
705 *offset = isize;
707 start:
708 error = -generic_write_checks(file, &pos, &count,
709 S_ISBLK(inode->i_mode));
710 if (error) {
711 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
712 goto out_unlock_isem;
715 new_size = pos + count;
716 if (new_size > isize)
717 io->io_new_size = new_size;
719 if ((DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_WRITE) &&
720 !(ioflags & IO_INVIS) && !eventsent)) {
721 loff_t savedsize = pos;
722 int dmflags = FILP_DELAY_FLAG(file);
724 if (need_isem)
725 dmflags |= DM_FLAGS_ISEM;
727 xfs_iunlock(xip, XFS_ILOCK_EXCL);
728 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,
729 pos, count,
730 dmflags, &locktype);
731 if (error) {
732 xfs_iunlock(xip, iolock);
733 goto out_unlock_isem;
735 xfs_ilock(xip, XFS_ILOCK_EXCL);
736 eventsent = 1;
739 * The iolock was dropped and reaquired in XFS_SEND_DATA
740 * so we have to recheck the size when appending.
741 * We will only "goto start;" once, since having sent the
742 * event prevents another call to XFS_SEND_DATA, which is
743 * what allows the size to change in the first place.
745 if ((file->f_flags & O_APPEND) && savedsize != isize) {
746 pos = isize = xip->i_d.di_size;
747 goto start;
752 * On Linux, generic_file_write updates the times even if
753 * no data is copied in so long as the write had a size.
755 * We must update xfs' times since revalidate will overcopy xfs.
757 if (!(ioflags & IO_INVIS)) {
758 xfs_ichgtime(xip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
759 inode_update_time(inode, 1);
763 * If the offset is beyond the size of the file, we have a couple
764 * of things to do. First, if there is already space allocated
765 * we need to either create holes or zero the disk or ...
767 * If there is a page where the previous size lands, we need
768 * to zero it out up to the new size.
771 if (pos > isize) {
772 error = xfs_zero_eof(BHV_TO_VNODE(bdp), io, pos,
773 isize, pos + count);
774 if (error) {
775 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
776 goto out_unlock_isem;
779 xfs_iunlock(xip, XFS_ILOCK_EXCL);
782 * If we're writing the file then make sure to clear the
783 * setuid and setgid bits if the process is not being run
784 * by root. This keeps people from modifying setuid and
785 * setgid binaries.
788 if (((xip->i_d.di_mode & S_ISUID) ||
789 ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
790 (S_ISGID | S_IXGRP))) &&
791 !capable(CAP_FSETID)) {
792 error = xfs_write_clear_setuid(xip);
793 if (likely(!error))
794 error = -remove_suid(file->f_dentry);
795 if (unlikely(error)) {
796 xfs_iunlock(xip, iolock);
797 goto out_unlock_isem;
801 retry:
802 /* We can write back this queue in page reclaim */
803 current->backing_dev_info = mapping->backing_dev_info;
805 if ((ioflags & IO_ISDIRECT)) {
806 if (need_flush) {
807 xfs_inval_cached_trace(io, pos, -1,
808 ctooff(offtoct(pos)), -1);
809 VOP_FLUSHINVAL_PAGES(vp, ctooff(offtoct(pos)),
810 -1, FI_REMAPF_LOCKED);
813 if (need_isem) {
814 /* demote the lock now the cached pages are gone */
815 XFS_ILOCK_DEMOTE(mp, io, XFS_IOLOCK_EXCL);
816 up(&inode->i_sem);
818 iolock = XFS_IOLOCK_SHARED;
819 locktype = VRWLOCK_WRITE_DIRECT;
820 need_isem = 0;
823 xfs_rw_enter_trace(XFS_DIOWR_ENTER, io, (void *)iovp, segs,
824 *offset, ioflags);
825 ret = generic_file_direct_write(iocb, iovp,
826 &segs, pos, offset, count, ocount);
829 * direct-io write to a hole: fall through to buffered I/O
830 * for completing the rest of the request.
832 if (ret >= 0 && ret != count) {
833 XFS_STATS_ADD(xs_write_bytes, ret);
835 pos += ret;
836 count -= ret;
838 need_isem = 1;
839 ioflags &= ~IO_ISDIRECT;
840 xfs_iunlock(xip, iolock);
841 goto relock;
843 } else {
844 xfs_rw_enter_trace(XFS_WRITE_ENTER, io, (void *)iovp, segs,
845 *offset, ioflags);
846 ret = generic_file_buffered_write(iocb, iovp, segs,
847 pos, offset, count, ret);
850 current->backing_dev_info = NULL;
852 if (ret == -EIOCBQUEUED)
853 ret = wait_on_sync_kiocb(iocb);
855 if ((ret == -ENOSPC) &&
856 DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_NOSPACE) &&
857 !(ioflags & IO_INVIS)) {
859 xfs_rwunlock(bdp, locktype);
860 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp,
861 DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL,
862 0, 0, 0); /* Delay flag intentionally unused */
863 if (error)
864 goto out_unlock_isem;
865 xfs_rwlock(bdp, locktype);
866 pos = xip->i_d.di_size;
867 goto retry;
870 if (*offset > xip->i_d.di_size) {
871 xfs_ilock(xip, XFS_ILOCK_EXCL);
872 if (*offset > xip->i_d.di_size) {
873 xip->i_d.di_size = *offset;
874 i_size_write(inode, *offset);
875 xip->i_update_core = 1;
876 xip->i_update_size = 1;
878 xfs_iunlock(xip, XFS_ILOCK_EXCL);
881 error = -ret;
882 if (ret <= 0)
883 goto out_unlock_internal;
885 XFS_STATS_ADD(xs_write_bytes, ret);
887 /* Handle various SYNC-type writes */
888 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
890 * If we're treating this as O_DSYNC and we have not updated the
891 * size, force the log.
893 if (!(mp->m_flags & XFS_MOUNT_OSYNCISOSYNC) &&
894 !(xip->i_update_size)) {
895 xfs_inode_log_item_t *iip = xip->i_itemp;
898 * If an allocation transaction occurred
899 * without extending the size, then we have to force
900 * the log up the proper point to ensure that the
901 * allocation is permanent. We can't count on
902 * the fact that buffered writes lock out direct I/O
903 * writes - the direct I/O write could have extended
904 * the size nontransactionally, then finished before
905 * we started. xfs_write_file will think that the file
906 * didn't grow but the update isn't safe unless the
907 * size change is logged.
909 * Force the log if we've committed a transaction
910 * against the inode or if someone else has and
911 * the commit record hasn't gone to disk (e.g.
912 * the inode is pinned). This guarantees that
913 * all changes affecting the inode are permanent
914 * when we return.
916 if (iip && iip->ili_last_lsn) {
917 xfs_log_force(mp, iip->ili_last_lsn,
918 XFS_LOG_FORCE | XFS_LOG_SYNC);
919 } else if (xfs_ipincount(xip) > 0) {
920 xfs_log_force(mp, (xfs_lsn_t)0,
921 XFS_LOG_FORCE | XFS_LOG_SYNC);
924 } else {
925 xfs_trans_t *tp;
928 * O_SYNC or O_DSYNC _with_ a size update are handled
929 * the same way.
931 * If the write was synchronous then we need to make
932 * sure that the inode modification time is permanent.
933 * We'll have updated the timestamp above, so here
934 * we use a synchronous transaction to log the inode.
935 * It's not fast, but it's necessary.
937 * If this a dsync write and the size got changed
938 * non-transactionally, then we need to ensure that
939 * the size change gets logged in a synchronous
940 * transaction.
943 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITE_SYNC);
944 if ((error = xfs_trans_reserve(tp, 0,
945 XFS_SWRITE_LOG_RES(mp),
946 0, 0, 0))) {
947 /* Transaction reserve failed */
948 xfs_trans_cancel(tp, 0);
949 } else {
950 /* Transaction reserve successful */
951 xfs_ilock(xip, XFS_ILOCK_EXCL);
952 xfs_trans_ijoin(tp, xip, XFS_ILOCK_EXCL);
953 xfs_trans_ihold(tp, xip);
954 xfs_trans_log_inode(tp, xip, XFS_ILOG_CORE);
955 xfs_trans_set_sync(tp);
956 error = xfs_trans_commit(tp, 0, NULL);
957 xfs_iunlock(xip, XFS_ILOCK_EXCL);
958 if (error)
959 goto out_unlock_internal;
963 xfs_rwunlock(bdp, locktype);
964 if (need_isem)
965 up(&inode->i_sem);
967 error = sync_page_range(inode, mapping, pos, ret);
968 if (!error)
969 error = ret;
970 return error;
973 out_unlock_internal:
974 xfs_rwunlock(bdp, locktype);
975 out_unlock_isem:
976 if (need_isem)
977 up(&inode->i_sem);
978 return -error;
982 * All xfs metadata buffers except log state machine buffers
983 * get this attached as their b_bdstrat callback function.
984 * This is so that we can catch a buffer
985 * after prematurely unpinning it to forcibly shutdown the filesystem.
988 xfs_bdstrat_cb(struct xfs_buf *bp)
990 xfs_mount_t *mp;
992 mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
993 if (!XFS_FORCED_SHUTDOWN(mp)) {
994 pagebuf_iorequest(bp);
995 return 0;
996 } else {
997 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
999 * Metadata write that didn't get logged but
1000 * written delayed anyway. These aren't associated
1001 * with a transaction, and can be ignored.
1003 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
1004 (XFS_BUF_ISREAD(bp)) == 0)
1005 return (xfs_bioerror_relse(bp));
1006 else
1007 return (xfs_bioerror(bp));
1013 xfs_bmap(bhv_desc_t *bdp,
1014 xfs_off_t offset,
1015 ssize_t count,
1016 int flags,
1017 xfs_iomap_t *iomapp,
1018 int *niomaps)
1020 xfs_inode_t *ip = XFS_BHVTOI(bdp);
1021 xfs_iocore_t *io = &ip->i_iocore;
1023 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
1024 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
1025 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
1027 return xfs_iomap(io, offset, count, flags, iomapp, niomaps);
1031 * Wrapper around bdstrat so that we can stop data
1032 * from going to disk in case we are shutting down the filesystem.
1033 * Typically user data goes thru this path; one of the exceptions
1034 * is the superblock.
1037 xfsbdstrat(
1038 struct xfs_mount *mp,
1039 struct xfs_buf *bp)
1041 ASSERT(mp);
1042 if (!XFS_FORCED_SHUTDOWN(mp)) {
1043 /* Grio redirection would go here
1044 * if (XFS_BUF_IS_GRIO(bp)) {
1047 pagebuf_iorequest(bp);
1048 return 0;
1051 xfs_buftrace("XFSBDSTRAT IOERROR", bp);
1052 return (xfs_bioerror_relse(bp));
1056 * If the underlying (data/log/rt) device is readonly, there are some
1057 * operations that cannot proceed.
1060 xfs_dev_is_read_only(
1061 xfs_mount_t *mp,
1062 char *message)
1064 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
1065 xfs_readonly_buftarg(mp->m_logdev_targp) ||
1066 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
1067 cmn_err(CE_NOTE,
1068 "XFS: %s required on read-only device.", message);
1069 cmn_err(CE_NOTE,
1070 "XFS: write access unavailable, cannot proceed.");
1071 return EROFS;
1073 return 0;