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"
43 #include "xfs_rtalloc.h"
44 #include "xfs_error.h"
45 #include "xfs_itable.h"
51 #include "xfs_buf_item.h"
52 #include "xfs_trans_space.h"
53 #include "xfs_utils.h"
54 #include "xfs_iomap.h"
56 #if defined(XFS_RW_TRACE)
58 xfs_iomap_enter_trace(
64 xfs_inode_t
*ip
= XFS_IO_INODE(io
);
69 ktrace_enter(ip
->i_rwtrace
,
70 (void *)((unsigned long)tag
),
72 (void *)((unsigned long)((ip
->i_d
.di_size
>> 32) & 0xffffffff)),
73 (void *)((unsigned long)(ip
->i_d
.di_size
& 0xffffffff)),
74 (void *)((unsigned long)((offset
>> 32) & 0xffffffff)),
75 (void *)((unsigned long)(offset
& 0xffffffff)),
76 (void *)((unsigned long)count
),
77 (void *)((unsigned long)((io
->io_new_size
>> 32) & 0xffffffff)),
78 (void *)((unsigned long)(io
->io_new_size
& 0xffffffff)),
95 xfs_bmbt_irec_t
*imapp
,
98 xfs_inode_t
*ip
= XFS_IO_INODE(io
);
103 ktrace_enter(ip
->i_rwtrace
,
104 (void *)((unsigned long)tag
),
106 (void *)((unsigned long)((ip
->i_d
.di_size
>> 32) & 0xffffffff)),
107 (void *)((unsigned long)(ip
->i_d
.di_size
& 0xffffffff)),
108 (void *)((unsigned long)((offset
>> 32) & 0xffffffff)),
109 (void *)((unsigned long)(offset
& 0xffffffff)),
110 (void *)((unsigned long)count
),
111 (void *)((unsigned long)flags
),
112 (void *)((unsigned long)((iomapp
->iomap_offset
>> 32) & 0xffffffff)),
113 (void *)((unsigned long)(iomapp
->iomap_offset
& 0xffffffff)),
114 (void *)((unsigned long)(iomapp
->iomap_delta
)),
115 (void *)((unsigned long)(iomapp
->iomap_bsize
)),
116 (void *)((unsigned long)(iomapp
->iomap_bn
)),
117 (void *)(__psint_t
)(imapp
->br_startoff
),
118 (void *)((unsigned long)(imapp
->br_blockcount
)),
119 (void *)(__psint_t
)(imapp
->br_startblock
));
122 #define xfs_iomap_enter_trace(tag, io, offset, count)
123 #define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags)
126 #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
127 << mp->m_writeio_log)
128 #define XFS_STRAT_WRITE_IMAPS 2
129 #define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
135 xfs_bmbt_irec_t
*imap
,
137 int imaps
, /* Number of imap entries */
138 int iomaps
, /* Number of iomap entries */
144 xfs_fsblock_t start_block
;
147 nisize
= XFS_SIZE(mp
, io
);
148 if (io
->io_new_size
> nisize
)
149 nisize
= io
->io_new_size
;
151 for (pbm
= 0; imaps
&& pbm
< iomaps
; imaps
--, iomapp
++, imap
++, pbm
++) {
152 iomapp
->iomap_offset
= XFS_FSB_TO_B(mp
, imap
->br_startoff
);
153 iomapp
->iomap_delta
= offset
- iomapp
->iomap_offset
;
154 iomapp
->iomap_bsize
= XFS_FSB_TO_B(mp
, imap
->br_blockcount
);
155 iomapp
->iomap_flags
= flags
;
157 if (io
->io_flags
& XFS_IOCORE_RT
) {
158 iomapp
->iomap_flags
|= IOMAP_REALTIME
;
159 iomapp
->iomap_target
= mp
->m_rtdev_targp
;
161 iomapp
->iomap_target
= mp
->m_ddev_targp
;
163 start_block
= imap
->br_startblock
;
164 if (start_block
== HOLESTARTBLOCK
) {
165 iomapp
->iomap_bn
= IOMAP_DADDR_NULL
;
166 iomapp
->iomap_flags
|= IOMAP_HOLE
;
167 } else if (start_block
== DELAYSTARTBLOCK
) {
168 iomapp
->iomap_bn
= IOMAP_DADDR_NULL
;
169 iomapp
->iomap_flags
|= IOMAP_DELAY
;
171 iomapp
->iomap_bn
= XFS_FSB_TO_DB_IO(io
, start_block
);
172 if (ISUNWRITTEN(imap
))
173 iomapp
->iomap_flags
|= IOMAP_UNWRITTEN
;
176 if ((iomapp
->iomap_offset
+ iomapp
->iomap_bsize
) >= nisize
) {
177 iomapp
->iomap_flags
|= IOMAP_EOF
;
180 offset
+= iomapp
->iomap_bsize
- iomapp
->iomap_delta
;
182 return pbm
; /* Return the number filled */
194 xfs_mount_t
*mp
= io
->io_mount
;
195 xfs_fileoff_t offset_fsb
, end_fsb
;
198 xfs_bmbt_irec_t imap
;
203 if (XFS_FORCED_SHUTDOWN(mp
))
204 return XFS_ERROR(EIO
);
207 (BMAPI_READ
| BMAPI_WRITE
| BMAPI_ALLOCATE
|
208 BMAPI_UNWRITTEN
| BMAPI_DEVICE
)) {
210 xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER
, io
, offset
, count
);
211 lockmode
= XFS_LCK_MAP_SHARED(mp
, io
);
212 bmapi_flags
= XFS_BMAPI_ENTIRE
;
215 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER
, io
, offset
, count
);
216 lockmode
= XFS_ILOCK_EXCL
|XFS_EXTSIZE_WR
;
217 if (flags
& BMAPI_IGNSTATE
)
218 bmapi_flags
|= XFS_BMAPI_IGSTATE
|XFS_BMAPI_ENTIRE
;
219 XFS_ILOCK(mp
, io
, lockmode
);
222 xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER
, io
, offset
, count
);
223 lockmode
= XFS_ILOCK_SHARED
|XFS_EXTSIZE_RD
;
224 bmapi_flags
= XFS_BMAPI_ENTIRE
;
225 /* Attempt non-blocking lock */
226 if (flags
& BMAPI_TRYLOCK
) {
227 if (!XFS_ILOCK_NOWAIT(mp
, io
, lockmode
))
228 return XFS_ERROR(EAGAIN
);
230 XFS_ILOCK(mp
, io
, lockmode
);
233 case BMAPI_UNWRITTEN
:
236 lockmode
= XFS_LCK_MAP_SHARED(mp
, io
);
237 iomapp
->iomap_target
= io
->io_flags
& XFS_IOCORE_RT
?
238 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
246 ASSERT(offset
<= mp
->m_maxioffset
);
247 if ((xfs_fsize_t
)offset
+ count
> mp
->m_maxioffset
)
248 count
= mp
->m_maxioffset
- offset
;
249 end_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)offset
+ count
);
250 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
252 error
= XFS_BMAPI(mp
, NULL
, io
, offset_fsb
,
253 (xfs_filblks_t
)(end_fsb
- offset_fsb
),
254 bmapi_flags
, NULL
, 0, &imap
,
261 switch (flags
& (BMAPI_WRITE
|BMAPI_ALLOCATE
|BMAPI_UNWRITTEN
)) {
263 /* If we found an extent, return it */
265 (imap
.br_startblock
!= HOLESTARTBLOCK
) &&
266 (imap
.br_startblock
!= DELAYSTARTBLOCK
)) {
267 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP
, io
,
268 offset
, count
, iomapp
, &imap
, flags
);
272 if (flags
& (BMAPI_DIRECT
|BMAPI_MMAP
)) {
273 error
= XFS_IOMAP_WRITE_DIRECT(mp
, io
, offset
,
274 count
, flags
, &imap
, &nimaps
, nimaps
);
276 error
= XFS_IOMAP_WRITE_DELAY(mp
, io
, offset
, count
,
277 flags
, &imap
, &nimaps
);
280 xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP
, io
,
281 offset
, count
, iomapp
, &imap
, flags
);
283 iomap_flags
= IOMAP_NEW
;
286 /* If we found an extent, return it */
287 XFS_IUNLOCK(mp
, io
, lockmode
);
290 if (nimaps
&& !ISNULLSTARTBLOCK(imap
.br_startblock
)) {
291 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP
, io
,
292 offset
, count
, iomapp
, &imap
, flags
);
296 error
= XFS_IOMAP_WRITE_ALLOCATE(mp
, io
, offset
, count
,
299 case BMAPI_UNWRITTEN
:
301 error
= XFS_IOMAP_WRITE_UNWRITTEN(mp
, io
, offset
, count
);
307 *niomaps
= xfs_imap_to_bmap(io
, offset
, &imap
,
308 iomapp
, nimaps
, *niomaps
, iomap_flags
);
309 } else if (niomaps
) {
315 XFS_IUNLOCK(mp
, io
, lockmode
);
316 return XFS_ERROR(error
);
327 if (ip
->i_delayed_blks
) {
328 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
330 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
333 *ioflags
|= BMAPI_SYNC
;
339 *ioflags
|= BMAPI_SYNC
;
342 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
343 xfs_flush_device(ip
);
344 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
352 xfs_iomap_write_direct(
357 xfs_bmbt_irec_t
*ret_imap
,
361 xfs_mount_t
*mp
= ip
->i_mount
;
362 xfs_iocore_t
*io
= &ip
->i_iocore
;
363 xfs_fileoff_t offset_fsb
;
364 xfs_fileoff_t last_fsb
;
365 xfs_filblks_t count_fsb
;
366 xfs_fsblock_t firstfsb
;
373 xfs_bmbt_irec_t imap
;
374 xfs_bmap_free_t free_list
;
375 xfs_filblks_t qblocks
, resblks
;
380 * Make sure that the dquots are there. This doesn't hold
381 * the ilock across a disk read.
383 error
= XFS_QM_DQATTACH(ip
->i_mount
, ip
, XFS_QMOPT_ILOCKED
);
385 return XFS_ERROR(error
);
387 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
388 last_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)(offset
+ count
)));
389 count_fsb
= last_fsb
- offset_fsb
;
390 if (found
&& (ret_imap
->br_startblock
== HOLESTARTBLOCK
)) {
391 xfs_fileoff_t map_last_fsb
;
393 map_last_fsb
= ret_imap
->br_blockcount
+ ret_imap
->br_startoff
;
394 if (map_last_fsb
< last_fsb
) {
395 last_fsb
= map_last_fsb
;
396 count_fsb
= last_fsb
- offset_fsb
;
398 ASSERT(count_fsb
> 0);
402 * Determine if reserving space on the data or realtime partition.
404 if ((rt
= XFS_IS_REALTIME_INODE(ip
))) {
407 if (!(extsz
= ip
->i_d
.di_extsize
))
408 extsz
= mp
->m_sb
.sb_rextsize
;
409 resrtextents
= qblocks
= (count_fsb
+ extsz
- 1);
410 do_div(resrtextents
, mp
->m_sb
.sb_rextsize
);
411 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
412 quota_flag
= XFS_QMOPT_RES_RTBLKS
;
415 resblks
= qblocks
= XFS_DIOSTRAT_SPACE_RES(mp
, count_fsb
);
416 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
420 * Allocate and setup the transaction
422 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
423 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
424 error
= xfs_trans_reserve(tp
, resblks
,
425 XFS_WRITE_LOG_RES(mp
), resrtextents
,
426 XFS_TRANS_PERM_LOG_RES
,
427 XFS_WRITE_LOG_COUNT
);
430 * Check for running out of space, note: need lock to return
433 xfs_trans_cancel(tp
, 0);
434 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
438 if (XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
, tp
, ip
, qblocks
, 0, quota_flag
)) {
443 bmapi_flag
= XFS_BMAPI_WRITE
;
444 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
445 xfs_trans_ihold(tp
, ip
);
447 if (!(flags
& BMAPI_MMAP
) && (offset
< ip
->i_d
.di_size
|| rt
))
448 bmapi_flag
|= XFS_BMAPI_PREALLOC
;
451 * Issue the bmapi() call to allocate the blocks
453 XFS_BMAP_INIT(&free_list
, &firstfsb
);
455 error
= xfs_bmapi(tp
, ip
, offset_fsb
, count_fsb
,
456 bmapi_flag
, &firstfsb
, 0, &imap
, &nimaps
, &free_list
);
461 * Complete the transaction
463 error
= xfs_bmap_finish(&tp
, &free_list
, firstfsb
, &committed
);
466 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
, NULL
);
471 * Copy any maps to caller's array and return any error.
480 if ( !(io
->io_flags
& XFS_IOCORE_RT
) && !ret_imap
->br_startblock
) {
481 cmn_err(CE_PANIC
,"Access to block zero: fs <%s> inode: %lld "
482 "start_block : %llx start_off : %llx blkcnt : %llx "
483 "extent-state : %x \n",
484 (ip
->i_mount
)->m_fsname
,
485 (long long)ip
->i_ino
,
486 ret_imap
->br_startblock
, ret_imap
->br_startoff
,
487 ret_imap
->br_blockcount
,ret_imap
->br_state
);
491 error0
: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
492 xfs_bmap_cancel(&free_list
);
493 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp
, tp
, ip
, qblocks
, 0, quota_flag
);
495 error1
: /* Just cancel transaction */
496 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
497 *nmaps
= 0; /* nothing set-up here */
500 return XFS_ERROR(error
);
504 xfs_iomap_write_delay(
509 xfs_bmbt_irec_t
*ret_imap
,
512 xfs_mount_t
*mp
= ip
->i_mount
;
513 xfs_iocore_t
*io
= &ip
->i_iocore
;
514 xfs_fileoff_t offset_fsb
;
515 xfs_fileoff_t last_fsb
;
517 xfs_fsblock_t firstblock
;
520 xfs_bmbt_irec_t imap
[XFS_WRITE_IMAPS
];
524 ASSERT(ismrlocked(&ip
->i_lock
, MR_UPDATE
) != 0);
527 * Make sure that the dquots are there. This doesn't hold
528 * the ilock across a disk read.
531 error
= XFS_QM_DQATTACH(mp
, ip
, XFS_QMOPT_ILOCKED
);
533 return XFS_ERROR(error
);
536 isize
= ip
->i_d
.di_size
;
537 if (io
->io_new_size
> isize
) {
538 isize
= io
->io_new_size
;
542 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
543 last_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)(offset
+ count
)));
545 * If the caller is doing a write at the end of the file,
546 * then extend the allocation (and the buffer used for the write)
547 * out to the file system's write iosize. We clean up any extra
548 * space left over when the file is closed in xfs_inactive().
550 * For sync writes, we are flushing delayed allocate space to
551 * try to make additional space available for allocation near
552 * the filesystem full boundary - preallocation hurts in that
553 * situation, of course.
555 if (!(ioflag
& BMAPI_SYNC
) && ((offset
+ count
) > ip
->i_d
.di_size
)) {
556 xfs_off_t aligned_offset
;
557 xfs_filblks_t count_fsb
;
559 xfs_fileoff_t ioalign
;
561 xfs_fileoff_t start_fsb
;
564 * If there are any real blocks past eof, then don't
565 * do any speculative allocation.
567 start_fsb
= XFS_B_TO_FSBT(mp
,
568 ((xfs_ufsize_t
)(offset
+ count
- 1)));
569 count_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)XFS_MAXIOFFSET(mp
));
570 while (count_fsb
> 0) {
571 nimaps
= XFS_WRITE_IMAPS
;
572 error
= XFS_BMAPI(mp
, NULL
, io
, start_fsb
, count_fsb
,
573 0, &firstblock
, 0, imap
, &nimaps
, NULL
);
577 for (n
= 0; n
< nimaps
; n
++) {
578 if ( !(io
->io_flags
& XFS_IOCORE_RT
) &&
579 !imap
[n
].br_startblock
) {
580 cmn_err(CE_PANIC
,"Access to block "
581 "zero: fs <%s> inode: %lld "
582 "start_block : %llx start_off "
583 ": %llx blkcnt : %llx "
584 "extent-state : %x \n",
585 (ip
->i_mount
)->m_fsname
,
586 (long long)ip
->i_ino
,
587 imap
[n
].br_startblock
,
589 imap
[n
].br_blockcount
,
592 if ((imap
[n
].br_startblock
!= HOLESTARTBLOCK
) &&
593 (imap
[n
].br_startblock
!= DELAYSTARTBLOCK
)) {
596 start_fsb
+= imap
[n
].br_blockcount
;
597 count_fsb
-= imap
[n
].br_blockcount
;
600 iosize
= mp
->m_writeio_blocks
;
601 aligned_offset
= XFS_WRITEIO_ALIGN(mp
, (offset
+ count
- 1));
602 ioalign
= XFS_B_TO_FSBT(mp
, aligned_offset
);
603 last_fsb
= ioalign
+ iosize
;
607 nimaps
= XFS_WRITE_IMAPS
;
608 firstblock
= NULLFSBLOCK
;
611 * If mounted with the "-o swalloc" option, roundup the allocation
612 * request to a stripe width boundary if the file size is >=
613 * stripe width and we are allocating past the allocation eof.
615 if (!(io
->io_flags
& XFS_IOCORE_RT
) && mp
->m_swidth
616 && (mp
->m_flags
& XFS_MOUNT_SWALLOC
)
617 && (isize
>= XFS_FSB_TO_B(mp
, mp
->m_swidth
)) && aeof
) {
619 xfs_fileoff_t new_last_fsb
;
621 new_last_fsb
= roundup_64(last_fsb
, mp
->m_swidth
);
622 error
= xfs_bmap_eof(ip
, new_last_fsb
, XFS_DATA_FORK
, &eof
);
627 last_fsb
= new_last_fsb
;
630 * Roundup the allocation request to a stripe unit (m_dalign) boundary
631 * if the file size is >= stripe unit size, and we are allocating past
632 * the allocation eof.
634 } else if (!(io
->io_flags
& XFS_IOCORE_RT
) && mp
->m_dalign
&&
635 (isize
>= XFS_FSB_TO_B(mp
, mp
->m_dalign
)) && aeof
) {
637 xfs_fileoff_t new_last_fsb
;
638 new_last_fsb
= roundup_64(last_fsb
, mp
->m_dalign
);
639 error
= xfs_bmap_eof(ip
, new_last_fsb
, XFS_DATA_FORK
, &eof
);
644 last_fsb
= new_last_fsb
;
647 * Round up the allocation request to a real-time extent boundary
648 * if the file is on the real-time subvolume.
650 } else if (io
->io_flags
& XFS_IOCORE_RT
&& aeof
) {
652 xfs_fileoff_t new_last_fsb
;
654 new_last_fsb
= roundup_64(last_fsb
, mp
->m_sb
.sb_rextsize
);
655 error
= XFS_BMAP_EOF(mp
, io
, new_last_fsb
, XFS_DATA_FORK
, &eof
);
660 last_fsb
= new_last_fsb
;
662 error
= xfs_bmapi(NULL
, ip
, offset_fsb
,
663 (xfs_filblks_t
)(last_fsb
- offset_fsb
),
664 XFS_BMAPI_DELAY
| XFS_BMAPI_WRITE
|
665 XFS_BMAPI_ENTIRE
, &firstblock
, 1, imap
,
668 * This can be EDQUOT, if nimaps == 0
670 if (error
&& (error
!= ENOSPC
)) {
671 return XFS_ERROR(error
);
674 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
675 * then we must have run out of space.
678 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE
,
680 if (xfs_flush_space(ip
, &fsynced
, &ioflag
))
681 return XFS_ERROR(ENOSPC
);
689 if ( !(io
->io_flags
& XFS_IOCORE_RT
) && !ret_imap
->br_startblock
) {
690 cmn_err(CE_PANIC
,"Access to block zero: fs <%s> inode: %lld "
691 "start_block : %llx start_off : %llx blkcnt : %llx "
692 "extent-state : %x \n",
693 (ip
->i_mount
)->m_fsname
,
694 (long long)ip
->i_ino
,
695 ret_imap
->br_startblock
, ret_imap
->br_startoff
,
696 ret_imap
->br_blockcount
,ret_imap
->br_state
);
702 * Pass in a delayed allocate extent, convert it to real extents;
703 * return to the caller the extent we create which maps on top of
704 * the originating callers request.
706 * Called without a lock on the inode.
709 xfs_iomap_write_allocate(
713 xfs_bmbt_irec_t
*map
,
716 xfs_mount_t
*mp
= ip
->i_mount
;
717 xfs_iocore_t
*io
= &ip
->i_iocore
;
718 xfs_fileoff_t offset_fsb
, last_block
;
719 xfs_fileoff_t end_fsb
, map_start_fsb
;
720 xfs_fsblock_t first_block
;
721 xfs_bmap_free_t free_list
;
722 xfs_filblks_t count_fsb
;
723 xfs_bmbt_irec_t imap
[XFS_STRAT_WRITE_IMAPS
];
725 int i
, nimaps
, committed
;
732 * Make sure that the dquots are there.
734 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
735 return XFS_ERROR(error
);
737 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
738 count_fsb
= map
->br_blockcount
;
739 map_start_fsb
= map
->br_startoff
;
741 XFS_STATS_ADD(xs_xstrat_bytes
, XFS_FSB_TO_B(mp
, count_fsb
));
743 while (count_fsb
!= 0) {
745 * Set up a transaction with which to allocate the
746 * backing store for the file. Do allocations in a
747 * loop until we get some space in the range we are
748 * interested in. The other space that might be allocated
749 * is in the delayed allocation extent on which we sit
750 * but before our buffer starts.
754 while (nimaps
== 0) {
755 tp
= xfs_trans_alloc(mp
, XFS_TRANS_STRAT_WRITE
);
756 nres
= XFS_EXTENTADD_SPACE_RES(mp
, XFS_DATA_FORK
);
757 error
= xfs_trans_reserve(tp
, nres
,
758 XFS_WRITE_LOG_RES(mp
),
759 0, XFS_TRANS_PERM_LOG_RES
,
760 XFS_WRITE_LOG_COUNT
);
761 if (error
== ENOSPC
) {
762 error
= xfs_trans_reserve(tp
, 0,
763 XFS_WRITE_LOG_RES(mp
),
765 XFS_TRANS_PERM_LOG_RES
,
766 XFS_WRITE_LOG_COUNT
);
769 xfs_trans_cancel(tp
, 0);
770 return XFS_ERROR(error
);
772 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
773 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
774 xfs_trans_ihold(tp
, ip
);
776 XFS_BMAP_INIT(&free_list
, &first_block
);
778 nimaps
= XFS_STRAT_WRITE_IMAPS
;
780 * Ensure we don't go beyond eof - it is possible
781 * the extents changed since we did the read call,
782 * we dropped the ilock in the interim.
785 end_fsb
= XFS_B_TO_FSB(mp
, ip
->i_d
.di_size
);
786 xfs_bmap_last_offset(NULL
, ip
, &last_block
,
788 last_block
= XFS_FILEOFF_MAX(last_block
, end_fsb
);
789 if ((map_start_fsb
+ count_fsb
) > last_block
) {
790 count_fsb
= last_block
- map_start_fsb
;
791 if (count_fsb
== 0) {
797 /* Go get the actual blocks */
798 error
= xfs_bmapi(tp
, ip
, map_start_fsb
, count_fsb
,
799 XFS_BMAPI_WRITE
, &first_block
, 1,
800 imap
, &nimaps
, &free_list
);
804 error
= xfs_bmap_finish(&tp
, &free_list
,
805 first_block
, &committed
);
809 error
= xfs_trans_commit(tp
,
810 XFS_TRANS_RELEASE_LOG_RES
, NULL
);
814 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
818 * See if we were able to allocate an extent that
819 * covers at least part of the callers request
822 for (i
= 0; i
< nimaps
; i
++) {
823 if ( !(io
->io_flags
& XFS_IOCORE_RT
) &&
824 !imap
[i
].br_startblock
) {
825 cmn_err(CE_PANIC
,"Access to block zero: "
826 "fs <%s> inode: %lld "
827 "start_block : %llx start_off : %llx "
828 "blkcnt : %llx extent-state : %x \n",
829 (ip
->i_mount
)->m_fsname
,
830 (long long)ip
->i_ino
,
831 imap
[i
].br_startblock
,
833 imap
[i
].br_blockcount
,imap
[i
].br_state
);
835 if ((offset_fsb
>= imap
[i
].br_startoff
) &&
836 (offset_fsb
< (imap
[i
].br_startoff
+
837 imap
[i
].br_blockcount
))) {
840 XFS_STATS_INC(xs_xstrat_quick
);
843 count_fsb
-= imap
[i
].br_blockcount
;
846 /* So far we have not mapped the requested part of the
847 * file, just surrounding data, try again.
850 map_start_fsb
= imap
[nimaps
].br_startoff
+
851 imap
[nimaps
].br_blockcount
;
855 xfs_bmap_cancel(&free_list
);
856 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
858 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
859 return XFS_ERROR(error
);
863 xfs_iomap_write_unwritten(
868 xfs_mount_t
*mp
= ip
->i_mount
;
869 xfs_iocore_t
*io
= &ip
->i_iocore
;
871 xfs_fileoff_t offset_fsb
;
872 xfs_filblks_t count_fsb
;
873 xfs_filblks_t numblks_fsb
;
874 xfs_bmbt_irec_t imap
;
879 xfs_fsblock_t firstfsb
;
880 xfs_bmap_free_t free_list
;
882 xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN
,
883 &ip
->i_iocore
, offset
, count
);
885 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
886 count_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)offset
+ count
);
887 count_fsb
= (xfs_filblks_t
)(count_fsb
- offset_fsb
);
890 nres
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
893 * set up a transaction to convert the range of extents
894 * from unwritten to real. Do allocations in a loop until
895 * we have covered the range passed in.
898 tp
= xfs_trans_alloc(mp
, XFS_TRANS_STRAT_WRITE
);
899 error
= xfs_trans_reserve(tp
, nres
,
900 XFS_WRITE_LOG_RES(mp
), 0,
901 XFS_TRANS_PERM_LOG_RES
,
902 XFS_WRITE_LOG_COUNT
);
904 xfs_trans_cancel(tp
, 0);
908 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
909 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
910 xfs_trans_ihold(tp
, ip
);
913 * Modify the unwritten extent state of the buffer.
915 XFS_BMAP_INIT(&free_list
, &firstfsb
);
917 error
= xfs_bmapi(tp
, ip
, offset_fsb
, count_fsb
,
918 XFS_BMAPI_WRITE
, &firstfsb
,
919 1, &imap
, &nimaps
, &free_list
);
921 goto error_on_bmapi_transaction
;
923 error
= xfs_bmap_finish(&(tp
), &(free_list
),
924 firstfsb
, &committed
);
926 goto error_on_bmapi_transaction
;
928 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
, NULL
);
929 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
933 if ( !(io
->io_flags
& XFS_IOCORE_RT
) && !imap
.br_startblock
) {
934 cmn_err(CE_PANIC
,"Access to block zero: fs <%s> "
935 "inode: %lld start_block : %llx start_off : "
936 "%llx blkcnt : %llx extent-state : %x \n",
937 (ip
->i_mount
)->m_fsname
,
938 (long long)ip
->i_ino
,
939 imap
.br_startblock
,imap
.br_startoff
,
940 imap
.br_blockcount
,imap
.br_state
);
943 if ((numblks_fsb
= imap
.br_blockcount
) == 0) {
945 * The numblks_fsb value should always get
946 * smaller, otherwise the loop is stuck.
948 ASSERT(imap
.br_blockcount
);
951 offset_fsb
+= numblks_fsb
;
952 count_fsb
-= numblks_fsb
;
953 } while (count_fsb
> 0);
957 error_on_bmapi_transaction
:
958 xfs_bmap_cancel(&free_list
);
959 xfs_trans_cancel(tp
, (XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
));
960 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
962 return XFS_ERROR(error
);