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
58 STATIC int xfs_iomap_write_direct(struct xfs_inode *, xfs_off_t, size_t,
59 int, struct xfs_bmbt_irec *, int *);
60 STATIC int xfs_iomap_write_delay(struct xfs_inode *, xfs_off_t, size_t, int,
61 struct xfs_bmbt_irec *, int *);
62 STATIC int xfs_iomap_write_allocate(struct xfs_inode *, xfs_off_t, size_t,
63 struct xfs_bmbt_irec *, int *);
71 struct xfs_bmbt_irec *imap,
75 struct xfs_mount *mp = ip->i_mount;
76 xfs_fileoff_t offset_fsb, end_fsb;
81 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
85 if (XFS_FORCED_SHUTDOWN(mp))
86 return XFS_ERROR(EIO);
88 trace_xfs_iomap_enter(ip, offset, count, flags, NULL);
90 switch (flags & (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE)) {
92 lockmode = xfs_ilock_map_shared(ip);
93 bmapi_flags = XFS_BMAPI_ENTIRE;
96 lockmode = XFS_ILOCK_EXCL;
97 if (flags & BMAPI_IGNSTATE)
98 bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
99 xfs_ilock(ip, lockmode);
102 lockmode = XFS_ILOCK_SHARED;
103 bmapi_flags = XFS_BMAPI_ENTIRE;
105 /* Attempt non-blocking lock */
106 if (flags & BMAPI_TRYLOCK) {
107 if (!xfs_ilock_nowait(ip, lockmode))
108 return XFS_ERROR(EAGAIN);
110 xfs_ilock(ip, lockmode);
117 ASSERT(offset <= mp->m_maxioffset);
118 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
119 count = mp->m_maxioffset - offset;
120 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
121 offset_fsb = XFS_B_TO_FSBT(mp, offset);
123 error = xfs_bmapi(NULL, ip, offset_fsb,
124 (xfs_filblks_t)(end_fsb - offset_fsb),
125 bmapi_flags, NULL, 0, imap,
131 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)) {
133 /* If we found an extent, return it */
135 (imap->br_startblock != HOLESTARTBLOCK) &&
136 (imap->br_startblock != DELAYSTARTBLOCK)) {
137 trace_xfs_iomap_found(ip, offset, count, flags, imap);
141 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
142 error = xfs_iomap_write_direct(ip, offset, count, flags,
145 error = xfs_iomap_write_delay(ip, offset, count, flags,
149 trace_xfs_iomap_alloc(ip, offset, count, flags, imap);
154 /* If we found an extent, return it */
155 xfs_iunlock(ip, lockmode);
158 if (*nimaps && !isnullstartblock(imap->br_startblock)) {
159 trace_xfs_iomap_found(ip, offset, count, flags, imap);
163 error = xfs_iomap_write_allocate(ip, offset, count,
168 ASSERT(*nimaps <= 1);
172 xfs_iunlock(ip, lockmode);
173 return XFS_ERROR(error);
177 xfs_iomap_eof_align_last_fsb(
180 xfs_extlen_t extsize,
181 xfs_fileoff_t *last_fsb)
183 xfs_fileoff_t new_last_fsb = 0;
187 if (XFS_IS_REALTIME_INODE(ip))
190 * If mounted with the "-o swalloc" option, roundup the allocation
191 * request to a stripe width boundary if the file size is >=
192 * stripe width and we are allocating past the allocation eof.
194 else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
195 (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_swidth)))
196 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
198 * Roundup the allocation request to a stripe unit (m_dalign) boundary
199 * if the file size is >= stripe unit size, and we are allocating past
200 * the allocation eof.
202 else if (mp->m_dalign && (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_dalign)))
203 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
206 * Always round up the allocation request to an extent boundary
207 * (when file on a real-time subvolume or has di_extsize hint).
211 align = roundup_64(new_last_fsb, extsize);
214 new_last_fsb = roundup_64(*last_fsb, align);
218 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
222 *last_fsb = new_last_fsb;
228 xfs_cmn_err_fsblock_zero(
230 xfs_bmbt_irec_t *imap)
232 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
233 "Access to block zero in inode %llu "
234 "start_block: %llx start_off: %llx "
235 "blkcnt: %llx extent-state: %x\n",
236 (unsigned long long)ip->i_ino,
237 (unsigned long long)imap->br_startblock,
238 (unsigned long long)imap->br_startoff,
239 (unsigned long long)imap->br_blockcount,
245 xfs_iomap_write_direct(
250 xfs_bmbt_irec_t *ret_imap,
253 xfs_mount_t *mp = ip->i_mount;
254 xfs_fileoff_t offset_fsb;
255 xfs_fileoff_t last_fsb;
256 xfs_filblks_t count_fsb, resaligned;
257 xfs_fsblock_t firstfsb;
258 xfs_extlen_t extsz, temp;
264 xfs_bmbt_irec_t imap;
265 xfs_bmap_free_t free_list;
266 uint qblocks, resblks, resrtextents;
271 * Make sure that the dquots are there. This doesn't hold
272 * the ilock across a disk read.
274 error = xfs_qm_dqattach_locked(ip, 0);
276 return XFS_ERROR(error);
278 rt = XFS_IS_REALTIME_INODE(ip);
279 extsz = xfs_get_extsz_hint(ip);
281 offset_fsb = XFS_B_TO_FSBT(mp, offset);
282 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
283 if ((offset + count) > ip->i_size) {
284 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
288 if (*nmaps && (ret_imap->br_startblock == HOLESTARTBLOCK))
289 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
290 ret_imap->br_blockcount +
291 ret_imap->br_startoff);
293 count_fsb = last_fsb - offset_fsb;
294 ASSERT(count_fsb > 0);
296 resaligned = count_fsb;
297 if (unlikely(extsz)) {
298 if ((temp = do_mod(offset_fsb, extsz)))
300 if ((temp = do_mod(resaligned, extsz)))
301 resaligned += extsz - temp;
305 resrtextents = qblocks = resaligned;
306 resrtextents /= mp->m_sb.sb_rextsize;
307 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
308 quota_flag = XFS_QMOPT_RES_RTBLKS;
311 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
312 quota_flag = XFS_QMOPT_RES_REGBLKS;
316 * Allocate and setup the transaction
318 xfs_iunlock(ip, XFS_ILOCK_EXCL);
319 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
320 error = xfs_trans_reserve(tp, resblks,
321 XFS_WRITE_LOG_RES(mp), resrtextents,
322 XFS_TRANS_PERM_LOG_RES,
323 XFS_WRITE_LOG_COUNT);
325 * Check for running out of space, note: need lock to return
328 xfs_trans_cancel(tp, 0);
329 xfs_ilock(ip, XFS_ILOCK_EXCL);
333 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
337 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
338 xfs_trans_ihold(tp, ip);
340 bmapi_flag = XFS_BMAPI_WRITE;
341 if ((flags & BMAPI_DIRECT) && (offset < ip->i_size || extsz))
342 bmapi_flag |= XFS_BMAPI_PREALLOC;
345 * Issue the xfs_bmapi() call to allocate the blocks
347 xfs_bmap_init(&free_list, &firstfsb);
349 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, bmapi_flag,
350 &firstfsb, 0, &imap, &nimaps, &free_list, NULL);
355 * Complete the transaction
357 error = xfs_bmap_finish(&tp, &free_list, &committed);
360 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
365 * Copy any maps to caller's array and return any error.
372 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip))) {
373 error = xfs_cmn_err_fsblock_zero(ip, &imap);
381 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
382 xfs_bmap_cancel(&free_list);
383 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
385 error1: /* Just cancel transaction */
386 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
387 *nmaps = 0; /* nothing set-up here */
390 return XFS_ERROR(error);
394 * If the caller is doing a write at the end of the file, then extend the
395 * allocation out to the file system's write iosize. We clean up any extra
396 * space left over when the file is closed in xfs_inactive().
399 xfs_iomap_eof_want_preallocate(
405 xfs_bmbt_irec_t *imap,
409 xfs_fileoff_t start_fsb;
410 xfs_filblks_t count_fsb;
411 xfs_fsblock_t firstblock;
415 if ((offset + count) <= ip->i_size)
419 * If there are any real blocks past eof, then don't
420 * do any speculative allocation.
422 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
423 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
424 while (count_fsb > 0) {
426 firstblock = NULLFSBLOCK;
427 error = xfs_bmapi(NULL, ip, start_fsb, count_fsb, 0,
428 &firstblock, 0, imap, &imaps, NULL, NULL);
431 for (n = 0; n < imaps; n++) {
432 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
433 (imap[n].br_startblock != DELAYSTARTBLOCK))
435 start_fsb += imap[n].br_blockcount;
436 count_fsb -= imap[n].br_blockcount;
444 xfs_iomap_write_delay(
449 xfs_bmbt_irec_t *ret_imap,
452 xfs_mount_t *mp = ip->i_mount;
453 xfs_fileoff_t offset_fsb;
454 xfs_fileoff_t last_fsb;
455 xfs_off_t aligned_offset;
456 xfs_fileoff_t ioalign;
457 xfs_fsblock_t firstblock;
460 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
461 int prealloc, flushed = 0;
464 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
467 * Make sure that the dquots are there. This doesn't hold
468 * the ilock across a disk read.
470 error = xfs_qm_dqattach_locked(ip, 0);
472 return XFS_ERROR(error);
474 extsz = xfs_get_extsz_hint(ip);
475 offset_fsb = XFS_B_TO_FSBT(mp, offset);
477 error = xfs_iomap_eof_want_preallocate(mp, ip, offset, count,
478 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
484 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
485 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
486 last_fsb = ioalign + mp->m_writeio_blocks;
488 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
491 if (prealloc || extsz) {
492 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
497 nimaps = XFS_WRITE_IMAPS;
498 firstblock = NULLFSBLOCK;
499 error = xfs_bmapi(NULL, ip, offset_fsb,
500 (xfs_filblks_t)(last_fsb - offset_fsb),
501 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
502 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
503 &nimaps, NULL, NULL);
504 if (error && (error != ENOSPC))
505 return XFS_ERROR(error);
508 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
509 * then we must have run out of space - flush all other inodes with
510 * delalloc blocks and retry without EOF preallocation.
513 trace_xfs_delalloc_enospc(ip, offset, count);
515 return XFS_ERROR(ENOSPC);
517 xfs_iunlock(ip, XFS_ILOCK_EXCL);
518 xfs_flush_inodes(ip);
519 xfs_ilock(ip, XFS_ILOCK_EXCL);
527 if (!(imap[0].br_startblock || XFS_IS_REALTIME_INODE(ip)))
528 return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
537 * Pass in a delayed allocate extent, convert it to real extents;
538 * return to the caller the extent we create which maps on top of
539 * the originating callers request.
541 * Called without a lock on the inode.
543 * We no longer bother to look at the incoming map - all we have to
544 * guarantee is that whatever we allocate fills the required range.
547 xfs_iomap_write_allocate(
551 xfs_bmbt_irec_t *map,
554 xfs_mount_t *mp = ip->i_mount;
555 xfs_fileoff_t offset_fsb, last_block;
556 xfs_fileoff_t end_fsb, map_start_fsb;
557 xfs_fsblock_t first_block;
558 xfs_bmap_free_t free_list;
559 xfs_filblks_t count_fsb;
560 xfs_bmbt_irec_t imap;
562 int nimaps, committed;
569 * Make sure that the dquots are there.
571 error = xfs_qm_dqattach(ip, 0);
573 return XFS_ERROR(error);
575 offset_fsb = XFS_B_TO_FSBT(mp, offset);
576 count_fsb = map->br_blockcount;
577 map_start_fsb = map->br_startoff;
579 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
581 while (count_fsb != 0) {
583 * Set up a transaction with which to allocate the
584 * backing store for the file. Do allocations in a
585 * loop until we get some space in the range we are
586 * interested in. The other space that might be allocated
587 * is in the delayed allocation extent on which we sit
588 * but before our buffer starts.
592 while (nimaps == 0) {
593 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
594 tp->t_flags |= XFS_TRANS_RESERVE;
595 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
596 error = xfs_trans_reserve(tp, nres,
597 XFS_WRITE_LOG_RES(mp),
598 0, XFS_TRANS_PERM_LOG_RES,
599 XFS_WRITE_LOG_COUNT);
601 xfs_trans_cancel(tp, 0);
602 return XFS_ERROR(error);
604 xfs_ilock(ip, XFS_ILOCK_EXCL);
605 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
606 xfs_trans_ihold(tp, ip);
608 xfs_bmap_init(&free_list, &first_block);
611 * it is possible that the extents have changed since
612 * we did the read call as we dropped the ilock for a
613 * while. We have to be careful about truncates or hole
614 * punchs here - we are not allowed to allocate
615 * non-delalloc blocks here.
617 * The only protection against truncation is the pages
618 * for the range we are being asked to convert are
619 * locked and hence a truncate will block on them
622 * As a result, if we go beyond the range we really
623 * need and hit an delalloc extent boundary followed by
624 * a hole while we have excess blocks in the map, we
625 * will fill the hole incorrectly and overrun the
626 * transaction reservation.
628 * Using a single map prevents this as we are forced to
629 * check each map we look for overlap with the desired
630 * range and abort as soon as we find it. Also, given
631 * that we only return a single map, having one beyond
632 * what we can return is probably a bit silly.
634 * We also need to check that we don't go beyond EOF;
635 * this is a truncate optimisation as a truncate sets
636 * the new file size before block on the pages we
637 * currently have locked under writeback. Because they
638 * are about to be tossed, we don't need to write them
642 end_fsb = XFS_B_TO_FSB(mp, ip->i_size);
643 error = xfs_bmap_last_offset(NULL, ip, &last_block,
648 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
649 if ((map_start_fsb + count_fsb) > last_block) {
650 count_fsb = last_block - map_start_fsb;
651 if (count_fsb == 0) {
657 /* Go get the actual blocks */
658 error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
659 XFS_BMAPI_WRITE, &first_block, 1,
660 &imap, &nimaps, &free_list, NULL);
664 error = xfs_bmap_finish(&tp, &free_list, &committed);
668 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
672 xfs_iunlock(ip, XFS_ILOCK_EXCL);
676 * See if we were able to allocate an extent that
677 * covers at least part of the callers request
679 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
680 return xfs_cmn_err_fsblock_zero(ip, &imap);
682 if ((offset_fsb >= imap.br_startoff) &&
683 (offset_fsb < (imap.br_startoff +
684 imap.br_blockcount))) {
687 XFS_STATS_INC(xs_xstrat_quick);
692 * So far we have not mapped the requested part of the
693 * file, just surrounding data, try again.
695 count_fsb -= imap.br_blockcount;
696 map_start_fsb = imap.br_startoff + imap.br_blockcount;
700 xfs_bmap_cancel(&free_list);
701 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
703 xfs_iunlock(ip, XFS_ILOCK_EXCL);
704 return XFS_ERROR(error);
708 xfs_iomap_write_unwritten(
713 xfs_mount_t *mp = ip->i_mount;
714 xfs_fileoff_t offset_fsb;
715 xfs_filblks_t count_fsb;
716 xfs_filblks_t numblks_fsb;
717 xfs_fsblock_t firstfsb;
720 xfs_bmbt_irec_t imap;
721 xfs_bmap_free_t free_list;
726 trace_xfs_unwritten_convert(ip, offset, count);
728 offset_fsb = XFS_B_TO_FSBT(mp, offset);
729 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
730 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
733 * Reserve enough blocks in this transaction for two complete extent
734 * btree splits. We may be converting the middle part of an unwritten
735 * extent and in this case we will insert two new extents in the btree
736 * each of which could cause a full split.
738 * This reservation amount will be used in the first call to
739 * xfs_bmbt_split() to select an AG with enough space to satisfy the
740 * rest of the operation.
742 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
746 * set up a transaction to convert the range of extents
747 * from unwritten to real. Do allocations in a loop until
748 * we have covered the range passed in.
750 * Note that we open code the transaction allocation here
751 * to pass KM_NOFS--we can't risk to recursing back into
752 * the filesystem here as we might be asked to write out
753 * the same inode that we complete here and might deadlock
756 xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
757 tp = _xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE, KM_NOFS);
758 tp->t_flags |= XFS_TRANS_RESERVE;
759 error = xfs_trans_reserve(tp, resblks,
760 XFS_WRITE_LOG_RES(mp), 0,
761 XFS_TRANS_PERM_LOG_RES,
762 XFS_WRITE_LOG_COUNT);
764 xfs_trans_cancel(tp, 0);
765 return XFS_ERROR(error);
768 xfs_ilock(ip, XFS_ILOCK_EXCL);
769 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
770 xfs_trans_ihold(tp, ip);
773 * Modify the unwritten extent state of the buffer.
775 xfs_bmap_init(&free_list, &firstfsb);
777 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
778 XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
779 1, &imap, &nimaps, &free_list, NULL);
781 goto error_on_bmapi_transaction;
783 error = xfs_bmap_finish(&(tp), &(free_list), &committed);
785 goto error_on_bmapi_transaction;
787 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
788 xfs_iunlock(ip, XFS_ILOCK_EXCL);
790 return XFS_ERROR(error);
792 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
793 return xfs_cmn_err_fsblock_zero(ip, &imap);
795 if ((numblks_fsb = imap.br_blockcount) == 0) {
797 * The numblks_fsb value should always get
798 * smaller, otherwise the loop is stuck.
800 ASSERT(imap.br_blockcount);
803 offset_fsb += numblks_fsb;
804 count_fsb -= numblks_fsb;
805 } while (count_fsb > 0);
809 error_on_bmapi_transaction:
810 xfs_bmap_cancel(&free_list);
811 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
812 xfs_iunlock(ip, XFS_ILOCK_EXCL);
813 return XFS_ERROR(error);