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
62 xfs_bmbt_irec_t *imap,
64 int imaps, /* Number of imap entries */
67 iomapp->iomap_offset = imap->br_startoff;
68 iomapp->iomap_bsize = imap->br_blockcount;
69 iomapp->iomap_flags = flags;
70 iomapp->iomap_bn = imap->br_startblock;
72 if (imap->br_startblock != HOLESTARTBLOCK &&
73 imap->br_startblock != DELAYSTARTBLOCK &&
75 iomapp->iomap_flags |= IOMAP_UNWRITTEN;
87 xfs_mount_t *mp = ip->i_mount;
88 xfs_fileoff_t offset_fsb, end_fsb;
96 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
97 ASSERT(niomaps && *niomaps == 1);
99 if (XFS_FORCED_SHUTDOWN(mp))
100 return XFS_ERROR(EIO);
102 trace_xfs_iomap_enter(ip, offset, count, flags, NULL);
104 switch (flags & (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE)) {
106 lockmode = xfs_ilock_map_shared(ip);
107 bmapi_flags = XFS_BMAPI_ENTIRE;
110 lockmode = XFS_ILOCK_EXCL;
111 if (flags & BMAPI_IGNSTATE)
112 bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
113 xfs_ilock(ip, lockmode);
116 lockmode = XFS_ILOCK_SHARED;
117 bmapi_flags = XFS_BMAPI_ENTIRE;
119 /* Attempt non-blocking lock */
120 if (flags & BMAPI_TRYLOCK) {
121 if (!xfs_ilock_nowait(ip, lockmode))
122 return XFS_ERROR(EAGAIN);
124 xfs_ilock(ip, lockmode);
131 ASSERT(offset <= mp->m_maxioffset);
132 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
133 count = mp->m_maxioffset - offset;
134 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
135 offset_fsb = XFS_B_TO_FSBT(mp, offset);
137 error = xfs_bmapi(NULL, ip, offset_fsb,
138 (xfs_filblks_t)(end_fsb - offset_fsb),
139 bmapi_flags, NULL, 0, &imap,
140 &nimaps, NULL, NULL);
145 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)) {
147 /* If we found an extent, return it */
149 (imap.br_startblock != HOLESTARTBLOCK) &&
150 (imap.br_startblock != DELAYSTARTBLOCK)) {
151 trace_xfs_iomap_found(ip, offset, count, flags, &imap);
155 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
156 error = xfs_iomap_write_direct(ip, offset, count, flags,
157 &imap, &nimaps, nimaps);
159 error = xfs_iomap_write_delay(ip, offset, count, flags,
163 trace_xfs_iomap_alloc(ip, offset, count, flags, &imap);
165 iomap_flags = IOMAP_NEW;
168 /* If we found an extent, return it */
169 xfs_iunlock(ip, lockmode);
172 if (nimaps && !isnullstartblock(imap.br_startblock)) {
173 trace_xfs_iomap_found(ip, offset, count, flags, &imap);
177 error = xfs_iomap_write_allocate(ip, offset, count,
185 xfs_imap_to_bmap(ip, offset, &imap, iomapp, nimaps, iomap_flags);
190 xfs_iunlock(ip, lockmode);
191 return XFS_ERROR(error);
196 xfs_iomap_eof_align_last_fsb(
199 xfs_extlen_t extsize,
200 xfs_fileoff_t *last_fsb)
202 xfs_fileoff_t new_last_fsb = 0;
206 if (XFS_IS_REALTIME_INODE(ip))
209 * If mounted with the "-o swalloc" option, roundup the allocation
210 * request to a stripe width boundary if the file size is >=
211 * stripe width and we are allocating past the allocation eof.
213 else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
214 (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_swidth)))
215 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
217 * Roundup the allocation request to a stripe unit (m_dalign) boundary
218 * if the file size is >= stripe unit size, and we are allocating past
219 * the allocation eof.
221 else if (mp->m_dalign && (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_dalign)))
222 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
225 * Always round up the allocation request to an extent boundary
226 * (when file on a real-time subvolume or has di_extsize hint).
230 align = roundup_64(new_last_fsb, extsize);
233 new_last_fsb = roundup_64(*last_fsb, align);
237 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
241 *last_fsb = new_last_fsb;
247 xfs_cmn_err_fsblock_zero(
249 xfs_bmbt_irec_t *imap)
251 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
252 "Access to block zero in inode %llu "
253 "start_block: %llx start_off: %llx "
254 "blkcnt: %llx extent-state: %x\n",
255 (unsigned long long)ip->i_ino,
256 (unsigned long long)imap->br_startblock,
257 (unsigned long long)imap->br_startoff,
258 (unsigned long long)imap->br_blockcount,
264 xfs_iomap_write_direct(
269 xfs_bmbt_irec_t *ret_imap,
273 xfs_mount_t *mp = ip->i_mount;
274 xfs_fileoff_t offset_fsb;
275 xfs_fileoff_t last_fsb;
276 xfs_filblks_t count_fsb, resaligned;
277 xfs_fsblock_t firstfsb;
278 xfs_extlen_t extsz, temp;
284 xfs_bmbt_irec_t imap;
285 xfs_bmap_free_t free_list;
286 uint qblocks, resblks, resrtextents;
291 * Make sure that the dquots are there. This doesn't hold
292 * the ilock across a disk read.
294 error = xfs_qm_dqattach_locked(ip, 0);
296 return XFS_ERROR(error);
298 rt = XFS_IS_REALTIME_INODE(ip);
299 extsz = xfs_get_extsz_hint(ip);
301 offset_fsb = XFS_B_TO_FSBT(mp, offset);
302 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
303 if ((offset + count) > ip->i_size) {
304 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
308 if (found && (ret_imap->br_startblock == HOLESTARTBLOCK))
309 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
310 ret_imap->br_blockcount +
311 ret_imap->br_startoff);
313 count_fsb = last_fsb - offset_fsb;
314 ASSERT(count_fsb > 0);
316 resaligned = count_fsb;
317 if (unlikely(extsz)) {
318 if ((temp = do_mod(offset_fsb, extsz)))
320 if ((temp = do_mod(resaligned, extsz)))
321 resaligned += extsz - temp;
325 resrtextents = qblocks = resaligned;
326 resrtextents /= mp->m_sb.sb_rextsize;
327 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
328 quota_flag = XFS_QMOPT_RES_RTBLKS;
331 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
332 quota_flag = XFS_QMOPT_RES_REGBLKS;
336 * Allocate and setup the transaction
338 xfs_iunlock(ip, XFS_ILOCK_EXCL);
339 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
340 error = xfs_trans_reserve(tp, resblks,
341 XFS_WRITE_LOG_RES(mp), resrtextents,
342 XFS_TRANS_PERM_LOG_RES,
343 XFS_WRITE_LOG_COUNT);
345 * Check for running out of space, note: need lock to return
348 xfs_trans_cancel(tp, 0);
349 xfs_ilock(ip, XFS_ILOCK_EXCL);
353 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
357 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
358 xfs_trans_ihold(tp, ip);
360 bmapi_flag = XFS_BMAPI_WRITE;
361 if ((flags & BMAPI_DIRECT) && (offset < ip->i_size || extsz))
362 bmapi_flag |= XFS_BMAPI_PREALLOC;
365 * Issue the xfs_bmapi() call to allocate the blocks
367 xfs_bmap_init(&free_list, &firstfsb);
369 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, bmapi_flag,
370 &firstfsb, 0, &imap, &nimaps, &free_list, NULL);
375 * Complete the transaction
377 error = xfs_bmap_finish(&tp, &free_list, &committed);
380 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
385 * Copy any maps to caller's array and return any error.
392 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip))) {
393 error = xfs_cmn_err_fsblock_zero(ip, &imap);
401 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
402 xfs_bmap_cancel(&free_list);
403 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
405 error1: /* Just cancel transaction */
406 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
407 *nmaps = 0; /* nothing set-up here */
410 return XFS_ERROR(error);
414 * If the caller is doing a write at the end of the file, then extend the
415 * allocation out to the file system's write iosize. We clean up any extra
416 * space left over when the file is closed in xfs_inactive().
419 xfs_iomap_eof_want_preallocate(
425 xfs_bmbt_irec_t *imap,
429 xfs_fileoff_t start_fsb;
430 xfs_filblks_t count_fsb;
431 xfs_fsblock_t firstblock;
435 if ((offset + count) <= ip->i_size)
439 * If there are any real blocks past eof, then don't
440 * do any speculative allocation.
442 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
443 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
444 while (count_fsb > 0) {
446 firstblock = NULLFSBLOCK;
447 error = xfs_bmapi(NULL, ip, start_fsb, count_fsb, 0,
448 &firstblock, 0, imap, &imaps, NULL, NULL);
451 for (n = 0; n < imaps; n++) {
452 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
453 (imap[n].br_startblock != DELAYSTARTBLOCK))
455 start_fsb += imap[n].br_blockcount;
456 count_fsb -= imap[n].br_blockcount;
464 xfs_iomap_write_delay(
469 xfs_bmbt_irec_t *ret_imap,
472 xfs_mount_t *mp = ip->i_mount;
473 xfs_fileoff_t offset_fsb;
474 xfs_fileoff_t last_fsb;
475 xfs_off_t aligned_offset;
476 xfs_fileoff_t ioalign;
477 xfs_fsblock_t firstblock;
480 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
481 int prealloc, flushed = 0;
484 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
487 * Make sure that the dquots are there. This doesn't hold
488 * the ilock across a disk read.
490 error = xfs_qm_dqattach_locked(ip, 0);
492 return XFS_ERROR(error);
494 extsz = xfs_get_extsz_hint(ip);
495 offset_fsb = XFS_B_TO_FSBT(mp, offset);
497 error = xfs_iomap_eof_want_preallocate(mp, ip, offset, count,
498 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
504 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
505 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
506 last_fsb = ioalign + mp->m_writeio_blocks;
508 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
511 if (prealloc || extsz) {
512 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
517 nimaps = XFS_WRITE_IMAPS;
518 firstblock = NULLFSBLOCK;
519 error = xfs_bmapi(NULL, ip, offset_fsb,
520 (xfs_filblks_t)(last_fsb - offset_fsb),
521 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
522 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
523 &nimaps, NULL, NULL);
524 if (error && (error != ENOSPC))
525 return XFS_ERROR(error);
528 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
529 * then we must have run out of space - flush all other inodes with
530 * delalloc blocks and retry without EOF preallocation.
533 trace_xfs_delalloc_enospc(ip, offset, count);
535 return XFS_ERROR(ENOSPC);
537 xfs_iunlock(ip, XFS_ILOCK_EXCL);
538 xfs_flush_inodes(ip);
539 xfs_ilock(ip, XFS_ILOCK_EXCL);
547 if (!(imap[0].br_startblock || XFS_IS_REALTIME_INODE(ip)))
548 return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
557 * Pass in a delayed allocate extent, convert it to real extents;
558 * return to the caller the extent we create which maps on top of
559 * the originating callers request.
561 * Called without a lock on the inode.
563 * We no longer bother to look at the incoming map - all we have to
564 * guarantee is that whatever we allocate fills the required range.
567 xfs_iomap_write_allocate(
571 xfs_bmbt_irec_t *map,
574 xfs_mount_t *mp = ip->i_mount;
575 xfs_fileoff_t offset_fsb, last_block;
576 xfs_fileoff_t end_fsb, map_start_fsb;
577 xfs_fsblock_t first_block;
578 xfs_bmap_free_t free_list;
579 xfs_filblks_t count_fsb;
580 xfs_bmbt_irec_t imap;
582 int nimaps, committed;
589 * Make sure that the dquots are there.
591 error = xfs_qm_dqattach(ip, 0);
593 return XFS_ERROR(error);
595 offset_fsb = XFS_B_TO_FSBT(mp, offset);
596 count_fsb = map->br_blockcount;
597 map_start_fsb = map->br_startoff;
599 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
601 while (count_fsb != 0) {
603 * Set up a transaction with which to allocate the
604 * backing store for the file. Do allocations in a
605 * loop until we get some space in the range we are
606 * interested in. The other space that might be allocated
607 * is in the delayed allocation extent on which we sit
608 * but before our buffer starts.
612 while (nimaps == 0) {
613 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
614 tp->t_flags |= XFS_TRANS_RESERVE;
615 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
616 error = xfs_trans_reserve(tp, nres,
617 XFS_WRITE_LOG_RES(mp),
618 0, XFS_TRANS_PERM_LOG_RES,
619 XFS_WRITE_LOG_COUNT);
621 xfs_trans_cancel(tp, 0);
622 return XFS_ERROR(error);
624 xfs_ilock(ip, XFS_ILOCK_EXCL);
625 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
626 xfs_trans_ihold(tp, ip);
628 xfs_bmap_init(&free_list, &first_block);
631 * it is possible that the extents have changed since
632 * we did the read call as we dropped the ilock for a
633 * while. We have to be careful about truncates or hole
634 * punchs here - we are not allowed to allocate
635 * non-delalloc blocks here.
637 * The only protection against truncation is the pages
638 * for the range we are being asked to convert are
639 * locked and hence a truncate will block on them
642 * As a result, if we go beyond the range we really
643 * need and hit an delalloc extent boundary followed by
644 * a hole while we have excess blocks in the map, we
645 * will fill the hole incorrectly and overrun the
646 * transaction reservation.
648 * Using a single map prevents this as we are forced to
649 * check each map we look for overlap with the desired
650 * range and abort as soon as we find it. Also, given
651 * that we only return a single map, having one beyond
652 * what we can return is probably a bit silly.
654 * We also need to check that we don't go beyond EOF;
655 * this is a truncate optimisation as a truncate sets
656 * the new file size before block on the pages we
657 * currently have locked under writeback. Because they
658 * are about to be tossed, we don't need to write them
662 end_fsb = XFS_B_TO_FSB(mp, ip->i_size);
663 error = xfs_bmap_last_offset(NULL, ip, &last_block,
668 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
669 if ((map_start_fsb + count_fsb) > last_block) {
670 count_fsb = last_block - map_start_fsb;
671 if (count_fsb == 0) {
677 /* Go get the actual blocks */
678 error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
679 XFS_BMAPI_WRITE, &first_block, 1,
680 &imap, &nimaps, &free_list, NULL);
684 error = xfs_bmap_finish(&tp, &free_list, &committed);
688 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
692 xfs_iunlock(ip, XFS_ILOCK_EXCL);
696 * See if we were able to allocate an extent that
697 * covers at least part of the callers request
699 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
700 return xfs_cmn_err_fsblock_zero(ip, &imap);
702 if ((offset_fsb >= imap.br_startoff) &&
703 (offset_fsb < (imap.br_startoff +
704 imap.br_blockcount))) {
707 XFS_STATS_INC(xs_xstrat_quick);
712 * So far we have not mapped the requested part of the
713 * file, just surrounding data, try again.
715 count_fsb -= imap.br_blockcount;
716 map_start_fsb = imap.br_startoff + imap.br_blockcount;
720 xfs_bmap_cancel(&free_list);
721 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
723 xfs_iunlock(ip, XFS_ILOCK_EXCL);
724 return XFS_ERROR(error);
728 xfs_iomap_write_unwritten(
733 xfs_mount_t *mp = ip->i_mount;
734 xfs_fileoff_t offset_fsb;
735 xfs_filblks_t count_fsb;
736 xfs_filblks_t numblks_fsb;
737 xfs_fsblock_t firstfsb;
740 xfs_bmbt_irec_t imap;
741 xfs_bmap_free_t free_list;
746 trace_xfs_unwritten_convert(ip, offset, count);
748 offset_fsb = XFS_B_TO_FSBT(mp, offset);
749 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
750 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
753 * Reserve enough blocks in this transaction for two complete extent
754 * btree splits. We may be converting the middle part of an unwritten
755 * extent and in this case we will insert two new extents in the btree
756 * each of which could cause a full split.
758 * This reservation amount will be used in the first call to
759 * xfs_bmbt_split() to select an AG with enough space to satisfy the
760 * rest of the operation.
762 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
766 * set up a transaction to convert the range of extents
767 * from unwritten to real. Do allocations in a loop until
768 * we have covered the range passed in.
770 * Note that we open code the transaction allocation here
771 * to pass KM_NOFS--we can't risk to recursing back into
772 * the filesystem here as we might be asked to write out
773 * the same inode that we complete here and might deadlock
776 xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
777 tp = _xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE, KM_NOFS);
778 tp->t_flags |= XFS_TRANS_RESERVE;
779 error = xfs_trans_reserve(tp, resblks,
780 XFS_WRITE_LOG_RES(mp), 0,
781 XFS_TRANS_PERM_LOG_RES,
782 XFS_WRITE_LOG_COUNT);
784 xfs_trans_cancel(tp, 0);
785 return XFS_ERROR(error);
788 xfs_ilock(ip, XFS_ILOCK_EXCL);
789 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
790 xfs_trans_ihold(tp, ip);
793 * Modify the unwritten extent state of the buffer.
795 xfs_bmap_init(&free_list, &firstfsb);
797 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
798 XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
799 1, &imap, &nimaps, &free_list, NULL);
801 goto error_on_bmapi_transaction;
803 error = xfs_bmap_finish(&(tp), &(free_list), &committed);
805 goto error_on_bmapi_transaction;
807 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
808 xfs_iunlock(ip, XFS_ILOCK_EXCL);
810 return XFS_ERROR(error);
812 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
813 return xfs_cmn_err_fsblock_zero(ip, &imap);
815 if ((numblks_fsb = imap.br_blockcount) == 0) {
817 * The numblks_fsb value should always get
818 * smaller, otherwise the loop is stuck.
820 ASSERT(imap.br_blockcount);
823 offset_fsb += numblks_fsb;
824 count_fsb -= numblks_fsb;
825 } while (count_fsb > 0);
829 error_on_bmapi_transaction:
830 xfs_bmap_cancel(&free_list);
831 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
832 xfs_iunlock(ip, XFS_ILOCK_EXCL);
833 return XFS_ERROR(error);