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(
63 ktrace_enter(ip
->i_rwtrace
,
64 (void *)((unsigned long)tag
),
66 (void *)((unsigned long)((ip
->i_d
.di_size
>> 32) & 0xffffffff)),
67 (void *)((unsigned long)(ip
->i_d
.di_size
& 0xffffffff)),
68 (void *)((unsigned long)((offset
>> 32) & 0xffffffff)),
69 (void *)((unsigned long)(offset
& 0xffffffff)),
70 (void *)((unsigned long)count
),
71 (void *)((unsigned long)((ip
->i_new_size
>> 32) & 0xffffffff)),
72 (void *)((unsigned long)(ip
->i_new_size
& 0xffffffff)),
73 (void *)((unsigned long)current_pid()),
89 xfs_bmbt_irec_t
*imapp
,
95 ktrace_enter(ip
->i_rwtrace
,
96 (void *)((unsigned long)tag
),
98 (void *)((unsigned long)((ip
->i_d
.di_size
>> 32) & 0xffffffff)),
99 (void *)((unsigned long)(ip
->i_d
.di_size
& 0xffffffff)),
100 (void *)((unsigned long)((offset
>> 32) & 0xffffffff)),
101 (void *)((unsigned long)(offset
& 0xffffffff)),
102 (void *)((unsigned long)count
),
103 (void *)((unsigned long)flags
),
104 (void *)((unsigned long)((iomapp
->iomap_offset
>> 32) & 0xffffffff)),
105 (void *)((unsigned long)(iomapp
->iomap_offset
& 0xffffffff)),
106 (void *)((unsigned long)(iomapp
->iomap_delta
)),
107 (void *)((unsigned long)(iomapp
->iomap_bsize
)),
108 (void *)((unsigned long)(iomapp
->iomap_bn
)),
109 (void *)(__psint_t
)(imapp
->br_startoff
),
110 (void *)((unsigned long)(imapp
->br_blockcount
)),
111 (void *)(__psint_t
)(imapp
->br_startblock
));
114 #define xfs_iomap_enter_trace(tag, io, offset, count)
115 #define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags)
118 #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
119 << mp->m_writeio_log)
120 #define XFS_STRAT_WRITE_IMAPS 2
121 #define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
127 xfs_bmbt_irec_t
*imap
,
129 int imaps
, /* Number of imap entries */
130 int iomaps
, /* Number of iomap entries */
133 xfs_mount_t
*mp
= ip
->i_mount
;
135 xfs_fsblock_t start_block
;
138 for (pbm
= 0; imaps
&& pbm
< iomaps
; imaps
--, iomapp
++, imap
++, pbm
++) {
139 iomapp
->iomap_offset
= XFS_FSB_TO_B(mp
, imap
->br_startoff
);
140 iomapp
->iomap_delta
= offset
- iomapp
->iomap_offset
;
141 iomapp
->iomap_bsize
= XFS_FSB_TO_B(mp
, imap
->br_blockcount
);
142 iomapp
->iomap_flags
= flags
;
144 if (XFS_IS_REALTIME_INODE(ip
)) {
145 iomapp
->iomap_flags
|= IOMAP_REALTIME
;
146 iomapp
->iomap_target
= mp
->m_rtdev_targp
;
148 iomapp
->iomap_target
= mp
->m_ddev_targp
;
150 start_block
= imap
->br_startblock
;
151 if (start_block
== HOLESTARTBLOCK
) {
152 iomapp
->iomap_bn
= IOMAP_DADDR_NULL
;
153 iomapp
->iomap_flags
|= IOMAP_HOLE
;
154 } else if (start_block
== DELAYSTARTBLOCK
) {
155 iomapp
->iomap_bn
= IOMAP_DADDR_NULL
;
156 iomapp
->iomap_flags
|= IOMAP_DELAY
;
158 iomapp
->iomap_bn
= xfs_fsb_to_db(ip
, start_block
);
159 if (ISUNWRITTEN(imap
))
160 iomapp
->iomap_flags
|= IOMAP_UNWRITTEN
;
163 offset
+= iomapp
->iomap_bsize
- iomapp
->iomap_delta
;
165 return pbm
; /* Return the number filled */
177 xfs_mount_t
*mp
= ip
->i_mount
;
178 xfs_fileoff_t offset_fsb
, end_fsb
;
181 xfs_bmbt_irec_t imap
;
186 ASSERT((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
);
188 if (XFS_FORCED_SHUTDOWN(mp
))
189 return XFS_ERROR(EIO
);
191 switch (flags
& (BMAPI_READ
| BMAPI_WRITE
| BMAPI_ALLOCATE
)) {
193 xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER
, ip
, offset
, count
);
194 lockmode
= xfs_ilock_map_shared(ip
);
195 bmapi_flags
= XFS_BMAPI_ENTIRE
;
198 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER
, ip
, offset
, count
);
199 lockmode
= XFS_ILOCK_EXCL
;
200 if (flags
& BMAPI_IGNSTATE
)
201 bmapi_flags
|= XFS_BMAPI_IGSTATE
|XFS_BMAPI_ENTIRE
;
202 xfs_ilock(ip
, lockmode
);
205 xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER
, ip
, offset
, count
);
206 lockmode
= XFS_ILOCK_SHARED
;
207 bmapi_flags
= XFS_BMAPI_ENTIRE
;
209 /* Attempt non-blocking lock */
210 if (flags
& BMAPI_TRYLOCK
) {
211 if (!xfs_ilock_nowait(ip
, lockmode
))
212 return XFS_ERROR(EAGAIN
);
214 xfs_ilock(ip
, lockmode
);
221 ASSERT(offset
<= mp
->m_maxioffset
);
222 if ((xfs_fsize_t
)offset
+ count
> mp
->m_maxioffset
)
223 count
= mp
->m_maxioffset
- offset
;
224 end_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)offset
+ count
);
225 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
227 error
= xfs_bmapi(NULL
, ip
, offset_fsb
,
228 (xfs_filblks_t
)(end_fsb
- offset_fsb
),
229 bmapi_flags
, NULL
, 0, &imap
,
230 &nimaps
, NULL
, NULL
);
235 switch (flags
& (BMAPI_WRITE
|BMAPI_ALLOCATE
)) {
237 /* If we found an extent, return it */
239 (imap
.br_startblock
!= HOLESTARTBLOCK
) &&
240 (imap
.br_startblock
!= DELAYSTARTBLOCK
)) {
241 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP
, ip
,
242 offset
, count
, iomapp
, &imap
, flags
);
246 if (flags
& (BMAPI_DIRECT
|BMAPI_MMAP
)) {
247 error
= xfs_iomap_write_direct(ip
, offset
, count
, flags
,
248 &imap
, &nimaps
, nimaps
);
250 error
= xfs_iomap_write_delay(ip
, offset
, count
, flags
,
254 xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP
, ip
,
255 offset
, count
, iomapp
, &imap
, flags
);
257 iomap_flags
= IOMAP_NEW
;
260 /* If we found an extent, return it */
261 xfs_iunlock(ip
, lockmode
);
264 if (nimaps
&& !isnullstartblock(imap
.br_startblock
)) {
265 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP
, ip
,
266 offset
, count
, iomapp
, &imap
, flags
);
270 error
= xfs_iomap_write_allocate(ip
, offset
, count
,
276 *niomaps
= xfs_imap_to_bmap(ip
, offset
, &imap
,
277 iomapp
, nimaps
, *niomaps
, iomap_flags
);
278 } else if (niomaps
) {
284 xfs_iunlock(ip
, lockmode
);
285 return XFS_ERROR(error
);
290 xfs_iomap_eof_align_last_fsb(
293 xfs_extlen_t extsize
,
294 xfs_fileoff_t
*last_fsb
)
296 xfs_fileoff_t new_last_fsb
= 0;
300 if (XFS_IS_REALTIME_INODE(ip
))
303 * If mounted with the "-o swalloc" option, roundup the allocation
304 * request to a stripe width boundary if the file size is >=
305 * stripe width and we are allocating past the allocation eof.
307 else if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
) &&
308 (ip
->i_size
>= XFS_FSB_TO_B(mp
, mp
->m_swidth
)))
309 new_last_fsb
= roundup_64(*last_fsb
, mp
->m_swidth
);
311 * Roundup the allocation request to a stripe unit (m_dalign) boundary
312 * if the file size is >= stripe unit size, and we are allocating past
313 * the allocation eof.
315 else if (mp
->m_dalign
&& (ip
->i_size
>= XFS_FSB_TO_B(mp
, mp
->m_dalign
)))
316 new_last_fsb
= roundup_64(*last_fsb
, mp
->m_dalign
);
319 * Always round up the allocation request to an extent boundary
320 * (when file on a real-time subvolume or has di_extsize hint).
324 align
= roundup_64(new_last_fsb
, extsize
);
327 new_last_fsb
= roundup_64(*last_fsb
, align
);
331 error
= xfs_bmap_eof(ip
, new_last_fsb
, XFS_DATA_FORK
, &eof
);
335 *last_fsb
= new_last_fsb
;
348 if (ip
->i_delayed_blks
) {
349 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
351 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
354 *ioflags
|= BMAPI_SYNC
;
360 *ioflags
|= BMAPI_SYNC
;
363 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
364 xfs_flush_device(ip
);
365 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
373 xfs_cmn_err_fsblock_zero(
375 xfs_bmbt_irec_t
*imap
)
377 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO
, CE_ALERT
, ip
->i_mount
,
378 "Access to block zero in inode %llu "
379 "start_block: %llx start_off: %llx "
380 "blkcnt: %llx extent-state: %x\n",
381 (unsigned long long)ip
->i_ino
,
382 (unsigned long long)imap
->br_startblock
,
383 (unsigned long long)imap
->br_startoff
,
384 (unsigned long long)imap
->br_blockcount
,
390 xfs_iomap_write_direct(
395 xfs_bmbt_irec_t
*ret_imap
,
399 xfs_mount_t
*mp
= ip
->i_mount
;
400 xfs_fileoff_t offset_fsb
;
401 xfs_fileoff_t last_fsb
;
402 xfs_filblks_t count_fsb
, resaligned
;
403 xfs_fsblock_t firstfsb
;
404 xfs_extlen_t extsz
, temp
;
410 xfs_bmbt_irec_t imap
;
411 xfs_bmap_free_t free_list
;
412 uint qblocks
, resblks
, resrtextents
;
417 * Make sure that the dquots are there. This doesn't hold
418 * the ilock across a disk read.
420 error
= XFS_QM_DQATTACH(ip
->i_mount
, ip
, XFS_QMOPT_ILOCKED
);
422 return XFS_ERROR(error
);
424 rt
= XFS_IS_REALTIME_INODE(ip
);
425 extsz
= xfs_get_extsz_hint(ip
);
427 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
428 last_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)(offset
+ count
)));
429 if ((offset
+ count
) > ip
->i_size
) {
430 error
= xfs_iomap_eof_align_last_fsb(mp
, ip
, extsz
, &last_fsb
);
434 if (found
&& (ret_imap
->br_startblock
== HOLESTARTBLOCK
))
435 last_fsb
= MIN(last_fsb
, (xfs_fileoff_t
)
436 ret_imap
->br_blockcount
+
437 ret_imap
->br_startoff
);
439 count_fsb
= last_fsb
- offset_fsb
;
440 ASSERT(count_fsb
> 0);
442 resaligned
= count_fsb
;
443 if (unlikely(extsz
)) {
444 if ((temp
= do_mod(offset_fsb
, extsz
)))
446 if ((temp
= do_mod(resaligned
, extsz
)))
447 resaligned
+= extsz
- temp
;
451 resrtextents
= qblocks
= resaligned
;
452 resrtextents
/= mp
->m_sb
.sb_rextsize
;
453 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
454 quota_flag
= XFS_QMOPT_RES_RTBLKS
;
457 resblks
= qblocks
= XFS_DIOSTRAT_SPACE_RES(mp
, resaligned
);
458 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
462 * Allocate and setup the transaction
464 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
465 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
466 error
= xfs_trans_reserve(tp
, resblks
,
467 XFS_WRITE_LOG_RES(mp
), resrtextents
,
468 XFS_TRANS_PERM_LOG_RES
,
469 XFS_WRITE_LOG_COUNT
);
471 * Check for running out of space, note: need lock to return
474 xfs_trans_cancel(tp
, 0);
475 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
479 error
= XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
, tp
, ip
,
480 qblocks
, 0, quota_flag
);
484 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
485 xfs_trans_ihold(tp
, ip
);
487 bmapi_flag
= XFS_BMAPI_WRITE
;
488 if ((flags
& BMAPI_DIRECT
) && (offset
< ip
->i_size
|| extsz
))
489 bmapi_flag
|= XFS_BMAPI_PREALLOC
;
492 * Issue the xfs_bmapi() call to allocate the blocks
494 xfs_bmap_init(&free_list
, &firstfsb
);
496 error
= xfs_bmapi(tp
, ip
, offset_fsb
, count_fsb
, bmapi_flag
,
497 &firstfsb
, 0, &imap
, &nimaps
, &free_list
, NULL
);
502 * Complete the transaction
504 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
507 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
512 * Copy any maps to caller's array and return any error.
519 if (!(imap
.br_startblock
|| XFS_IS_REALTIME_INODE(ip
))) {
520 error
= xfs_cmn_err_fsblock_zero(ip
, &imap
);
528 error0
: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
529 xfs_bmap_cancel(&free_list
);
530 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp
, tp
, ip
, qblocks
, 0, quota_flag
);
532 error1
: /* Just cancel transaction */
533 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
534 *nmaps
= 0; /* nothing set-up here */
537 return XFS_ERROR(error
);
541 * If the caller is doing a write at the end of the file,
542 * then extend the allocation out to the file system's write
543 * iosize. We clean up any extra space left over when the
544 * file is closed in xfs_inactive().
546 * For sync writes, we are flushing delayed allocate space to
547 * try to make additional space available for allocation near
548 * the filesystem full boundary - preallocation hurts in that
549 * situation, of course.
552 xfs_iomap_eof_want_preallocate(
558 xfs_bmbt_irec_t
*imap
,
562 xfs_fileoff_t start_fsb
;
563 xfs_filblks_t count_fsb
;
564 xfs_fsblock_t firstblock
;
568 if ((ioflag
& BMAPI_SYNC
) || (offset
+ count
) <= ip
->i_size
)
572 * If there are any real blocks past eof, then don't
573 * do any speculative allocation.
575 start_fsb
= XFS_B_TO_FSBT(mp
, ((xfs_ufsize_t
)(offset
+ count
- 1)));
576 count_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)XFS_MAXIOFFSET(mp
));
577 while (count_fsb
> 0) {
579 firstblock
= NULLFSBLOCK
;
580 error
= xfs_bmapi(NULL
, ip
, start_fsb
, count_fsb
, 0,
581 &firstblock
, 0, imap
, &imaps
, NULL
, NULL
);
584 for (n
= 0; n
< imaps
; n
++) {
585 if ((imap
[n
].br_startblock
!= HOLESTARTBLOCK
) &&
586 (imap
[n
].br_startblock
!= DELAYSTARTBLOCK
))
588 start_fsb
+= imap
[n
].br_blockcount
;
589 count_fsb
-= imap
[n
].br_blockcount
;
597 xfs_iomap_write_delay(
602 xfs_bmbt_irec_t
*ret_imap
,
605 xfs_mount_t
*mp
= ip
->i_mount
;
606 xfs_fileoff_t offset_fsb
;
607 xfs_fileoff_t last_fsb
;
608 xfs_off_t aligned_offset
;
609 xfs_fileoff_t ioalign
;
610 xfs_fsblock_t firstblock
;
613 xfs_bmbt_irec_t imap
[XFS_WRITE_IMAPS
];
614 int prealloc
, fsynced
= 0;
617 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
620 * Make sure that the dquots are there. This doesn't hold
621 * the ilock across a disk read.
623 error
= XFS_QM_DQATTACH(mp
, ip
, XFS_QMOPT_ILOCKED
);
625 return XFS_ERROR(error
);
627 extsz
= xfs_get_extsz_hint(ip
);
628 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
631 error
= xfs_iomap_eof_want_preallocate(mp
, ip
, offset
, count
,
632 ioflag
, imap
, XFS_WRITE_IMAPS
, &prealloc
);
637 aligned_offset
= XFS_WRITEIO_ALIGN(mp
, (offset
+ count
- 1));
638 ioalign
= XFS_B_TO_FSBT(mp
, aligned_offset
);
639 last_fsb
= ioalign
+ mp
->m_writeio_blocks
;
641 last_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)(offset
+ count
)));
644 if (prealloc
|| extsz
) {
645 error
= xfs_iomap_eof_align_last_fsb(mp
, ip
, extsz
, &last_fsb
);
650 nimaps
= XFS_WRITE_IMAPS
;
651 firstblock
= NULLFSBLOCK
;
652 error
= xfs_bmapi(NULL
, ip
, offset_fsb
,
653 (xfs_filblks_t
)(last_fsb
- offset_fsb
),
654 XFS_BMAPI_DELAY
| XFS_BMAPI_WRITE
|
655 XFS_BMAPI_ENTIRE
, &firstblock
, 1, imap
,
656 &nimaps
, NULL
, NULL
);
657 if (error
&& (error
!= ENOSPC
))
658 return XFS_ERROR(error
);
661 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
662 * then we must have run out of space - flush delalloc, and retry..
665 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE
,
667 if (xfs_flush_space(ip
, &fsynced
, &ioflag
))
668 return XFS_ERROR(ENOSPC
);
674 if (!(imap
[0].br_startblock
|| XFS_IS_REALTIME_INODE(ip
)))
675 return xfs_cmn_err_fsblock_zero(ip
, &imap
[0]);
684 * Pass in a delayed allocate extent, convert it to real extents;
685 * return to the caller the extent we create which maps on top of
686 * the originating callers request.
688 * Called without a lock on the inode.
690 * We no longer bother to look at the incoming map - all we have to
691 * guarantee is that whatever we allocate fills the required range.
694 xfs_iomap_write_allocate(
698 xfs_bmbt_irec_t
*map
,
701 xfs_mount_t
*mp
= ip
->i_mount
;
702 xfs_fileoff_t offset_fsb
, last_block
;
703 xfs_fileoff_t end_fsb
, map_start_fsb
;
704 xfs_fsblock_t first_block
;
705 xfs_bmap_free_t free_list
;
706 xfs_filblks_t count_fsb
;
707 xfs_bmbt_irec_t imap
;
709 int nimaps
, committed
;
716 * Make sure that the dquots are there.
718 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
719 return XFS_ERROR(error
);
721 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
722 count_fsb
= map
->br_blockcount
;
723 map_start_fsb
= map
->br_startoff
;
725 XFS_STATS_ADD(xs_xstrat_bytes
, XFS_FSB_TO_B(mp
, count_fsb
));
727 while (count_fsb
!= 0) {
729 * Set up a transaction with which to allocate the
730 * backing store for the file. Do allocations in a
731 * loop until we get some space in the range we are
732 * interested in. The other space that might be allocated
733 * is in the delayed allocation extent on which we sit
734 * but before our buffer starts.
738 while (nimaps
== 0) {
739 tp
= xfs_trans_alloc(mp
, XFS_TRANS_STRAT_WRITE
);
740 tp
->t_flags
|= XFS_TRANS_RESERVE
;
741 nres
= XFS_EXTENTADD_SPACE_RES(mp
, XFS_DATA_FORK
);
742 error
= xfs_trans_reserve(tp
, nres
,
743 XFS_WRITE_LOG_RES(mp
),
744 0, XFS_TRANS_PERM_LOG_RES
,
745 XFS_WRITE_LOG_COUNT
);
747 xfs_trans_cancel(tp
, 0);
748 return XFS_ERROR(error
);
750 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
751 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
752 xfs_trans_ihold(tp
, ip
);
754 xfs_bmap_init(&free_list
, &first_block
);
757 * it is possible that the extents have changed since
758 * we did the read call as we dropped the ilock for a
759 * while. We have to be careful about truncates or hole
760 * punchs here - we are not allowed to allocate
761 * non-delalloc blocks here.
763 * The only protection against truncation is the pages
764 * for the range we are being asked to convert are
765 * locked and hence a truncate will block on them
768 * As a result, if we go beyond the range we really
769 * need and hit an delalloc extent boundary followed by
770 * a hole while we have excess blocks in the map, we
771 * will fill the hole incorrectly and overrun the
772 * transaction reservation.
774 * Using a single map prevents this as we are forced to
775 * check each map we look for overlap with the desired
776 * range and abort as soon as we find it. Also, given
777 * that we only return a single map, having one beyond
778 * what we can return is probably a bit silly.
780 * We also need to check that we don't go beyond EOF;
781 * this is a truncate optimisation as a truncate sets
782 * the new file size before block on the pages we
783 * currently have locked under writeback. Because they
784 * are about to be tossed, we don't need to write them
788 end_fsb
= XFS_B_TO_FSB(mp
, ip
->i_size
);
789 error
= xfs_bmap_last_offset(NULL
, ip
, &last_block
,
794 last_block
= XFS_FILEOFF_MAX(last_block
, end_fsb
);
795 if ((map_start_fsb
+ count_fsb
) > last_block
) {
796 count_fsb
= last_block
- map_start_fsb
;
797 if (count_fsb
== 0) {
803 /* Go get the actual blocks */
804 error
= xfs_bmapi(tp
, ip
, map_start_fsb
, count_fsb
,
805 XFS_BMAPI_WRITE
, &first_block
, 1,
806 &imap
, &nimaps
, &free_list
, NULL
);
810 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
814 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
818 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
822 * See if we were able to allocate an extent that
823 * covers at least part of the callers request
825 if (!(imap
.br_startblock
|| XFS_IS_REALTIME_INODE(ip
)))
826 return xfs_cmn_err_fsblock_zero(ip
, &imap
);
828 if ((offset_fsb
>= imap
.br_startoff
) &&
829 (offset_fsb
< (imap
.br_startoff
+
830 imap
.br_blockcount
))) {
833 XFS_STATS_INC(xs_xstrat_quick
);
838 * So far we have not mapped the requested part of the
839 * file, just surrounding data, try again.
841 count_fsb
-= imap
.br_blockcount
;
842 map_start_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
846 xfs_bmap_cancel(&free_list
);
847 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
849 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
850 return XFS_ERROR(error
);
854 xfs_iomap_write_unwritten(
859 xfs_mount_t
*mp
= ip
->i_mount
;
860 xfs_fileoff_t offset_fsb
;
861 xfs_filblks_t count_fsb
;
862 xfs_filblks_t numblks_fsb
;
863 xfs_fsblock_t firstfsb
;
866 xfs_bmbt_irec_t imap
;
867 xfs_bmap_free_t free_list
;
872 xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN
, ip
, offset
, count
);
874 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
875 count_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)offset
+ count
);
876 count_fsb
= (xfs_filblks_t
)(count_fsb
- offset_fsb
);
879 * Reserve enough blocks in this transaction for two complete extent
880 * btree splits. We may be converting the middle part of an unwritten
881 * extent and in this case we will insert two new extents in the btree
882 * each of which could cause a full split.
884 * This reservation amount will be used in the first call to
885 * xfs_bmbt_split() to select an AG with enough space to satisfy the
886 * rest of the operation.
888 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0) << 1;
892 * set up a transaction to convert the range of extents
893 * from unwritten to real. Do allocations in a loop until
894 * we have covered the range passed in.
896 tp
= xfs_trans_alloc(mp
, XFS_TRANS_STRAT_WRITE
);
897 tp
->t_flags
|= XFS_TRANS_RESERVE
;
898 error
= xfs_trans_reserve(tp
, resblks
,
899 XFS_WRITE_LOG_RES(mp
), 0,
900 XFS_TRANS_PERM_LOG_RES
,
901 XFS_WRITE_LOG_COUNT
);
903 xfs_trans_cancel(tp
, 0);
904 return XFS_ERROR(error
);
907 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
908 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
909 xfs_trans_ihold(tp
, ip
);
912 * Modify the unwritten extent state of the buffer.
914 xfs_bmap_init(&free_list
, &firstfsb
);
916 error
= xfs_bmapi(tp
, ip
, offset_fsb
, count_fsb
,
917 XFS_BMAPI_WRITE
|XFS_BMAPI_CONVERT
, &firstfsb
,
918 1, &imap
, &nimaps
, &free_list
, NULL
);
920 goto error_on_bmapi_transaction
;
922 error
= xfs_bmap_finish(&(tp
), &(free_list
), &committed
);
924 goto error_on_bmapi_transaction
;
926 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
927 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
929 return XFS_ERROR(error
);
931 if (!(imap
.br_startblock
|| XFS_IS_REALTIME_INODE(ip
)))
932 return xfs_cmn_err_fsblock_zero(ip
, &imap
);
934 if ((numblks_fsb
= imap
.br_blockcount
) == 0) {
936 * The numblks_fsb value should always get
937 * smaller, otherwise the loop is stuck.
939 ASSERT(imap
.br_blockcount
);
942 offset_fsb
+= numblks_fsb
;
943 count_fsb
-= numblks_fsb
;
944 } while (count_fsb
> 0);
948 error_on_bmapi_transaction
:
949 xfs_bmap_cancel(&free_list
);
950 xfs_trans_cancel(tp
, (XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
));
951 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
952 return XFS_ERROR(error
);