2 * Copyright (c) 2000-2004 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:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
38 #include "xfs_trans.h"
43 #include "xfs_alloc.h"
44 #include "xfs_dmapi.h"
45 #include "xfs_quota.h"
46 #include "xfs_mount.h"
47 #include "xfs_alloc_btree.h"
48 #include "xfs_bmap_btree.h"
49 #include "xfs_ialloc_btree.h"
50 #include "xfs_btree.h"
51 #include "xfs_ialloc.h"
52 #include "xfs_attr_sf.h"
53 #include "xfs_dir_sf.h"
54 #include "xfs_dir2_sf.h"
55 #include "xfs_dinode.h"
56 #include "xfs_inode.h"
59 #include "xfs_rtalloc.h"
60 #include "xfs_error.h"
61 #include "xfs_itable.h"
67 #include "xfs_buf_item.h"
68 #include "xfs_trans_space.h"
69 #include "xfs_utils.h"
70 #include "xfs_iomap.h"
72 #if defined(XFS_RW_TRACE)
74 xfs_iomap_enter_trace(
80 xfs_inode_t *ip = XFS_IO_INODE(io);
85 ktrace_enter(ip->i_rwtrace,
86 (void *)((unsigned long)tag),
88 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
89 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
90 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
91 (void *)((unsigned long)(offset & 0xffffffff)),
92 (void *)((unsigned long)count),
93 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
94 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
111 xfs_bmbt_irec_t *imapp,
114 xfs_inode_t *ip = XFS_IO_INODE(io);
119 ktrace_enter(ip->i_rwtrace,
120 (void *)((unsigned long)tag),
122 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
123 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
124 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
125 (void *)((unsigned long)(offset & 0xffffffff)),
126 (void *)((unsigned long)count),
127 (void *)((unsigned long)flags),
128 (void *)((unsigned long)((iomapp->iomap_offset >> 32) & 0xffffffff)),
129 (void *)((unsigned long)(iomapp->iomap_offset & 0xffffffff)),
130 (void *)((unsigned long)(iomapp->iomap_delta)),
131 (void *)((unsigned long)(iomapp->iomap_bsize)),
132 (void *)((unsigned long)(iomapp->iomap_bn)),
133 (void *)(__psint_t)(imapp->br_startoff),
134 (void *)((unsigned long)(imapp->br_blockcount)),
135 (void *)(__psint_t)(imapp->br_startblock));
138 #define xfs_iomap_enter_trace(tag, io, offset, count)
139 #define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags)
142 #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
143 << mp->m_writeio_log)
144 #define XFS_STRAT_WRITE_IMAPS 2
145 #define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
151 xfs_bmbt_irec_t *imap,
153 int imaps, /* Number of imap entries */
154 int iomaps, /* Number of iomap entries */
160 xfs_fsblock_t start_block;
163 nisize = XFS_SIZE(mp, io);
164 if (io->io_new_size > nisize)
165 nisize = io->io_new_size;
167 for (pbm = 0; imaps && pbm < iomaps; imaps--, iomapp++, imap++, pbm++) {
168 iomapp->iomap_offset = XFS_FSB_TO_B(mp, imap->br_startoff);
169 iomapp->iomap_delta = offset - iomapp->iomap_offset;
170 iomapp->iomap_bsize = XFS_FSB_TO_B(mp, imap->br_blockcount);
171 iomapp->iomap_flags = flags;
173 if (io->io_flags & XFS_IOCORE_RT) {
174 iomapp->iomap_flags |= IOMAP_REALTIME;
175 iomapp->iomap_target = mp->m_rtdev_targp;
177 iomapp->iomap_target = mp->m_ddev_targp;
179 start_block = imap->br_startblock;
180 if (start_block == HOLESTARTBLOCK) {
181 iomapp->iomap_bn = IOMAP_DADDR_NULL;
182 iomapp->iomap_flags |= IOMAP_HOLE;
183 } else if (start_block == DELAYSTARTBLOCK) {
184 iomapp->iomap_bn = IOMAP_DADDR_NULL;
185 iomapp->iomap_flags |= IOMAP_DELAY;
187 iomapp->iomap_bn = XFS_FSB_TO_DB_IO(io, start_block);
188 if (ISUNWRITTEN(imap))
189 iomapp->iomap_flags |= IOMAP_UNWRITTEN;
192 if ((iomapp->iomap_offset + iomapp->iomap_bsize) >= nisize) {
193 iomapp->iomap_flags |= IOMAP_EOF;
196 offset += iomapp->iomap_bsize - iomapp->iomap_delta;
198 return pbm; /* Return the number filled */
210 xfs_mount_t *mp = io->io_mount;
211 xfs_fileoff_t offset_fsb, end_fsb;
214 xfs_bmbt_irec_t imap;
219 if (XFS_FORCED_SHUTDOWN(mp))
220 return XFS_ERROR(EIO);
223 (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE |
224 BMAPI_UNWRITTEN | BMAPI_DEVICE)) {
226 xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER, io, offset, count);
227 lockmode = XFS_LCK_MAP_SHARED(mp, io);
228 bmapi_flags = XFS_BMAPI_ENTIRE;
229 if (flags & BMAPI_IGNSTATE)
230 bmapi_flags |= XFS_BMAPI_IGSTATE;
233 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER, io, offset, count);
234 lockmode = XFS_ILOCK_EXCL|XFS_EXTSIZE_WR;
236 XFS_ILOCK(mp, io, lockmode);
239 xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER, io, offset, count);
240 lockmode = XFS_ILOCK_SHARED|XFS_EXTSIZE_RD;
241 bmapi_flags = XFS_BMAPI_ENTIRE;
242 /* Attempt non-blocking lock */
243 if (flags & BMAPI_TRYLOCK) {
244 if (!XFS_ILOCK_NOWAIT(mp, io, lockmode))
245 return XFS_ERROR(EAGAIN);
247 XFS_ILOCK(mp, io, lockmode);
250 case BMAPI_UNWRITTEN:
253 lockmode = XFS_LCK_MAP_SHARED(mp, io);
254 iomapp->iomap_target = io->io_flags & XFS_IOCORE_RT ?
255 mp->m_rtdev_targp : mp->m_ddev_targp;
263 ASSERT(offset <= mp->m_maxioffset);
264 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
265 count = mp->m_maxioffset - offset;
266 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
267 offset_fsb = XFS_B_TO_FSBT(mp, offset);
269 error = XFS_BMAPI(mp, NULL, io, offset_fsb,
270 (xfs_filblks_t)(end_fsb - offset_fsb),
271 bmapi_flags, NULL, 0, &imap,
278 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE|BMAPI_UNWRITTEN)) {
280 /* If we found an extent, return it */
282 (imap.br_startblock != HOLESTARTBLOCK) &&
283 (imap.br_startblock != DELAYSTARTBLOCK)) {
284 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
285 offset, count, iomapp, &imap, flags);
289 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
290 error = XFS_IOMAP_WRITE_DIRECT(mp, io, offset,
291 count, flags, &imap, &nimaps, nimaps);
293 error = XFS_IOMAP_WRITE_DELAY(mp, io, offset, count,
294 flags, &imap, &nimaps);
297 xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP, io,
298 offset, count, iomapp, &imap, flags);
300 iomap_flags = IOMAP_NEW;
303 /* If we found an extent, return it */
304 XFS_IUNLOCK(mp, io, lockmode);
307 if (nimaps && !ISNULLSTARTBLOCK(imap.br_startblock)) {
308 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
309 offset, count, iomapp, &imap, flags);
313 error = XFS_IOMAP_WRITE_ALLOCATE(mp, io, offset, count,
316 case BMAPI_UNWRITTEN:
318 error = XFS_IOMAP_WRITE_UNWRITTEN(mp, io, offset, count);
324 *niomaps = xfs_imap_to_bmap(io, offset, &imap,
325 iomapp, nimaps, *niomaps, iomap_flags);
326 } else if (niomaps) {
332 XFS_IUNLOCK(mp, io, lockmode);
333 return XFS_ERROR(error);
344 if (ip->i_delayed_blks) {
345 xfs_iunlock(ip, XFS_ILOCK_EXCL);
347 xfs_ilock(ip, XFS_ILOCK_EXCL);
350 *ioflags |= BMAPI_SYNC;
356 *ioflags |= BMAPI_SYNC;
359 xfs_iunlock(ip, XFS_ILOCK_EXCL);
360 xfs_flush_device(ip);
361 xfs_ilock(ip, XFS_ILOCK_EXCL);
369 xfs_iomap_write_direct(
374 xfs_bmbt_irec_t *ret_imap,
378 xfs_mount_t *mp = ip->i_mount;
379 xfs_iocore_t *io = &ip->i_iocore;
380 xfs_fileoff_t offset_fsb;
381 xfs_fileoff_t last_fsb;
382 xfs_filblks_t count_fsb;
384 xfs_fsblock_t firstfsb;
391 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS], *imapp;
392 xfs_bmap_free_t free_list;
394 xfs_filblks_t datablocks, qblocks, resblks;
399 * Make sure that the dquots are there. This doesn't hold
400 * the ilock across a disk read.
402 error = XFS_QM_DQATTACH(ip->i_mount, ip, XFS_QMOPT_ILOCKED);
404 return XFS_ERROR(error);
406 maps = min(XFS_WRITE_IMAPS, *nmaps);
409 isize = ip->i_d.di_size;
410 aeof = (offset + count) > isize;
412 if (io->io_new_size > isize)
413 isize = io->io_new_size;
415 offset_fsb = XFS_B_TO_FSBT(mp, offset);
416 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
417 count_fsb = last_fsb - offset_fsb;
418 if (found && (ret_imap->br_startblock == HOLESTARTBLOCK)) {
419 xfs_fileoff_t map_last_fsb;
421 map_last_fsb = ret_imap->br_blockcount + ret_imap->br_startoff;
422 if (map_last_fsb < last_fsb) {
423 last_fsb = map_last_fsb;
424 count_fsb = last_fsb - offset_fsb;
426 ASSERT(count_fsb > 0);
430 * Determine if reserving space on the data or realtime partition.
432 if ((rt = XFS_IS_REALTIME_INODE(ip))) {
435 if (!(extsz = ip->i_d.di_extsize))
436 extsz = mp->m_sb.sb_rextsize;
437 numrtextents = qblocks = (count_fsb + extsz - 1);
438 do_div(numrtextents, mp->m_sb.sb_rextsize);
439 quota_flag = XFS_QMOPT_RES_RTBLKS;
442 datablocks = qblocks = count_fsb;
443 quota_flag = XFS_QMOPT_RES_REGBLKS;
448 * Allocate and setup the transaction
450 xfs_iunlock(ip, XFS_ILOCK_EXCL);
451 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
452 resblks = XFS_DIOSTRAT_SPACE_RES(mp, datablocks);
453 error = xfs_trans_reserve(tp, resblks,
454 XFS_WRITE_LOG_RES(mp), numrtextents,
455 XFS_TRANS_PERM_LOG_RES,
456 XFS_WRITE_LOG_COUNT);
459 * Check for running out of space, note: need lock to return
462 xfs_trans_cancel(tp, 0);
463 xfs_ilock(ip, XFS_ILOCK_EXCL);
467 if (XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag)) {
472 bmapi_flag = XFS_BMAPI_WRITE;
473 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
474 xfs_trans_ihold(tp, ip);
476 if (!(flags & BMAPI_MMAP) && (offset < ip->i_d.di_size || rt))
477 bmapi_flag |= XFS_BMAPI_PREALLOC;
480 * Issue the bmapi() call to allocate the blocks
482 XFS_BMAP_INIT(&free_list, &firstfsb);
485 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
486 bmapi_flag, &firstfsb, 0, imapp, &nimaps, &free_list);
491 * Complete the transaction
493 error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
496 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
501 * Copy any maps to caller's array and return any error.
510 if ( !(io->io_flags & XFS_IOCORE_RT) && !ret_imap->br_startblock) {
511 cmn_err(CE_PANIC,"Access to block zero: fs <%s> inode: %lld "
512 "start_block : %llx start_off : %llx blkcnt : %llx "
513 "extent-state : %x \n",
514 (ip->i_mount)->m_fsname,
515 (long long)ip->i_ino,
516 ret_imap->br_startblock, ret_imap->br_startoff,
517 ret_imap->br_blockcount,ret_imap->br_state);
521 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
522 xfs_bmap_cancel(&free_list);
523 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
525 error1: /* Just cancel transaction */
526 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
527 *nmaps = 0; /* nothing set-up here */
530 return XFS_ERROR(error);
534 xfs_iomap_write_delay(
539 xfs_bmbt_irec_t *ret_imap,
542 xfs_mount_t *mp = ip->i_mount;
543 xfs_iocore_t *io = &ip->i_iocore;
544 xfs_fileoff_t offset_fsb;
545 xfs_fileoff_t last_fsb;
547 xfs_fsblock_t firstblock;
550 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
554 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
557 * Make sure that the dquots are there. This doesn't hold
558 * the ilock across a disk read.
561 error = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
563 return XFS_ERROR(error);
566 isize = ip->i_d.di_size;
567 if (io->io_new_size > isize) {
568 isize = io->io_new_size;
572 offset_fsb = XFS_B_TO_FSBT(mp, offset);
573 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
575 * If the caller is doing a write at the end of the file,
576 * then extend the allocation (and the buffer used for the write)
577 * out to the file system's write iosize. We clean up any extra
578 * space left over when the file is closed in xfs_inactive().
580 * For sync writes, we are flushing delayed allocate space to
581 * try to make additional space available for allocation near
582 * the filesystem full boundary - preallocation hurts in that
583 * situation, of course.
585 if (!(ioflag & BMAPI_SYNC) && ((offset + count) > ip->i_d.di_size)) {
586 xfs_off_t aligned_offset;
587 xfs_filblks_t count_fsb;
589 xfs_fileoff_t ioalign;
591 xfs_fileoff_t start_fsb;
594 * If there are any real blocks past eof, then don't
595 * do any speculative allocation.
597 start_fsb = XFS_B_TO_FSBT(mp,
598 ((xfs_ufsize_t)(offset + count - 1)));
599 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
600 while (count_fsb > 0) {
601 nimaps = XFS_WRITE_IMAPS;
602 error = XFS_BMAPI(mp, NULL, io, start_fsb, count_fsb,
603 0, &firstblock, 0, imap, &nimaps, NULL);
607 for (n = 0; n < nimaps; n++) {
608 if ( !(io->io_flags & XFS_IOCORE_RT) &&
609 !imap[n].br_startblock) {
610 cmn_err(CE_PANIC,"Access to block "
611 "zero: fs <%s> inode: %lld "
612 "start_block : %llx start_off "
613 ": %llx blkcnt : %llx "
614 "extent-state : %x \n",
615 (ip->i_mount)->m_fsname,
616 (long long)ip->i_ino,
617 imap[n].br_startblock,
619 imap[n].br_blockcount,
622 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
623 (imap[n].br_startblock != DELAYSTARTBLOCK)) {
626 start_fsb += imap[n].br_blockcount;
627 count_fsb -= imap[n].br_blockcount;
630 iosize = mp->m_writeio_blocks;
631 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
632 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
633 last_fsb = ioalign + iosize;
637 nimaps = XFS_WRITE_IMAPS;
638 firstblock = NULLFSBLOCK;
641 * If mounted with the "-o swalloc" option, roundup the allocation
642 * request to a stripe width boundary if the file size is >=
643 * stripe width and we are allocating past the allocation eof.
645 if (!(io->io_flags & XFS_IOCORE_RT) && mp->m_swidth
646 && (mp->m_flags & XFS_MOUNT_SWALLOC)
647 && (isize >= XFS_FSB_TO_B(mp, mp->m_swidth)) && aeof) {
649 xfs_fileoff_t new_last_fsb;
651 new_last_fsb = roundup_64(last_fsb, mp->m_swidth);
652 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
657 last_fsb = new_last_fsb;
660 * Roundup the allocation request to a stripe unit (m_dalign) boundary
661 * if the file size is >= stripe unit size, and we are allocating past
662 * the allocation eof.
664 } else if (!(io->io_flags & XFS_IOCORE_RT) && mp->m_dalign &&
665 (isize >= XFS_FSB_TO_B(mp, mp->m_dalign)) && aeof) {
667 xfs_fileoff_t new_last_fsb;
668 new_last_fsb = roundup_64(last_fsb, mp->m_dalign);
669 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
674 last_fsb = new_last_fsb;
677 * Round up the allocation request to a real-time extent boundary
678 * if the file is on the real-time subvolume.
680 } else if (io->io_flags & XFS_IOCORE_RT && aeof) {
682 xfs_fileoff_t new_last_fsb;
684 new_last_fsb = roundup_64(last_fsb, mp->m_sb.sb_rextsize);
685 error = XFS_BMAP_EOF(mp, io, new_last_fsb, XFS_DATA_FORK, &eof);
690 last_fsb = new_last_fsb;
692 error = xfs_bmapi(NULL, ip, offset_fsb,
693 (xfs_filblks_t)(last_fsb - offset_fsb),
694 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
695 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
698 * This can be EDQUOT, if nimaps == 0
700 if (error && (error != ENOSPC)) {
701 return XFS_ERROR(error);
704 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
705 * then we must have run out of space.
708 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE,
710 if (xfs_flush_space(ip, &fsynced, &ioflag))
711 return XFS_ERROR(ENOSPC);
719 if ( !(io->io_flags & XFS_IOCORE_RT) && !ret_imap->br_startblock) {
720 cmn_err(CE_PANIC,"Access to block zero: fs <%s> inode: %lld "
721 "start_block : %llx start_off : %llx blkcnt : %llx "
722 "extent-state : %x \n",
723 (ip->i_mount)->m_fsname,
724 (long long)ip->i_ino,
725 ret_imap->br_startblock, ret_imap->br_startoff,
726 ret_imap->br_blockcount,ret_imap->br_state);
732 * Pass in a delayed allocate extent, convert it to real extents;
733 * return to the caller the extent we create which maps on top of
734 * the originating callers request.
736 * Called without a lock on the inode.
739 xfs_iomap_write_allocate(
743 xfs_bmbt_irec_t *map,
746 xfs_mount_t *mp = ip->i_mount;
747 xfs_iocore_t *io = &ip->i_iocore;
748 xfs_fileoff_t offset_fsb, last_block;
749 xfs_fileoff_t end_fsb, map_start_fsb;
750 xfs_fsblock_t first_block;
751 xfs_bmap_free_t free_list;
752 xfs_filblks_t count_fsb;
753 xfs_bmbt_irec_t imap[XFS_STRAT_WRITE_IMAPS];
755 int i, nimaps, committed;
762 * Make sure that the dquots are there.
764 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
765 return XFS_ERROR(error);
767 offset_fsb = XFS_B_TO_FSBT(mp, offset);
768 count_fsb = map->br_blockcount;
769 map_start_fsb = map->br_startoff;
771 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
773 while (count_fsb != 0) {
775 * Set up a transaction with which to allocate the
776 * backing store for the file. Do allocations in a
777 * loop until we get some space in the range we are
778 * interested in. The other space that might be allocated
779 * is in the delayed allocation extent on which we sit
780 * but before our buffer starts.
784 while (nimaps == 0) {
785 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
786 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
787 error = xfs_trans_reserve(tp, nres,
788 XFS_WRITE_LOG_RES(mp),
789 0, XFS_TRANS_PERM_LOG_RES,
790 XFS_WRITE_LOG_COUNT);
791 if (error == ENOSPC) {
792 error = xfs_trans_reserve(tp, 0,
793 XFS_WRITE_LOG_RES(mp),
795 XFS_TRANS_PERM_LOG_RES,
796 XFS_WRITE_LOG_COUNT);
799 xfs_trans_cancel(tp, 0);
800 return XFS_ERROR(error);
802 xfs_ilock(ip, XFS_ILOCK_EXCL);
803 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
804 xfs_trans_ihold(tp, ip);
806 XFS_BMAP_INIT(&free_list, &first_block);
808 nimaps = XFS_STRAT_WRITE_IMAPS;
810 * Ensure we don't go beyond eof - it is possible
811 * the extents changed since we did the read call,
812 * we dropped the ilock in the interim.
815 end_fsb = XFS_B_TO_FSB(mp, ip->i_d.di_size);
816 xfs_bmap_last_offset(NULL, ip, &last_block,
818 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
819 if ((map_start_fsb + count_fsb) > last_block) {
820 count_fsb = last_block - map_start_fsb;
821 if (count_fsb == 0) {
827 /* Go get the actual blocks */
828 error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
829 XFS_BMAPI_WRITE, &first_block, 1,
830 imap, &nimaps, &free_list);
834 error = xfs_bmap_finish(&tp, &free_list,
835 first_block, &committed);
839 error = xfs_trans_commit(tp,
840 XFS_TRANS_RELEASE_LOG_RES, NULL);
844 xfs_iunlock(ip, XFS_ILOCK_EXCL);
848 * See if we were able to allocate an extent that
849 * covers at least part of the callers request
852 for (i = 0; i < nimaps; i++) {
853 if ( !(io->io_flags & XFS_IOCORE_RT) &&
854 !imap[i].br_startblock) {
855 cmn_err(CE_PANIC,"Access to block zero: "
856 "fs <%s> inode: %lld "
857 "start_block : %llx start_off : %llx "
858 "blkcnt : %llx extent-state : %x \n",
859 (ip->i_mount)->m_fsname,
860 (long long)ip->i_ino,
861 imap[i].br_startblock,
863 imap[i].br_blockcount,imap[i].br_state);
865 if ((offset_fsb >= imap[i].br_startoff) &&
866 (offset_fsb < (imap[i].br_startoff +
867 imap[i].br_blockcount))) {
870 XFS_STATS_INC(xs_xstrat_quick);
873 count_fsb -= imap[i].br_blockcount;
876 /* So far we have not mapped the requested part of the
877 * file, just surrounding data, try again.
880 map_start_fsb = imap[nimaps].br_startoff +
881 imap[nimaps].br_blockcount;
885 xfs_bmap_cancel(&free_list);
886 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
888 xfs_iunlock(ip, XFS_ILOCK_EXCL);
889 return XFS_ERROR(error);
893 xfs_iomap_write_unwritten(
898 xfs_mount_t *mp = ip->i_mount;
899 xfs_iocore_t *io = &ip->i_iocore;
901 xfs_fileoff_t offset_fsb;
902 xfs_filblks_t count_fsb;
903 xfs_filblks_t numblks_fsb;
904 xfs_bmbt_irec_t imap;
909 xfs_fsblock_t firstfsb;
910 xfs_bmap_free_t free_list;
912 xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN,
913 &ip->i_iocore, offset, count);
915 offset_fsb = XFS_B_TO_FSBT(mp, offset);
916 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
917 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
920 nres = XFS_DIOSTRAT_SPACE_RES(mp, 0);
923 * set up a transaction to convert the range of extents
924 * from unwritten to real. Do allocations in a loop until
925 * we have covered the range passed in.
928 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
929 error = xfs_trans_reserve(tp, nres,
930 XFS_WRITE_LOG_RES(mp), 0,
931 XFS_TRANS_PERM_LOG_RES,
932 XFS_WRITE_LOG_COUNT);
934 xfs_trans_cancel(tp, 0);
938 xfs_ilock(ip, XFS_ILOCK_EXCL);
939 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
940 xfs_trans_ihold(tp, ip);
943 * Modify the unwritten extent state of the buffer.
945 XFS_BMAP_INIT(&free_list, &firstfsb);
947 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
948 XFS_BMAPI_WRITE, &firstfsb,
949 1, &imap, &nimaps, &free_list);
951 goto error_on_bmapi_transaction;
953 error = xfs_bmap_finish(&(tp), &(free_list),
954 firstfsb, &committed);
956 goto error_on_bmapi_transaction;
958 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
959 xfs_iunlock(ip, XFS_ILOCK_EXCL);
963 if ( !(io->io_flags & XFS_IOCORE_RT) && !imap.br_startblock) {
964 cmn_err(CE_PANIC,"Access to block zero: fs <%s> "
965 "inode: %lld start_block : %llx start_off : "
966 "%llx blkcnt : %llx extent-state : %x \n",
967 (ip->i_mount)->m_fsname,
968 (long long)ip->i_ino,
969 imap.br_startblock,imap.br_startoff,
970 imap.br_blockcount,imap.br_state);
973 if ((numblks_fsb = imap.br_blockcount) == 0) {
975 * The numblks_fsb value should always get
976 * smaller, otherwise the loop is stuck.
978 ASSERT(imap.br_blockcount);
981 offset_fsb += numblks_fsb;
982 count_fsb -= numblks_fsb;
983 } while (count_fsb > 0);
987 error_on_bmapi_transaction:
988 xfs_bmap_cancel(&free_list);
989 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
990 xfs_iunlock(ip, XFS_ILOCK_EXCL);
992 return XFS_ERROR(error);