2 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
37 #include "xfs_trans.h"
42 #include "xfs_alloc.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_quota.h"
45 #include "xfs_mount.h"
46 #include "xfs_bmap_btree.h"
47 #include "xfs_alloc_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_dir_sf.h"
50 #include "xfs_dir2_sf.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dinode.h"
53 #include "xfs_inode.h"
54 #include "xfs_ialloc.h"
55 #include "xfs_btree.h"
58 #include "xfs_rtalloc.h"
59 #include "xfs_error.h"
60 #include "xfs_itable.h"
66 #include "xfs_buf_item.h"
67 #include "xfs_trans_space.h"
68 #include "xfs_utils.h"
69 #include "xfs_iomap.h"
71 #if defined(XFS_RW_TRACE)
73 xfs_iomap_enter_trace(
79 xfs_inode_t
*ip
= XFS_IO_INODE(io
);
84 ktrace_enter(ip
->i_rwtrace
,
85 (void *)((unsigned long)tag
),
87 (void *)((unsigned long)((ip
->i_d
.di_size
>> 32) & 0xffffffff)),
88 (void *)((unsigned long)(ip
->i_d
.di_size
& 0xffffffff)),
89 (void *)((unsigned long)((offset
>> 32) & 0xffffffff)),
90 (void *)((unsigned long)(offset
& 0xffffffff)),
91 (void *)((unsigned long)count
),
92 (void *)((unsigned long)((io
->io_new_size
>> 32) & 0xffffffff)),
93 (void *)((unsigned long)(io
->io_new_size
& 0xffffffff)),
110 xfs_bmbt_irec_t
*imapp
,
113 xfs_inode_t
*ip
= XFS_IO_INODE(io
);
118 ktrace_enter(ip
->i_rwtrace
,
119 (void *)((unsigned long)tag
),
121 (void *)((unsigned long)((ip
->i_d
.di_size
>> 32) & 0xffffffff)),
122 (void *)((unsigned long)(ip
->i_d
.di_size
& 0xffffffff)),
123 (void *)((unsigned long)((offset
>> 32) & 0xffffffff)),
124 (void *)((unsigned long)(offset
& 0xffffffff)),
125 (void *)((unsigned long)count
),
126 (void *)((unsigned long)flags
),
127 (void *)((unsigned long)((iomapp
->iomap_offset
>> 32) & 0xffffffff)),
128 (void *)((unsigned long)(iomapp
->iomap_offset
& 0xffffffff)),
129 (void *)((unsigned long)(iomapp
->iomap_delta
)),
130 (void *)((unsigned long)(iomapp
->iomap_bsize
)),
131 (void *)((unsigned long)(iomapp
->iomap_bn
)),
132 (void *)(__psint_t
)(imapp
->br_startoff
),
133 (void *)((unsigned long)(imapp
->br_blockcount
)),
134 (void *)(__psint_t
)(imapp
->br_startblock
));
137 #define xfs_iomap_enter_trace(tag, io, offset, count)
138 #define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags)
141 #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
142 << mp->m_writeio_log)
143 #define XFS_STRAT_WRITE_IMAPS 2
144 #define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
150 xfs_bmbt_irec_t
*imap
,
152 int imaps
, /* Number of imap entries */
153 int iomaps
, /* Number of iomap entries */
159 xfs_fsblock_t start_block
;
162 nisize
= XFS_SIZE(mp
, io
);
163 if (io
->io_new_size
> nisize
)
164 nisize
= io
->io_new_size
;
166 for (pbm
= 0; imaps
&& pbm
< iomaps
; imaps
--, iomapp
++, imap
++, pbm
++) {
167 iomapp
->iomap_offset
= XFS_FSB_TO_B(mp
, imap
->br_startoff
);
168 iomapp
->iomap_delta
= offset
- iomapp
->iomap_offset
;
169 iomapp
->iomap_bsize
= XFS_FSB_TO_B(mp
, imap
->br_blockcount
);
170 iomapp
->iomap_flags
= flags
;
172 if (io
->io_flags
& XFS_IOCORE_RT
) {
173 iomapp
->iomap_flags
|= IOMAP_REALTIME
;
174 iomapp
->iomap_target
= mp
->m_rtdev_targp
;
176 iomapp
->iomap_target
= mp
->m_ddev_targp
;
178 start_block
= imap
->br_startblock
;
179 if (start_block
== HOLESTARTBLOCK
) {
180 iomapp
->iomap_bn
= IOMAP_DADDR_NULL
;
181 iomapp
->iomap_flags
|= IOMAP_HOLE
;
182 } else if (start_block
== DELAYSTARTBLOCK
) {
183 iomapp
->iomap_bn
= IOMAP_DADDR_NULL
;
184 iomapp
->iomap_flags
|= IOMAP_DELAY
;
186 iomapp
->iomap_bn
= XFS_FSB_TO_DB_IO(io
, start_block
);
187 if (ISUNWRITTEN(imap
))
188 iomapp
->iomap_flags
|= IOMAP_UNWRITTEN
;
191 if ((iomapp
->iomap_offset
+ iomapp
->iomap_bsize
) >= nisize
) {
192 iomapp
->iomap_flags
|= IOMAP_EOF
;
195 offset
+= iomapp
->iomap_bsize
- iomapp
->iomap_delta
;
197 return pbm
; /* Return the number filled */
209 xfs_mount_t
*mp
= io
->io_mount
;
210 xfs_fileoff_t offset_fsb
, end_fsb
;
213 xfs_bmbt_irec_t imap
;
218 if (XFS_FORCED_SHUTDOWN(mp
))
219 return XFS_ERROR(EIO
);
222 (BMAPI_READ
| BMAPI_WRITE
| BMAPI_ALLOCATE
|
223 BMAPI_UNWRITTEN
| BMAPI_DEVICE
)) {
225 xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER
, io
, offset
, count
);
226 lockmode
= XFS_LCK_MAP_SHARED(mp
, io
);
227 bmapi_flags
= XFS_BMAPI_ENTIRE
;
230 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER
, io
, offset
, count
);
231 lockmode
= XFS_ILOCK_EXCL
|XFS_EXTSIZE_WR
;
232 if (flags
& BMAPI_IGNSTATE
)
233 bmapi_flags
|= XFS_BMAPI_IGSTATE
|XFS_BMAPI_ENTIRE
;
234 XFS_ILOCK(mp
, io
, lockmode
);
237 xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER
, io
, offset
, count
);
238 lockmode
= XFS_ILOCK_SHARED
|XFS_EXTSIZE_RD
;
239 bmapi_flags
= XFS_BMAPI_ENTIRE
;
240 /* Attempt non-blocking lock */
241 if (flags
& BMAPI_TRYLOCK
) {
242 if (!XFS_ILOCK_NOWAIT(mp
, io
, lockmode
))
243 return XFS_ERROR(EAGAIN
);
245 XFS_ILOCK(mp
, io
, lockmode
);
248 case BMAPI_UNWRITTEN
:
251 lockmode
= XFS_LCK_MAP_SHARED(mp
, io
);
252 iomapp
->iomap_target
= io
->io_flags
& XFS_IOCORE_RT
?
253 mp
->m_rtdev_targp
: mp
->m_ddev_targp
;
261 ASSERT(offset
<= mp
->m_maxioffset
);
262 if ((xfs_fsize_t
)offset
+ count
> mp
->m_maxioffset
)
263 count
= mp
->m_maxioffset
- offset
;
264 end_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)offset
+ count
);
265 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
267 error
= XFS_BMAPI(mp
, NULL
, io
, offset_fsb
,
268 (xfs_filblks_t
)(end_fsb
- offset_fsb
),
269 bmapi_flags
, NULL
, 0, &imap
,
276 switch (flags
& (BMAPI_WRITE
|BMAPI_ALLOCATE
|BMAPI_UNWRITTEN
)) {
278 /* If we found an extent, return it */
280 (imap
.br_startblock
!= HOLESTARTBLOCK
) &&
281 (imap
.br_startblock
!= DELAYSTARTBLOCK
)) {
282 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP
, io
,
283 offset
, count
, iomapp
, &imap
, flags
);
287 if (flags
& (BMAPI_DIRECT
|BMAPI_MMAP
)) {
288 error
= XFS_IOMAP_WRITE_DIRECT(mp
, io
, offset
,
289 count
, flags
, &imap
, &nimaps
, nimaps
);
291 error
= XFS_IOMAP_WRITE_DELAY(mp
, io
, offset
, count
,
292 flags
, &imap
, &nimaps
);
295 xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP
, io
,
296 offset
, count
, iomapp
, &imap
, flags
);
298 iomap_flags
= IOMAP_NEW
;
301 /* If we found an extent, return it */
302 XFS_IUNLOCK(mp
, io
, lockmode
);
305 if (nimaps
&& !ISNULLSTARTBLOCK(imap
.br_startblock
)) {
306 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP
, io
,
307 offset
, count
, iomapp
, &imap
, flags
);
311 error
= XFS_IOMAP_WRITE_ALLOCATE(mp
, io
, offset
, count
,
314 case BMAPI_UNWRITTEN
:
316 error
= XFS_IOMAP_WRITE_UNWRITTEN(mp
, io
, offset
, count
);
322 *niomaps
= xfs_imap_to_bmap(io
, offset
, &imap
,
323 iomapp
, nimaps
, *niomaps
, iomap_flags
);
324 } else if (niomaps
) {
330 XFS_IUNLOCK(mp
, io
, lockmode
);
331 return XFS_ERROR(error
);
342 if (ip
->i_delayed_blks
) {
343 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
345 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
348 *ioflags
|= BMAPI_SYNC
;
354 *ioflags
|= BMAPI_SYNC
;
357 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
358 xfs_flush_device(ip
);
359 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
367 xfs_iomap_write_direct(
372 xfs_bmbt_irec_t
*ret_imap
,
376 xfs_mount_t
*mp
= ip
->i_mount
;
377 xfs_iocore_t
*io
= &ip
->i_iocore
;
378 xfs_fileoff_t offset_fsb
;
379 xfs_fileoff_t last_fsb
;
380 xfs_filblks_t count_fsb
;
382 xfs_fsblock_t firstfsb
;
389 xfs_bmbt_irec_t imap
[XFS_WRITE_IMAPS
], *imapp
;
390 xfs_bmap_free_t free_list
;
392 xfs_filblks_t qblocks
, resblks
;
397 * Make sure that the dquots are there. This doesn't hold
398 * the ilock across a disk read.
400 error
= XFS_QM_DQATTACH(ip
->i_mount
, ip
, XFS_QMOPT_ILOCKED
);
402 return XFS_ERROR(error
);
404 maps
= min(XFS_WRITE_IMAPS
, *nmaps
);
407 isize
= ip
->i_d
.di_size
;
408 aeof
= (offset
+ count
) > isize
;
410 if (io
->io_new_size
> isize
)
411 isize
= io
->io_new_size
;
413 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
414 last_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)(offset
+ count
)));
415 count_fsb
= last_fsb
- offset_fsb
;
416 if (found
&& (ret_imap
->br_startblock
== HOLESTARTBLOCK
)) {
417 xfs_fileoff_t map_last_fsb
;
419 map_last_fsb
= ret_imap
->br_blockcount
+ ret_imap
->br_startoff
;
420 if (map_last_fsb
< last_fsb
) {
421 last_fsb
= map_last_fsb
;
422 count_fsb
= last_fsb
- offset_fsb
;
424 ASSERT(count_fsb
> 0);
428 * Determine if reserving space on the data or realtime partition.
430 if ((rt
= XFS_IS_REALTIME_INODE(ip
))) {
433 if (!(extsz
= ip
->i_d
.di_extsize
))
434 extsz
= mp
->m_sb
.sb_rextsize
;
435 resrtextents
= qblocks
= (count_fsb
+ extsz
- 1);
436 do_div(resrtextents
, mp
->m_sb
.sb_rextsize
);
437 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
438 quota_flag
= XFS_QMOPT_RES_RTBLKS
;
441 resblks
= qblocks
= XFS_DIOSTRAT_SPACE_RES(mp
, count_fsb
);
442 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
446 * Allocate and setup the transaction
448 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
449 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
450 error
= xfs_trans_reserve(tp
, resblks
,
451 XFS_WRITE_LOG_RES(mp
), resrtextents
,
452 XFS_TRANS_PERM_LOG_RES
,
453 XFS_WRITE_LOG_COUNT
);
456 * Check for running out of space, note: need lock to return
459 xfs_trans_cancel(tp
, 0);
460 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
464 if (XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
, tp
, ip
, qblocks
, 0, quota_flag
)) {
469 bmapi_flag
= XFS_BMAPI_WRITE
;
470 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
471 xfs_trans_ihold(tp
, ip
);
473 if (!(flags
& BMAPI_MMAP
) && (offset
< ip
->i_d
.di_size
|| rt
))
474 bmapi_flag
|= XFS_BMAPI_PREALLOC
;
477 * Issue the bmapi() call to allocate the blocks
479 XFS_BMAP_INIT(&free_list
, &firstfsb
);
482 error
= xfs_bmapi(tp
, ip
, offset_fsb
, count_fsb
,
483 bmapi_flag
, &firstfsb
, 0, imapp
, &nimaps
, &free_list
);
488 * Complete the transaction
490 error
= xfs_bmap_finish(&tp
, &free_list
, firstfsb
, &committed
);
493 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
, NULL
);
498 * Copy any maps to caller's array and return any error.
507 if ( !(io
->io_flags
& XFS_IOCORE_RT
) && !ret_imap
->br_startblock
) {
508 cmn_err(CE_PANIC
,"Access to block zero: fs <%s> inode: %lld "
509 "start_block : %llx start_off : %llx blkcnt : %llx "
510 "extent-state : %x \n",
511 (ip
->i_mount
)->m_fsname
,
512 (long long)ip
->i_ino
,
513 ret_imap
->br_startblock
, ret_imap
->br_startoff
,
514 ret_imap
->br_blockcount
,ret_imap
->br_state
);
518 error0
: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
519 xfs_bmap_cancel(&free_list
);
520 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp
, tp
, ip
, qblocks
, 0, quota_flag
);
522 error1
: /* Just cancel transaction */
523 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
524 *nmaps
= 0; /* nothing set-up here */
527 return XFS_ERROR(error
);
531 xfs_iomap_write_delay(
536 xfs_bmbt_irec_t
*ret_imap
,
539 xfs_mount_t
*mp
= ip
->i_mount
;
540 xfs_iocore_t
*io
= &ip
->i_iocore
;
541 xfs_fileoff_t offset_fsb
;
542 xfs_fileoff_t last_fsb
;
544 xfs_fsblock_t firstblock
;
547 xfs_bmbt_irec_t imap
[XFS_WRITE_IMAPS
];
551 ASSERT(ismrlocked(&ip
->i_lock
, MR_UPDATE
) != 0);
554 * Make sure that the dquots are there. This doesn't hold
555 * the ilock across a disk read.
558 error
= XFS_QM_DQATTACH(mp
, ip
, XFS_QMOPT_ILOCKED
);
560 return XFS_ERROR(error
);
563 isize
= ip
->i_d
.di_size
;
564 if (io
->io_new_size
> isize
) {
565 isize
= io
->io_new_size
;
569 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
570 last_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)(offset
+ count
)));
572 * If the caller is doing a write at the end of the file,
573 * then extend the allocation (and the buffer used for the write)
574 * out to the file system's write iosize. We clean up any extra
575 * space left over when the file is closed in xfs_inactive().
577 * For sync writes, we are flushing delayed allocate space to
578 * try to make additional space available for allocation near
579 * the filesystem full boundary - preallocation hurts in that
580 * situation, of course.
582 if (!(ioflag
& BMAPI_SYNC
) && ((offset
+ count
) > ip
->i_d
.di_size
)) {
583 xfs_off_t aligned_offset
;
584 xfs_filblks_t count_fsb
;
586 xfs_fileoff_t ioalign
;
588 xfs_fileoff_t start_fsb
;
591 * If there are any real blocks past eof, then don't
592 * do any speculative allocation.
594 start_fsb
= XFS_B_TO_FSBT(mp
,
595 ((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) {
598 nimaps
= XFS_WRITE_IMAPS
;
599 error
= XFS_BMAPI(mp
, NULL
, io
, start_fsb
, count_fsb
,
600 0, &firstblock
, 0, imap
, &nimaps
, NULL
);
604 for (n
= 0; n
< nimaps
; n
++) {
605 if ( !(io
->io_flags
& XFS_IOCORE_RT
) &&
606 !imap
[n
].br_startblock
) {
607 cmn_err(CE_PANIC
,"Access to block "
608 "zero: fs <%s> inode: %lld "
609 "start_block : %llx start_off "
610 ": %llx blkcnt : %llx "
611 "extent-state : %x \n",
612 (ip
->i_mount
)->m_fsname
,
613 (long long)ip
->i_ino
,
614 imap
[n
].br_startblock
,
616 imap
[n
].br_blockcount
,
619 if ((imap
[n
].br_startblock
!= HOLESTARTBLOCK
) &&
620 (imap
[n
].br_startblock
!= DELAYSTARTBLOCK
)) {
623 start_fsb
+= imap
[n
].br_blockcount
;
624 count_fsb
-= imap
[n
].br_blockcount
;
627 iosize
= mp
->m_writeio_blocks
;
628 aligned_offset
= XFS_WRITEIO_ALIGN(mp
, (offset
+ count
- 1));
629 ioalign
= XFS_B_TO_FSBT(mp
, aligned_offset
);
630 last_fsb
= ioalign
+ iosize
;
634 nimaps
= XFS_WRITE_IMAPS
;
635 firstblock
= NULLFSBLOCK
;
638 * If mounted with the "-o swalloc" option, roundup the allocation
639 * request to a stripe width boundary if the file size is >=
640 * stripe width and we are allocating past the allocation eof.
642 if (!(io
->io_flags
& XFS_IOCORE_RT
) && mp
->m_swidth
643 && (mp
->m_flags
& XFS_MOUNT_SWALLOC
)
644 && (isize
>= XFS_FSB_TO_B(mp
, mp
->m_swidth
)) && aeof
) {
646 xfs_fileoff_t new_last_fsb
;
648 new_last_fsb
= roundup_64(last_fsb
, mp
->m_swidth
);
649 error
= xfs_bmap_eof(ip
, new_last_fsb
, XFS_DATA_FORK
, &eof
);
654 last_fsb
= new_last_fsb
;
657 * Roundup the allocation request to a stripe unit (m_dalign) boundary
658 * if the file size is >= stripe unit size, and we are allocating past
659 * the allocation eof.
661 } else if (!(io
->io_flags
& XFS_IOCORE_RT
) && mp
->m_dalign
&&
662 (isize
>= XFS_FSB_TO_B(mp
, mp
->m_dalign
)) && aeof
) {
664 xfs_fileoff_t new_last_fsb
;
665 new_last_fsb
= roundup_64(last_fsb
, mp
->m_dalign
);
666 error
= xfs_bmap_eof(ip
, new_last_fsb
, XFS_DATA_FORK
, &eof
);
671 last_fsb
= new_last_fsb
;
674 * Round up the allocation request to a real-time extent boundary
675 * if the file is on the real-time subvolume.
677 } else if (io
->io_flags
& XFS_IOCORE_RT
&& aeof
) {
679 xfs_fileoff_t new_last_fsb
;
681 new_last_fsb
= roundup_64(last_fsb
, mp
->m_sb
.sb_rextsize
);
682 error
= XFS_BMAP_EOF(mp
, io
, new_last_fsb
, XFS_DATA_FORK
, &eof
);
687 last_fsb
= new_last_fsb
;
689 error
= xfs_bmapi(NULL
, ip
, offset_fsb
,
690 (xfs_filblks_t
)(last_fsb
- offset_fsb
),
691 XFS_BMAPI_DELAY
| XFS_BMAPI_WRITE
|
692 XFS_BMAPI_ENTIRE
, &firstblock
, 1, imap
,
695 * This can be EDQUOT, if nimaps == 0
697 if (error
&& (error
!= ENOSPC
)) {
698 return XFS_ERROR(error
);
701 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
702 * then we must have run out of space.
705 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE
,
707 if (xfs_flush_space(ip
, &fsynced
, &ioflag
))
708 return XFS_ERROR(ENOSPC
);
716 if ( !(io
->io_flags
& XFS_IOCORE_RT
) && !ret_imap
->br_startblock
) {
717 cmn_err(CE_PANIC
,"Access to block zero: fs <%s> inode: %lld "
718 "start_block : %llx start_off : %llx blkcnt : %llx "
719 "extent-state : %x \n",
720 (ip
->i_mount
)->m_fsname
,
721 (long long)ip
->i_ino
,
722 ret_imap
->br_startblock
, ret_imap
->br_startoff
,
723 ret_imap
->br_blockcount
,ret_imap
->br_state
);
729 * Pass in a delayed allocate extent, convert it to real extents;
730 * return to the caller the extent we create which maps on top of
731 * the originating callers request.
733 * Called without a lock on the inode.
736 xfs_iomap_write_allocate(
740 xfs_bmbt_irec_t
*map
,
743 xfs_mount_t
*mp
= ip
->i_mount
;
744 xfs_iocore_t
*io
= &ip
->i_iocore
;
745 xfs_fileoff_t offset_fsb
, last_block
;
746 xfs_fileoff_t end_fsb
, map_start_fsb
;
747 xfs_fsblock_t first_block
;
748 xfs_bmap_free_t free_list
;
749 xfs_filblks_t count_fsb
;
750 xfs_bmbt_irec_t imap
[XFS_STRAT_WRITE_IMAPS
];
752 int i
, nimaps
, committed
;
759 * Make sure that the dquots are there.
761 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
762 return XFS_ERROR(error
);
764 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
765 count_fsb
= map
->br_blockcount
;
766 map_start_fsb
= map
->br_startoff
;
768 XFS_STATS_ADD(xs_xstrat_bytes
, XFS_FSB_TO_B(mp
, count_fsb
));
770 while (count_fsb
!= 0) {
772 * Set up a transaction with which to allocate the
773 * backing store for the file. Do allocations in a
774 * loop until we get some space in the range we are
775 * interested in. The other space that might be allocated
776 * is in the delayed allocation extent on which we sit
777 * but before our buffer starts.
781 while (nimaps
== 0) {
782 tp
= xfs_trans_alloc(mp
, XFS_TRANS_STRAT_WRITE
);
783 nres
= XFS_EXTENTADD_SPACE_RES(mp
, XFS_DATA_FORK
);
784 error
= xfs_trans_reserve(tp
, nres
,
785 XFS_WRITE_LOG_RES(mp
),
786 0, XFS_TRANS_PERM_LOG_RES
,
787 XFS_WRITE_LOG_COUNT
);
788 if (error
== ENOSPC
) {
789 error
= xfs_trans_reserve(tp
, 0,
790 XFS_WRITE_LOG_RES(mp
),
792 XFS_TRANS_PERM_LOG_RES
,
793 XFS_WRITE_LOG_COUNT
);
796 xfs_trans_cancel(tp
, 0);
797 return XFS_ERROR(error
);
799 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
800 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
801 xfs_trans_ihold(tp
, ip
);
803 XFS_BMAP_INIT(&free_list
, &first_block
);
805 nimaps
= XFS_STRAT_WRITE_IMAPS
;
807 * Ensure we don't go beyond eof - it is possible
808 * the extents changed since we did the read call,
809 * we dropped the ilock in the interim.
812 end_fsb
= XFS_B_TO_FSB(mp
, ip
->i_d
.di_size
);
813 xfs_bmap_last_offset(NULL
, ip
, &last_block
,
815 last_block
= XFS_FILEOFF_MAX(last_block
, end_fsb
);
816 if ((map_start_fsb
+ count_fsb
) > last_block
) {
817 count_fsb
= last_block
- map_start_fsb
;
818 if (count_fsb
== 0) {
824 /* Go get the actual blocks */
825 error
= xfs_bmapi(tp
, ip
, map_start_fsb
, count_fsb
,
826 XFS_BMAPI_WRITE
, &first_block
, 1,
827 imap
, &nimaps
, &free_list
);
831 error
= xfs_bmap_finish(&tp
, &free_list
,
832 first_block
, &committed
);
836 error
= xfs_trans_commit(tp
,
837 XFS_TRANS_RELEASE_LOG_RES
, NULL
);
841 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
845 * See if we were able to allocate an extent that
846 * covers at least part of the callers request
849 for (i
= 0; i
< nimaps
; i
++) {
850 if ( !(io
->io_flags
& XFS_IOCORE_RT
) &&
851 !imap
[i
].br_startblock
) {
852 cmn_err(CE_PANIC
,"Access to block zero: "
853 "fs <%s> inode: %lld "
854 "start_block : %llx start_off : %llx "
855 "blkcnt : %llx extent-state : %x \n",
856 (ip
->i_mount
)->m_fsname
,
857 (long long)ip
->i_ino
,
858 imap
[i
].br_startblock
,
860 imap
[i
].br_blockcount
,imap
[i
].br_state
);
862 if ((offset_fsb
>= imap
[i
].br_startoff
) &&
863 (offset_fsb
< (imap
[i
].br_startoff
+
864 imap
[i
].br_blockcount
))) {
867 XFS_STATS_INC(xs_xstrat_quick
);
870 count_fsb
-= imap
[i
].br_blockcount
;
873 /* So far we have not mapped the requested part of the
874 * file, just surrounding data, try again.
877 map_start_fsb
= imap
[nimaps
].br_startoff
+
878 imap
[nimaps
].br_blockcount
;
882 xfs_bmap_cancel(&free_list
);
883 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
885 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
886 return XFS_ERROR(error
);
890 xfs_iomap_write_unwritten(
895 xfs_mount_t
*mp
= ip
->i_mount
;
896 xfs_iocore_t
*io
= &ip
->i_iocore
;
898 xfs_fileoff_t offset_fsb
;
899 xfs_filblks_t count_fsb
;
900 xfs_filblks_t numblks_fsb
;
901 xfs_bmbt_irec_t imap
;
906 xfs_fsblock_t firstfsb
;
907 xfs_bmap_free_t free_list
;
909 xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN
,
910 &ip
->i_iocore
, offset
, count
);
912 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
913 count_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)offset
+ count
);
914 count_fsb
= (xfs_filblks_t
)(count_fsb
- offset_fsb
);
917 nres
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
920 * set up a transaction to convert the range of extents
921 * from unwritten to real. Do allocations in a loop until
922 * we have covered the range passed in.
925 tp
= xfs_trans_alloc(mp
, XFS_TRANS_STRAT_WRITE
);
926 error
= xfs_trans_reserve(tp
, nres
,
927 XFS_WRITE_LOG_RES(mp
), 0,
928 XFS_TRANS_PERM_LOG_RES
,
929 XFS_WRITE_LOG_COUNT
);
931 xfs_trans_cancel(tp
, 0);
935 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
936 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
937 xfs_trans_ihold(tp
, ip
);
940 * Modify the unwritten extent state of the buffer.
942 XFS_BMAP_INIT(&free_list
, &firstfsb
);
944 error
= xfs_bmapi(tp
, ip
, offset_fsb
, count_fsb
,
945 XFS_BMAPI_WRITE
, &firstfsb
,
946 1, &imap
, &nimaps
, &free_list
);
948 goto error_on_bmapi_transaction
;
950 error
= xfs_bmap_finish(&(tp
), &(free_list
),
951 firstfsb
, &committed
);
953 goto error_on_bmapi_transaction
;
955 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
, NULL
);
956 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
960 if ( !(io
->io_flags
& XFS_IOCORE_RT
) && !imap
.br_startblock
) {
961 cmn_err(CE_PANIC
,"Access to block zero: fs <%s> "
962 "inode: %lld start_block : %llx start_off : "
963 "%llx blkcnt : %llx extent-state : %x \n",
964 (ip
->i_mount
)->m_fsname
,
965 (long long)ip
->i_ino
,
966 imap
.br_startblock
,imap
.br_startoff
,
967 imap
.br_blockcount
,imap
.br_state
);
970 if ((numblks_fsb
= imap
.br_blockcount
) == 0) {
972 * The numblks_fsb value should always get
973 * smaller, otherwise the loop is stuck.
975 ASSERT(imap
.br_blockcount
);
978 offset_fsb
+= numblks_fsb
;
979 count_fsb
-= numblks_fsb
;
980 } while (count_fsb
> 0);
984 error_on_bmapi_transaction
:
985 xfs_bmap_cancel(&free_list
);
986 xfs_trans_cancel(tp
, (XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
));
987 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
989 return XFS_ERROR(error
);