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_inode_t
*ip
= XFS_IO_INODE(io
);
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
,
94 xfs_inode_t
*ip
= XFS_IO_INODE(io
);
99 ktrace_enter(ip
->i_rwtrace
,
100 (void *)((unsigned long)tag
),
102 (void *)((unsigned long)((ip
->i_d
.di_size
>> 32) & 0xffffffff)),
103 (void *)((unsigned long)(ip
->i_d
.di_size
& 0xffffffff)),
104 (void *)((unsigned long)((offset
>> 32) & 0xffffffff)),
105 (void *)((unsigned long)(offset
& 0xffffffff)),
106 (void *)((unsigned long)count
),
107 (void *)((unsigned long)flags
),
108 (void *)((unsigned long)((iomapp
->iomap_offset
>> 32) & 0xffffffff)),
109 (void *)((unsigned long)(iomapp
->iomap_offset
& 0xffffffff)),
110 (void *)((unsigned long)(iomapp
->iomap_delta
)),
111 (void *)((unsigned long)(iomapp
->iomap_bsize
)),
112 (void *)((unsigned long)(iomapp
->iomap_bn
)),
113 (void *)(__psint_t
)(imapp
->br_startoff
),
114 (void *)((unsigned long)(imapp
->br_blockcount
)),
115 (void *)(__psint_t
)(imapp
->br_startblock
));
118 #define xfs_iomap_enter_trace(tag, io, offset, count)
119 #define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags)
122 #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
123 << mp->m_writeio_log)
124 #define XFS_STRAT_WRITE_IMAPS 2
125 #define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
131 xfs_bmbt_irec_t
*imap
,
133 int imaps
, /* Number of imap entries */
134 int iomaps
, /* Number of iomap entries */
139 xfs_fsblock_t start_block
;
143 for (pbm
= 0; imaps
&& pbm
< iomaps
; imaps
--, iomapp
++, imap
++, pbm
++) {
144 iomapp
->iomap_offset
= XFS_FSB_TO_B(mp
, imap
->br_startoff
);
145 iomapp
->iomap_delta
= offset
- iomapp
->iomap_offset
;
146 iomapp
->iomap_bsize
= XFS_FSB_TO_B(mp
, imap
->br_blockcount
);
147 iomapp
->iomap_flags
= flags
;
149 if (io
->io_flags
& XFS_IOCORE_RT
) {
150 iomapp
->iomap_flags
|= IOMAP_REALTIME
;
151 iomapp
->iomap_target
= mp
->m_rtdev_targp
;
153 iomapp
->iomap_target
= mp
->m_ddev_targp
;
155 start_block
= imap
->br_startblock
;
156 if (start_block
== HOLESTARTBLOCK
) {
157 iomapp
->iomap_bn
= IOMAP_DADDR_NULL
;
158 iomapp
->iomap_flags
|= IOMAP_HOLE
;
159 } else if (start_block
== DELAYSTARTBLOCK
) {
160 iomapp
->iomap_bn
= IOMAP_DADDR_NULL
;
161 iomapp
->iomap_flags
|= IOMAP_DELAY
;
163 iomapp
->iomap_bn
= XFS_FSB_TO_DB_IO(io
, start_block
);
164 if (ISUNWRITTEN(imap
))
165 iomapp
->iomap_flags
|= IOMAP_UNWRITTEN
;
168 offset
+= iomapp
->iomap_bsize
- iomapp
->iomap_delta
;
170 return pbm
; /* Return the number filled */
182 xfs_mount_t
*mp
= io
->io_mount
;
183 xfs_fileoff_t offset_fsb
, end_fsb
;
186 xfs_bmbt_irec_t imap
;
191 if (XFS_FORCED_SHUTDOWN(mp
))
192 return XFS_ERROR(EIO
);
195 (BMAPI_READ
| BMAPI_WRITE
| BMAPI_ALLOCATE
|
198 xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER
, io
, offset
, count
);
199 lockmode
= XFS_LCK_MAP_SHARED(mp
, io
);
200 bmapi_flags
= XFS_BMAPI_ENTIRE
;
203 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER
, io
, offset
, count
);
204 lockmode
= XFS_ILOCK_EXCL
|XFS_EXTSIZE_WR
;
205 if (flags
& BMAPI_IGNSTATE
)
206 bmapi_flags
|= XFS_BMAPI_IGSTATE
|XFS_BMAPI_ENTIRE
;
207 XFS_ILOCK(mp
, io
, lockmode
);
210 xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER
, io
, offset
, count
);
211 lockmode
= XFS_ILOCK_SHARED
|XFS_EXTSIZE_RD
;
212 bmapi_flags
= XFS_BMAPI_ENTIRE
;
213 /* Attempt non-blocking lock */
214 if (flags
& BMAPI_TRYLOCK
) {
215 if (!XFS_ILOCK_NOWAIT(mp
, io
, lockmode
))
216 return XFS_ERROR(EAGAIN
);
218 XFS_ILOCK(mp
, io
, lockmode
);
221 case BMAPI_UNWRITTEN
:
227 ASSERT(offset
<= mp
->m_maxioffset
);
228 if ((xfs_fsize_t
)offset
+ count
> mp
->m_maxioffset
)
229 count
= mp
->m_maxioffset
- offset
;
230 end_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)offset
+ count
);
231 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
233 error
= XFS_BMAPI(mp
, NULL
, io
, offset_fsb
,
234 (xfs_filblks_t
)(end_fsb
- offset_fsb
),
235 bmapi_flags
, NULL
, 0, &imap
,
236 &nimaps
, NULL
, NULL
);
242 switch (flags
& (BMAPI_WRITE
|BMAPI_ALLOCATE
|BMAPI_UNWRITTEN
)) {
244 /* If we found an extent, return it */
246 (imap
.br_startblock
!= HOLESTARTBLOCK
) &&
247 (imap
.br_startblock
!= DELAYSTARTBLOCK
)) {
248 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP
, io
,
249 offset
, count
, iomapp
, &imap
, flags
);
253 if (flags
& (BMAPI_DIRECT
|BMAPI_MMAP
)) {
254 error
= XFS_IOMAP_WRITE_DIRECT(mp
, io
, offset
,
255 count
, flags
, &imap
, &nimaps
, nimaps
);
257 error
= XFS_IOMAP_WRITE_DELAY(mp
, io
, offset
, count
,
258 flags
, &imap
, &nimaps
);
261 xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP
, io
,
262 offset
, count
, iomapp
, &imap
, flags
);
264 iomap_flags
= IOMAP_NEW
;
267 /* If we found an extent, return it */
268 XFS_IUNLOCK(mp
, io
, lockmode
);
271 if (nimaps
&& !ISNULLSTARTBLOCK(imap
.br_startblock
)) {
272 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP
, io
,
273 offset
, count
, iomapp
, &imap
, flags
);
277 error
= XFS_IOMAP_WRITE_ALLOCATE(mp
, io
, offset
, count
,
280 case BMAPI_UNWRITTEN
:
282 error
= XFS_IOMAP_WRITE_UNWRITTEN(mp
, io
, offset
, count
);
288 *niomaps
= xfs_imap_to_bmap(io
, offset
, &imap
,
289 iomapp
, nimaps
, *niomaps
, iomap_flags
);
290 } else if (niomaps
) {
296 XFS_IUNLOCK(mp
, io
, lockmode
);
297 return XFS_ERROR(error
);
301 xfs_iomap_eof_align_last_fsb(
305 xfs_extlen_t extsize
,
306 xfs_fileoff_t
*last_fsb
)
308 xfs_fileoff_t new_last_fsb
= 0;
312 if (io
->io_flags
& XFS_IOCORE_RT
)
315 * If mounted with the "-o swalloc" option, roundup the allocation
316 * request to a stripe width boundary if the file size is >=
317 * stripe width and we are allocating past the allocation eof.
319 else if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
) &&
320 (isize
>= XFS_FSB_TO_B(mp
, mp
->m_swidth
)))
321 new_last_fsb
= roundup_64(*last_fsb
, mp
->m_swidth
);
323 * Roundup the allocation request to a stripe unit (m_dalign) boundary
324 * if the file size is >= stripe unit size, and we are allocating past
325 * the allocation eof.
327 else if (mp
->m_dalign
&& (isize
>= XFS_FSB_TO_B(mp
, mp
->m_dalign
)))
328 new_last_fsb
= roundup_64(*last_fsb
, mp
->m_dalign
);
331 * Always round up the allocation request to an extent boundary
332 * (when file on a real-time subvolume or has di_extsize hint).
336 align
= roundup_64(new_last_fsb
, extsize
);
339 new_last_fsb
= roundup_64(*last_fsb
, align
);
343 error
= XFS_BMAP_EOF(mp
, io
, new_last_fsb
, XFS_DATA_FORK
, &eof
);
347 *last_fsb
= new_last_fsb
;
360 if (ip
->i_delayed_blks
) {
361 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
363 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
366 *ioflags
|= BMAPI_SYNC
;
372 *ioflags
|= BMAPI_SYNC
;
375 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
376 xfs_flush_device(ip
);
377 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
385 xfs_cmn_err_fsblock_zero(
387 xfs_bmbt_irec_t
*imap
)
389 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO
, CE_ALERT
, ip
->i_mount
,
390 "Access to block zero in inode %llu "
391 "start_block: %llx start_off: %llx "
392 "blkcnt: %llx extent-state: %x\n",
393 (unsigned long long)ip
->i_ino
,
394 (unsigned long long)imap
->br_startblock
,
395 (unsigned long long)imap
->br_startoff
,
396 (unsigned long long)imap
->br_blockcount
,
402 xfs_iomap_write_direct(
407 xfs_bmbt_irec_t
*ret_imap
,
411 xfs_mount_t
*mp
= ip
->i_mount
;
412 xfs_iocore_t
*io
= &ip
->i_iocore
;
413 xfs_fileoff_t offset_fsb
;
414 xfs_fileoff_t last_fsb
;
415 xfs_filblks_t count_fsb
, resaligned
;
416 xfs_fsblock_t firstfsb
;
417 xfs_extlen_t extsz
, temp
;
424 xfs_bmbt_irec_t imap
;
425 xfs_bmap_free_t free_list
;
426 uint qblocks
, resblks
, resrtextents
;
431 * Make sure that the dquots are there. This doesn't hold
432 * the ilock across a disk read.
434 error
= XFS_QM_DQATTACH(ip
->i_mount
, ip
, XFS_QMOPT_ILOCKED
);
436 return XFS_ERROR(error
);
438 rt
= XFS_IS_REALTIME_INODE(ip
);
439 extsz
= xfs_get_extsz_hint(ip
);
442 if (io
->io_new_size
> isize
)
443 isize
= io
->io_new_size
;
445 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
446 last_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)(offset
+ count
)));
447 if ((offset
+ count
) > isize
) {
448 error
= xfs_iomap_eof_align_last_fsb(mp
, io
, isize
, extsz
,
453 if (found
&& (ret_imap
->br_startblock
== HOLESTARTBLOCK
))
454 last_fsb
= MIN(last_fsb
, (xfs_fileoff_t
)
455 ret_imap
->br_blockcount
+
456 ret_imap
->br_startoff
);
458 count_fsb
= last_fsb
- offset_fsb
;
459 ASSERT(count_fsb
> 0);
461 resaligned
= count_fsb
;
462 if (unlikely(extsz
)) {
463 if ((temp
= do_mod(offset_fsb
, extsz
)))
465 if ((temp
= do_mod(resaligned
, extsz
)))
466 resaligned
+= extsz
- temp
;
470 resrtextents
= qblocks
= resaligned
;
471 resrtextents
/= mp
->m_sb
.sb_rextsize
;
472 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
473 quota_flag
= XFS_QMOPT_RES_RTBLKS
;
476 resblks
= qblocks
= XFS_DIOSTRAT_SPACE_RES(mp
, resaligned
);
477 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
481 * Allocate and setup the transaction
483 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
484 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
485 error
= xfs_trans_reserve(tp
, resblks
,
486 XFS_WRITE_LOG_RES(mp
), resrtextents
,
487 XFS_TRANS_PERM_LOG_RES
,
488 XFS_WRITE_LOG_COUNT
);
490 * Check for running out of space, note: need lock to return
493 xfs_trans_cancel(tp
, 0);
494 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
498 error
= XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
, tp
, ip
,
499 qblocks
, 0, quota_flag
);
503 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
504 xfs_trans_ihold(tp
, ip
);
506 bmapi_flag
= XFS_BMAPI_WRITE
;
507 if ((flags
& BMAPI_DIRECT
) && (offset
< ip
->i_size
|| extsz
))
508 bmapi_flag
|= XFS_BMAPI_PREALLOC
;
511 * Issue the xfs_bmapi() call to allocate the blocks
513 XFS_BMAP_INIT(&free_list
, &firstfsb
);
515 error
= XFS_BMAPI(mp
, tp
, io
, offset_fsb
, count_fsb
, bmapi_flag
,
516 &firstfsb
, 0, &imap
, &nimaps
, &free_list
, NULL
);
521 * Complete the transaction
523 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
526 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
531 * Copy any maps to caller's array and return any error.
538 if (unlikely(!imap
.br_startblock
&& !(io
->io_flags
& XFS_IOCORE_RT
))) {
539 error
= xfs_cmn_err_fsblock_zero(ip
, &imap
);
547 error0
: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
548 xfs_bmap_cancel(&free_list
);
549 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp
, tp
, ip
, qblocks
, 0, quota_flag
);
551 error1
: /* Just cancel transaction */
552 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
553 *nmaps
= 0; /* nothing set-up here */
556 return XFS_ERROR(error
);
560 * If the caller is doing a write at the end of the file,
561 * then extend the allocation out to the file system's write
562 * iosize. We clean up any extra space left over when the
563 * file is closed in xfs_inactive().
565 * For sync writes, we are flushing delayed allocate space to
566 * try to make additional space available for allocation near
567 * the filesystem full boundary - preallocation hurts in that
568 * situation, of course.
571 xfs_iomap_eof_want_preallocate(
578 xfs_bmbt_irec_t
*imap
,
582 xfs_fileoff_t start_fsb
;
583 xfs_filblks_t count_fsb
;
584 xfs_fsblock_t firstblock
;
588 if ((ioflag
& BMAPI_SYNC
) || (offset
+ count
) <= isize
)
592 * If there are any real blocks past eof, then don't
593 * do any speculative allocation.
595 start_fsb
= XFS_B_TO_FSBT(mp
, ((xfs_ufsize_t
)(offset
+ count
- 1)));
596 count_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)XFS_MAXIOFFSET(mp
));
597 while (count_fsb
> 0) {
599 firstblock
= NULLFSBLOCK
;
600 error
= XFS_BMAPI(mp
, NULL
, io
, start_fsb
, count_fsb
, 0,
601 &firstblock
, 0, imap
, &imaps
, NULL
, NULL
);
604 for (n
= 0; n
< imaps
; n
++) {
605 if ((imap
[n
].br_startblock
!= HOLESTARTBLOCK
) &&
606 (imap
[n
].br_startblock
!= DELAYSTARTBLOCK
))
608 start_fsb
+= imap
[n
].br_blockcount
;
609 count_fsb
-= imap
[n
].br_blockcount
;
617 xfs_iomap_write_delay(
622 xfs_bmbt_irec_t
*ret_imap
,
625 xfs_mount_t
*mp
= ip
->i_mount
;
626 xfs_iocore_t
*io
= &ip
->i_iocore
;
627 xfs_fileoff_t offset_fsb
;
628 xfs_fileoff_t last_fsb
;
629 xfs_off_t aligned_offset
;
630 xfs_fileoff_t ioalign
;
631 xfs_fsblock_t firstblock
;
635 xfs_bmbt_irec_t imap
[XFS_WRITE_IMAPS
];
636 int prealloc
, fsynced
= 0;
639 ASSERT(ismrlocked(&ip
->i_lock
, MR_UPDATE
) != 0);
642 * Make sure that the dquots are there. This doesn't hold
643 * the ilock across a disk read.
645 error
= XFS_QM_DQATTACH(mp
, ip
, XFS_QMOPT_ILOCKED
);
647 return XFS_ERROR(error
);
649 extsz
= xfs_get_extsz_hint(ip
);
650 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
654 if (io
->io_new_size
> isize
)
655 isize
= io
->io_new_size
;
657 error
= xfs_iomap_eof_want_preallocate(mp
, io
, isize
, offset
, count
,
658 ioflag
, imap
, XFS_WRITE_IMAPS
, &prealloc
);
663 aligned_offset
= XFS_WRITEIO_ALIGN(mp
, (offset
+ count
- 1));
664 ioalign
= XFS_B_TO_FSBT(mp
, aligned_offset
);
665 last_fsb
= ioalign
+ mp
->m_writeio_blocks
;
667 last_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)(offset
+ count
)));
670 if (prealloc
|| extsz
) {
671 error
= xfs_iomap_eof_align_last_fsb(mp
, io
, isize
, extsz
,
677 nimaps
= XFS_WRITE_IMAPS
;
678 firstblock
= NULLFSBLOCK
;
679 error
= XFS_BMAPI(mp
, NULL
, io
, offset_fsb
,
680 (xfs_filblks_t
)(last_fsb
- offset_fsb
),
681 XFS_BMAPI_DELAY
| XFS_BMAPI_WRITE
|
682 XFS_BMAPI_ENTIRE
, &firstblock
, 1, imap
,
683 &nimaps
, NULL
, NULL
);
684 if (error
&& (error
!= ENOSPC
))
685 return XFS_ERROR(error
);
688 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
689 * then we must have run out of space - flush delalloc, and retry..
692 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE
,
694 if (xfs_flush_space(ip
, &fsynced
, &ioflag
))
695 return XFS_ERROR(ENOSPC
);
701 if (unlikely(!imap
[0].br_startblock
&& !(io
->io_flags
& XFS_IOCORE_RT
)))
702 return xfs_cmn_err_fsblock_zero(ip
, &imap
[0]);
711 * Pass in a delayed allocate extent, convert it to real extents;
712 * return to the caller the extent we create which maps on top of
713 * the originating callers request.
715 * Called without a lock on the inode.
718 xfs_iomap_write_allocate(
722 xfs_bmbt_irec_t
*map
,
725 xfs_mount_t
*mp
= ip
->i_mount
;
726 xfs_iocore_t
*io
= &ip
->i_iocore
;
727 xfs_fileoff_t offset_fsb
, last_block
;
728 xfs_fileoff_t end_fsb
, map_start_fsb
;
729 xfs_fsblock_t first_block
;
730 xfs_bmap_free_t free_list
;
731 xfs_filblks_t count_fsb
;
732 xfs_bmbt_irec_t imap
[XFS_STRAT_WRITE_IMAPS
];
734 int i
, nimaps
, committed
;
741 * Make sure that the dquots are there.
743 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
744 return XFS_ERROR(error
);
746 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
747 count_fsb
= map
->br_blockcount
;
748 map_start_fsb
= map
->br_startoff
;
750 XFS_STATS_ADD(xs_xstrat_bytes
, XFS_FSB_TO_B(mp
, count_fsb
));
752 while (count_fsb
!= 0) {
754 * Set up a transaction with which to allocate the
755 * backing store for the file. Do allocations in a
756 * loop until we get some space in the range we are
757 * interested in. The other space that might be allocated
758 * is in the delayed allocation extent on which we sit
759 * but before our buffer starts.
763 while (nimaps
== 0) {
764 tp
= xfs_trans_alloc(mp
, XFS_TRANS_STRAT_WRITE
);
765 tp
->t_flags
|= XFS_TRANS_RESERVE
;
766 nres
= XFS_EXTENTADD_SPACE_RES(mp
, XFS_DATA_FORK
);
767 error
= xfs_trans_reserve(tp
, nres
,
768 XFS_WRITE_LOG_RES(mp
),
769 0, XFS_TRANS_PERM_LOG_RES
,
770 XFS_WRITE_LOG_COUNT
);
772 xfs_trans_cancel(tp
, 0);
773 return XFS_ERROR(error
);
775 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
776 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
777 xfs_trans_ihold(tp
, ip
);
779 XFS_BMAP_INIT(&free_list
, &first_block
);
781 nimaps
= XFS_STRAT_WRITE_IMAPS
;
783 * Ensure we don't go beyond eof - it is possible
784 * the extents changed since we did the read call,
785 * we dropped the ilock in the interim.
788 end_fsb
= XFS_B_TO_FSB(mp
, ip
->i_size
);
789 xfs_bmap_last_offset(NULL
, ip
, &last_block
,
791 last_block
= XFS_FILEOFF_MAX(last_block
, end_fsb
);
792 if ((map_start_fsb
+ count_fsb
) > last_block
) {
793 count_fsb
= last_block
- map_start_fsb
;
794 if (count_fsb
== 0) {
800 /* Go get the actual blocks */
801 error
= XFS_BMAPI(mp
, tp
, io
, map_start_fsb
, count_fsb
,
802 XFS_BMAPI_WRITE
, &first_block
, 1,
803 imap
, &nimaps
, &free_list
, NULL
);
807 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
811 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
815 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
819 * See if we were able to allocate an extent that
820 * covers at least part of the callers request
822 for (i
= 0; i
< nimaps
; i
++) {
823 if (unlikely(!imap
[i
].br_startblock
&&
824 !(io
->io_flags
& XFS_IOCORE_RT
)))
825 return xfs_cmn_err_fsblock_zero(ip
, &imap
[i
]);
826 if ((offset_fsb
>= imap
[i
].br_startoff
) &&
827 (offset_fsb
< (imap
[i
].br_startoff
+
828 imap
[i
].br_blockcount
))) {
831 XFS_STATS_INC(xs_xstrat_quick
);
834 count_fsb
-= imap
[i
].br_blockcount
;
837 /* So far we have not mapped the requested part of the
838 * file, just surrounding data, try again.
841 map_start_fsb
= imap
[nimaps
].br_startoff
+
842 imap
[nimaps
].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_iocore_t
*io
= &ip
->i_iocore
;
861 xfs_fileoff_t offset_fsb
;
862 xfs_filblks_t count_fsb
;
863 xfs_filblks_t numblks_fsb
;
864 xfs_fsblock_t firstfsb
;
867 xfs_bmbt_irec_t imap
;
868 xfs_bmap_free_t free_list
;
873 xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN
,
874 &ip
->i_iocore
, offset
, count
);
876 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
877 count_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)offset
+ count
);
878 count_fsb
= (xfs_filblks_t
)(count_fsb
- offset_fsb
);
880 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0) << 1;
884 * set up a transaction to convert the range of extents
885 * from unwritten to real. Do allocations in a loop until
886 * we have covered the range passed in.
888 tp
= xfs_trans_alloc(mp
, XFS_TRANS_STRAT_WRITE
);
889 tp
->t_flags
|= XFS_TRANS_RESERVE
;
890 error
= xfs_trans_reserve(tp
, resblks
,
891 XFS_WRITE_LOG_RES(mp
), 0,
892 XFS_TRANS_PERM_LOG_RES
,
893 XFS_WRITE_LOG_COUNT
);
895 xfs_trans_cancel(tp
, 0);
896 return XFS_ERROR(error
);
899 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
900 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
901 xfs_trans_ihold(tp
, ip
);
904 * Modify the unwritten extent state of the buffer.
906 XFS_BMAP_INIT(&free_list
, &firstfsb
);
908 error
= XFS_BMAPI(mp
, tp
, io
, offset_fsb
, count_fsb
,
909 XFS_BMAPI_WRITE
|XFS_BMAPI_CONVERT
, &firstfsb
,
910 1, &imap
, &nimaps
, &free_list
, NULL
);
912 goto error_on_bmapi_transaction
;
914 error
= xfs_bmap_finish(&(tp
), &(free_list
), &committed
);
916 goto error_on_bmapi_transaction
;
918 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
919 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
921 return XFS_ERROR(error
);
923 if (unlikely(!imap
.br_startblock
&&
924 !(io
->io_flags
& XFS_IOCORE_RT
)))
925 return xfs_cmn_err_fsblock_zero(ip
, &imap
);
927 if ((numblks_fsb
= imap
.br_blockcount
) == 0) {
929 * The numblks_fsb value should always get
930 * smaller, otherwise the loop is stuck.
932 ASSERT(imap
.br_blockcount
);
935 offset_fsb
+= numblks_fsb
;
936 count_fsb
-= numblks_fsb
;
937 } while (count_fsb
> 0);
941 error_on_bmapi_transaction
:
942 xfs_bmap_cancel(&free_list
);
943 xfs_trans_cancel(tp
, (XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
));
944 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
945 return XFS_ERROR(error
);