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"
49 #include "xfs_buf_item.h"
50 #include "xfs_trans_space.h"
51 #include "xfs_utils.h"
52 #include "xfs_iomap.h"
54 #if defined(XFS_RW_TRACE)
56 xfs_iomap_enter_trace(
62 xfs_inode_t *ip = XFS_IO_INODE(io);
67 ktrace_enter(ip->i_rwtrace,
68 (void *)((unsigned long)tag),
70 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
71 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
72 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
73 (void *)((unsigned long)(offset & 0xffffffff)),
74 (void *)((unsigned long)count),
75 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
76 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
77 (void *)((unsigned long)current_pid()),
93 xfs_bmbt_irec_t *imapp,
96 xfs_inode_t *ip = XFS_IO_INODE(io);
101 ktrace_enter(ip->i_rwtrace,
102 (void *)((unsigned long)tag),
104 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
105 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
106 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
107 (void *)((unsigned long)(offset & 0xffffffff)),
108 (void *)((unsigned long)count),
109 (void *)((unsigned long)flags),
110 (void *)((unsigned long)((iomapp->iomap_offset >> 32) & 0xffffffff)),
111 (void *)((unsigned long)(iomapp->iomap_offset & 0xffffffff)),
112 (void *)((unsigned long)(iomapp->iomap_delta)),
113 (void *)((unsigned long)(iomapp->iomap_bsize)),
114 (void *)((unsigned long)(iomapp->iomap_bn)),
115 (void *)(__psint_t)(imapp->br_startoff),
116 (void *)((unsigned long)(imapp->br_blockcount)),
117 (void *)(__psint_t)(imapp->br_startblock));
120 #define xfs_iomap_enter_trace(tag, io, offset, count)
121 #define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags)
124 #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
125 << mp->m_writeio_log)
126 #define XFS_STRAT_WRITE_IMAPS 2
127 #define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
133 xfs_bmbt_irec_t *imap,
135 int imaps, /* Number of imap entries */
136 int iomaps, /* Number of iomap entries */
142 xfs_fsblock_t start_block;
145 nisize = XFS_SIZE(mp, io);
146 if (io->io_new_size > nisize)
147 nisize = io->io_new_size;
149 for (pbm = 0; imaps && pbm < iomaps; imaps--, iomapp++, imap++, pbm++) {
150 iomapp->iomap_offset = XFS_FSB_TO_B(mp, imap->br_startoff);
151 iomapp->iomap_delta = offset - iomapp->iomap_offset;
152 iomapp->iomap_bsize = XFS_FSB_TO_B(mp, imap->br_blockcount);
153 iomapp->iomap_flags = flags;
155 if (io->io_flags & XFS_IOCORE_RT) {
156 iomapp->iomap_flags |= IOMAP_REALTIME;
157 iomapp->iomap_target = mp->m_rtdev_targp;
159 iomapp->iomap_target = mp->m_ddev_targp;
161 start_block = imap->br_startblock;
162 if (start_block == HOLESTARTBLOCK) {
163 iomapp->iomap_bn = IOMAP_DADDR_NULL;
164 iomapp->iomap_flags |= IOMAP_HOLE;
165 } else if (start_block == DELAYSTARTBLOCK) {
166 iomapp->iomap_bn = IOMAP_DADDR_NULL;
167 iomapp->iomap_flags |= IOMAP_DELAY;
169 iomapp->iomap_bn = XFS_FSB_TO_DB_IO(io, start_block);
170 if (ISUNWRITTEN(imap))
171 iomapp->iomap_flags |= IOMAP_UNWRITTEN;
174 if ((iomapp->iomap_offset + iomapp->iomap_bsize) >= nisize) {
175 iomapp->iomap_flags |= IOMAP_EOF;
178 offset += iomapp->iomap_bsize - iomapp->iomap_delta;
180 return pbm; /* Return the number filled */
192 xfs_mount_t *mp = io->io_mount;
193 xfs_fileoff_t offset_fsb, end_fsb;
196 xfs_bmbt_irec_t imap;
201 if (XFS_FORCED_SHUTDOWN(mp))
202 return XFS_ERROR(EIO);
205 (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE |
206 BMAPI_UNWRITTEN | BMAPI_DEVICE)) {
208 xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER, io, offset, count);
209 lockmode = XFS_LCK_MAP_SHARED(mp, io);
210 bmapi_flags = XFS_BMAPI_ENTIRE;
213 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER, io, offset, count);
214 lockmode = XFS_ILOCK_EXCL|XFS_EXTSIZE_WR;
215 if (flags & BMAPI_IGNSTATE)
216 bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
217 XFS_ILOCK(mp, io, lockmode);
220 xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER, io, offset, count);
221 lockmode = XFS_ILOCK_SHARED|XFS_EXTSIZE_RD;
222 bmapi_flags = XFS_BMAPI_ENTIRE;
223 /* Attempt non-blocking lock */
224 if (flags & BMAPI_TRYLOCK) {
225 if (!XFS_ILOCK_NOWAIT(mp, io, lockmode))
226 return XFS_ERROR(EAGAIN);
228 XFS_ILOCK(mp, io, lockmode);
231 case BMAPI_UNWRITTEN:
234 lockmode = XFS_LCK_MAP_SHARED(mp, io);
235 iomapp->iomap_target = io->io_flags & XFS_IOCORE_RT ?
236 mp->m_rtdev_targp : mp->m_ddev_targp;
244 ASSERT(offset <= mp->m_maxioffset);
245 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
246 count = mp->m_maxioffset - offset;
247 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
248 offset_fsb = XFS_B_TO_FSBT(mp, offset);
250 error = XFS_BMAPI(mp, NULL, io, offset_fsb,
251 (xfs_filblks_t)(end_fsb - offset_fsb),
252 bmapi_flags, NULL, 0, &imap,
253 &nimaps, NULL, NULL);
259 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE|BMAPI_UNWRITTEN)) {
261 /* If we found an extent, return it */
263 (imap.br_startblock != HOLESTARTBLOCK) &&
264 (imap.br_startblock != DELAYSTARTBLOCK)) {
265 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
266 offset, count, iomapp, &imap, flags);
270 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
271 error = XFS_IOMAP_WRITE_DIRECT(mp, io, offset,
272 count, flags, &imap, &nimaps, nimaps);
274 error = XFS_IOMAP_WRITE_DELAY(mp, io, offset, count,
275 flags, &imap, &nimaps);
278 xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP, io,
279 offset, count, iomapp, &imap, flags);
281 iomap_flags = IOMAP_NEW;
284 /* If we found an extent, return it */
285 XFS_IUNLOCK(mp, io, lockmode);
288 if (nimaps && !ISNULLSTARTBLOCK(imap.br_startblock)) {
289 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
290 offset, count, iomapp, &imap, flags);
294 error = XFS_IOMAP_WRITE_ALLOCATE(mp, io, offset, count,
297 case BMAPI_UNWRITTEN:
299 error = XFS_IOMAP_WRITE_UNWRITTEN(mp, io, offset, count);
305 *niomaps = xfs_imap_to_bmap(io, offset, &imap,
306 iomapp, nimaps, *niomaps, iomap_flags);
307 } else if (niomaps) {
313 XFS_IUNLOCK(mp, io, lockmode);
314 return XFS_ERROR(error);
318 xfs_iomap_eof_align_last_fsb(
322 xfs_extlen_t extsize,
323 xfs_fileoff_t *last_fsb)
325 xfs_fileoff_t new_last_fsb = 0;
329 if (io->io_flags & XFS_IOCORE_RT)
332 * If mounted with the "-o swalloc" option, roundup the allocation
333 * request to a stripe width boundary if the file size is >=
334 * stripe width and we are allocating past the allocation eof.
336 else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
337 (isize >= XFS_FSB_TO_B(mp, mp->m_swidth)))
338 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
340 * Roundup the allocation request to a stripe unit (m_dalign) boundary
341 * if the file size is >= stripe unit size, and we are allocating past
342 * the allocation eof.
344 else if (mp->m_dalign && (isize >= XFS_FSB_TO_B(mp, mp->m_dalign)))
345 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
348 * Always round up the allocation request to an extent boundary
349 * (when file on a real-time subvolume or has di_extsize hint).
353 align = roundup_64(new_last_fsb, extsize);
356 new_last_fsb = roundup_64(*last_fsb, align);
360 error = XFS_BMAP_EOF(mp, io, new_last_fsb, XFS_DATA_FORK, &eof);
364 *last_fsb = new_last_fsb;
377 if (ip->i_delayed_blks) {
378 xfs_iunlock(ip, XFS_ILOCK_EXCL);
380 xfs_ilock(ip, XFS_ILOCK_EXCL);
383 *ioflags |= BMAPI_SYNC;
389 *ioflags |= BMAPI_SYNC;
392 xfs_iunlock(ip, XFS_ILOCK_EXCL);
393 xfs_flush_device(ip);
394 xfs_ilock(ip, XFS_ILOCK_EXCL);
402 xfs_cmn_err_fsblock_zero(
404 xfs_bmbt_irec_t *imap)
406 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
407 "Access to block zero in inode %llu "
408 "start_block: %llx start_off: %llx "
409 "blkcnt: %llx extent-state: %x\n",
410 (unsigned long long)ip->i_ino,
411 (unsigned long long)imap->br_startblock,
412 (unsigned long long)imap->br_startoff,
413 (unsigned long long)imap->br_blockcount,
419 xfs_iomap_write_direct(
424 xfs_bmbt_irec_t *ret_imap,
428 xfs_mount_t *mp = ip->i_mount;
429 xfs_iocore_t *io = &ip->i_iocore;
430 xfs_fileoff_t offset_fsb;
431 xfs_fileoff_t last_fsb;
432 xfs_filblks_t count_fsb, resaligned;
433 xfs_fsblock_t firstfsb;
434 xfs_extlen_t extsz, temp;
441 xfs_bmbt_irec_t imap;
442 xfs_bmap_free_t free_list;
443 uint qblocks, resblks, resrtextents;
448 * Make sure that the dquots are there. This doesn't hold
449 * the ilock across a disk read.
451 error = XFS_QM_DQATTACH(ip->i_mount, ip, XFS_QMOPT_ILOCKED);
453 return XFS_ERROR(error);
455 rt = XFS_IS_REALTIME_INODE(ip);
457 if (!(extsz = ip->i_d.di_extsize))
458 extsz = mp->m_sb.sb_rextsize;
460 extsz = ip->i_d.di_extsize;
463 isize = ip->i_d.di_size;
464 if (io->io_new_size > isize)
465 isize = io->io_new_size;
467 offset_fsb = XFS_B_TO_FSBT(mp, offset);
468 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
469 if ((offset + count) > isize) {
470 error = xfs_iomap_eof_align_last_fsb(mp, io, isize, extsz,
475 if (found && (ret_imap->br_startblock == HOLESTARTBLOCK))
476 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
477 ret_imap->br_blockcount +
478 ret_imap->br_startoff);
480 count_fsb = last_fsb - offset_fsb;
481 ASSERT(count_fsb > 0);
483 resaligned = count_fsb;
484 if (unlikely(extsz)) {
485 if ((temp = do_mod(offset_fsb, extsz)))
487 if ((temp = do_mod(resaligned, extsz)))
488 resaligned += extsz - temp;
492 resrtextents = qblocks = resaligned;
493 resrtextents /= mp->m_sb.sb_rextsize;
494 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
495 quota_flag = XFS_QMOPT_RES_RTBLKS;
498 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
499 quota_flag = XFS_QMOPT_RES_REGBLKS;
503 * Allocate and setup the transaction
505 xfs_iunlock(ip, XFS_ILOCK_EXCL);
506 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
507 error = xfs_trans_reserve(tp, resblks,
508 XFS_WRITE_LOG_RES(mp), resrtextents,
509 XFS_TRANS_PERM_LOG_RES,
510 XFS_WRITE_LOG_COUNT);
512 * Check for running out of space, note: need lock to return
515 xfs_trans_cancel(tp, 0);
516 xfs_ilock(ip, XFS_ILOCK_EXCL);
520 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
521 qblocks, 0, quota_flag);
525 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
526 xfs_trans_ihold(tp, ip);
528 bmapi_flag = XFS_BMAPI_WRITE;
529 if ((flags & BMAPI_DIRECT) && (offset < ip->i_d.di_size || extsz))
530 bmapi_flag |= XFS_BMAPI_PREALLOC;
533 * Issue the xfs_bmapi() call to allocate the blocks
535 XFS_BMAP_INIT(&free_list, &firstfsb);
537 error = XFS_BMAPI(mp, tp, io, offset_fsb, count_fsb, bmapi_flag,
538 &firstfsb, 0, &imap, &nimaps, &free_list, NULL);
543 * Complete the transaction
545 error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
548 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
553 * Copy any maps to caller's array and return any error.
560 if (unlikely(!imap.br_startblock && !(io->io_flags & XFS_IOCORE_RT))) {
561 error = xfs_cmn_err_fsblock_zero(ip, &imap);
569 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
570 xfs_bmap_cancel(&free_list);
571 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
573 error1: /* Just cancel transaction */
574 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
575 *nmaps = 0; /* nothing set-up here */
578 return XFS_ERROR(error);
582 * If the caller is doing a write at the end of the file,
583 * then extend the allocation out to the file system's write
584 * iosize. We clean up any extra space left over when the
585 * file is closed in xfs_inactive().
587 * For sync writes, we are flushing delayed allocate space to
588 * try to make additional space available for allocation near
589 * the filesystem full boundary - preallocation hurts in that
590 * situation, of course.
593 xfs_iomap_eof_want_preallocate(
600 xfs_bmbt_irec_t *imap,
604 xfs_fileoff_t start_fsb;
605 xfs_filblks_t count_fsb;
606 xfs_fsblock_t firstblock;
610 if ((ioflag & BMAPI_SYNC) || (offset + count) <= isize)
614 * If there are any real blocks past eof, then don't
615 * do any speculative allocation.
617 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
618 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
619 while (count_fsb > 0) {
621 firstblock = NULLFSBLOCK;
622 error = XFS_BMAPI(mp, NULL, io, start_fsb, count_fsb, 0,
623 &firstblock, 0, imap, &imaps, NULL, NULL);
626 for (n = 0; n < imaps; n++) {
627 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
628 (imap[n].br_startblock != DELAYSTARTBLOCK))
630 start_fsb += imap[n].br_blockcount;
631 count_fsb -= imap[n].br_blockcount;
639 xfs_iomap_write_delay(
644 xfs_bmbt_irec_t *ret_imap,
647 xfs_mount_t *mp = ip->i_mount;
648 xfs_iocore_t *io = &ip->i_iocore;
649 xfs_fileoff_t offset_fsb;
650 xfs_fileoff_t last_fsb;
651 xfs_off_t aligned_offset;
652 xfs_fileoff_t ioalign;
653 xfs_fsblock_t firstblock;
657 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
658 int prealloc, fsynced = 0;
661 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
664 * Make sure that the dquots are there. This doesn't hold
665 * the ilock across a disk read.
667 error = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
669 return XFS_ERROR(error);
671 if (XFS_IS_REALTIME_INODE(ip)) {
672 if (!(extsz = ip->i_d.di_extsize))
673 extsz = mp->m_sb.sb_rextsize;
675 extsz = ip->i_d.di_extsize;
678 offset_fsb = XFS_B_TO_FSBT(mp, offset);
681 isize = ip->i_d.di_size;
682 if (io->io_new_size > isize)
683 isize = io->io_new_size;
685 error = xfs_iomap_eof_want_preallocate(mp, io, isize, offset, count,
686 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
691 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
692 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
693 last_fsb = ioalign + mp->m_writeio_blocks;
695 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
698 if (prealloc || extsz) {
699 error = xfs_iomap_eof_align_last_fsb(mp, io, isize, extsz,
705 nimaps = XFS_WRITE_IMAPS;
706 firstblock = NULLFSBLOCK;
707 error = XFS_BMAPI(mp, NULL, io, offset_fsb,
708 (xfs_filblks_t)(last_fsb - offset_fsb),
709 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
710 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
711 &nimaps, NULL, NULL);
712 if (error && (error != ENOSPC))
713 return XFS_ERROR(error);
716 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
717 * then we must have run out of space - flush delalloc, and retry..
720 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE,
722 if (xfs_flush_space(ip, &fsynced, &ioflag))
723 return XFS_ERROR(ENOSPC);
729 if (unlikely(!imap[0].br_startblock && !(io->io_flags & XFS_IOCORE_RT)))
730 return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
739 * Pass in a delayed allocate extent, convert it to real extents;
740 * return to the caller the extent we create which maps on top of
741 * the originating callers request.
743 * Called without a lock on the inode.
746 xfs_iomap_write_allocate(
750 xfs_bmbt_irec_t *map,
753 xfs_mount_t *mp = ip->i_mount;
754 xfs_iocore_t *io = &ip->i_iocore;
755 xfs_fileoff_t offset_fsb, last_block;
756 xfs_fileoff_t end_fsb, map_start_fsb;
757 xfs_fsblock_t first_block;
758 xfs_bmap_free_t free_list;
759 xfs_filblks_t count_fsb;
760 xfs_bmbt_irec_t imap[XFS_STRAT_WRITE_IMAPS];
762 int i, nimaps, committed;
769 * Make sure that the dquots are there.
771 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
772 return XFS_ERROR(error);
774 offset_fsb = XFS_B_TO_FSBT(mp, offset);
775 count_fsb = map->br_blockcount;
776 map_start_fsb = map->br_startoff;
778 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
780 while (count_fsb != 0) {
782 * Set up a transaction with which to allocate the
783 * backing store for the file. Do allocations in a
784 * loop until we get some space in the range we are
785 * interested in. The other space that might be allocated
786 * is in the delayed allocation extent on which we sit
787 * but before our buffer starts.
791 while (nimaps == 0) {
792 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
793 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
794 error = xfs_trans_reserve(tp, nres,
795 XFS_WRITE_LOG_RES(mp),
796 0, XFS_TRANS_PERM_LOG_RES,
797 XFS_WRITE_LOG_COUNT);
798 if (error == ENOSPC) {
799 error = xfs_trans_reserve(tp, 0,
800 XFS_WRITE_LOG_RES(mp),
802 XFS_TRANS_PERM_LOG_RES,
803 XFS_WRITE_LOG_COUNT);
806 xfs_trans_cancel(tp, 0);
807 return XFS_ERROR(error);
809 xfs_ilock(ip, XFS_ILOCK_EXCL);
810 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
811 xfs_trans_ihold(tp, ip);
813 XFS_BMAP_INIT(&free_list, &first_block);
815 nimaps = XFS_STRAT_WRITE_IMAPS;
817 * Ensure we don't go beyond eof - it is possible
818 * the extents changed since we did the read call,
819 * we dropped the ilock in the interim.
822 end_fsb = XFS_B_TO_FSB(mp, ip->i_d.di_size);
823 xfs_bmap_last_offset(NULL, ip, &last_block,
825 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
826 if ((map_start_fsb + count_fsb) > last_block) {
827 count_fsb = last_block - map_start_fsb;
828 if (count_fsb == 0) {
834 /* Go get the actual blocks */
835 error = XFS_BMAPI(mp, tp, io, map_start_fsb, count_fsb,
836 XFS_BMAPI_WRITE, &first_block, 1,
837 imap, &nimaps, &free_list, NULL);
841 error = xfs_bmap_finish(&tp, &free_list,
842 first_block, &committed);
846 error = xfs_trans_commit(tp,
847 XFS_TRANS_RELEASE_LOG_RES, NULL);
851 xfs_iunlock(ip, XFS_ILOCK_EXCL);
855 * See if we were able to allocate an extent that
856 * covers at least part of the callers request
858 for (i = 0; i < nimaps; i++) {
859 if (unlikely(!imap[i].br_startblock &&
860 !(io->io_flags & XFS_IOCORE_RT)))
861 return xfs_cmn_err_fsblock_zero(ip, &imap[i]);
862 if ((offset_fsb >= imap[i].br_startoff) &&
863 (offset_fsb < (imap[i].br_startoff +
864 imap[i].br_blockcount))) {
867 XFS_STATS_INC(xs_xstrat_quick);
870 count_fsb -= imap[i].br_blockcount;
873 /* So far we have not mapped the requested part of the
874 * file, just surrounding data, try again.
877 map_start_fsb = imap[nimaps].br_startoff +
878 imap[nimaps].br_blockcount;
882 xfs_bmap_cancel(&free_list);
883 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
885 xfs_iunlock(ip, XFS_ILOCK_EXCL);
886 return XFS_ERROR(error);
890 xfs_iomap_write_unwritten(
895 xfs_mount_t *mp = ip->i_mount;
896 xfs_iocore_t *io = &ip->i_iocore;
897 xfs_fileoff_t offset_fsb;
898 xfs_filblks_t count_fsb;
899 xfs_filblks_t numblks_fsb;
900 xfs_fsblock_t firstfsb;
903 xfs_bmbt_irec_t imap;
904 xfs_bmap_free_t free_list;
909 xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN,
910 &ip->i_iocore, offset, count);
912 offset_fsb = XFS_B_TO_FSBT(mp, offset);
913 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
914 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
916 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
920 * set up a transaction to convert the range of extents
921 * from unwritten to real. Do allocations in a loop until
922 * we have covered the range passed in.
925 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
926 error = xfs_trans_reserve(tp, resblks,
927 XFS_WRITE_LOG_RES(mp), 0,
928 XFS_TRANS_PERM_LOG_RES,
929 XFS_WRITE_LOG_COUNT);
931 xfs_trans_cancel(tp, 0);
932 return XFS_ERROR(error);
935 xfs_ilock(ip, XFS_ILOCK_EXCL);
936 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
937 xfs_trans_ihold(tp, ip);
940 * Modify the unwritten extent state of the buffer.
942 XFS_BMAP_INIT(&free_list, &firstfsb);
944 error = XFS_BMAPI(mp, tp, io, offset_fsb, count_fsb,
945 XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
946 1, &imap, &nimaps, &free_list, NULL);
948 goto error_on_bmapi_transaction;
950 error = xfs_bmap_finish(&(tp), &(free_list),
951 firstfsb, &committed);
953 goto error_on_bmapi_transaction;
955 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
956 xfs_iunlock(ip, XFS_ILOCK_EXCL);
958 return XFS_ERROR(error);
960 if (unlikely(!imap.br_startblock &&
961 !(io->io_flags & XFS_IOCORE_RT)))
962 return xfs_cmn_err_fsblock_zero(ip, &imap);
964 if ((numblks_fsb = imap.br_blockcount) == 0) {
966 * The numblks_fsb value should always get
967 * smaller, otherwise the loop is stuck.
969 ASSERT(imap.br_blockcount);
972 offset_fsb += numblks_fsb;
973 count_fsb -= numblks_fsb;
974 } while (count_fsb > 0);
978 error_on_bmapi_transaction:
979 xfs_bmap_cancel(&free_list);
980 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
981 xfs_iunlock(ip, XFS_ILOCK_EXCL);
982 return XFS_ERROR(error);