2 * Copyright (c) 2000-2005 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"
28 #include "xfs_alloc.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_quota.h"
31 #include "xfs_mount.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir_sf.h"
36 #include "xfs_dir2_sf.h"
37 #include "xfs_attr_sf.h"
38 #include "xfs_dinode.h"
39 #include "xfs_inode.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_btree.h"
44 #include "xfs_rtalloc.h"
45 #include "xfs_error.h"
46 #include "xfs_itable.h"
52 #include "xfs_buf_item.h"
53 #include "xfs_trans_space.h"
54 #include "xfs_utils.h"
55 #include "xfs_iomap.h"
57 #if defined(XFS_RW_TRACE)
59 xfs_iomap_enter_trace(
65 xfs_inode_t
*ip
= XFS_IO_INODE(io
);
70 ktrace_enter(ip
->i_rwtrace
,
71 (void *)((unsigned long)tag
),
73 (void *)((unsigned long)((ip
->i_d
.di_size
>> 32) & 0xffffffff)),
74 (void *)((unsigned long)(ip
->i_d
.di_size
& 0xffffffff)),
75 (void *)((unsigned long)((offset
>> 32) & 0xffffffff)),
76 (void *)((unsigned long)(offset
& 0xffffffff)),
77 (void *)((unsigned long)count
),
78 (void *)((unsigned long)((io
->io_new_size
>> 32) & 0xffffffff)),
79 (void *)((unsigned long)(io
->io_new_size
& 0xffffffff)),
96 xfs_bmbt_irec_t
*imapp
,
99 xfs_inode_t
*ip
= XFS_IO_INODE(io
);
104 ktrace_enter(ip
->i_rwtrace
,
105 (void *)((unsigned long)tag
),
107 (void *)((unsigned long)((ip
->i_d
.di_size
>> 32) & 0xffffffff)),
108 (void *)((unsigned long)(ip
->i_d
.di_size
& 0xffffffff)),
109 (void *)((unsigned long)((offset
>> 32) & 0xffffffff)),
110 (void *)((unsigned long)(offset
& 0xffffffff)),
111 (void *)((unsigned long)count
),
112 (void *)((unsigned long)flags
),
113 (void *)((unsigned long)((iomapp
->iomap_offset
>> 32) & 0xffffffff)),
114 (void *)((unsigned long)(iomapp
->iomap_offset
& 0xffffffff)),
115 (void *)((unsigned long)(iomapp
->iomap_delta
)),
116 (void *)((unsigned long)(iomapp
->iomap_bsize
)),
117 (void *)((unsigned long)(iomapp
->iomap_bn
)),
118 (void *)(__psint_t
)(imapp
->br_startoff
),
119 (void *)((unsigned long)(imapp
->br_blockcount
)),
120 (void *)(__psint_t
)(imapp
->br_startblock
));
123 #define xfs_iomap_enter_trace(tag, io, offset, count)
124 #define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags)
127 #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
128 << mp->m_writeio_log)
129 #define XFS_STRAT_WRITE_IMAPS 2
130 #define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
136 xfs_bmbt_irec_t
*imap
,
138 int imaps
, /* Number of imap entries */
139 int iomaps
, /* Number of iomap entries */
145 xfs_fsblock_t start_block
;
148 nisize
= XFS_SIZE(mp
, io
);
149 if (io
->io_new_size
> nisize
)
150 nisize
= io
->io_new_size
;
152 for (pbm
= 0; imaps
&& pbm
< iomaps
; imaps
--, iomapp
++, imap
++, pbm
++) {
153 iomapp
->iomap_offset
= XFS_FSB_TO_B(mp
, imap
->br_startoff
);
154 iomapp
->iomap_delta
= offset
- iomapp
->iomap_offset
;
155 iomapp
->iomap_bsize
= XFS_FSB_TO_B(mp
, imap
->br_blockcount
);
156 iomapp
->iomap_flags
= flags
;
158 if (io
->io_flags
& XFS_IOCORE_RT
) {
159 iomapp
->iomap_flags
|= IOMAP_REALTIME
;
160 iomapp
->iomap_target
= mp
->m_rtdev_targp
;
162 iomapp
->iomap_target
= mp
->m_ddev_targp
;
164 start_block
= imap
->br_startblock
;
165 if (start_block
== HOLESTARTBLOCK
) {
166 iomapp
->iomap_bn
= IOMAP_DADDR_NULL
;
167 iomapp
->iomap_flags
|= IOMAP_HOLE
;
168 } else if (start_block
== DELAYSTARTBLOCK
) {
169 iomapp
->iomap_bn
= IOMAP_DADDR_NULL
;
170 iomapp
->iomap_flags
|= IOMAP_DELAY
;
172 iomapp
->iomap_bn
= XFS_FSB_TO_DB_IO(io
, start_block
);
173 if (ISUNWRITTEN(imap
))
174 iomapp
->iomap_flags
|= IOMAP_UNWRITTEN
;
177 if ((iomapp
->iomap_offset
+ iomapp
->iomap_bsize
) >= nisize
) {
178 iomapp
->iomap_flags
|= IOMAP_EOF
;
181 offset
+= iomapp
->iomap_bsize
- iomapp
->iomap_delta
;
183 return pbm
; /* Return the number filled */
195 xfs_mount_t
*mp
= io
->io_mount
;
196 xfs_fileoff_t offset_fsb
, end_fsb
;
199 xfs_bmbt_irec_t imap
;
204 if (XFS_FORCED_SHUTDOWN(mp
))
205 return XFS_ERROR(EIO
);
208 (BMAPI_READ
| BMAPI_WRITE
| BMAPI_ALLOCATE
|
209 BMAPI_UNWRITTEN
| BMAPI_DEVICE
)) {
211 xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER
, io
, offset
, count
);
212 lockmode
= XFS_LCK_MAP_SHARED(mp
, io
);
213 bmapi_flags
= XFS_BMAPI_ENTIRE
;
216 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER
, io
, offset
, count
);
217 lockmode
= XFS_ILOCK_EXCL
|XFS_EXTSIZE_WR
;
218 if (flags
& BMAPI_IGNSTATE
)
219 bmapi_flags
|= XFS_BMAPI_IGSTATE
|XFS_BMAPI_ENTIRE
;
220 XFS_ILOCK(mp
, io
, lockmode
);
223 xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER
, io
, offset
, count
);
224 lockmode
= XFS_ILOCK_SHARED
|XFS_EXTSIZE_RD
;
225 bmapi_flags
= XFS_BMAPI_ENTIRE
;
226 /* Attempt non-blocking lock */
227 if (flags
& BMAPI_TRYLOCK
) {
228 if (!XFS_ILOCK_NOWAIT(mp
, io
, lockmode
))
229 return XFS_ERROR(EAGAIN
);
231 XFS_ILOCK(mp
, io
, lockmode
);
234 case BMAPI_UNWRITTEN
:
237 lockmode
= XFS_LCK_MAP_SHARED(mp
, io
);
238 iomapp
->iomap_target
= io
->io_flags
& XFS_IOCORE_RT
?
239 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
247 ASSERT(offset
<= mp
->m_maxioffset
);
248 if ((xfs_fsize_t
)offset
+ count
> mp
->m_maxioffset
)
249 count
= mp
->m_maxioffset
- offset
;
250 end_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)offset
+ count
);
251 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
253 error
= XFS_BMAPI(mp
, NULL
, io
, offset_fsb
,
254 (xfs_filblks_t
)(end_fsb
- offset_fsb
),
255 bmapi_flags
, NULL
, 0, &imap
,
262 switch (flags
& (BMAPI_WRITE
|BMAPI_ALLOCATE
|BMAPI_UNWRITTEN
)) {
264 /* If we found an extent, return it */
266 (imap
.br_startblock
!= HOLESTARTBLOCK
) &&
267 (imap
.br_startblock
!= DELAYSTARTBLOCK
)) {
268 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP
, io
,
269 offset
, count
, iomapp
, &imap
, flags
);
273 if (flags
& (BMAPI_DIRECT
|BMAPI_MMAP
)) {
274 error
= XFS_IOMAP_WRITE_DIRECT(mp
, io
, offset
,
275 count
, flags
, &imap
, &nimaps
, nimaps
);
277 error
= XFS_IOMAP_WRITE_DELAY(mp
, io
, offset
, count
,
278 flags
, &imap
, &nimaps
);
281 xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP
, io
,
282 offset
, count
, iomapp
, &imap
, flags
);
284 iomap_flags
= IOMAP_NEW
;
287 /* If we found an extent, return it */
288 XFS_IUNLOCK(mp
, io
, lockmode
);
291 if (nimaps
&& !ISNULLSTARTBLOCK(imap
.br_startblock
)) {
292 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP
, io
,
293 offset
, count
, iomapp
, &imap
, flags
);
297 error
= XFS_IOMAP_WRITE_ALLOCATE(mp
, io
, offset
, count
,
300 case BMAPI_UNWRITTEN
:
302 error
= XFS_IOMAP_WRITE_UNWRITTEN(mp
, io
, offset
, count
);
308 *niomaps
= xfs_imap_to_bmap(io
, offset
, &imap
,
309 iomapp
, nimaps
, *niomaps
, iomap_flags
);
310 } else if (niomaps
) {
316 XFS_IUNLOCK(mp
, io
, lockmode
);
317 return XFS_ERROR(error
);
328 if (ip
->i_delayed_blks
) {
329 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
331 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
334 *ioflags
|= BMAPI_SYNC
;
340 *ioflags
|= BMAPI_SYNC
;
343 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
344 xfs_flush_device(ip
);
345 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
353 xfs_iomap_write_direct(
358 xfs_bmbt_irec_t
*ret_imap
,
362 xfs_mount_t
*mp
= ip
->i_mount
;
363 xfs_iocore_t
*io
= &ip
->i_iocore
;
364 xfs_fileoff_t offset_fsb
;
365 xfs_fileoff_t last_fsb
;
366 xfs_filblks_t count_fsb
;
367 xfs_fsblock_t firstfsb
;
374 xfs_bmbt_irec_t imap
;
375 xfs_bmap_free_t free_list
;
376 xfs_filblks_t qblocks
, resblks
;
381 * Make sure that the dquots are there. This doesn't hold
382 * the ilock across a disk read.
384 error
= XFS_QM_DQATTACH(ip
->i_mount
, ip
, XFS_QMOPT_ILOCKED
);
386 return XFS_ERROR(error
);
388 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
389 last_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)(offset
+ count
)));
390 count_fsb
= last_fsb
- offset_fsb
;
391 if (found
&& (ret_imap
->br_startblock
== HOLESTARTBLOCK
)) {
392 xfs_fileoff_t map_last_fsb
;
394 map_last_fsb
= ret_imap
->br_blockcount
+ ret_imap
->br_startoff
;
395 if (map_last_fsb
< last_fsb
) {
396 last_fsb
= map_last_fsb
;
397 count_fsb
= last_fsb
- offset_fsb
;
399 ASSERT(count_fsb
> 0);
403 * Determine if reserving space on the data or realtime partition.
405 if ((rt
= XFS_IS_REALTIME_INODE(ip
))) {
408 if (!(extsz
= ip
->i_d
.di_extsize
))
409 extsz
= mp
->m_sb
.sb_rextsize
;
410 resrtextents
= qblocks
= (count_fsb
+ extsz
- 1);
411 do_div(resrtextents
, mp
->m_sb
.sb_rextsize
);
412 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
413 quota_flag
= XFS_QMOPT_RES_RTBLKS
;
416 resblks
= qblocks
= XFS_DIOSTRAT_SPACE_RES(mp
, count_fsb
);
417 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
421 * Allocate and setup the transaction
423 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
424 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
425 error
= xfs_trans_reserve(tp
, resblks
,
426 XFS_WRITE_LOG_RES(mp
), resrtextents
,
427 XFS_TRANS_PERM_LOG_RES
,
428 XFS_WRITE_LOG_COUNT
);
431 * Check for running out of space, note: need lock to return
434 xfs_trans_cancel(tp
, 0);
435 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
439 if (XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
, tp
, ip
, qblocks
, 0, quota_flag
)) {
444 bmapi_flag
= XFS_BMAPI_WRITE
;
445 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
446 xfs_trans_ihold(tp
, ip
);
448 if (!(flags
& BMAPI_MMAP
) && (offset
< ip
->i_d
.di_size
|| rt
))
449 bmapi_flag
|= XFS_BMAPI_PREALLOC
;
452 * Issue the bmapi() call to allocate the blocks
454 XFS_BMAP_INIT(&free_list
, &firstfsb
);
456 error
= xfs_bmapi(tp
, ip
, offset_fsb
, count_fsb
,
457 bmapi_flag
, &firstfsb
, 0, &imap
, &nimaps
, &free_list
);
462 * Complete the transaction
464 error
= xfs_bmap_finish(&tp
, &free_list
, firstfsb
, &committed
);
467 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
, NULL
);
472 * Copy any maps to caller's array and return any error.
481 if ( !(io
->io_flags
& XFS_IOCORE_RT
) && !ret_imap
->br_startblock
) {
482 cmn_err(CE_PANIC
,"Access to block zero: fs <%s> inode: %lld "
483 "start_block : %llx start_off : %llx blkcnt : %llx "
484 "extent-state : %x \n",
485 (ip
->i_mount
)->m_fsname
,
486 (long long)ip
->i_ino
,
487 ret_imap
->br_startblock
, ret_imap
->br_startoff
,
488 ret_imap
->br_blockcount
,ret_imap
->br_state
);
492 error0
: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
493 xfs_bmap_cancel(&free_list
);
494 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp
, tp
, ip
, qblocks
, 0, quota_flag
);
496 error1
: /* Just cancel transaction */
497 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
498 *nmaps
= 0; /* nothing set-up here */
501 return XFS_ERROR(error
);
505 xfs_iomap_write_delay(
510 xfs_bmbt_irec_t
*ret_imap
,
513 xfs_mount_t
*mp
= ip
->i_mount
;
514 xfs_iocore_t
*io
= &ip
->i_iocore
;
515 xfs_fileoff_t offset_fsb
;
516 xfs_fileoff_t last_fsb
;
518 xfs_fsblock_t firstblock
;
521 xfs_bmbt_irec_t imap
[XFS_WRITE_IMAPS
];
525 ASSERT(ismrlocked(&ip
->i_lock
, MR_UPDATE
) != 0);
528 * Make sure that the dquots are there. This doesn't hold
529 * the ilock across a disk read.
532 error
= XFS_QM_DQATTACH(mp
, ip
, XFS_QMOPT_ILOCKED
);
534 return XFS_ERROR(error
);
537 isize
= ip
->i_d
.di_size
;
538 if (io
->io_new_size
> isize
) {
539 isize
= io
->io_new_size
;
543 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
544 last_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)(offset
+ count
)));
546 * If the caller is doing a write at the end of the file,
547 * then extend the allocation (and the buffer used for the write)
548 * out to the file system's write iosize. We clean up any extra
549 * space left over when the file is closed in xfs_inactive().
551 * For sync writes, we are flushing delayed allocate space to
552 * try to make additional space available for allocation near
553 * the filesystem full boundary - preallocation hurts in that
554 * situation, of course.
556 if (!(ioflag
& BMAPI_SYNC
) && ((offset
+ count
) > ip
->i_d
.di_size
)) {
557 xfs_off_t aligned_offset
;
558 xfs_filblks_t count_fsb
;
560 xfs_fileoff_t ioalign
;
562 xfs_fileoff_t start_fsb
;
565 * If there are any real blocks past eof, then don't
566 * do any speculative allocation.
568 start_fsb
= XFS_B_TO_FSBT(mp
,
569 ((xfs_ufsize_t
)(offset
+ count
- 1)));
570 count_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)XFS_MAXIOFFSET(mp
));
571 while (count_fsb
> 0) {
572 nimaps
= XFS_WRITE_IMAPS
;
573 error
= XFS_BMAPI(mp
, NULL
, io
, start_fsb
, count_fsb
,
574 0, &firstblock
, 0, imap
, &nimaps
, NULL
);
578 for (n
= 0; n
< nimaps
; n
++) {
579 if ( !(io
->io_flags
& XFS_IOCORE_RT
) &&
580 !imap
[n
].br_startblock
) {
581 cmn_err(CE_PANIC
,"Access to block "
582 "zero: fs <%s> inode: %lld "
583 "start_block : %llx start_off "
584 ": %llx blkcnt : %llx "
585 "extent-state : %x \n",
586 (ip
->i_mount
)->m_fsname
,
587 (long long)ip
->i_ino
,
588 imap
[n
].br_startblock
,
590 imap
[n
].br_blockcount
,
593 if ((imap
[n
].br_startblock
!= HOLESTARTBLOCK
) &&
594 (imap
[n
].br_startblock
!= DELAYSTARTBLOCK
)) {
597 start_fsb
+= imap
[n
].br_blockcount
;
598 count_fsb
-= imap
[n
].br_blockcount
;
601 iosize
= mp
->m_writeio_blocks
;
602 aligned_offset
= XFS_WRITEIO_ALIGN(mp
, (offset
+ count
- 1));
603 ioalign
= XFS_B_TO_FSBT(mp
, aligned_offset
);
604 last_fsb
= ioalign
+ iosize
;
608 nimaps
= XFS_WRITE_IMAPS
;
609 firstblock
= NULLFSBLOCK
;
612 * If mounted with the "-o swalloc" option, roundup the allocation
613 * request to a stripe width boundary if the file size is >=
614 * stripe width and we are allocating past the allocation eof.
616 if (!(io
->io_flags
& XFS_IOCORE_RT
) && mp
->m_swidth
617 && (mp
->m_flags
& XFS_MOUNT_SWALLOC
)
618 && (isize
>= XFS_FSB_TO_B(mp
, mp
->m_swidth
)) && aeof
) {
620 xfs_fileoff_t new_last_fsb
;
622 new_last_fsb
= roundup_64(last_fsb
, mp
->m_swidth
);
623 error
= xfs_bmap_eof(ip
, new_last_fsb
, XFS_DATA_FORK
, &eof
);
628 last_fsb
= new_last_fsb
;
631 * Roundup the allocation request to a stripe unit (m_dalign) boundary
632 * if the file size is >= stripe unit size, and we are allocating past
633 * the allocation eof.
635 } else if (!(io
->io_flags
& XFS_IOCORE_RT
) && mp
->m_dalign
&&
636 (isize
>= XFS_FSB_TO_B(mp
, mp
->m_dalign
)) && aeof
) {
638 xfs_fileoff_t new_last_fsb
;
639 new_last_fsb
= roundup_64(last_fsb
, mp
->m_dalign
);
640 error
= xfs_bmap_eof(ip
, new_last_fsb
, XFS_DATA_FORK
, &eof
);
645 last_fsb
= new_last_fsb
;
648 * Round up the allocation request to a real-time extent boundary
649 * if the file is on the real-time subvolume.
651 } else if (io
->io_flags
& XFS_IOCORE_RT
&& aeof
) {
653 xfs_fileoff_t new_last_fsb
;
655 new_last_fsb
= roundup_64(last_fsb
, mp
->m_sb
.sb_rextsize
);
656 error
= XFS_BMAP_EOF(mp
, io
, new_last_fsb
, XFS_DATA_FORK
, &eof
);
661 last_fsb
= new_last_fsb
;
663 error
= xfs_bmapi(NULL
, ip
, offset_fsb
,
664 (xfs_filblks_t
)(last_fsb
- offset_fsb
),
665 XFS_BMAPI_DELAY
| XFS_BMAPI_WRITE
|
666 XFS_BMAPI_ENTIRE
, &firstblock
, 1, imap
,
669 * This can be EDQUOT, if nimaps == 0
671 if (error
&& (error
!= ENOSPC
)) {
672 return XFS_ERROR(error
);
675 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
676 * then we must have run out of space.
679 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE
,
681 if (xfs_flush_space(ip
, &fsynced
, &ioflag
))
682 return XFS_ERROR(ENOSPC
);
690 if ( !(io
->io_flags
& XFS_IOCORE_RT
) && !ret_imap
->br_startblock
) {
691 cmn_err(CE_PANIC
,"Access to block zero: fs <%s> inode: %lld "
692 "start_block : %llx start_off : %llx blkcnt : %llx "
693 "extent-state : %x \n",
694 (ip
->i_mount
)->m_fsname
,
695 (long long)ip
->i_ino
,
696 ret_imap
->br_startblock
, ret_imap
->br_startoff
,
697 ret_imap
->br_blockcount
,ret_imap
->br_state
);
703 * Pass in a delayed allocate extent, convert it to real extents;
704 * return to the caller the extent we create which maps on top of
705 * the originating callers request.
707 * Called without a lock on the inode.
710 xfs_iomap_write_allocate(
714 xfs_bmbt_irec_t
*map
,
717 xfs_mount_t
*mp
= ip
->i_mount
;
718 xfs_iocore_t
*io
= &ip
->i_iocore
;
719 xfs_fileoff_t offset_fsb
, last_block
;
720 xfs_fileoff_t end_fsb
, map_start_fsb
;
721 xfs_fsblock_t first_block
;
722 xfs_bmap_free_t free_list
;
723 xfs_filblks_t count_fsb
;
724 xfs_bmbt_irec_t imap
[XFS_STRAT_WRITE_IMAPS
];
726 int i
, nimaps
, committed
;
733 * Make sure that the dquots are there.
735 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
736 return XFS_ERROR(error
);
738 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
739 count_fsb
= map
->br_blockcount
;
740 map_start_fsb
= map
->br_startoff
;
742 XFS_STATS_ADD(xs_xstrat_bytes
, XFS_FSB_TO_B(mp
, count_fsb
));
744 while (count_fsb
!= 0) {
746 * Set up a transaction with which to allocate the
747 * backing store for the file. Do allocations in a
748 * loop until we get some space in the range we are
749 * interested in. The other space that might be allocated
750 * is in the delayed allocation extent on which we sit
751 * but before our buffer starts.
755 while (nimaps
== 0) {
756 tp
= xfs_trans_alloc(mp
, XFS_TRANS_STRAT_WRITE
);
757 nres
= XFS_EXTENTADD_SPACE_RES(mp
, XFS_DATA_FORK
);
758 error
= xfs_trans_reserve(tp
, nres
,
759 XFS_WRITE_LOG_RES(mp
),
760 0, XFS_TRANS_PERM_LOG_RES
,
761 XFS_WRITE_LOG_COUNT
);
762 if (error
== ENOSPC
) {
763 error
= xfs_trans_reserve(tp
, 0,
764 XFS_WRITE_LOG_RES(mp
),
766 XFS_TRANS_PERM_LOG_RES
,
767 XFS_WRITE_LOG_COUNT
);
770 xfs_trans_cancel(tp
, 0);
771 return XFS_ERROR(error
);
773 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
774 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
775 xfs_trans_ihold(tp
, ip
);
777 XFS_BMAP_INIT(&free_list
, &first_block
);
779 nimaps
= XFS_STRAT_WRITE_IMAPS
;
781 * Ensure we don't go beyond eof - it is possible
782 * the extents changed since we did the read call,
783 * we dropped the ilock in the interim.
786 end_fsb
= XFS_B_TO_FSB(mp
, ip
->i_d
.di_size
);
787 xfs_bmap_last_offset(NULL
, ip
, &last_block
,
789 last_block
= XFS_FILEOFF_MAX(last_block
, end_fsb
);
790 if ((map_start_fsb
+ count_fsb
) > last_block
) {
791 count_fsb
= last_block
- map_start_fsb
;
792 if (count_fsb
== 0) {
798 /* Go get the actual blocks */
799 error
= xfs_bmapi(tp
, ip
, map_start_fsb
, count_fsb
,
800 XFS_BMAPI_WRITE
, &first_block
, 1,
801 imap
, &nimaps
, &free_list
);
805 error
= xfs_bmap_finish(&tp
, &free_list
,
806 first_block
, &committed
);
810 error
= xfs_trans_commit(tp
,
811 XFS_TRANS_RELEASE_LOG_RES
, NULL
);
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
823 for (i
= 0; i
< nimaps
; i
++) {
824 if ( !(io
->io_flags
& XFS_IOCORE_RT
) &&
825 !imap
[i
].br_startblock
) {
826 cmn_err(CE_PANIC
,"Access to block zero: "
827 "fs <%s> inode: %lld "
828 "start_block : %llx start_off : %llx "
829 "blkcnt : %llx extent-state : %x \n",
830 (ip
->i_mount
)->m_fsname
,
831 (long long)ip
->i_ino
,
832 imap
[i
].br_startblock
,
834 imap
[i
].br_blockcount
,imap
[i
].br_state
);
836 if ((offset_fsb
>= imap
[i
].br_startoff
) &&
837 (offset_fsb
< (imap
[i
].br_startoff
+
838 imap
[i
].br_blockcount
))) {
841 XFS_STATS_INC(xs_xstrat_quick
);
844 count_fsb
-= imap
[i
].br_blockcount
;
847 /* So far we have not mapped the requested part of the
848 * file, just surrounding data, try again.
851 map_start_fsb
= imap
[nimaps
].br_startoff
+
852 imap
[nimaps
].br_blockcount
;
856 xfs_bmap_cancel(&free_list
);
857 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
859 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
860 return XFS_ERROR(error
);
864 xfs_iomap_write_unwritten(
869 xfs_mount_t
*mp
= ip
->i_mount
;
870 xfs_iocore_t
*io
= &ip
->i_iocore
;
872 xfs_fileoff_t offset_fsb
;
873 xfs_filblks_t count_fsb
;
874 xfs_filblks_t numblks_fsb
;
875 xfs_bmbt_irec_t imap
;
880 xfs_fsblock_t firstfsb
;
881 xfs_bmap_free_t free_list
;
883 xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN
,
884 &ip
->i_iocore
, offset
, count
);
886 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
887 count_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)offset
+ count
);
888 count_fsb
= (xfs_filblks_t
)(count_fsb
- offset_fsb
);
891 nres
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
894 * set up a transaction to convert the range of extents
895 * from unwritten to real. Do allocations in a loop until
896 * we have covered the range passed in.
899 tp
= xfs_trans_alloc(mp
, XFS_TRANS_STRAT_WRITE
);
900 error
= xfs_trans_reserve(tp
, nres
,
901 XFS_WRITE_LOG_RES(mp
), 0,
902 XFS_TRANS_PERM_LOG_RES
,
903 XFS_WRITE_LOG_COUNT
);
905 xfs_trans_cancel(tp
, 0);
909 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
910 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
911 xfs_trans_ihold(tp
, ip
);
914 * Modify the unwritten extent state of the buffer.
916 XFS_BMAP_INIT(&free_list
, &firstfsb
);
918 error
= xfs_bmapi(tp
, ip
, offset_fsb
, count_fsb
,
919 XFS_BMAPI_WRITE
, &firstfsb
,
920 1, &imap
, &nimaps
, &free_list
);
922 goto error_on_bmapi_transaction
;
924 error
= xfs_bmap_finish(&(tp
), &(free_list
),
925 firstfsb
, &committed
);
927 goto error_on_bmapi_transaction
;
929 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
, NULL
);
930 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
934 if ( !(io
->io_flags
& XFS_IOCORE_RT
) && !imap
.br_startblock
) {
935 cmn_err(CE_PANIC
,"Access to block zero: fs <%s> "
936 "inode: %lld start_block : %llx start_off : "
937 "%llx blkcnt : %llx extent-state : %x \n",
938 (ip
->i_mount
)->m_fsname
,
939 (long long)ip
->i_ino
,
940 imap
.br_startblock
,imap
.br_startoff
,
941 imap
.br_blockcount
,imap
.br_state
);
944 if ((numblks_fsb
= imap
.br_blockcount
) == 0) {
946 * The numblks_fsb value should always get
947 * smaller, otherwise the loop is stuck.
949 ASSERT(imap
.br_blockcount
);
952 offset_fsb
+= numblks_fsb
;
953 count_fsb
-= numblks_fsb
;
954 } while (count_fsb
> 0);
958 error_on_bmapi_transaction
:
959 xfs_bmap_cancel(&free_list
);
960 xfs_trans_cancel(tp
, (XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
));
961 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
963 return XFS_ERROR(error
);