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"
46 #include "xfs_buf_item.h"
47 #include "xfs_trans_space.h"
48 #include "xfs_utils.h"
49 #include "xfs_iomap.h"
50 #include "xfs_trace.h"
53 #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
55 #define XFS_STRAT_WRITE_IMAPS 2
56 #define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
64 struct xfs_bmbt_irec *imap,
68 struct xfs_mount *mp = ip->i_mount;
69 xfs_fileoff_t offset_fsb, end_fsb;
74 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
78 if (XFS_FORCED_SHUTDOWN(mp))
79 return XFS_ERROR(EIO);
81 trace_xfs_iomap_enter(ip, offset, count, flags, NULL);
83 switch (flags & (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE)) {
85 lockmode = xfs_ilock_map_shared(ip);
86 bmapi_flags = XFS_BMAPI_ENTIRE;
89 lockmode = XFS_ILOCK_EXCL;
90 if (flags & BMAPI_IGNSTATE)
91 bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
92 xfs_ilock(ip, lockmode);
95 lockmode = XFS_ILOCK_SHARED;
96 bmapi_flags = XFS_BMAPI_ENTIRE;
98 /* Attempt non-blocking lock */
99 if (flags & BMAPI_TRYLOCK) {
100 if (!xfs_ilock_nowait(ip, lockmode))
101 return XFS_ERROR(EAGAIN);
103 xfs_ilock(ip, lockmode);
110 ASSERT(offset <= mp->m_maxioffset);
111 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
112 count = mp->m_maxioffset - offset;
113 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
114 offset_fsb = XFS_B_TO_FSBT(mp, offset);
116 error = xfs_bmapi(NULL, ip, offset_fsb,
117 (xfs_filblks_t)(end_fsb - offset_fsb),
118 bmapi_flags, NULL, 0, imap,
124 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)) {
126 /* If we found an extent, return it */
128 (imap->br_startblock != HOLESTARTBLOCK) &&
129 (imap->br_startblock != DELAYSTARTBLOCK)) {
130 trace_xfs_iomap_found(ip, offset, count, flags, imap);
134 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
135 error = xfs_iomap_write_direct(ip, offset, count, flags,
136 imap, nimaps, *nimaps);
138 error = xfs_iomap_write_delay(ip, offset, count, flags,
142 trace_xfs_iomap_alloc(ip, offset, count, flags, imap);
147 /* If we found an extent, return it */
148 xfs_iunlock(ip, lockmode);
151 if (*nimaps && !isnullstartblock(imap->br_startblock)) {
152 trace_xfs_iomap_found(ip, offset, count, flags, imap);
156 error = xfs_iomap_write_allocate(ip, offset, count,
161 ASSERT(*nimaps <= 1);
165 xfs_iunlock(ip, lockmode);
166 return XFS_ERROR(error);
170 xfs_iomap_eof_align_last_fsb(
173 xfs_extlen_t extsize,
174 xfs_fileoff_t *last_fsb)
176 xfs_fileoff_t new_last_fsb = 0;
180 if (XFS_IS_REALTIME_INODE(ip))
183 * If mounted with the "-o swalloc" option, roundup the allocation
184 * request to a stripe width boundary if the file size is >=
185 * stripe width and we are allocating past the allocation eof.
187 else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
188 (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_swidth)))
189 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
191 * Roundup the allocation request to a stripe unit (m_dalign) boundary
192 * if the file size is >= stripe unit size, and we are allocating past
193 * the allocation eof.
195 else if (mp->m_dalign && (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_dalign)))
196 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
199 * Always round up the allocation request to an extent boundary
200 * (when file on a real-time subvolume or has di_extsize hint).
204 align = roundup_64(new_last_fsb, extsize);
207 new_last_fsb = roundup_64(*last_fsb, align);
211 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
215 *last_fsb = new_last_fsb;
221 xfs_cmn_err_fsblock_zero(
223 xfs_bmbt_irec_t *imap)
225 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
226 "Access to block zero in inode %llu "
227 "start_block: %llx start_off: %llx "
228 "blkcnt: %llx extent-state: %x\n",
229 (unsigned long long)ip->i_ino,
230 (unsigned long long)imap->br_startblock,
231 (unsigned long long)imap->br_startoff,
232 (unsigned long long)imap->br_blockcount,
238 xfs_iomap_write_direct(
243 xfs_bmbt_irec_t *ret_imap,
247 xfs_mount_t *mp = ip->i_mount;
248 xfs_fileoff_t offset_fsb;
249 xfs_fileoff_t last_fsb;
250 xfs_filblks_t count_fsb, resaligned;
251 xfs_fsblock_t firstfsb;
252 xfs_extlen_t extsz, temp;
258 xfs_bmbt_irec_t imap;
259 xfs_bmap_free_t free_list;
260 uint qblocks, resblks, resrtextents;
265 * Make sure that the dquots are there. This doesn't hold
266 * the ilock across a disk read.
268 error = xfs_qm_dqattach_locked(ip, 0);
270 return XFS_ERROR(error);
272 rt = XFS_IS_REALTIME_INODE(ip);
273 extsz = xfs_get_extsz_hint(ip);
275 offset_fsb = XFS_B_TO_FSBT(mp, offset);
276 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
277 if ((offset + count) > ip->i_size) {
278 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
282 if (found && (ret_imap->br_startblock == HOLESTARTBLOCK))
283 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
284 ret_imap->br_blockcount +
285 ret_imap->br_startoff);
287 count_fsb = last_fsb - offset_fsb;
288 ASSERT(count_fsb > 0);
290 resaligned = count_fsb;
291 if (unlikely(extsz)) {
292 if ((temp = do_mod(offset_fsb, extsz)))
294 if ((temp = do_mod(resaligned, extsz)))
295 resaligned += extsz - temp;
299 resrtextents = qblocks = resaligned;
300 resrtextents /= mp->m_sb.sb_rextsize;
301 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
302 quota_flag = XFS_QMOPT_RES_RTBLKS;
305 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
306 quota_flag = XFS_QMOPT_RES_REGBLKS;
310 * Allocate and setup the transaction
312 xfs_iunlock(ip, XFS_ILOCK_EXCL);
313 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
314 error = xfs_trans_reserve(tp, resblks,
315 XFS_WRITE_LOG_RES(mp), resrtextents,
316 XFS_TRANS_PERM_LOG_RES,
317 XFS_WRITE_LOG_COUNT);
319 * Check for running out of space, note: need lock to return
322 xfs_trans_cancel(tp, 0);
323 xfs_ilock(ip, XFS_ILOCK_EXCL);
327 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
331 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
332 xfs_trans_ihold(tp, ip);
334 bmapi_flag = XFS_BMAPI_WRITE;
335 if ((flags & BMAPI_DIRECT) && (offset < ip->i_size || extsz))
336 bmapi_flag |= XFS_BMAPI_PREALLOC;
339 * Issue the xfs_bmapi() call to allocate the blocks
341 xfs_bmap_init(&free_list, &firstfsb);
343 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, bmapi_flag,
344 &firstfsb, 0, &imap, &nimaps, &free_list, NULL);
349 * Complete the transaction
351 error = xfs_bmap_finish(&tp, &free_list, &committed);
354 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
359 * Copy any maps to caller's array and return any error.
366 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip))) {
367 error = xfs_cmn_err_fsblock_zero(ip, &imap);
375 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
376 xfs_bmap_cancel(&free_list);
377 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
379 error1: /* Just cancel transaction */
380 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
381 *nmaps = 0; /* nothing set-up here */
384 return XFS_ERROR(error);
388 * If the caller is doing a write at the end of the file, then extend the
389 * allocation out to the file system's write iosize. We clean up any extra
390 * space left over when the file is closed in xfs_inactive().
393 xfs_iomap_eof_want_preallocate(
399 xfs_bmbt_irec_t *imap,
403 xfs_fileoff_t start_fsb;
404 xfs_filblks_t count_fsb;
405 xfs_fsblock_t firstblock;
409 if ((offset + count) <= ip->i_size)
413 * If there are any real blocks past eof, then don't
414 * do any speculative allocation.
416 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
417 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
418 while (count_fsb > 0) {
420 firstblock = NULLFSBLOCK;
421 error = xfs_bmapi(NULL, ip, start_fsb, count_fsb, 0,
422 &firstblock, 0, imap, &imaps, NULL, NULL);
425 for (n = 0; n < imaps; n++) {
426 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
427 (imap[n].br_startblock != DELAYSTARTBLOCK))
429 start_fsb += imap[n].br_blockcount;
430 count_fsb -= imap[n].br_blockcount;
438 xfs_iomap_write_delay(
443 xfs_bmbt_irec_t *ret_imap,
446 xfs_mount_t *mp = ip->i_mount;
447 xfs_fileoff_t offset_fsb;
448 xfs_fileoff_t last_fsb;
449 xfs_off_t aligned_offset;
450 xfs_fileoff_t ioalign;
451 xfs_fsblock_t firstblock;
454 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
455 int prealloc, flushed = 0;
458 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
461 * Make sure that the dquots are there. This doesn't hold
462 * the ilock across a disk read.
464 error = xfs_qm_dqattach_locked(ip, 0);
466 return XFS_ERROR(error);
468 extsz = xfs_get_extsz_hint(ip);
469 offset_fsb = XFS_B_TO_FSBT(mp, offset);
471 error = xfs_iomap_eof_want_preallocate(mp, ip, offset, count,
472 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
478 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
479 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
480 last_fsb = ioalign + mp->m_writeio_blocks;
482 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
485 if (prealloc || extsz) {
486 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
491 nimaps = XFS_WRITE_IMAPS;
492 firstblock = NULLFSBLOCK;
493 error = xfs_bmapi(NULL, ip, offset_fsb,
494 (xfs_filblks_t)(last_fsb - offset_fsb),
495 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
496 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
497 &nimaps, NULL, NULL);
498 if (error && (error != ENOSPC))
499 return XFS_ERROR(error);
502 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
503 * then we must have run out of space - flush all other inodes with
504 * delalloc blocks and retry without EOF preallocation.
507 trace_xfs_delalloc_enospc(ip, offset, count);
509 return XFS_ERROR(ENOSPC);
511 xfs_iunlock(ip, XFS_ILOCK_EXCL);
512 xfs_flush_inodes(ip);
513 xfs_ilock(ip, XFS_ILOCK_EXCL);
521 if (!(imap[0].br_startblock || XFS_IS_REALTIME_INODE(ip)))
522 return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
531 * Pass in a delayed allocate extent, convert it to real extents;
532 * return to the caller the extent we create which maps on top of
533 * the originating callers request.
535 * Called without a lock on the inode.
537 * We no longer bother to look at the incoming map - all we have to
538 * guarantee is that whatever we allocate fills the required range.
541 xfs_iomap_write_allocate(
545 xfs_bmbt_irec_t *map,
548 xfs_mount_t *mp = ip->i_mount;
549 xfs_fileoff_t offset_fsb, last_block;
550 xfs_fileoff_t end_fsb, map_start_fsb;
551 xfs_fsblock_t first_block;
552 xfs_bmap_free_t free_list;
553 xfs_filblks_t count_fsb;
554 xfs_bmbt_irec_t imap;
556 int nimaps, committed;
563 * Make sure that the dquots are there.
565 error = xfs_qm_dqattach(ip, 0);
567 return XFS_ERROR(error);
569 offset_fsb = XFS_B_TO_FSBT(mp, offset);
570 count_fsb = map->br_blockcount;
571 map_start_fsb = map->br_startoff;
573 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
575 while (count_fsb != 0) {
577 * Set up a transaction with which to allocate the
578 * backing store for the file. Do allocations in a
579 * loop until we get some space in the range we are
580 * interested in. The other space that might be allocated
581 * is in the delayed allocation extent on which we sit
582 * but before our buffer starts.
586 while (nimaps == 0) {
587 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
588 tp->t_flags |= XFS_TRANS_RESERVE;
589 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
590 error = xfs_trans_reserve(tp, nres,
591 XFS_WRITE_LOG_RES(mp),
592 0, XFS_TRANS_PERM_LOG_RES,
593 XFS_WRITE_LOG_COUNT);
595 xfs_trans_cancel(tp, 0);
596 return XFS_ERROR(error);
598 xfs_ilock(ip, XFS_ILOCK_EXCL);
599 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
600 xfs_trans_ihold(tp, ip);
602 xfs_bmap_init(&free_list, &first_block);
605 * it is possible that the extents have changed since
606 * we did the read call as we dropped the ilock for a
607 * while. We have to be careful about truncates or hole
608 * punchs here - we are not allowed to allocate
609 * non-delalloc blocks here.
611 * The only protection against truncation is the pages
612 * for the range we are being asked to convert are
613 * locked and hence a truncate will block on them
616 * As a result, if we go beyond the range we really
617 * need and hit an delalloc extent boundary followed by
618 * a hole while we have excess blocks in the map, we
619 * will fill the hole incorrectly and overrun the
620 * transaction reservation.
622 * Using a single map prevents this as we are forced to
623 * check each map we look for overlap with the desired
624 * range and abort as soon as we find it. Also, given
625 * that we only return a single map, having one beyond
626 * what we can return is probably a bit silly.
628 * We also need to check that we don't go beyond EOF;
629 * this is a truncate optimisation as a truncate sets
630 * the new file size before block on the pages we
631 * currently have locked under writeback. Because they
632 * are about to be tossed, we don't need to write them
636 end_fsb = XFS_B_TO_FSB(mp, ip->i_size);
637 error = xfs_bmap_last_offset(NULL, ip, &last_block,
642 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
643 if ((map_start_fsb + count_fsb) > last_block) {
644 count_fsb = last_block - map_start_fsb;
645 if (count_fsb == 0) {
651 /* Go get the actual blocks */
652 error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
653 XFS_BMAPI_WRITE, &first_block, 1,
654 &imap, &nimaps, &free_list, NULL);
658 error = xfs_bmap_finish(&tp, &free_list, &committed);
662 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
666 xfs_iunlock(ip, XFS_ILOCK_EXCL);
670 * See if we were able to allocate an extent that
671 * covers at least part of the callers request
673 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
674 return xfs_cmn_err_fsblock_zero(ip, &imap);
676 if ((offset_fsb >= imap.br_startoff) &&
677 (offset_fsb < (imap.br_startoff +
678 imap.br_blockcount))) {
681 XFS_STATS_INC(xs_xstrat_quick);
686 * So far we have not mapped the requested part of the
687 * file, just surrounding data, try again.
689 count_fsb -= imap.br_blockcount;
690 map_start_fsb = imap.br_startoff + imap.br_blockcount;
694 xfs_bmap_cancel(&free_list);
695 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
697 xfs_iunlock(ip, XFS_ILOCK_EXCL);
698 return XFS_ERROR(error);
702 xfs_iomap_write_unwritten(
707 xfs_mount_t *mp = ip->i_mount;
708 xfs_fileoff_t offset_fsb;
709 xfs_filblks_t count_fsb;
710 xfs_filblks_t numblks_fsb;
711 xfs_fsblock_t firstfsb;
714 xfs_bmbt_irec_t imap;
715 xfs_bmap_free_t free_list;
720 trace_xfs_unwritten_convert(ip, offset, count);
722 offset_fsb = XFS_B_TO_FSBT(mp, offset);
723 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
724 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
727 * Reserve enough blocks in this transaction for two complete extent
728 * btree splits. We may be converting the middle part of an unwritten
729 * extent and in this case we will insert two new extents in the btree
730 * each of which could cause a full split.
732 * This reservation amount will be used in the first call to
733 * xfs_bmbt_split() to select an AG with enough space to satisfy the
734 * rest of the operation.
736 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
740 * set up a transaction to convert the range of extents
741 * from unwritten to real. Do allocations in a loop until
742 * we have covered the range passed in.
744 * Note that we open code the transaction allocation here
745 * to pass KM_NOFS--we can't risk to recursing back into
746 * the filesystem here as we might be asked to write out
747 * the same inode that we complete here and might deadlock
750 xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
751 tp = _xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE, KM_NOFS);
752 tp->t_flags |= XFS_TRANS_RESERVE;
753 error = xfs_trans_reserve(tp, resblks,
754 XFS_WRITE_LOG_RES(mp), 0,
755 XFS_TRANS_PERM_LOG_RES,
756 XFS_WRITE_LOG_COUNT);
758 xfs_trans_cancel(tp, 0);
759 return XFS_ERROR(error);
762 xfs_ilock(ip, XFS_ILOCK_EXCL);
763 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
764 xfs_trans_ihold(tp, ip);
767 * Modify the unwritten extent state of the buffer.
769 xfs_bmap_init(&free_list, &firstfsb);
771 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
772 XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
773 1, &imap, &nimaps, &free_list, NULL);
775 goto error_on_bmapi_transaction;
777 error = xfs_bmap_finish(&(tp), &(free_list), &committed);
779 goto error_on_bmapi_transaction;
781 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
782 xfs_iunlock(ip, XFS_ILOCK_EXCL);
784 return XFS_ERROR(error);
786 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
787 return xfs_cmn_err_fsblock_zero(ip, &imap);
789 if ((numblks_fsb = imap.br_blockcount) == 0) {
791 * The numblks_fsb value should always get
792 * smaller, otherwise the loop is stuck.
794 ASSERT(imap.br_blockcount);
797 offset_fsb += numblks_fsb;
798 count_fsb -= numblks_fsb;
799 } while (count_fsb > 0);
803 error_on_bmapi_transaction:
804 xfs_bmap_cancel(&free_list);
805 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
806 xfs_iunlock(ip, XFS_ILOCK_EXCL);
807 return XFS_ERROR(error);