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)),
79 (void *)((unsigned long)current_pid()),
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
);
320 xfs_iomap_eof_align_last_fsb(
324 xfs_extlen_t extsize
,
325 xfs_fileoff_t
*last_fsb
)
327 xfs_fileoff_t new_last_fsb
= 0;
331 if (io
->io_flags
& XFS_IOCORE_RT
)
334 * If mounted with the "-o swalloc" option, roundup the allocation
335 * request to a stripe width boundary if the file size is >=
336 * stripe width and we are allocating past the allocation eof.
338 else if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
) &&
339 (isize
>= XFS_FSB_TO_B(mp
, mp
->m_swidth
)))
340 new_last_fsb
= roundup_64(*last_fsb
, mp
->m_swidth
);
342 * Roundup the allocation request to a stripe unit (m_dalign) boundary
343 * if the file size is >= stripe unit size, and we are allocating past
344 * the allocation eof.
346 else if (mp
->m_dalign
&& (isize
>= XFS_FSB_TO_B(mp
, mp
->m_dalign
)))
347 new_last_fsb
= roundup_64(*last_fsb
, mp
->m_dalign
);
350 * Always round up the allocation request to an extent boundary
351 * (when file on a real-time subvolume or has di_extsize hint).
355 align
= roundup_64(new_last_fsb
, extsize
);
358 new_last_fsb
= roundup_64(*last_fsb
, align
);
362 error
= XFS_BMAP_EOF(mp
, io
, new_last_fsb
, XFS_DATA_FORK
, &eof
);
366 *last_fsb
= new_last_fsb
;
379 if (ip
->i_delayed_blks
) {
380 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
382 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
385 *ioflags
|= BMAPI_SYNC
;
391 *ioflags
|= BMAPI_SYNC
;
394 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
395 xfs_flush_device(ip
);
396 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
404 xfs_iomap_write_direct(
409 xfs_bmbt_irec_t
*ret_imap
,
413 xfs_mount_t
*mp
= ip
->i_mount
;
414 xfs_iocore_t
*io
= &ip
->i_iocore
;
415 xfs_fileoff_t offset_fsb
;
416 xfs_fileoff_t last_fsb
;
417 xfs_filblks_t count_fsb
, resaligned
;
418 xfs_fsblock_t firstfsb
;
419 xfs_extlen_t extsz
, temp
;
426 xfs_bmbt_irec_t imap
;
427 xfs_bmap_free_t free_list
;
428 uint qblocks
, resblks
, resrtextents
;
433 * Make sure that the dquots are there. This doesn't hold
434 * the ilock across a disk read.
436 error
= XFS_QM_DQATTACH(ip
->i_mount
, ip
, XFS_QMOPT_ILOCKED
);
438 return XFS_ERROR(error
);
440 rt
= XFS_IS_REALTIME_INODE(ip
);
442 if (!(extsz
= ip
->i_d
.di_extsize
))
443 extsz
= mp
->m_sb
.sb_rextsize
;
445 extsz
= ip
->i_d
.di_extsize
;
448 isize
= ip
->i_d
.di_size
;
449 if (io
->io_new_size
> isize
)
450 isize
= io
->io_new_size
;
452 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
453 last_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)(offset
+ count
)));
454 if ((offset
+ count
) > isize
) {
455 error
= xfs_iomap_eof_align_last_fsb(mp
, io
, isize
, extsz
,
460 if (found
&& (ret_imap
->br_startblock
== HOLESTARTBLOCK
))
461 last_fsb
= MIN(last_fsb
, (xfs_fileoff_t
)
462 ret_imap
->br_blockcount
+
463 ret_imap
->br_startoff
);
465 count_fsb
= last_fsb
- offset_fsb
;
466 ASSERT(count_fsb
> 0);
468 resaligned
= count_fsb
;
469 if (unlikely(extsz
)) {
470 if ((temp
= do_mod(offset_fsb
, extsz
)))
472 if ((temp
= do_mod(resaligned
, extsz
)))
473 resaligned
+= extsz
- temp
;
477 resrtextents
= qblocks
= resaligned
;
478 resrtextents
/= mp
->m_sb
.sb_rextsize
;
479 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
480 quota_flag
= XFS_QMOPT_RES_RTBLKS
;
483 resblks
= qblocks
= XFS_DIOSTRAT_SPACE_RES(mp
, resaligned
);
484 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
488 * Allocate and setup the transaction
490 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
491 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
492 error
= xfs_trans_reserve(tp
, resblks
,
493 XFS_WRITE_LOG_RES(mp
), resrtextents
,
494 XFS_TRANS_PERM_LOG_RES
,
495 XFS_WRITE_LOG_COUNT
);
497 * Check for running out of space, note: need lock to return
500 xfs_trans_cancel(tp
, 0);
501 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
505 error
= XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
, tp
, ip
,
506 qblocks
, 0, quota_flag
);
510 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
511 xfs_trans_ihold(tp
, ip
);
513 bmapi_flag
= XFS_BMAPI_WRITE
;
514 if ((flags
& BMAPI_DIRECT
) && (offset
< ip
->i_d
.di_size
|| extsz
))
515 bmapi_flag
|= XFS_BMAPI_PREALLOC
;
518 * Issue the xfs_bmapi() call to allocate the blocks
520 XFS_BMAP_INIT(&free_list
, &firstfsb
);
522 error
= xfs_bmapi(tp
, ip
, offset_fsb
, count_fsb
,
523 bmapi_flag
, &firstfsb
, 0, &imap
, &nimaps
, &free_list
);
528 * Complete the transaction
530 error
= xfs_bmap_finish(&tp
, &free_list
, firstfsb
, &committed
);
533 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
, NULL
);
538 * Copy any maps to caller's array and return any error.
547 if ( !(io
->io_flags
& XFS_IOCORE_RT
) && !ret_imap
->br_startblock
) {
548 cmn_err(CE_PANIC
,"Access to block zero: fs <%s> inode: %lld "
549 "start_block : %llx start_off : %llx blkcnt : %llx "
550 "extent-state : %x \n",
551 (ip
->i_mount
)->m_fsname
,
552 (long long)ip
->i_ino
,
553 (unsigned long long)ret_imap
->br_startblock
,
554 (unsigned long long)ret_imap
->br_startoff
,
555 (unsigned long long)ret_imap
->br_blockcount
,
560 error0
: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
561 xfs_bmap_cancel(&free_list
);
562 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp
, tp
, ip
, qblocks
, 0, quota_flag
);
564 error1
: /* Just cancel transaction */
565 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
566 *nmaps
= 0; /* nothing set-up here */
569 return XFS_ERROR(error
);
573 * If the caller is doing a write at the end of the file,
574 * then extend the allocation out to the file system's write
575 * iosize. We clean up any extra space left over when the
576 * file is closed in xfs_inactive().
578 * For sync writes, we are flushing delayed allocate space to
579 * try to make additional space available for allocation near
580 * the filesystem full boundary - preallocation hurts in that
581 * situation, of course.
584 xfs_iomap_eof_want_preallocate(
591 xfs_bmbt_irec_t
*imap
,
595 xfs_fileoff_t start_fsb
;
596 xfs_filblks_t count_fsb
;
597 xfs_fsblock_t firstblock
;
601 if ((ioflag
& BMAPI_SYNC
) || (offset
+ count
) <= isize
)
605 * If there are any real blocks past eof, then don't
606 * do any speculative allocation.
608 start_fsb
= XFS_B_TO_FSBT(mp
, ((xfs_ufsize_t
)(offset
+ count
- 1)));
609 count_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)XFS_MAXIOFFSET(mp
));
610 while (count_fsb
> 0) {
612 firstblock
= NULLFSBLOCK
;
613 error
= XFS_BMAPI(mp
, NULL
, io
, start_fsb
, count_fsb
,
614 0, &firstblock
, 0, imap
, &imaps
, NULL
);
617 for (n
= 0; n
< imaps
; n
++) {
618 if ((imap
[n
].br_startblock
!= HOLESTARTBLOCK
) &&
619 (imap
[n
].br_startblock
!= DELAYSTARTBLOCK
))
621 start_fsb
+= imap
[n
].br_blockcount
;
622 count_fsb
-= imap
[n
].br_blockcount
;
630 xfs_iomap_write_delay(
635 xfs_bmbt_irec_t
*ret_imap
,
638 xfs_mount_t
*mp
= ip
->i_mount
;
639 xfs_iocore_t
*io
= &ip
->i_iocore
;
640 xfs_fileoff_t offset_fsb
;
641 xfs_fileoff_t last_fsb
;
642 xfs_off_t aligned_offset
;
643 xfs_fileoff_t ioalign
;
644 xfs_fsblock_t firstblock
;
648 xfs_bmbt_irec_t imap
[XFS_WRITE_IMAPS
];
649 int prealloc
, fsynced
= 0;
652 ASSERT(ismrlocked(&ip
->i_lock
, MR_UPDATE
) != 0);
655 * Make sure that the dquots are there. This doesn't hold
656 * the ilock across a disk read.
658 error
= XFS_QM_DQATTACH(mp
, ip
, XFS_QMOPT_ILOCKED
);
660 return XFS_ERROR(error
);
662 if (XFS_IS_REALTIME_INODE(ip
)) {
663 if (!(extsz
= ip
->i_d
.di_extsize
))
664 extsz
= mp
->m_sb
.sb_rextsize
;
666 extsz
= ip
->i_d
.di_extsize
;
669 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
672 isize
= ip
->i_d
.di_size
;
673 if (io
->io_new_size
> isize
)
674 isize
= io
->io_new_size
;
676 error
= xfs_iomap_eof_want_preallocate(mp
, io
, isize
, offset
, count
,
677 ioflag
, imap
, XFS_WRITE_IMAPS
, &prealloc
);
682 aligned_offset
= XFS_WRITEIO_ALIGN(mp
, (offset
+ count
- 1));
683 ioalign
= XFS_B_TO_FSBT(mp
, aligned_offset
);
684 last_fsb
= ioalign
+ mp
->m_writeio_blocks
;
686 last_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)(offset
+ count
)));
689 if (prealloc
|| extsz
) {
690 error
= xfs_iomap_eof_align_last_fsb(mp
, io
, isize
, extsz
,
696 nimaps
= XFS_WRITE_IMAPS
;
697 firstblock
= NULLFSBLOCK
;
698 error
= xfs_bmapi(NULL
, ip
, offset_fsb
,
699 (xfs_filblks_t
)(last_fsb
- offset_fsb
),
700 XFS_BMAPI_DELAY
| XFS_BMAPI_WRITE
|
701 XFS_BMAPI_ENTIRE
, &firstblock
, 1, imap
,
703 if (error
&& (error
!= ENOSPC
))
704 return XFS_ERROR(error
);
707 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
708 * then we must have run out of space - flush delalloc, and retry..
711 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE
,
713 if (xfs_flush_space(ip
, &fsynced
, &ioflag
))
714 return XFS_ERROR(ENOSPC
);
720 if (!(io
->io_flags
& XFS_IOCORE_RT
) && !ret_imap
->br_startblock
) {
721 cmn_err(CE_PANIC
,"Access to block zero: fs <%s> inode: %lld "
722 "start_block : %llx start_off : %llx blkcnt : %llx "
723 "extent-state : %x \n",
724 (ip
->i_mount
)->m_fsname
,
725 (long long)ip
->i_ino
,
726 (unsigned long long)ret_imap
->br_startblock
,
727 (unsigned long long)ret_imap
->br_startoff
,
728 (unsigned long long)ret_imap
->br_blockcount
,
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(tp
, ip
, map_start_fsb
, count_fsb
,
836 XFS_BMAPI_WRITE
, &first_block
, 1,
837 imap
, &nimaps
, &free_list
);
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
859 for (i
= 0; i
< nimaps
; i
++) {
860 if (!(io
->io_flags
& XFS_IOCORE_RT
) &&
861 !imap
[i
].br_startblock
) {
862 cmn_err(CE_PANIC
,"Access to block zero: "
863 "fs <%s> inode: %lld "
864 "start_block : %llx start_off : %llx "
865 "blkcnt : %llx extent-state : %x \n",
866 (ip
->i_mount
)->m_fsname
,
867 (long long)ip
->i_ino
,
869 imap
[i
].br_startblock
,
873 imap
[i
].br_blockcount
,
876 if ((offset_fsb
>= imap
[i
].br_startoff
) &&
877 (offset_fsb
< (imap
[i
].br_startoff
+
878 imap
[i
].br_blockcount
))) {
881 XFS_STATS_INC(xs_xstrat_quick
);
884 count_fsb
-= imap
[i
].br_blockcount
;
887 /* So far we have not mapped the requested part of the
888 * file, just surrounding data, try again.
891 map_start_fsb
= imap
[nimaps
].br_startoff
+
892 imap
[nimaps
].br_blockcount
;
896 xfs_bmap_cancel(&free_list
);
897 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
899 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
900 return XFS_ERROR(error
);
904 xfs_iomap_write_unwritten(
909 xfs_mount_t
*mp
= ip
->i_mount
;
910 xfs_iocore_t
*io
= &ip
->i_iocore
;
911 xfs_fileoff_t offset_fsb
;
912 xfs_filblks_t count_fsb
;
913 xfs_filblks_t numblks_fsb
;
914 xfs_fsblock_t firstfsb
;
917 xfs_bmbt_irec_t imap
;
918 xfs_bmap_free_t free_list
;
923 xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN
,
924 &ip
->i_iocore
, offset
, count
);
926 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
927 count_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)offset
+ count
);
928 count_fsb
= (xfs_filblks_t
)(count_fsb
- offset_fsb
);
930 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0) << 1;
934 * set up a transaction to convert the range of extents
935 * from unwritten to real. Do allocations in a loop until
936 * we have covered the range passed in.
939 tp
= xfs_trans_alloc(mp
, XFS_TRANS_STRAT_WRITE
);
940 error
= xfs_trans_reserve(tp
, resblks
,
941 XFS_WRITE_LOG_RES(mp
), 0,
942 XFS_TRANS_PERM_LOG_RES
,
943 XFS_WRITE_LOG_COUNT
);
945 xfs_trans_cancel(tp
, 0);
949 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
950 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
951 xfs_trans_ihold(tp
, ip
);
954 * Modify the unwritten extent state of the buffer.
956 XFS_BMAP_INIT(&free_list
, &firstfsb
);
958 error
= xfs_bmapi(tp
, ip
, offset_fsb
, count_fsb
,
959 XFS_BMAPI_WRITE
|XFS_BMAPI_CONVERT
, &firstfsb
,
960 1, &imap
, &nimaps
, &free_list
);
962 goto error_on_bmapi_transaction
;
964 error
= xfs_bmap_finish(&(tp
), &(free_list
),
965 firstfsb
, &committed
);
967 goto error_on_bmapi_transaction
;
969 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
, NULL
);
970 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
974 if ( !(io
->io_flags
& XFS_IOCORE_RT
) && !imap
.br_startblock
) {
975 cmn_err(CE_PANIC
,"Access to block zero: fs <%s> "
976 "inode: %lld start_block : %llx start_off : "
977 "%llx blkcnt : %llx extent-state : %x \n",
978 (ip
->i_mount
)->m_fsname
,
979 (long long)ip
->i_ino
,
980 (unsigned long long)imap
.br_startblock
,
981 (unsigned long long)imap
.br_startoff
,
982 (unsigned long long)imap
.br_blockcount
,
986 if ((numblks_fsb
= imap
.br_blockcount
) == 0) {
988 * The numblks_fsb value should always get
989 * smaller, otherwise the loop is stuck.
991 ASSERT(imap
.br_blockcount
);
994 offset_fsb
+= numblks_fsb
;
995 count_fsb
-= numblks_fsb
;
996 } while (count_fsb
> 0);
1000 error_on_bmapi_transaction
:
1001 xfs_bmap_cancel(&free_list
);
1002 xfs_trans_cancel(tp
, (XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
));
1003 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1005 return XFS_ERROR(error
);