2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "xfs_trans.h"
27 #include "xfs_alloc.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_quota.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
38 #include "xfs_ialloc.h"
39 #include "xfs_btree.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_error.h"
43 #include "xfs_itable.h"
47 #include "xfs_buf_item.h"
48 #include "xfs_trans_space.h"
49 #include "xfs_utils.h"
50 #include "xfs_iomap.h"
52 #if defined(XFS_RW_TRACE)
54 xfs_iomap_enter_trace(
60 xfs_iocore_t *io = &ip->i_iocore;
65 ktrace_enter(ip->i_rwtrace,
66 (void *)((unsigned long)tag),
68 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
69 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
70 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
71 (void *)((unsigned long)(offset & 0xffffffff)),
72 (void *)((unsigned long)count),
73 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
74 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
75 (void *)((unsigned long)current_pid()),
91 xfs_bmbt_irec_t *imapp,
97 ktrace_enter(ip->i_rwtrace,
98 (void *)((unsigned long)tag),
100 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
101 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
102 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
103 (void *)((unsigned long)(offset & 0xffffffff)),
104 (void *)((unsigned long)count),
105 (void *)((unsigned long)flags),
106 (void *)((unsigned long)((iomapp->iomap_offset >> 32) & 0xffffffff)),
107 (void *)((unsigned long)(iomapp->iomap_offset & 0xffffffff)),
108 (void *)((unsigned long)(iomapp->iomap_delta)),
109 (void *)((unsigned long)(iomapp->iomap_bsize)),
110 (void *)((unsigned long)(iomapp->iomap_bn)),
111 (void *)(__psint_t)(imapp->br_startoff),
112 (void *)((unsigned long)(imapp->br_blockcount)),
113 (void *)(__psint_t)(imapp->br_startblock));
116 #define xfs_iomap_enter_trace(tag, io, offset, count)
117 #define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags)
120 #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
121 << mp->m_writeio_log)
122 #define XFS_STRAT_WRITE_IMAPS 2
123 #define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
129 xfs_bmbt_irec_t *imap,
131 int imaps, /* Number of imap entries */
132 int iomaps, /* Number of iomap entries */
135 xfs_mount_t *mp = ip->i_mount;
137 xfs_fsblock_t start_block;
140 for (pbm = 0; imaps && pbm < iomaps; imaps--, iomapp++, imap++, pbm++) {
141 iomapp->iomap_offset = XFS_FSB_TO_B(mp, imap->br_startoff);
142 iomapp->iomap_delta = offset - iomapp->iomap_offset;
143 iomapp->iomap_bsize = XFS_FSB_TO_B(mp, imap->br_blockcount);
144 iomapp->iomap_flags = flags;
146 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) {
147 iomapp->iomap_flags |= IOMAP_REALTIME;
148 iomapp->iomap_target = mp->m_rtdev_targp;
150 iomapp->iomap_target = mp->m_ddev_targp;
152 start_block = imap->br_startblock;
153 if (start_block == HOLESTARTBLOCK) {
154 iomapp->iomap_bn = IOMAP_DADDR_NULL;
155 iomapp->iomap_flags |= IOMAP_HOLE;
156 } else if (start_block == DELAYSTARTBLOCK) {
157 iomapp->iomap_bn = IOMAP_DADDR_NULL;
158 iomapp->iomap_flags |= IOMAP_DELAY;
160 iomapp->iomap_bn = XFS_FSB_TO_DB(ip, start_block);
161 if (ISUNWRITTEN(imap))
162 iomapp->iomap_flags |= IOMAP_UNWRITTEN;
165 offset += iomapp->iomap_bsize - iomapp->iomap_delta;
167 return pbm; /* Return the number filled */
179 xfs_mount_t *mp = ip->i_mount;
180 xfs_fileoff_t offset_fsb, end_fsb;
183 xfs_bmbt_irec_t imap;
188 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
189 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
190 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
192 if (XFS_FORCED_SHUTDOWN(mp))
193 return XFS_ERROR(EIO);
195 switch (flags & (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE)) {
197 xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER, ip, offset, count);
198 lockmode = xfs_ilock_map_shared(ip);
199 bmapi_flags = XFS_BMAPI_ENTIRE;
202 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER, ip, offset, count);
203 lockmode = XFS_ILOCK_EXCL|XFS_EXTSIZE_WR;
204 if (flags & BMAPI_IGNSTATE)
205 bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
206 xfs_ilock(ip, lockmode);
209 xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER, ip, offset, count);
210 lockmode = XFS_ILOCK_SHARED|XFS_EXTSIZE_RD;
211 bmapi_flags = XFS_BMAPI_ENTIRE;
213 /* Attempt non-blocking lock */
214 if (flags & BMAPI_TRYLOCK) {
215 if (!xfs_ilock_nowait(ip, lockmode))
216 return XFS_ERROR(EAGAIN);
218 xfs_ilock(ip, lockmode);
225 ASSERT(offset <= mp->m_maxioffset);
226 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
227 count = mp->m_maxioffset - offset;
228 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
229 offset_fsb = XFS_B_TO_FSBT(mp, offset);
231 error = xfs_bmapi(NULL, ip, offset_fsb,
232 (xfs_filblks_t)(end_fsb - offset_fsb),
233 bmapi_flags, NULL, 0, &imap,
234 &nimaps, NULL, NULL);
239 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)) {
241 /* If we found an extent, return it */
243 (imap.br_startblock != HOLESTARTBLOCK) &&
244 (imap.br_startblock != DELAYSTARTBLOCK)) {
245 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, ip,
246 offset, count, iomapp, &imap, flags);
250 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
251 error = xfs_iomap_write_direct(ip, offset, count, flags,
252 &imap, &nimaps, nimaps);
254 error = xfs_iomap_write_delay(ip, offset, count, flags,
258 xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP, ip,
259 offset, count, iomapp, &imap, flags);
261 iomap_flags = IOMAP_NEW;
264 /* If we found an extent, return it */
265 xfs_iunlock(ip, lockmode);
268 if (nimaps && !ISNULLSTARTBLOCK(imap.br_startblock)) {
269 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, ip,
270 offset, count, iomapp, &imap, flags);
274 error = xfs_iomap_write_allocate(ip, offset, count,
280 *niomaps = xfs_imap_to_bmap(ip, offset, &imap,
281 iomapp, nimaps, *niomaps, iomap_flags);
282 } else if (niomaps) {
288 xfs_iunlock(ip, lockmode);
289 return XFS_ERROR(error);
294 xfs_iomap_eof_align_last_fsb(
298 xfs_extlen_t extsize,
299 xfs_fileoff_t *last_fsb)
301 xfs_fileoff_t new_last_fsb = 0;
305 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)
308 * If mounted with the "-o swalloc" option, roundup the allocation
309 * request to a stripe width boundary if the file size is >=
310 * stripe width and we are allocating past the allocation eof.
312 else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
313 (isize >= XFS_FSB_TO_B(mp, mp->m_swidth)))
314 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
316 * Roundup the allocation request to a stripe unit (m_dalign) boundary
317 * if the file size is >= stripe unit size, and we are allocating past
318 * the allocation eof.
320 else if (mp->m_dalign && (isize >= XFS_FSB_TO_B(mp, mp->m_dalign)))
321 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
324 * Always round up the allocation request to an extent boundary
325 * (when file on a real-time subvolume or has di_extsize hint).
329 align = roundup_64(new_last_fsb, extsize);
332 new_last_fsb = roundup_64(*last_fsb, align);
336 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
340 *last_fsb = new_last_fsb;
353 if (ip->i_delayed_blks) {
354 xfs_iunlock(ip, XFS_ILOCK_EXCL);
356 xfs_ilock(ip, XFS_ILOCK_EXCL);
359 *ioflags |= BMAPI_SYNC;
365 *ioflags |= BMAPI_SYNC;
368 xfs_iunlock(ip, XFS_ILOCK_EXCL);
369 xfs_flush_device(ip);
370 xfs_ilock(ip, XFS_ILOCK_EXCL);
378 xfs_cmn_err_fsblock_zero(
380 xfs_bmbt_irec_t *imap)
382 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
383 "Access to block zero in inode %llu "
384 "start_block: %llx start_off: %llx "
385 "blkcnt: %llx extent-state: %x\n",
386 (unsigned long long)ip->i_ino,
387 (unsigned long long)imap->br_startblock,
388 (unsigned long long)imap->br_startoff,
389 (unsigned long long)imap->br_blockcount,
395 xfs_iomap_write_direct(
400 xfs_bmbt_irec_t *ret_imap,
404 xfs_mount_t *mp = ip->i_mount;
405 xfs_iocore_t *io = &ip->i_iocore;
406 xfs_fileoff_t offset_fsb;
407 xfs_fileoff_t last_fsb;
408 xfs_filblks_t count_fsb, resaligned;
409 xfs_fsblock_t firstfsb;
410 xfs_extlen_t extsz, temp;
417 xfs_bmbt_irec_t imap;
418 xfs_bmap_free_t free_list;
419 uint qblocks, resblks, resrtextents;
424 * Make sure that the dquots are there. This doesn't hold
425 * the ilock across a disk read.
427 error = XFS_QM_DQATTACH(ip->i_mount, ip, XFS_QMOPT_ILOCKED);
429 return XFS_ERROR(error);
431 rt = XFS_IS_REALTIME_INODE(ip);
432 extsz = xfs_get_extsz_hint(ip);
435 if (io->io_new_size > isize)
436 isize = io->io_new_size;
438 offset_fsb = XFS_B_TO_FSBT(mp, offset);
439 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
440 if ((offset + count) > isize) {
441 error = xfs_iomap_eof_align_last_fsb(mp, ip, isize, extsz,
446 if (found && (ret_imap->br_startblock == HOLESTARTBLOCK))
447 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
448 ret_imap->br_blockcount +
449 ret_imap->br_startoff);
451 count_fsb = last_fsb - offset_fsb;
452 ASSERT(count_fsb > 0);
454 resaligned = count_fsb;
455 if (unlikely(extsz)) {
456 if ((temp = do_mod(offset_fsb, extsz)))
458 if ((temp = do_mod(resaligned, extsz)))
459 resaligned += extsz - temp;
463 resrtextents = qblocks = resaligned;
464 resrtextents /= mp->m_sb.sb_rextsize;
465 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
466 quota_flag = XFS_QMOPT_RES_RTBLKS;
469 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
470 quota_flag = XFS_QMOPT_RES_REGBLKS;
474 * Allocate and setup the transaction
476 xfs_iunlock(ip, XFS_ILOCK_EXCL);
477 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
478 error = xfs_trans_reserve(tp, resblks,
479 XFS_WRITE_LOG_RES(mp), resrtextents,
480 XFS_TRANS_PERM_LOG_RES,
481 XFS_WRITE_LOG_COUNT);
483 * Check for running out of space, note: need lock to return
486 xfs_trans_cancel(tp, 0);
487 xfs_ilock(ip, XFS_ILOCK_EXCL);
491 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
492 qblocks, 0, quota_flag);
496 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
497 xfs_trans_ihold(tp, ip);
499 bmapi_flag = XFS_BMAPI_WRITE;
500 if ((flags & BMAPI_DIRECT) && (offset < ip->i_size || extsz))
501 bmapi_flag |= XFS_BMAPI_PREALLOC;
504 * Issue the xfs_bmapi() call to allocate the blocks
506 XFS_BMAP_INIT(&free_list, &firstfsb);
508 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, bmapi_flag,
509 &firstfsb, 0, &imap, &nimaps, &free_list, NULL);
514 * Complete the transaction
516 error = xfs_bmap_finish(&tp, &free_list, &committed);
519 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
524 * Copy any maps to caller's array and return any error.
531 if (unlikely(!imap.br_startblock && !(io->io_flags & XFS_IOCORE_RT))) {
532 error = xfs_cmn_err_fsblock_zero(ip, &imap);
540 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
541 xfs_bmap_cancel(&free_list);
542 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
544 error1: /* Just cancel transaction */
545 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
546 *nmaps = 0; /* nothing set-up here */
549 return XFS_ERROR(error);
553 * If the caller is doing a write at the end of the file,
554 * then extend the allocation out to the file system's write
555 * iosize. We clean up any extra space left over when the
556 * file is closed in xfs_inactive().
558 * For sync writes, we are flushing delayed allocate space to
559 * try to make additional space available for allocation near
560 * the filesystem full boundary - preallocation hurts in that
561 * situation, of course.
564 xfs_iomap_eof_want_preallocate(
571 xfs_bmbt_irec_t *imap,
575 xfs_fileoff_t start_fsb;
576 xfs_filblks_t count_fsb;
577 xfs_fsblock_t firstblock;
581 if ((ioflag & BMAPI_SYNC) || (offset + count) <= isize)
585 * If there are any real blocks past eof, then don't
586 * do any speculative allocation.
588 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
589 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
590 while (count_fsb > 0) {
592 firstblock = NULLFSBLOCK;
593 error = xfs_bmapi(NULL, ip, start_fsb, count_fsb, 0,
594 &firstblock, 0, imap, &imaps, NULL, NULL);
597 for (n = 0; n < imaps; n++) {
598 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
599 (imap[n].br_startblock != DELAYSTARTBLOCK))
601 start_fsb += imap[n].br_blockcount;
602 count_fsb -= imap[n].br_blockcount;
610 xfs_iomap_write_delay(
615 xfs_bmbt_irec_t *ret_imap,
618 xfs_mount_t *mp = ip->i_mount;
619 xfs_iocore_t *io = &ip->i_iocore;
620 xfs_fileoff_t offset_fsb;
621 xfs_fileoff_t last_fsb;
622 xfs_off_t aligned_offset;
623 xfs_fileoff_t ioalign;
624 xfs_fsblock_t firstblock;
628 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
629 int prealloc, fsynced = 0;
632 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
635 * Make sure that the dquots are there. This doesn't hold
636 * the ilock across a disk read.
638 error = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
640 return XFS_ERROR(error);
642 extsz = xfs_get_extsz_hint(ip);
643 offset_fsb = XFS_B_TO_FSBT(mp, offset);
647 if (io->io_new_size > isize)
648 isize = io->io_new_size;
650 error = xfs_iomap_eof_want_preallocate(mp, ip, isize, offset, count,
651 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
656 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
657 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
658 last_fsb = ioalign + mp->m_writeio_blocks;
660 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
663 if (prealloc || extsz) {
664 error = xfs_iomap_eof_align_last_fsb(mp, ip, isize, extsz,
670 nimaps = XFS_WRITE_IMAPS;
671 firstblock = NULLFSBLOCK;
672 error = xfs_bmapi(NULL, ip, offset_fsb,
673 (xfs_filblks_t)(last_fsb - offset_fsb),
674 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
675 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
676 &nimaps, NULL, NULL);
677 if (error && (error != ENOSPC))
678 return XFS_ERROR(error);
681 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
682 * then we must have run out of space - flush delalloc, and retry..
685 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE,
687 if (xfs_flush_space(ip, &fsynced, &ioflag))
688 return XFS_ERROR(ENOSPC);
694 if (unlikely(!imap[0].br_startblock && !(io->io_flags & XFS_IOCORE_RT)))
695 return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
704 * Pass in a delayed allocate extent, convert it to real extents;
705 * return to the caller the extent we create which maps on top of
706 * the originating callers request.
708 * Called without a lock on the inode.
711 xfs_iomap_write_allocate(
715 xfs_bmbt_irec_t *map,
718 xfs_mount_t *mp = ip->i_mount;
719 xfs_iocore_t *io = &ip->i_iocore;
720 xfs_fileoff_t offset_fsb, last_block;
721 xfs_fileoff_t end_fsb, map_start_fsb;
722 xfs_fsblock_t first_block;
723 xfs_bmap_free_t free_list;
724 xfs_filblks_t count_fsb;
725 xfs_bmbt_irec_t imap[XFS_STRAT_WRITE_IMAPS];
727 int i, nimaps, committed;
734 * Make sure that the dquots are there.
736 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
737 return XFS_ERROR(error);
739 offset_fsb = XFS_B_TO_FSBT(mp, offset);
740 count_fsb = map->br_blockcount;
741 map_start_fsb = map->br_startoff;
743 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
745 while (count_fsb != 0) {
747 * Set up a transaction with which to allocate the
748 * backing store for the file. Do allocations in a
749 * loop until we get some space in the range we are
750 * interested in. The other space that might be allocated
751 * is in the delayed allocation extent on which we sit
752 * but before our buffer starts.
756 while (nimaps == 0) {
757 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
758 tp->t_flags |= XFS_TRANS_RESERVE;
759 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
760 error = xfs_trans_reserve(tp, nres,
761 XFS_WRITE_LOG_RES(mp),
762 0, XFS_TRANS_PERM_LOG_RES,
763 XFS_WRITE_LOG_COUNT);
765 xfs_trans_cancel(tp, 0);
766 return XFS_ERROR(error);
768 xfs_ilock(ip, XFS_ILOCK_EXCL);
769 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
770 xfs_trans_ihold(tp, ip);
772 XFS_BMAP_INIT(&free_list, &first_block);
774 nimaps = XFS_STRAT_WRITE_IMAPS;
776 * Ensure we don't go beyond eof - it is possible
777 * the extents changed since we did the read call,
778 * we dropped the ilock in the interim.
781 end_fsb = XFS_B_TO_FSB(mp, ip->i_size);
782 xfs_bmap_last_offset(NULL, ip, &last_block,
784 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
785 if ((map_start_fsb + count_fsb) > last_block) {
786 count_fsb = last_block - map_start_fsb;
787 if (count_fsb == 0) {
793 /* Go get the actual blocks */
794 error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
795 XFS_BMAPI_WRITE, &first_block, 1,
796 imap, &nimaps, &free_list, NULL);
800 error = xfs_bmap_finish(&tp, &free_list, &committed);
804 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
808 xfs_iunlock(ip, XFS_ILOCK_EXCL);
812 * See if we were able to allocate an extent that
813 * covers at least part of the callers request
815 for (i = 0; i < nimaps; i++) {
816 if (unlikely(!imap[i].br_startblock &&
817 !(io->io_flags & XFS_IOCORE_RT)))
818 return xfs_cmn_err_fsblock_zero(ip, &imap[i]);
819 if ((offset_fsb >= imap[i].br_startoff) &&
820 (offset_fsb < (imap[i].br_startoff +
821 imap[i].br_blockcount))) {
824 XFS_STATS_INC(xs_xstrat_quick);
827 count_fsb -= imap[i].br_blockcount;
830 /* So far we have not mapped the requested part of the
831 * file, just surrounding data, try again.
834 map_start_fsb = imap[nimaps].br_startoff +
835 imap[nimaps].br_blockcount;
839 xfs_bmap_cancel(&free_list);
840 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
842 xfs_iunlock(ip, XFS_ILOCK_EXCL);
843 return XFS_ERROR(error);
847 xfs_iomap_write_unwritten(
852 xfs_mount_t *mp = ip->i_mount;
853 xfs_iocore_t *io = &ip->i_iocore;
854 xfs_fileoff_t offset_fsb;
855 xfs_filblks_t count_fsb;
856 xfs_filblks_t numblks_fsb;
857 xfs_fsblock_t firstfsb;
860 xfs_bmbt_irec_t imap;
861 xfs_bmap_free_t free_list;
866 xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN, ip, offset, count);
868 offset_fsb = XFS_B_TO_FSBT(mp, offset);
869 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
870 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
872 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
876 * set up a transaction to convert the range of extents
877 * from unwritten to real. Do allocations in a loop until
878 * we have covered the range passed in.
880 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
881 tp->t_flags |= XFS_TRANS_RESERVE;
882 error = xfs_trans_reserve(tp, resblks,
883 XFS_WRITE_LOG_RES(mp), 0,
884 XFS_TRANS_PERM_LOG_RES,
885 XFS_WRITE_LOG_COUNT);
887 xfs_trans_cancel(tp, 0);
888 return XFS_ERROR(error);
891 xfs_ilock(ip, XFS_ILOCK_EXCL);
892 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
893 xfs_trans_ihold(tp, ip);
896 * Modify the unwritten extent state of the buffer.
898 XFS_BMAP_INIT(&free_list, &firstfsb);
900 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
901 XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
902 1, &imap, &nimaps, &free_list, NULL);
904 goto error_on_bmapi_transaction;
906 error = xfs_bmap_finish(&(tp), &(free_list), &committed);
908 goto error_on_bmapi_transaction;
910 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
911 xfs_iunlock(ip, XFS_ILOCK_EXCL);
913 return XFS_ERROR(error);
915 if (unlikely(!imap.br_startblock &&
916 !(io->io_flags & XFS_IOCORE_RT)))
917 return xfs_cmn_err_fsblock_zero(ip, &imap);
919 if ((numblks_fsb = imap.br_blockcount) == 0) {
921 * The numblks_fsb value should always get
922 * smaller, otherwise the loop is stuck.
924 ASSERT(imap.br_blockcount);
927 offset_fsb += numblks_fsb;
928 count_fsb -= numblks_fsb;
929 } while (count_fsb > 0);
933 error_on_bmapi_transaction:
934 xfs_bmap_cancel(&free_list);
935 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
936 xfs_iunlock(ip, XFS_ILOCK_EXCL);
937 return XFS_ERROR(error);