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_target = io->io_flags & XFS_IOCORE_RT ?
169 mp->m_rtdev_targp : mp->m_ddev_targp;
170 iomapp->iomap_offset = XFS_FSB_TO_B(mp, imap->br_startoff);
171 iomapp->iomap_delta = offset - iomapp->iomap_offset;
172 iomapp->iomap_bsize = XFS_FSB_TO_B(mp, imap->br_blockcount);
173 iomapp->iomap_flags = flags;
175 start_block = imap->br_startblock;
176 if (start_block == HOLESTARTBLOCK) {
177 iomapp->iomap_bn = IOMAP_DADDR_NULL;
178 iomapp->iomap_flags = IOMAP_HOLE;
179 } else if (start_block == DELAYSTARTBLOCK) {
180 iomapp->iomap_bn = IOMAP_DADDR_NULL;
181 iomapp->iomap_flags = IOMAP_DELAY;
183 iomapp->iomap_bn = XFS_FSB_TO_DB_IO(io, start_block);
184 if (ISUNWRITTEN(imap))
185 iomapp->iomap_flags |= IOMAP_UNWRITTEN;
188 if ((iomapp->iomap_offset + iomapp->iomap_bsize) >= nisize) {
189 iomapp->iomap_flags |= IOMAP_EOF;
192 offset += iomapp->iomap_bsize - iomapp->iomap_delta;
194 return pbm; /* Return the number filled */
206 xfs_mount_t *mp = io->io_mount;
207 xfs_fileoff_t offset_fsb, end_fsb;
210 xfs_bmbt_irec_t imap;
215 if (XFS_FORCED_SHUTDOWN(mp))
216 return XFS_ERROR(EIO);
219 (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE |
220 BMAPI_UNWRITTEN | BMAPI_DEVICE)) {
222 xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER, io, offset, count);
223 lockmode = XFS_LCK_MAP_SHARED(mp, io);
224 bmapi_flags = XFS_BMAPI_ENTIRE;
225 if (flags & BMAPI_IGNSTATE)
226 bmapi_flags |= XFS_BMAPI_IGSTATE;
229 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER, io, offset, count);
230 lockmode = XFS_ILOCK_EXCL|XFS_EXTSIZE_WR;
232 XFS_ILOCK(mp, io, lockmode);
235 xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER, io, offset, count);
236 lockmode = XFS_ILOCK_SHARED|XFS_EXTSIZE_RD;
237 bmapi_flags = XFS_BMAPI_ENTIRE;
238 /* Attempt non-blocking lock */
239 if (flags & BMAPI_TRYLOCK) {
240 if (!XFS_ILOCK_NOWAIT(mp, io, lockmode))
241 return XFS_ERROR(EAGAIN);
243 XFS_ILOCK(mp, io, lockmode);
246 case BMAPI_UNWRITTEN:
249 lockmode = XFS_LCK_MAP_SHARED(mp, io);
250 iomapp->iomap_target = io->io_flags & XFS_IOCORE_RT ?
251 mp->m_rtdev_targp : mp->m_ddev_targp;
259 ASSERT(offset <= mp->m_maxioffset);
260 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
261 count = mp->m_maxioffset - offset;
262 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
263 offset_fsb = XFS_B_TO_FSBT(mp, offset);
265 error = XFS_BMAPI(mp, NULL, io, offset_fsb,
266 (xfs_filblks_t)(end_fsb - offset_fsb),
267 bmapi_flags, NULL, 0, &imap,
274 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE|BMAPI_UNWRITTEN)) {
276 /* If we found an extent, return it */
277 if (nimaps && (imap.br_startblock != HOLESTARTBLOCK)) {
278 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
279 offset, count, iomapp, &imap, flags);
283 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
284 error = XFS_IOMAP_WRITE_DIRECT(mp, io, offset,
285 count, flags, &imap, &nimaps, nimaps);
287 error = XFS_IOMAP_WRITE_DELAY(mp, io, offset, count,
288 flags, &imap, &nimaps);
291 xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP, io,
292 offset, count, iomapp, &imap, flags);
294 iomap_flags = IOMAP_NEW;
297 /* If we found an extent, return it */
298 XFS_IUNLOCK(mp, io, lockmode);
301 if (nimaps && !ISNULLSTARTBLOCK(imap.br_startblock)) {
302 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
303 offset, count, iomapp, &imap, flags);
307 error = XFS_IOMAP_WRITE_ALLOCATE(mp, io, &imap, &nimaps);
309 case BMAPI_UNWRITTEN:
311 error = XFS_IOMAP_WRITE_UNWRITTEN(mp, io, offset, count);
317 *niomaps = xfs_imap_to_bmap(io, offset, &imap,
318 iomapp, nimaps, *niomaps, iomap_flags);
319 } else if (niomaps) {
325 XFS_IUNLOCK(mp, io, lockmode);
326 return XFS_ERROR(error);
337 if (ip->i_delayed_blks) {
338 xfs_iunlock(ip, XFS_ILOCK_EXCL);
340 xfs_ilock(ip, XFS_ILOCK_EXCL);
343 *ioflags |= BMAPI_SYNC;
349 *ioflags |= BMAPI_SYNC;
352 xfs_iunlock(ip, XFS_ILOCK_EXCL);
353 xfs_flush_device(ip);
354 xfs_ilock(ip, XFS_ILOCK_EXCL);
362 xfs_iomap_write_direct(
367 xfs_bmbt_irec_t *ret_imap,
371 xfs_mount_t *mp = ip->i_mount;
372 xfs_iocore_t *io = &ip->i_iocore;
373 xfs_fileoff_t offset_fsb;
374 xfs_fileoff_t last_fsb;
375 xfs_filblks_t count_fsb;
377 xfs_fsblock_t firstfsb;
383 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS], *imapp;
384 xfs_bmap_free_t free_list;
386 xfs_filblks_t datablocks;
392 * Make sure that the dquots are there. This doesn't hold
393 * the ilock across a disk read.
395 error = XFS_QM_DQATTACH(ip->i_mount, ip, XFS_QMOPT_ILOCKED);
397 return XFS_ERROR(error);
399 maps = min(XFS_WRITE_IMAPS, *nmaps);
402 isize = ip->i_d.di_size;
403 aeof = (offset + count) > isize;
405 if (io->io_new_size > isize)
406 isize = io->io_new_size;
408 offset_fsb = XFS_B_TO_FSBT(mp, offset);
409 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
410 count_fsb = last_fsb - offset_fsb;
411 if (found && (ret_imap->br_startblock == HOLESTARTBLOCK)) {
412 xfs_fileoff_t map_last_fsb;
414 map_last_fsb = ret_imap->br_blockcount + ret_imap->br_startoff;
416 if (map_last_fsb < last_fsb) {
417 last_fsb = map_last_fsb;
418 count_fsb = last_fsb - offset_fsb;
420 ASSERT(count_fsb > 0);
424 * determine if reserving space on
425 * the data or realtime partition.
427 if ((rt = XFS_IS_REALTIME_INODE(ip))) {
428 int sbrtextsize, iprtextsize;
430 sbrtextsize = mp->m_sb.sb_rextsize;
432 ip->i_d.di_extsize ? ip->i_d.di_extsize : sbrtextsize;
433 numrtextents = (count_fsb + iprtextsize - 1);
434 do_div(numrtextents, sbrtextsize);
437 datablocks = count_fsb;
442 * allocate and setup the transaction
444 xfs_iunlock(ip, XFS_ILOCK_EXCL);
445 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
447 resblks = XFS_DIOSTRAT_SPACE_RES(mp, datablocks);
449 error = xfs_trans_reserve(tp, resblks,
450 XFS_WRITE_LOG_RES(mp), numrtextents,
451 XFS_TRANS_PERM_LOG_RES,
452 XFS_WRITE_LOG_COUNT);
455 * check for running out of space
459 * Free the transaction structure.
461 xfs_trans_cancel(tp, 0);
463 xfs_ilock(ip, XFS_ILOCK_EXCL);
466 goto error_out; /* Don't return in above if .. trans ..,
467 need lock to return */
469 if (XFS_TRANS_RESERVE_BLKQUOTA(mp, tp, ip, resblks)) {
475 bmapi_flag = XFS_BMAPI_WRITE;
476 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
477 xfs_trans_ihold(tp, ip);
479 if (!(flags & BMAPI_MMAP) && (offset < ip->i_d.di_size || rt))
480 bmapi_flag |= XFS_BMAPI_PREALLOC;
483 * issue the bmapi() call to allocate the blocks
485 XFS_BMAP_INIT(&free_list, &firstfsb);
487 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
488 bmapi_flag, &firstfsb, 0, imapp, &nimaps, &free_list);
494 * complete the transaction
497 error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
502 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
507 /* copy any maps to caller's array and return any error. */
517 error0: /* Cancel bmap, unlock inode, and cancel trans */
518 xfs_bmap_cancel(&free_list);
520 error1: /* Just cancel transaction */
521 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
522 *nmaps = 0; /* nothing set-up here */
525 return XFS_ERROR(error);
529 xfs_iomap_write_delay(
534 xfs_bmbt_irec_t *ret_imap,
537 xfs_mount_t *mp = ip->i_mount;
538 xfs_iocore_t *io = &ip->i_iocore;
539 xfs_fileoff_t offset_fsb;
540 xfs_fileoff_t last_fsb;
542 xfs_fsblock_t firstblock;
545 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
549 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
552 * Make sure that the dquots are there. This doesn't hold
553 * the ilock across a disk read.
556 error = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
558 return XFS_ERROR(error);
561 isize = ip->i_d.di_size;
562 if (io->io_new_size > isize) {
563 isize = io->io_new_size;
567 offset_fsb = XFS_B_TO_FSBT(mp, offset);
568 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
570 * If the caller is doing a write at the end of the file,
571 * then extend the allocation (and the buffer used for the write)
572 * out to the file system's write iosize. We clean up any extra
573 * space left over when the file is closed in xfs_inactive().
575 * We don't bother with this for sync writes, because we need
576 * to minimize the amount we write for good performance.
578 if (!(ioflag & BMAPI_SYNC) && ((offset + count) > ip->i_d.di_size)) {
579 xfs_off_t aligned_offset;
580 xfs_filblks_t count_fsb;
582 xfs_fileoff_t ioalign;
584 xfs_fileoff_t start_fsb;
587 * If there are any real blocks past eof, then don't
588 * do any speculative allocation.
590 start_fsb = XFS_B_TO_FSBT(mp,
591 ((xfs_ufsize_t)(offset + count - 1)));
592 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
593 while (count_fsb > 0) {
594 nimaps = XFS_WRITE_IMAPS;
595 error = XFS_BMAPI(mp, NULL, io, start_fsb, count_fsb,
596 0, &firstblock, 0, imap, &nimaps, NULL);
600 for (n = 0; n < nimaps; n++) {
601 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
602 (imap[n].br_startblock != DELAYSTARTBLOCK)) {
605 start_fsb += imap[n].br_blockcount;
606 count_fsb -= imap[n].br_blockcount;
609 iosize = mp->m_writeio_blocks;
610 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
611 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
612 last_fsb = ioalign + iosize;
616 nimaps = XFS_WRITE_IMAPS;
617 firstblock = NULLFSBLOCK;
620 * If mounted with the "-o swalloc" option, roundup the allocation
621 * request to a stripe width boundary if the file size is >=
622 * stripe width and we are allocating past the allocation eof.
624 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)
625 && (isize >= XFS_FSB_TO_B(mp, mp->m_swidth)) && aeof) {
627 xfs_fileoff_t new_last_fsb;
629 new_last_fsb = roundup_64(last_fsb, mp->m_swidth);
630 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
635 last_fsb = new_last_fsb;
638 * Roundup the allocation request to a stripe unit (m_dalign) boundary
639 * if the file size is >= stripe unit size, and we are allocating past
640 * the allocation eof.
642 } else if (mp->m_dalign && (isize >= XFS_FSB_TO_B(mp, mp->m_dalign))
645 xfs_fileoff_t new_last_fsb;
646 new_last_fsb = roundup_64(last_fsb, mp->m_dalign);
647 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
652 last_fsb = new_last_fsb;
656 error = xfs_bmapi(NULL, ip, offset_fsb,
657 (xfs_filblks_t)(last_fsb - offset_fsb),
658 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
659 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
662 * This can be EDQUOT, if nimaps == 0
664 if (error && (error != ENOSPC)) {
665 return XFS_ERROR(error);
668 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
669 * then we must have run out of space.
672 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE,
674 if (xfs_flush_space(ip, &fsynced, &ioflag))
675 return XFS_ERROR(ENOSPC);
687 * Pass in a delayed allocate extent, convert it to real extents;
688 * return to the caller the extent we create which maps on top of
689 * the originating callers request.
691 * Called without a lock on the inode.
694 xfs_iomap_write_allocate(
696 xfs_bmbt_irec_t *map,
699 xfs_mount_t *mp = ip->i_mount;
700 xfs_fileoff_t offset_fsb, last_block;
701 xfs_fileoff_t end_fsb, map_start_fsb;
702 xfs_fsblock_t first_block;
703 xfs_bmap_free_t free_list;
704 xfs_filblks_t count_fsb;
705 xfs_bmbt_irec_t imap[XFS_STRAT_WRITE_IMAPS];
707 int i, nimaps, committed;
714 * Make sure that the dquots are there.
716 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
717 return XFS_ERROR(error);
719 offset_fsb = map->br_startoff;
720 count_fsb = map->br_blockcount;
721 map_start_fsb = offset_fsb;
723 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
725 while (count_fsb != 0) {
727 * Set up a transaction with which to allocate the
728 * backing store for the file. Do allocations in a
729 * loop until we get some space in the range we are
730 * interested in. The other space that might be allocated
731 * is in the delayed allocation extent on which we sit
732 * but before our buffer starts.
736 while (nimaps == 0) {
737 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
738 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
739 error = xfs_trans_reserve(tp, nres,
740 XFS_WRITE_LOG_RES(mp),
741 0, XFS_TRANS_PERM_LOG_RES,
742 XFS_WRITE_LOG_COUNT);
743 if (error == ENOSPC) {
744 error = xfs_trans_reserve(tp, 0,
745 XFS_WRITE_LOG_RES(mp),
747 XFS_TRANS_PERM_LOG_RES,
748 XFS_WRITE_LOG_COUNT);
751 xfs_trans_cancel(tp, 0);
752 return XFS_ERROR(error);
754 xfs_ilock(ip, XFS_ILOCK_EXCL);
755 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
756 xfs_trans_ihold(tp, ip);
758 XFS_BMAP_INIT(&free_list, &first_block);
760 nimaps = XFS_STRAT_WRITE_IMAPS;
762 * Ensure we don't go beyond eof - it is possible
763 * the extents changed since we did the read call,
764 * we dropped the ilock in the interim.
767 end_fsb = XFS_B_TO_FSB(mp, ip->i_d.di_size);
768 xfs_bmap_last_offset(NULL, ip, &last_block,
770 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
771 if ((map_start_fsb + count_fsb) > last_block) {
772 count_fsb = last_block - map_start_fsb;
773 if (count_fsb == 0) {
779 /* Go get the actual blocks */
780 error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
781 XFS_BMAPI_WRITE, &first_block, 1,
782 imap, &nimaps, &free_list);
786 error = xfs_bmap_finish(&tp, &free_list,
787 first_block, &committed);
791 error = xfs_trans_commit(tp,
792 XFS_TRANS_RELEASE_LOG_RES, NULL);
796 xfs_iunlock(ip, XFS_ILOCK_EXCL);
800 * See if we were able to allocate an extent that
801 * covers at least part of the callers request
804 for (i = 0; i < nimaps; i++) {
805 if ((map->br_startoff >= imap[i].br_startoff) &&
806 (map->br_startoff < (imap[i].br_startoff +
807 imap[i].br_blockcount))) {
810 XFS_STATS_INC(xs_xstrat_quick);
813 count_fsb -= imap[i].br_blockcount;
816 /* So far we have not mapped the requested part of the
817 * file, just surrounding data, try again.
820 offset_fsb = imap[nimaps].br_startoff +
821 imap[nimaps].br_blockcount;
822 map_start_fsb = offset_fsb;
826 xfs_bmap_cancel(&free_list);
827 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
829 xfs_iunlock(ip, XFS_ILOCK_EXCL);
830 return XFS_ERROR(error);
834 xfs_iomap_write_unwritten(
839 xfs_mount_t *mp = ip->i_mount;
841 xfs_fileoff_t offset_fsb;
842 xfs_filblks_t count_fsb;
843 xfs_filblks_t numblks_fsb;
844 xfs_bmbt_irec_t imap;
849 xfs_fsblock_t firstfsb;
850 xfs_bmap_free_t free_list;
852 xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN,
853 &ip->i_iocore, offset, count);
855 offset_fsb = XFS_B_TO_FSBT(mp, offset);
856 count_fsb = XFS_B_TO_FSB(mp, count);
859 nres = XFS_DIOSTRAT_SPACE_RES(mp, 0);
862 * set up a transaction to convert the range of extents
863 * from unwritten to real. Do allocations in a loop until
864 * we have covered the range passed in.
867 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
868 error = xfs_trans_reserve(tp, nres,
869 XFS_WRITE_LOG_RES(mp), 0,
870 XFS_TRANS_PERM_LOG_RES,
871 XFS_WRITE_LOG_COUNT);
873 xfs_trans_cancel(tp, 0);
877 xfs_ilock(ip, XFS_ILOCK_EXCL);
878 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
879 xfs_trans_ihold(tp, ip);
882 * Modify the unwritten extent state of the buffer.
884 XFS_BMAP_INIT(&free_list, &firstfsb);
886 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
887 XFS_BMAPI_WRITE, &firstfsb,
888 1, &imap, &nimaps, &free_list);
890 goto error_on_bmapi_transaction;
892 error = xfs_bmap_finish(&(tp), &(free_list),
893 firstfsb, &committed);
895 goto error_on_bmapi_transaction;
897 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
898 xfs_iunlock(ip, XFS_ILOCK_EXCL);
902 if ((numblks_fsb = imap.br_blockcount) == 0) {
904 * The numblks_fsb value should always get
905 * smaller, otherwise the loop is stuck.
907 ASSERT(imap.br_blockcount);
910 offset_fsb += numblks_fsb;
911 count_fsb -= numblks_fsb;
912 } while (count_fsb > 0);
916 error_on_bmapi_transaction:
917 xfs_bmap_cancel(&free_list);
918 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
919 xfs_iunlock(ip, XFS_ILOCK_EXCL);
921 return XFS_ERROR(error);